// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/vort.c
// Function: do_image
static void do_image(deark *c, lctx *d)
{
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	dbuf *unc_pixels = NULL;
	i64 i, j;
	i64 pos = 0;
	u8 uses_colormap = 0;
	u8 has_alpha = 0;
	i64 bytes_per_pixel = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	if(d->image_data_pos==0) goto done;

	de_dbg(c, "image data at %"I64_FMT, d->image_data_pos);
	de_dbg_indent(c, 1);

	if(!de_good_image_dimensions(c, d->image_width, d->image_height)) goto done;

	if(d->image_colormap_addr!=0 && !d->image_alpha_flag &&
		(d->reported_image_depth==8 || d->reported_image_depth==24))
	{
		bytes_per_pixel = 1;
		uses_colormap = 1;
	}
	else if(d->image_colormap_addr!=0 && d->image_alpha_flag &&
		(d->reported_image_depth==16 || d->reported_image_depth==32))
	{
		bytes_per_pixel = 2;
		uses_colormap = 1;
		has_alpha = 1;
	}
	else if(d->image_colormap_addr==0 && !d->image_alpha_flag && d->reported_image_depth==24)
	{
		bytes_per_pixel = 3;
	}
	else if(d->image_colormap_addr==0 && d->image_alpha_flag && d->reported_image_depth==32)
	{
		bytes_per_pixel = 4;
		has_alpha = 1;
	}

	if(!bytes_per_pixel) {
		d->need_errmsg = 1;
		goto done;
	}

	if(uses_colormap) {
		// FIXME: We're cheating, by assuming there's only one colormap in the file.
		if(d->colormap_pos==0) {
			de_err(c, "Missing colormap");
			goto done;
		}
	}

	de_dbg(c, "interpreted bytes/pixel: %d", (int)bytes_per_pixel);

	img = de_bitmap_create(c, d->image_width, d->image_height, has_alpha?4:3);

	if(d->rle_flag) {
		unc_pixels = dbuf_create_membuf(c, 0, 0);
		dbuf_enable_wbuffer(unc_pixels);
		do_decompress(c, d, d->image_data_pos, unc_pixels,
			d->image_width*d->image_height, bytes_per_pixel);
	}
	else {
		unc_pixels = dbuf_open_input_subfile(c->infile,
			d->image_data_pos, c->infile->len-d->image_data_pos);
	}

	for(j=0; j<d->image_height; j++) {
		for(i=0; i<d->image_width; i++) {
			u8 b;
			de_color clr;

			if(uses_colormap) {
				b = dbuf_getbyte_p(unc_pixels, &pos);
				clr = d->pal[(UI)b];
			}
			else {
				clr = dbuf_getRGB(unc_pixels, pos, 0);
				pos += 3;
			}
			if(has_alpha) {
				b = dbuf_getbyte_p(unc_pixels, &pos);
				clr = DE_SET_ALPHA(clr, b);
			}
			de_bitmap_setpixel_rgba(img, i, j, clr);
		}
	}

	fi = de_finfo_create(c);
	fi->internal_mod_time = d->timestamp;

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movb $0x0, -0x42(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x137cfd
jmp 0x138042
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x8ed55(%rip), %rsi     # 0x1c6a65
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x137d48
jmp 0x138042
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x137d84
movq -0x10(%rbp), %rax
cmpb $0x0, 0x41(%rax)
jne 0x137d84
movq -0x10(%rbp), %rax
cmpq $0x8, 0x20(%rax)
je 0x137d73
movq -0x10(%rbp), %rax
cmpq $0x18, 0x20(%rax)
jne 0x137d84
movq $0x1, -0x50(%rbp)
movb $0x1, -0x41(%rbp)
jmp 0x137e23
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x137dc4
movq -0x10(%rbp), %rax
movzbl 0x41(%rax), %eax
cmpl $0x0, %eax
je 0x137dc4
movq -0x10(%rbp), %rax
cmpq $0x10, 0x20(%rax)
je 0x137db2
movq -0x10(%rbp), %rax
cmpq $0x20, 0x20(%rax)
jne 0x137dc4
movq $0x2, -0x50(%rbp)
movb $0x1, -0x41(%rbp)
movb $0x1, -0x42(%rbp)
jmp 0x137e21
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x137dee
movq -0x10(%rbp), %rax
cmpb $0x0, 0x41(%rax)
jne 0x137dee
movq -0x10(%rbp), %rax
cmpq $0x18, 0x20(%rax)
jne 0x137dee
movq $0x3, -0x50(%rbp)
jmp 0x137e1f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x137e1d
movq -0x10(%rbp), %rax
movzbl 0x41(%rax), %eax
cmpl $0x0, %eax
je 0x137e1d
movq -0x10(%rbp), %rax
cmpq $0x20, 0x20(%rax)
jne 0x137e1d
movq $0x4, -0x50(%rbp)
movb $0x1, -0x42(%rbp)
jmp 0x137e1f
jmp 0x137e21
jmp 0x137e23
cmpq $0x0, -0x50(%rbp)
jne 0x137e36
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x138042
cmpb $0x0, -0x41(%rbp)
je 0x137e60
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x137e5e
movq -0x8(%rbp), %rdi
leaq 0x9456d(%rip), %rsi     # 0x1cc3bf
movb $0x0, %al
callq 0x15bfb0
jmp 0x138042
jmp 0x137e60
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x9455f(%rip), %rsi     # 0x1cc3d0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movzbl -0x42(%rbp), %r8d
movl $0x3, %ecx
movl $0x4, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x40(%rax)
je 0x137f01
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %r8
movq -0x10(%rbp), %rax
imulq 0x18(%rax), %r8
movq -0x50(%rbp), %r9
callq 0x138630
jmp 0x137f2e
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x8(%rax), %rdx
callq 0x1691b0
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x13800b
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x137ff8
cmpb $0x0, -0x41(%rbp)
je 0x137f8b
movq -0x28(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x55(%rbp)
movq -0x10(%rbp), %rax
movzbl -0x55(%rbp), %ecx
movl %ecx, %ecx
movl 0x58(%rax,%rcx,4), %eax
movl %eax, -0x5c(%rbp)
jmp 0x137fa9
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, -0x5c(%rbp)
movq -0x40(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x40(%rbp)
cmpb $0x0, -0x42(%rbp)
je 0x137fd3
movq -0x28(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x55(%rbp)
movl -0x5c(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movzbl -0x55(%rbp), %ecx
shll $0x18, %ecx
orl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x5c(%rbp), %ecx
callq 0x16b350
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x137f50
jmp 0x137ffa
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x137f36
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rdx
movq %rdx, 0x68(%rax)
movq 0x50(%rcx), %rcx
movq %rcx, 0x70(%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x28(%rbp), %rdi
callq 0x1676a0
movq -0x18(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x15b5a0
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)