// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tiff.c
// Function: paint_decompressed_strile_to_image
static void paint_decompressed_strile_to_image(deark *c, lctx *d, struct page_ctx *pg,
	struct decode_page_ctx *dctx, de_bitmap *img, de_bitmap *img_lo)
{
	i64 i, j;
	u32 s_idx;
	u32 sample_mask;
	u32 sample_scalefactor;
	u32 prev_sample[DE_TIFF_MAX_SAMPLES];
	u32 sample[DE_TIFF_MAX_SAMPLES];
	u8 sample_lo[DE_TIFF_MAX_SAMPLES];
	de_color clr_lo = 0;

	de_zeromem(&prev_sample, sizeof(prev_sample));
	de_zeromem(&sample, sizeof(sample));
	de_zeromem(&sample_lo, sizeof(sample_lo));

	sample_mask = (1U<<(u32)pg->bits_per_sample)-1U;
	if(pg->bits_per_sample<=8) {
		sample_scalefactor = 255/sample_mask;
	}
	else {
		sample_scalefactor = 1;
	}

	for(j=0; j<dctx->strileset_height; j++) {

		for(s_idx=0; s_idx<dctx->samples_per_pixel_to_decode; s_idx++) {
			prev_sample[s_idx] = 0;
		}

		for(i=0; i<dctx->strileset_width; i++) {
			de_color clr;

			for(s_idx=0; s_idx<dctx->samples_per_pixel_to_decode; s_idx++) {
				UI dbuf_idx;
				i64 sample_offset;
				i64 bytepos;

				if(dctx->is_separated) {
					dbuf_idx = s_idx;
					sample_offset = 0;
				}
				else {
					dbuf_idx = 0;
					sample_offset = (i64)s_idx;
				}

				switch(pg->bits_per_sample) {
				case 1: case 2: case 4:
					sample[s_idx] = de_get_bits_symbol(dctx->unc_strile_dbuf[dbuf_idx], (i64)pg->bits_per_sample,
						dctx->strileset_rowspan*j, i*(i64)dctx->samples_per_pixel_per_plane + sample_offset);
					break;
				case 8:
					bytepos = dctx->strileset_rowspan*j + i*(i64)dctx->samples_per_pixel_per_plane + sample_offset;
					sample[s_idx] = dbuf_getbyte(dctx->unc_strile_dbuf[dbuf_idx], bytepos);
					break;
				case 16:
					bytepos = dctx->strileset_rowspan*j + (i*(i64)dctx->samples_per_pixel_per_plane + sample_offset)*2;
					// Byte order for 16-bit samples is dictated by the TIFF file header -- at least
					// for uncompressed images.
					sample[s_idx] = (u32)dbuf_getu16x(dctx->unc_strile_dbuf[dbuf_idx], bytepos, d->is_le);
					break;
				default:
					sample[s_idx] = 0;
				}
			}

			if(pg->predictor==2) {
				for(s_idx=0; s_idx<dctx->samples_per_pixel_to_decode; s_idx++) {
					sample[s_idx] = (sample[s_idx] + prev_sample[s_idx]) & sample_mask;
					prev_sample[s_idx] = sample[s_idx];
				}
			}

			if(!dctx->use_pal && sample_scalefactor>1) { // Scale samples to 255
				for(s_idx=0; s_idx<dctx->samples_per_pixel_to_decode; s_idx++) {
					sample[s_idx] *= sample_scalefactor;
				}
			}

			if(dctx->grayscale_reverse_polarity) {
				if(pg->bits_per_sample==16)
					sample[0] = 0xffff - sample[0];
				else
					sample[0] = 0xff - sample[0];
			}

			if(pg->bits_per_sample==16) {
				for(s_idx=0; s_idx<dctx->samples_per_pixel_to_decode; s_idx++) {
					sample_lo[s_idx] = sample[s_idx] & 0xff;
					sample[s_idx] >>= 8;
				}
			}

			if(dctx->use_pal) {
				UI palidx;

				if(pg->is_dexxa && pg->dexxa_ncolors==16) {
					palidx = (sample[3]<<3) | (sample[2]<<2) | (sample[1]<<1) | sample[0];
					if(pg->compression==CMPR_CCITTRLE) {
						palidx = 15 - palidx;
					}
				}
				else {
					palidx = sample[0];
				}
				clr = pg->pal[palidx & 0xff];
			}
			else if(dctx->is_grayscale) {
				clr = DE_MAKE_GRAY(sample[0]);
				if(dctx->dst_flag_16bit) {
					clr_lo = DE_MAKE_GRAY(sample_lo[0]);
				}
			}
			else {
				clr = DE_MAKE_RGB(sample[0], sample[1], sample[2]);
			}

			if(dctx->has_alpha) {
				clr = DE_SET_ALPHA(clr, sample[dctx->alpha_sample_idx]);
				if(dctx->is_assoc_alpha) {
					clr = de_unpremultiply_alpha_clr(clr);
				}
			}

			de_bitmap_setpixel_rgba(img, dctx->strileset_xpos+i, dctx->strileset_ypos+j, clr);
			if(dctx->dst_flag_16bit) {
				de_bitmap_setpixel_rgba(img_lo, dctx->strileset_xpos+i, dctx->strileset_ypos+j, clr_lo);
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0xc0(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x19140
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x19140
leaq -0xba(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x19140
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x8, 0x28(%rax)
ja 0x1352e0
movl $0xff, %eax
xorl %edx, %edx
divl -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x1352e7
movl $0x1, -0x4c(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jge 0x13583f
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jae 0x13532d
movl -0x44(%rbp), %eax
movl $0x0, -0x80(%rbp,%rax,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x135308
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jge 0x13582c
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jae 0x1354f0
movq -0x20(%rbp), %rax
cmpb $0x0, 0xcd(%rax)
je 0x135384
movl -0x44(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
jmp 0x135398
movl $0x0, -0xc8(%rbp)
movl -0x44(%rbp), %eax
movq %rax, -0xd0(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xe8(%rbp)
subl $0xf, %eax
ja 0x1354d2
movq -0xe8(%rbp), %rax
leaq 0x9315b(%rip), %rcx     # 0x1c851c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rax
movl -0xc8(%rbp), %ecx
movq 0x70(%rax,%rcx,8), %rdi
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %esi
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rdx
imulq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movl 0x30(%rax), %eax
imulq %rax, %rcx
addq -0xd0(%rbp), %rcx
callq 0x16b8d0
movzbl %al, %ecx
movl -0x44(%rbp), %eax
movl %ecx, -0xb0(%rbp,%rax,4)
jmp 0x1354e0
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
imulq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl 0x30(%rdx), %edx
imulq %rdx, %rcx
addq %rcx, %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movl -0xc8(%rbp), %ecx
movq 0x70(%rax,%rcx,8), %rdi
movq -0xd8(%rbp), %rsi
callq 0x165060
movzbl %al, %ecx
movl -0x44(%rbp), %eax
movl %ecx, -0xb0(%rbp,%rax,4)
jmp 0x1354e0
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
imulq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl 0x30(%rdx), %edx
imulq %rdx, %rcx
addq -0xd0(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movl -0xc8(%rbp), %ecx
movq 0x70(%rax,%rcx,8), %rdi
movq -0xd8(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x165820
movl %eax, %ecx
movl -0x44(%rbp), %eax
movl %ecx, -0xb0(%rbp,%rax,4)
jmp 0x1354e0
movl -0x44(%rbp), %eax
movl $0x0, -0xb0(%rbp,%rax,4)
jmp 0x1354e2
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13534e
movq -0x18(%rbp), %rax
cmpl $0x2, 0x3c(%rax)
jne 0x13554c
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jae 0x13554a
movl -0x44(%rbp), %eax
movl -0xb0(%rbp,%rax,4), %ecx
movl -0x44(%rbp), %eax
addl -0x80(%rbp,%rax,4), %ecx
andl -0x48(%rbp), %ecx
movl -0x44(%rbp), %eax
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x44(%rbp), %eax
movl -0xb0(%rbp,%rax,4), %ecx
movl -0x44(%rbp), %eax
movl %ecx, -0x80(%rbp,%rax,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x135501
jmp 0x13554c
movq -0x20(%rbp), %rax
cmpb $0x0, 0xd3(%rax)
jne 0x135597
cmpl $0x1, -0x4c(%rbp)
jbe 0x135597
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jae 0x135595
movl -0x4c(%rbp), %ecx
movl -0x44(%rbp), %eax
imull -0xb0(%rbp,%rax,4), %ecx
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x135566
jmp 0x135597
movq -0x20(%rbp), %rax
cmpb $0x0, 0xd0(%rax)
je 0x1355d4
movq -0x18(%rbp), %rax
cmpl $0x10, 0x28(%rax)
jne 0x1355c1
movl $0xffff, %eax           # imm = 0xFFFF
subl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x1355d2
movl $0xff, %eax
subl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x1355d4
movq -0x18(%rbp), %rax
cmpl $0x10, 0x28(%rax)
jne 0x135630
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc4(%rcx), %eax
jae 0x13562e
movl -0x44(%rbp), %eax
movl -0xb0(%rbp,%rax,4), %eax
andl $0xff, %eax
movb %al, %cl
movl -0x44(%rbp), %eax
movb %cl, -0xba(%rbp,%rax)
movl -0x44(%rbp), %eax
movl -0xb0(%rbp,%rax,4), %ecx
shrl $0x8, %ecx
movl %ecx, -0xb0(%rbp,%rax,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1355e5
jmp 0x135630
movq -0x20(%rbp), %rax
cmpb $0x0, 0xd3(%rax)
je 0x1356d8
movq -0x18(%rbp), %rax
movzbl 0x99(%rax), %eax
cmpl $0x0, %eax
je 0x1356a8
movq -0x18(%rbp), %rax
movzbl 0x9a(%rax), %eax
cmpl $0x10, %eax
jne 0x1356a8
movl -0xa4(%rbp), %eax
shll $0x3, %eax
movl -0xa8(%rbp), %ecx
shll $0x2, %ecx
orl %ecx, %eax
movl -0xac(%rbp), %ecx
shll %ecx
orl %ecx, %eax
orl -0xb0(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x1c(%rax)
jne 0x1356a6
movl $0xf, %eax
subl -0xdc(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0x1356b4
movl -0xb0(%rbp), %eax
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rax
movl -0xdc(%rbp), %ecx
andl $0xff, %ecx
movl %ecx, %ecx
movl 0xd0(%rax,%rcx,4), %eax
movl %eax, -0xc4(%rbp)
jmp 0x13576a
movq -0x20(%rbp), %rax
cmpb $0x0, 0xcf(%rax)
je 0x135743
movl -0xb0(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl -0xb0(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
orl -0xb0(%rbp), %eax
movl %eax, -0xc4(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, 0xce(%rax)
je 0x135741
movzbl -0xba(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0xba(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0xba(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0xc0(%rbp)
jmp 0x135768
movl -0xb0(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl -0xac(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
orl -0xa8(%rbp), %eax
movl %eax, -0xc4(%rbp)
jmp 0x13576a
movq -0x20(%rbp), %rax
cmpb $0x0, 0xd4(%rax)
je 0x1357be
movl -0xc4(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq -0x20(%rbp), %rcx
movl 0xd8(%rcx), %ecx
movl -0xb0(%rbp,%rcx,4), %ecx
shll $0x18, %ecx
orl %ecx, %eax
movl %eax, -0xc4(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, 0xd5(%rax)
je 0x1357bc
movl -0xc4(%rbp), %edi
callq 0x16d6c0
movl %eax, -0xc4(%rbp)
jmp 0x1357be
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rsi
addq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
addq -0x40(%rbp), %rdx
movl -0xc4(%rbp), %ecx
callq 0x16b350
movq -0x20(%rbp), %rax
cmpb $0x0, 0xce(%rax)
je 0x135819
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rsi
addq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
addq -0x40(%rbp), %rdx
movl -0xc0(%rbp), %ecx
callq 0x16b350
jmp 0x13581b
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x135335
jmp 0x13582e
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x1352ef
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)