// File: /workspace/github_c_makefile_star2_O2/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, 0x40(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
callq 0xdfd1a
movq %rax, %rsi
testq %rbx, %rbx
je 0xc26b4
pushq $0x5
popq %r8
movq %r14, %rdi
movq %rsi, %r12
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0xdffbd
movq %r12, %rsi
movq %r14, 0x18(%rsp)
cmpb $0x0, 0x4e8(%r15)
je 0xc26d1
leaq 0x4e8(%r15), %rax
movups (%rax), %xmm0
movups %xmm0, 0x68(%rsi)
cmpq $0x1, 0x70(%r15)
movq 0x18(%rsp), %rdi
movq 0x40(%rsp), %r14
movq %rsi, 0x58(%rsp)
jne 0xc2711
leaq 0x6ca0a(%rip), %rsi     # 0x12f0f8
xorl %eax, %eax
callq 0xdf05a
movq (%r13), %rdi
pushq $0x8
popq %rbx
movq %rbx, %rsi
callq 0xdfbb6
movq %rax, %r9
cqto
idivq %rbx
movq %rax, %rbp
jmp 0xc2723
movq (%r13), %rax
movq 0xb8(%r15), %rbp
movq %rax, %r9
imulq %rax, %rbp
pushq $0x1
popq %r8
cmpl $0x3, 0xa4(%r15)
je 0xc2749
movq 0x70(%r15), %rax
xorl %ecx, %ecx
cmpq $0x20, %rax
sete %cl
addl $0x3, %ecx
cmpq $0x1, %rax
cmovnel %ecx, %r8d
xorl %eax, %eax
cmpl $0x1, 0xc(%r15)
setne %al
movl %eax, 0x4c(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rcx
movq 0x18(%rsp), %rdi
movq %r9, %rbx
movq %r9, %rdx
callq 0xe746f
movq %rax, 0x8(%rsp)
movb 0x8c(%r15), %al
xorl %ecx, %ecx
cmpb $0x2, %al
sete %cl
leaq (%rcx,%rcx,2), %rcx
incq %rcx
cmpb $0x1, %al
pushq $0x2
popq %rax
cmovneq %rcx, %rax
movq %rax, 0x68(%rsp)
xorl %r12d, %r12d
testq %rbx, %rbx
cmovleq %r12, %rbx
movq $0x0, 0x50(%rsp)
xorl %edx, %edx
movl $0x0, 0x14(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, 0x34(%rsp)
movq %r13, 0x38(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x8(%r13), %rax
cmpq %rax, %r12
jge 0xc29bd
movq 0x68(%rsp), %rcx
leaq (%rdx,%rcx), %rsi
movq %rdx, %rcx
movq 0x50(%rsp), %rdx
leaq 0x1(%rdx), %rdi
cmpq %rax, %rsi
cmovgeq %rdi, %rdx
movq %rdx, 0x50(%rsp)
cmovlq %rsi, %rdi
movq %rdi, 0x78(%rsp)
movq %r12, 0x80(%rsp)
imulq %rbp, %r12
cmpq $0x1, 0x70(%r15)
jne 0xc2830
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
xorl %r8d, %r8d
callq 0xe779f
jmp 0xc29a8
movq %rcx, 0x28(%rsp)
leaq 0x3(%r12), %rax
movq %rax, 0x70(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
cmpq %rbp, %rbx
je 0xc299e
movl 0xa4(%r15), %eax
cmpl $0x1, %eax
je 0xc28ce
cmpl $0x3, %eax
je 0xc28a3
cmpl $0x2, %eax
jne 0xc28ff
movq 0x70(%r15), %rax
addq $-0xf, %rax
movq 0xb8(%r15), %rsi
imulq %rbp, %rsi
addq %r12, %rsi
movq %r14, %rdi
cmpq $0x1, %rax
ja 0xc2907
callq 0xe3fa3
movl %eax, %edi
callq 0xe1ea3
movq %r13, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
movl %eax, %ecx
jmp 0xc28fa
movq 0xb8(%r15), %rsi
imulq %rbp, %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0xe3eb4
movzbl %al, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0xe71da
jmp 0xc28ff
movq 0xb8(%r15), %rsi
imulq %rbp, %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0xe3eb4
movzbl %al, %eax
movl 0xe8(%r15,%rax,4), %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0xe7287
incq %rbp
jmp 0xc2846
movl 0x4c(%rsp), %edx
callq 0xe45a4
movl %eax, %r13d
cmpq $0x20, 0x70(%r15)
jne 0xc2972
movq 0xb8(%r15), %rsi
imulq %rbp, %rsi
addq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0xe3eb4
andl $0xffffff, %r13d        # imm = 0xFFFFFF
movzbl %al, %r14d
movl %r14d, %ecx
shll $0x18, %ecx
orl %r13d, %ecx
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
callq 0xe728c
pushq $0x1
popq %rax
testb %r14b, %r14b
je 0xc298a
cmpl $0xff, %r14d
jne 0xc2990
movl %eax, 0x34(%rsp)
jmp 0xc2994
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %rdx
movl %r13d, %ecx
movq %rdi, %r13
jmp 0xc28fa
movl %eax, 0x14(%rsp)
jmp 0xc2994
movl %eax, 0x24(%rsp)
movq 0x40(%rsp), %r14
jmp 0xc28ff
movq 0x38(%rsp), %r13
movq 0x60(%rsp), %rbp
movq 0x80(%rsp), %r12
incq %r12
movq 0x78(%rsp), %rdx
jmp 0xc27ce
cmpq $0x20, 0x70(%r15)
jne 0xc2b7e
movl 0x18(%r13), %ecx
testl %ecx, %ecx
leaq 0x7528b(%rip), %rax     # 0x137c60
leaq 0x6c778(%rip), %rdx     # 0x12f154
cmoveq %rax, %rdx
movb 0x10(%r15), %sil
xorl %ebp, %ebp
leaq 0x58629(%rip), %rbx     # 0x11b016
cmpb $0x65, %sil
je 0xc2ac7
movl 0x9c(%r15), %edi
testl %edi, %edi
je 0xc2a21
movzbl 0x88(%r15), %eax
cmpl $0x4, %eax
ja 0xc2ac7
pushq $0x19
popq %r8
btl %eax, %r8d
jae 0xc2ac7
cmpl $0x2, 0xa4(%r15)
jne 0xc2ac7
movq 0x80(%r15), %rax
testq $-0x9, %rax
jne 0xc2aac
andb $-0x2, %sil
pushq $0x1
popq %rbp
leaq 0x673ec(%rip), %r14     # 0x129e38
cmpb $0x66, %sil
je 0xc2aa7
testl %edi, %edi
je 0xc2a6e
movzbl 0x88(%r15), %ecx
cmpl $0x4, %ecx
je 0xc2a68
cmpl $0x3, %ecx
jne 0xc2a6e
cmpq $0x8, %rax
je 0xc2aa7
cmpl $0x0, 0x24(%rsp)
jne 0xc2a8f
cmpl $0x0, 0x14(%rsp)
sete %cl
cmpl $0x0, 0x34(%rsp)
sete %sil
orb %cl, %sil
jne 0xc2be2
testq %rax, %rax
jne 0xc2aa7
leaq 0x6c705(%rip), %rsi     # 0x12f1a0
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0xdf05a
movq %r14, %rbx
jmp 0xc2ac7
testl %ecx, %ecx
jne 0xc2ac7
leaq 0x6c6aa(%rip), %rsi     # 0x12f161
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xdf05a
movq 0x38(%rsp), %rax
movl %ebp, 0x1c(%rax)
cmpl $0x0, 0x18(%rax)
jne 0xc2af3
leaq 0x6c660(%rip), %rsi     # 0x12f13c
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x38(%rsp), %rax
movl 0x1c(%rax), %ebp
testl %ebp, %ebp
movq 0x38(%rsp), %rbx
je 0xc2b06
movl 0x24(%rsp), %eax
orl %eax, 0x14(%rsp)
jne 0xc2b16
movq 0x8(%rsp), %rdi
callq 0xe8472
cmpl $0x0, 0x1c(%rbx)
je 0xc2b7e
cmpb $0x0, 0x89(%r15)
je 0xc2b7e
movq 0x8(%rsp), %rax
movl 0x24(%rax), %eax
cmpl $0x4, %eax
je 0xc2b32
cmpl $0x2, %eax
jne 0xc2b7e
xorl %ebx, %ebx
movq 0x8(%rsp), %r12
cmpq 0x10(%r12), %rbx
jge 0xc2b7e
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
cmpq 0x8(%r12), %r14
jge 0xc2b79
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xe735f
movl %eax, %edi
callq 0xe8851
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %eax, %ecx
callq 0xe728c
incq %r14
jmp 0xc2b48
incq %rbx
jmp 0xc2b39
cmpb $0x0, 0x8b(%r15)
movq 0x8(%rsp), %r14
je 0xc2b95
movq %r14, %rdi
callq 0xe8086
movl 0x48(%rsp), %eax
movl %eax, %edx
orl $0x4, %edx
cmpb $0x0, 0x8a(%r15)
cmovnel %eax, %edx
orl $0x2, %edx
movq %r14, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0xe70ab
movq %r14, %rdi
callq 0xe7084
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdfd24
cmpl $0x0, 0x14(%rsp)
je 0xc2c08
testq %rax, %rax
movq 0x18(%rsp), %rdi
je 0xc2c18
leaq 0x6c613(%rip), %rsi     # 0x12f20d
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xdf05a
jmp 0xc2ac7
leaq 0x6c660(%rip), %rsi     # 0x12f26f
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
jmp 0xc2c21
leaq 0x6c624(%rip), %rsi     # 0x12f243
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xde896
jmp 0xc2ac7