// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %r9d, 0x30(%rsp)
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbp
callq 0xea9a9
movq %rax, %r14
testq %rbx, %rbx
je 0xcb560
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xeac7c
cmpb $0x0, 0x4e8(%r15)
je 0xcb579
leaq 0x4e8(%r15), %rax
movups (%rax), %xmm0
movups %xmm0, 0x68(%r14)
movq %r14, 0x50(%rsp)
cmpq $0x1, 0x70(%r15)
jne 0xcb5b8
leaq 0x70abc(%rip), %rsi     # 0x13c048
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9b27
movq (%r12), %rdi
movl $0x8, %esi
callq 0xea80b
movq %rax, %r14
addq $0x7, %rax
testq %r14, %r14
cmovnsq %r14, %rax
sarq $0x3, %rax
jmp 0xcb5c7
movq (%r12), %r14
movq 0xb8(%r15), %rax
imulq %r14, %rax
movq %rax, 0x20(%rsp)
movl $0x1, %r8d
cmpl $0x3, 0xa4(%r15)
je 0xcb5f4
movq 0x70(%r15), %rax
xorl %ecx, %ecx
cmpq $0x20, %rax
sete %cl
addl $0x3, %ecx
cmpq $0x1, %rax
cmovnel %ecx, %r8d
xorl %ebx, %ebx
cmpl $0x1, 0xc(%r15)
setne 0x28(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rcx
movq %rbp, %rdi
movq %r14, %rdx
callq 0xf2ce6
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, 0x60(%rsp)
movq %rax, (%rsp)
movq 0x8(%r12), %rax
movq $0x0, 0x38(%rsp)
testq %rax, %rax
movq %r12, 0x8(%rsp)
jle 0xcb8d3
movq %rbp, 0x48(%rsp)
movb 0x28(%rsp), %cl
movb %cl, %bl
movl %ebx, 0x34(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
xorl %ebx, %ebx
xorl %esi, %esi
xorl %ebp, %ebp
movq %r13, %rdx
movq %r13, 0x40(%rsp)
movq 0x60(%rsp), %rcx
addq %rbx, %rcx
leaq 0x1(%rsi), %rdi
cmpq %rax, %rcx
cmovgeq %rdi, %rsi
cmovlq %rcx, %rdi
movq %rdi, 0x68(%rsp)
cmpq $0x1, 0x70(%r15)
movq %rsi, 0x28(%rsp)
movq %rbp, 0x70(%rsp)
jne 0xcb6d6
imulq 0x20(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq (%rsp), %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0xf3069
movq %r13, %rdx
jmp 0xcb88c
testq %r14, %r14
jle 0xcb88c
imulq 0x20(%rsp), %rbp
leaq 0x3(%rbp), %rax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
movl 0xa4(%r15), %eax
cmpl $0x1, %eax
je 0xcb81f
cmpl $0x3, %eax
je 0xcb7f5
cmpl $0x2, %eax
jne 0xcb852
movq 0x70(%r15), %rcx
addq $-0xf, %rcx
cmpq $0x1, %rcx
ja 0xcb74f
movq 0xb8(%r15), %rsi
imulq %r12, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0xef1ca
movl %eax, %edi
callq 0xecf84
movq (%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %eax, %ecx
jmp 0xcb84a
cmpl $0x1, %eax
je 0xcb81f
cmpl $0x3, %eax
je 0xcb7f5
cmpl $0x2, %eax
jne 0xcb852
movq 0xb8(%r15), %rsi
imulq %r12, %rsi
addq %rbp, %rsi
movq %rdx, %rdi
movl 0x34(%rsp), %edx
callq 0xefa60
cmpq $0x20, 0x70(%r15)
jne 0xcb860
movq 0xb8(%r15), %rsi
imulq %r12, %rsi
addq 0x58(%rsp), %rsi
movq 0x40(%rsp), %rdi
movl %eax, 0x14(%rsp)
callq 0xef0d5
movl 0x14(%rsp), %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
movzbl %al, %ecx
movl %ecx, 0x14(%rsp)
shll $0x18, %ecx
orl %edx, %ecx
movq (%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2b02
movl 0x14(%rsp), %eax
testb %al, %al
je 0xcb878
cmpl $0xff, %eax
jne 0xcb882
movl $0x1, %eax
movq %rax, 0x38(%rsp)
jmp 0xcb84f
movq 0xb8(%r15), %rsi
imulq %r12, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0xef0d5
movzbl %al, %ecx
movq (%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2a4a
jmp 0xcb84f
movq 0xb8(%r15), %rsi
imulq %r12, %rsi
addq %rbp, %rsi
movq %r13, %rdi
callq 0xef0d5
movzbl %al, %eax
movl 0xe8(%r15,%rax,4), %ecx
movq (%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2afd
movq %r13, %rdx
incq %r12
cmpq %r12, %r14
jne 0xcb6f1
jmp 0xcb88c
movq (%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %eax, %ecx
callq 0xf2afd
movq 0x40(%rsp), %rdx
jmp 0xcb852
movl $0x1, 0x18(%rsp)
jmp 0xcb84f
movl $0x1, 0x1c(%rsp)
jmp 0xcb84f
movq 0x70(%rsp), %rbp
incq %rbp
movq 0x8(%rsp), %r12
movq 0x8(%r12), %rax
cmpq %rax, %rbp
movq 0x28(%rsp), %rsi
movq 0x68(%rsp), %rbx
jl 0xcb686
cmpl $0x0, 0x18(%rsp)
setne %al
cmpl $0x0, 0x38(%rsp)
setne %cl
cmpl $0x0, 0x1c(%rsp)
setne %r14b
movl %eax, %ebx
movq 0x48(%rsp), %rbp
jmp 0xcb8da
xorl %r14d, %r14d
xorl %ecx, %ecx
xorl %ebx, %ebx
cmpq $0x20, 0x70(%r15)
jne 0xcbaec
movl 0x18(%r12), %esi
testl %esi, %esi
leaq 0x792dd(%rip), %rax     # 0x144bd0
leaq 0x707aa(%rip), %rdx     # 0x13c0a4
cmoveq %rax, %rdx
movb 0x10(%r15), %dil
xorl %r13d, %r13d
leaq 0x5c71a(%rip), %r10     # 0x128026
cmpb $0x65, %dil
je 0xcba3a
movl 0x9c(%r15), %r8d
testl %r8d, %r8d
je 0xcb943
movzbl 0x88(%r15), %eax
cmpl $0x4, %eax
ja 0xcba3a
movl $0x19, %r9d
btl %eax, %r9d
jae 0xcba3a
cmpl $0x2, 0xa4(%r15)
jne 0xcba3a
movq 0x80(%r15), %rax
testq $-0x9, %rax
jne 0xcb9bd
andb $-0x2, %dil
movl $0x1, %r13d
leaq 0x6b3fd(%rip), %r12     # 0x136d6e
cmpb $0x66, %dil
je 0xcba15
testl %r8d, %r8d
je 0xcb997
cmpq $0x8, %rax
jne 0xcb997
movb 0x88(%r15), %sil
addb $-0x3, %sil
cmpb $0x2, %sil
jb 0xcba15
movq %rbx, %rsi
andb %sil, %cl
orb %r14b, %cl
testb $0x1, %cl
je 0xcb9d9
testq %rax, %rax
jne 0xcba15
leaq 0x7073f(%rip), %rsi     # 0x13c0f0
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0xcba15
testl %esi, %esi
jne 0xcba3a
leaq 0x706e9(%rip), %rsi     # 0x13c0b1
xorl %r13d, %r13d
movq %rbp, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9b27
jmp 0xcba33
testb %sil, %sil
je 0xcb9fe
testq %rax, %rax
movq 0x8(%rsp), %r12
je 0xcba1f
leaq 0x7076e(%rip), %rsi     # 0x13c15d
xorl %r13d, %r13d
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0xcba33
leaq 0x707ba(%rip), %rsi     # 0x13c1bf
xorl %r13d, %r13d
movq %rbp, %rdi
xorl %eax, %eax
movq %r10, %r12
callq 0xe9226
movq %r12, %r10
movq 0x8(%rsp), %r12
jmp 0xcba3a
leaq 0x7076d(%rip), %rsi     # 0x13c193
xorl %r13d, %r13d
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9226
leaq 0x5c5ec(%rip), %r10     # 0x128026
movl %r13d, 0x1c(%r12)
cmpl $0x0, 0x18(%r12)
jne 0xcba5b
leaq 0x7063e(%rip), %rsi     # 0x13c08c
movq %rbp, %rdi
movq %r10, %rdx
xorl %eax, %eax
callq 0xe9226
cmpl $0x0, 0x1c(%r12)
je 0xcba6b
orb %r14b, %bl
testb $0x1, %bl
jne 0xcba74
movq (%rsp), %rdi
callq 0xf3e98
cmpl $0x0, 0x1c(%r12)
je 0xcbaec
cmpb $0x0, 0x89(%r15)
je 0xcbaec
movq (%rsp), %rax
movl 0x24(%rax), %eax
cmpl $0x4, %eax
je 0xcba97
cmpl $0x2, %eax
jne 0xcbaec
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
jle 0xcbaec
xorl %ebx, %ebx
movq (%rsp), %r12
cmpq $0x0, 0x8(%r12)
jle 0xcbae2
xorl %r14d, %r14d
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xf2bd3
movl %eax, %edi
callq 0xf429c
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %eax, %ecx
callq 0xf2b02
incq %r14
cmpq 0x8(%r12), %r14
jl 0xcbab3
incq %rbx
cmpq 0x10(%r12), %rbx
jl 0xcbaa8
cmpb $0x0, 0x8b(%r15)
movq (%rsp), %r14
je 0xcbb02
movq %r14, %rdi
callq 0xf3a1b
movl 0x30(%rsp), %eax
movl %eax, %edx
orl $0x4, %edx
cmpb $0x0, 0x8a(%r15)
cmovnel %eax, %edx
orl $0x2, %edx
movq %r14, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
callq 0xf2917
movq %r14, %rdi
callq 0xf28d4
movq %rbp, %rdi
movq %rbx, %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea9b3