// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/tga.c
// Function: do_decode_image
static void do_decode_image(deark *c, lctx *d, struct tgaimginfo *imginfo, dbuf *unc_pixels,
	const char *token, UI createflags1)
{
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	i64 i, j;
	i64 pdwidth;
	u8 b;
	u32 clr;
	u8 a;
	i64 rowspan;
	int output_bypp;
	UI getrgbflags;
	i64 interleave_stride;
	i64 interleave_pass;
	i64 cur_rownum; // 0-based, does not account for bottom-up orientation
	int has_alpha_0 = 0;
	int has_alpha_partial = 0;
	int has_alpha_255 = 0;
	UI createflags = createflags1;

	fi = de_finfo_create(c);

	if(token) {
		de_finfo_set_name_from_sz(c, fi, token, 0, DE_ENCODING_LATIN1);
	}
	if(d->mod_time.is_valid) {
		fi->internal_mod_time = d->mod_time;
	}

	pdwidth = imginfo->width;
	if(d->pixel_depth==1) {
		de_warn(c, "1-bit TGA images are not portable, and may not be decoded correctly");
		pdwidth = de_pad_to_n(imginfo->width, 8);
		rowspan = pdwidth/8;
	}
	else {
		rowspan = imginfo->width*d->bytes_per_pixel;
	}

	if(d->color_type==TGA_CLRTYPE_GRAYSCALE || d->pixel_depth==1)
		output_bypp=1;
	else if(d->pixel_depth==32)
		output_bypp=4;
	else
		output_bypp=3;

	if(d->file_format==FMT_VST)
		getrgbflags = 0;
	else
		getrgbflags = DE_GETRGBFLAG_BGR;

	img = de_bitmap_create2(c, imginfo->width, pdwidth, imginfo->height, output_bypp);

	switch(d->interleave_mode) {
	case 1: interleave_stride = 2; break;
	case 2: interleave_stride = 4; break;
	default: interleave_stride = 1;
	}

	cur_rownum = 0;
	interleave_pass = 0;

	for(j=0; j<imginfo->height; j++) {
		i64 j_adj;

		j_adj = cur_rownum;

		// Update the row number for next time
		cur_rownum += interleave_stride;
		if(cur_rownum >= imginfo->height) {
			// Went past the end of the image; move back to near the start.
			interleave_pass++;
			cur_rownum = interleave_pass;
		}

		if(d->pixel_depth==1) {
			de_convert_row_bilevel(unc_pixels, j*rowspan, img, j_adj, 0);
			continue;
		}

		for(i=0; i<pdwidth; i++) {
			if(d->color_type==TGA_CLRTYPE_TRUECOLOR && (d->pixel_depth==15 || d->pixel_depth==16)) {
				clr = (u32)dbuf_getu16le(unc_pixels, j*rowspan + i*d->bytes_per_pixel);
				clr = de_rgb555_to_888(clr);
				de_bitmap_setpixel_rgb(img, i, j_adj, clr);
			}
			else if(d->color_type==TGA_CLRTYPE_TRUECOLOR) {
				clr = dbuf_getRGB(unc_pixels, j*rowspan + i*d->bytes_per_pixel, getrgbflags);
				if(d->pixel_depth==32) {
					a = dbuf_getbyte(unc_pixels, j*rowspan + i*d->bytes_per_pixel+3);
					de_bitmap_setpixel_rgba(img, i, j_adj, DE_SET_ALPHA(clr, a));

					// Collect metrics that we may need, to decide whether to keep the
					// might-be-alpha channel.
					if(a==0) {
						has_alpha_0 = 1;
					}
					else if(a==0xff) {
						has_alpha_255 = 1;
					}
					else {
						has_alpha_partial = 1;
					}
				}
				else {
					de_bitmap_setpixel_rgb(img, i, j_adj, clr);
				}
			}
			else if(d->color_type==TGA_CLRTYPE_GRAYSCALE) {
				b = dbuf_getbyte(unc_pixels, j*rowspan + i*d->bytes_per_pixel);
				de_bitmap_setpixel_gray(img, i, j_adj, b);
			}
			else if(d->color_type==TGA_CLRTYPE_PALETTE) {
				b = dbuf_getbyte(unc_pixels, j*rowspan + i*d->bytes_per_pixel);
				de_bitmap_setpixel_rgb(img, i, j_adj, d->pal[(UI)b]);
			}
		}
	}

	// TODO: 16-bit images could theoretically have a transparency bit, but I don't
	// know how to detect that.
	if(d->pixel_depth==32) {
		// TODO: The transparency logic is a bit messy. It probably ought to be
		// redesigned from scratch.

		imginfo->respect_alpha_channel = should_use_alpha_channel(c, d, imginfo,
			has_alpha_0, has_alpha_partial, has_alpha_255);

		if(!imginfo->is_thumb) {
			de_dbg(c, "using alpha channel: %s", imginfo->respect_alpha_channel?"yes":"no");
		}

		if(!imginfo->respect_alpha_channel || (!has_alpha_0 && !has_alpha_partial)) {
			de_bitmap_remove_alpha(img);
		}

		if(imginfo->respect_alpha_channel && d->alpha_is_premultiplied) {
			bitmap_unpremultiply_alpha(img);
		}
	}

	if(d->right_to_left) {
		de_bitmap_mirror(img);
	}
	if(!d->top_down) {
		createflags |= DE_CREATEFLAG_FLIP_IMAGE;
	}

	createflags |= DE_CREATEFLAG_OPT_IMAGE;
	de_bitmap_write_to_file_finfo(img, fi, createflags);

	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, 0x48(%rsp)
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, 0x10(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
callq 0xea504
movq %rax, %rsi
testq %rbx, %rbx
movq %r14, 0x8(%rsp)
je 0xca916
movq %rbp, %rdi
movq %rsi, %r14
movq %rbx, %rdx
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xea78f
movq %r14, %rsi
movq 0x8(%rsp), %r14
cmpb $0x0, 0x4e8(%r14)
je 0xca92e
leaq 0x4e8(%r14), %rax
movups (%rax), %xmm0
movups %xmm0, 0x68(%rsi)
cmpq $0x1, 0x70(%r14)
movq %rsi, 0x58(%rsp)
jne 0xca971
leaq 0x71aa7(%rip), %rsi     # 0x13c3e8
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl $0x8, %esi
callq 0xe9cb4
movq %rax, %r13
addq $0x7, %rax
testq %r13, %r13
cmovnsq %r13, %rax
sarq $0x3, %rax
jmp 0xca984
movq 0x10(%rsp), %rax
movq (%rax), %r13
movq 0xb8(%r14), %rax
imulq %r13, %rax
movq %rax, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movl $0x1, %r8d
cmpl $0x3, 0xa4(%r14)
je 0xca9b6
movq 0x70(%r14), %rax
xorl %ecx, %ecx
cmpq $0x20, %rax
sete %cl
addl $0x3, %ecx
cmpq $0x1, %rax
cmovnel %ecx, %r8d
xorl %r12d, %r12d
cmpl $0x1, 0xc(%r14)
setne %bl
movq %r14, %r15
movq 0x10(%rsp), %r14
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movq %rbp, %rdi
movq %r13, %rdx
callq 0xf26a5
movb 0x8c(%r15), %cl
xorl %edx, %edx
cmpb $0x2, %cl
sete %dl
cmpb $0x1, %cl
leaq 0x1(%rdx,%rdx,2), %rcx
movl $0x2, %edx
cmovneq %rcx, %rdx
movq %rdx, 0x68(%rsp)
movq %rbp, %r15
movq %rax, %rbp
movq 0x8(%r14), %rax
movq $0x0, 0x18(%rsp)
testq %rax, %rax
jle 0xcac85
movq %r15, 0x50(%rsp)
movb %bl, %r12b
movl %r12d, 0x4c(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %edx, %edx
xorl %esi, %esi
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r15
movq %r13, 0x38(%rsp)
movq 0x68(%rsp), %rcx
addq %r12, %rcx
leaq 0x1(%rdx), %rdi
cmpq %rax, %rcx
cmovgeq %rdi, %rdx
cmovlq %rcx, %rdi
movq %rdi, 0x70(%rsp)
cmpq $0x1, 0x70(%r14)
movq %rdx, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
jne 0xcaa9b
imulq 0x30(%rsp), %rsi
movq %r15, %rdi
movq %rbp, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf2a28
jmp 0xcac39
testq %r13, %r13
jle 0xcac39
imulq 0x30(%rsp), %rsi
movq %rsi, 0x40(%rsp)
leaq 0x3(%rsi), %rax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movl 0xa4(%r14), %eax
cmpl $0x1, %eax
je 0xcab3d
cmpl $0x3, %eax
je 0xcab12
cmpl $0x2, %eax
jne 0xcab6e
movq 0x70(%r14), %rax
addq $-0xf, %rax
movq 0xb8(%r14), %rsi
imulq %rbx, %rsi
addq 0x40(%rsp), %rsi
cmpq $0x1, %rax
ja 0xcab7f
movq %r15, %rdi
callq 0xeebc8
movl %eax, %edi
callq 0xec947
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movl %eax, %ecx
jmp 0xcab69
movq 0xb8(%r14), %rsi
imulq %rbx, %rsi
addq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0xeead3
movzbl %al, %ecx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2405
jmp 0xcab6e
movq 0xb8(%r14), %rsi
imulq %rbx, %rsi
addq 0x40(%rsp), %rsi
movq %r15, %rdi
callq 0xeead3
movzbl %al, %eax
movl 0xe8(%r14,%rax,4), %ecx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbx
cmpq %rbx, %r13
jne 0xcaaba
jmp 0xcac39
movq %rbp, %r13
movq %r15, %rdi
movl 0x4c(%rsp), %edx
callq 0xef42f
movl %eax, %ebp
cmpq $0x20, 0x70(%r14)
jne 0xcabf1
movq 0xb8(%r14), %rsi
imulq %rbx, %rsi
addq 0x60(%rsp), %rsi
movq %r15, %rdi
callq 0xeead3
andl $0xffffff, %ebp         # imm = 0xFFFFFF
movzbl %al, %r14d
movl %r14d, %ecx
shll $0x18, %ecx
orl %ebp, %ecx
movq %r13, %rbp
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf24bf
testb %r14b, %r14b
je 0xcac06
cmpl $0xff, %r14d
movq 0x38(%rsp), %r13
jne 0xcac22
movl $0x1, %eax
movq %rax, 0x18(%rsp)
jmp 0xcac2a
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movl %ebp, %ecx
movq %r13, %rbp
callq 0xf24ba
jmp 0xcac18
movl $0x1, 0x20(%rsp)
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %r13
jmp 0xcab6e
movl $0x1, 0x24(%rsp)
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r15
jmp 0xcab6e
movq 0x78(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
cmpq %rax, %rsi
movq 0x80(%rsp), %rdx
movq 0x70(%rsp), %r12
jl 0xcaa4f
cmpl $0x0, 0x20(%rsp)
setne %al
cmpl $0x0, 0x18(%rsp)
setne %cl
cmpl $0x0, 0x24(%rsp)
setne %r14b
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %r15
jmp 0xcac8a
xorl %r14d, %r14d
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
cmpq $0x20, 0x70(%rax)
jne 0xcaead
movq %rbp, %r13
movq 0x10(%rsp), %rax
movl 0x18(%rax), %esi
testl %esi, %esi
leaq 0x7a402(%rip), %rax     # 0x1450b0
leaq 0x7178f(%rip), %rdx     # 0x13c444
cmoveq %rax, %rdx
movq 0x8(%rsp), %rax
movb 0x10(%rax), %dil
xorl %ebp, %ebp
leaq 0x5d58b(%rip), %rbx     # 0x128256
cmpb $0x65, %dil
je 0xcadee
movl 0x9c(%rax), %r8d
testl %r8d, %r8d
je 0xcad06
movzbl 0x88(%rax), %eax
cmpl $0x4, %eax
ja 0xcadee
movl $0x19, %r9d
btl %eax, %r9d
movq 0x8(%rsp), %rax
jae 0xcadee
cmpl $0x2, 0xa4(%rax)
jne 0xcadee
movq 0x80(%rax), %rax
testq $-0x9, %rax
jne 0xcad40
andb $-0x2, %dil
movl $0x1, %ebp
leaq 0x6c406(%rip), %r12     # 0x137138
cmpb $0x66, %dil
jne 0xcad62
movq %r12, %rbx
jmp 0xcadee
testl %esi, %esi
jne 0xcadee
leaq 0x71702(%rip), %rsi     # 0x13c451
xorl %ebp, %ebp
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8faa
jmp 0xcadee
testl %r8d, %r8d
je 0xcad83
movq 0x8(%rsp), %rsi
movzbl 0x88(%rsi), %esi
cmpl $0x4, %esi
je 0xcad7d
cmpl $0x3, %esi
jne 0xcad83
cmpq $0x8, %rax
je 0xcad38
movq 0x18(%rsp), %rsi
andb %sil, %cl
orb %r14b, %cl
testb $0x1, %cl
je 0xcadab
testq %rax, %rax
jne 0xcad38
leaq 0x716f1(%rip), %rsi     # 0x13c490
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0xcad38
testb %sil, %sil
je 0xcadcd
testq %rax, %rax
movq %r15, %rdi
je 0xcaddb
leaq 0x7173e(%rip), %rsi     # 0x13c4fd
xorl %ebp, %ebp
movq %rdi, %r15
xorl %eax, %eax
callq 0xe8faa
jmp 0xcadee
leaq 0x7178b(%rip), %rsi     # 0x13c55f
xorl %ebp, %ebp
movq %r15, %rdi
jmp 0xcade7
leaq 0x71751(%rip), %rsi     # 0x13c533
xorl %ebp, %ebp
movq %rdi, %r15
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rsp), %rax
movl %ebp, 0x1c(%rax)
cmpl $0x0, 0x18(%rax)
jne 0xcae18
leaq 0x71629(%rip), %rsi     # 0x13c42c
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %ebp
testl %ebp, %ebp
movq %r13, %rbp
je 0xcae2b
movq 0x18(%rsp), %rax
orb %r14b, %al
testb $0x1, %al
jne 0xcae3e
movq %rbp, %rdi
callq 0xf383e
movq 0x10(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
je 0xcaead
movq 0x8(%rsp), %rax
cmpb $0x0, 0x89(%rax)
je 0xcaead
movl 0x24(%rbp), %eax
cmpl $0x4, %eax
je 0xcae59
cmpl $0x2, %eax
jne 0xcaead
movq 0x10(%rbp), %rcx
testq %rcx, %rcx
jle 0xcaead
movq 0x8(%rbp), %rax
xorl %ebx, %ebx
testq %rax, %rax
jle 0xcaea5
xorl %r14d, %r14d
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xf2592
movl %eax, %edi
callq 0xf3d94
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %eax, %ecx
callq 0xf24bf
incq %r14
movq 0x8(%rbp), %rax
cmpq %rax, %r14
jl 0xcae70
movq 0x10(%rbp), %rcx
incq %rbx
cmpq %rcx, %rbx
jl 0xcae68
movq 0x8(%rsp), %rbx
cmpb $0x0, 0x8b(%rbx)
je 0xcaec3
movq %rbp, %rdi
callq 0xf33d6
movl 0x48(%rsp), %eax
movl %eax, %edx
orl $0x4, %edx
cmpb $0x0, 0x8a(%rbx)
cmovnel %eax, %edx
orl $0x2, %edx
movq %rbp, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0xf22d0
movq %rbp, %rdi
callq 0xf228d
movq %r15, %rdi
movq %rbx, %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea50e