// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/atari-img.c
// Function: de_run_prismpaint
static void de_run_prismpaint(deark *c, de_module_params *mparams)
{
	prismctx *d = NULL;
	i64 pixels_start;
	struct atari_img_decode_data *adata = NULL;

	d = de_malloc(c, sizeof(prismctx));

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));

	adata->pal = d->pal;
	d->pal_size = de_getu16be(6);
	de_dbg(c, "pal_size: %d", (int)d->pal_size);
	adata->w = de_getu16be(8);
	adata->h = de_getu16be(10);
	de_dbg_dimensions(c, adata->w, adata->h);
	if(!de_good_image_dimensions(c, adata->w, adata->h)) goto done;

	adata->bpp = de_getu16be(12);
	de_dbg(c, "bits/pixel: %d", (int)adata->bpp);
	d->compression_code = (UI)de_getu16be(14);
	de_dbg(c, "compression: %u", d->compression_code);

	d->pic_data_size = de_getu32be(16);
	de_dbg(c, "reported (uncompressed) picture data size: %"I64_FMT, d->pic_data_size);

	do_prism_read_palette(c, d, adata);

	if(adata->bpp!=1 && adata->bpp!=2 && adata->bpp!=4
		&& adata->bpp!=8 && adata->bpp!=16)
	{
		de_err(c, "Unsupported bits/pixel: %d", (int)adata->bpp);
		goto done;
	}
	if(d->compression_code!=0 && d->compression_code!=1) {
		de_err(c, "Unsupported compression: %u", d->compression_code);
		goto done;
	}
	if(adata->bpp==16 && d->compression_code!=0) {
		de_warn(c, "Compressed 16-bit image support is untested, and may not work.");
	}

	pixels_start = 128 + 2*3*d->pal_size;
	de_dbg(c, "pixel data starts at %"I64_FMT, pixels_start);
	if(pixels_start >= c->infile->len) goto done;

	if(d->compression_code==0) {
		adata->unc_pixels = dbuf_open_input_subfile(c->infile, pixels_start,
			c->infile->len - pixels_start);
	}
	else {
		adata->was_compressed = 1;
		// TODO: Calculate the initial size more accurately.
		adata->unc_pixels = dbuf_create_membuf(c, adata->w*adata->h, 0);
		//dbuf_set_max_length(unc_pixels, ...);
		dbuf_enable_wbuffer(adata->unc_pixels);

		fmtutil_decompress_packbits(c->infile, pixels_start, c->infile->len - pixels_start,
			adata->unc_pixels, NULL);
		dbuf_flush(adata->unc_pixels);
		de_dbg(c, "decompressed to %"I64_FMT" bytes", adata->unc_pixels->len);
	}

	adata->img = de_bitmap_create(c, adata->w, adata->h, 3);
	fmtutil_atari_decode_image(c, adata);
	de_bitmap_write_to_file(adata->img, NULL, 0);

done:
	if(adata) {
		dbuf_close(adata->unc_pixels);
		de_bitmap_destroy(adata->img);
		de_free(c, adata);
	}
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, %r12
movl $0x418, %esi            # imm = 0x418
callq 0xe8b2b
movq %rax, %rbx
movl $0x40, %esi
movq %r12, %rdi
callq 0xe8b2b
movq %rax, %r15
leaq 0x18(%rbx), %r14
movq %r14, 0x30(%rax)
movq 0x18(%r12), %rdi
movl $0x6, %esi
callq 0xeeb67
movq %rax, (%rbx)
leaq 0xf1bcd(%rip), %rsi     # 0x1137fb
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movl $0x8, %esi
callq 0xeeb67
movq %rax, 0x10(%r15)
movq 0x18(%r12), %rdi
movl $0xa, %esi
callq 0xeeb67
movq %rax, 0x18(%r15)
movq 0x10(%r15), %rsi
movq %r12, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r12, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x2204f
movq 0x18(%r12), %rdi
movl $0xc, %esi
callq 0xeeb67
movq %rax, (%r15)
leaq 0x12448e(%rip), %rsi    # 0x14612e
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movl $0xe, %esi
callq 0xeeb67
movl %eax, 0x8(%rbx)
leaq 0xf1b99(%rip), %rsi     # 0x11385e
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movl $0x10, %esi
callq 0xeed0d
movq %rax, 0x10(%rbx)
leaq 0xf1b1d(%rip), %rsi     # 0x113808
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x50(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
movq (%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xe9c3b
testq %rax, %rax
jle 0x21f03
movq %r14, (%rsp)
movq %r15, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl $0x84, %r13d
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%r12), %rdi
leaq -0x4(%r13), %rsi
callq 0xeeb67
movq %rax, %rbx
movq %rax, 0x28(%rsp)
movq 0x18(%r12), %rdi
leaq -0x2(%r13), %rsi
callq 0xeeb67
movq %rax, %r14
movq 0x18(%r12), %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rax, %r12
movq %rbx, %rdi
callq 0xec7af
movl %eax, %ebx
movq %r14, %rdi
callq 0xec7af
movl %eax, %ebp
movq %r12, %rdi
callq 0xec7af
movzbl %bl, %ecx
shll $0x10, %ecx
movzbl %bpl, %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %ebx
orl %edx, %ebx
orl $0xff000000, %ebx       # imm = 0xFF000000
movl $0x20, %esi
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
leaq 0xf1b1f(%rip), %rdx     # 0x1138e0
movq 0x28(%rsp), %rcx
movl %r14d, %r8d
movl %r12d, %r9d
movq 0x20(%rsp), %r12
xorl %eax, %eax
callq 0xe8193
movq %r12, %rdi
movq %r15, %rsi
movl %ebx, %edx
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
movq 0x18(%rsp), %rax
movl %ebx, 0x50(%rsp,%r15,4)
incq %r15
addq $0x6, %r13
cmpq %r15, %rax
jne 0x21d40
movq 0x8(%rsp), %r15
movq (%r15), %rdx
cmpq $0x8, %rdx
movl $0xff, %ecx
movl $0x1, %r10d
cmoveq %rcx, %r10
xorl %ecx, %ecx
cmpq $0x3, %rdx
setge %cl
leaq (%rcx,%rcx,4), %rcx
incq %rcx
xorl %esi, %esi
leaq 0xf14c4(%rip), %rdi     # 0x113300
movq (%rsp), %r9
movq %rsi, %r8
cmpq $0x1, %rdx
jne 0x21e62
movl %r8d, %r8d
movl 0x50(%rsp,%r8,4), %r8d
movl %r8d, (%r9,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x21e40
jmp 0x21efc
leal -0x1(%rsi), %r8d
cmpl $0xe, %r8d
ja 0x21e7e
movslq (%rdi,%r8,4), %r8
addq %rdi, %r8
jmpq *%r8
movl $0x2, %r8d
jmp 0x21e49
cmpl $0xff, %esi
jne 0x21e8e
movl $0x1, %r8d
jmp 0x21e49
movq %rsi, %r8
jmp 0x21e49
movq %r10, %r8
jmp 0x21e49
movl $0xd, %r8d
jmp 0x21e49
movl $0xf, %r8d
jmp 0x21e49
movl $0x3, %r8d
jmp 0x21e49
movl $0x5, %r8d
jmp 0x21e49
movl $0xa, %r8d
jmp 0x21e49
movq %rcx, %r8
jmp 0x21e49
movl $0xe, %r8d
jmp 0x21e49
movl $0x7, %r8d
jmp 0x21e49
movl $0x9, %r8d
jmp 0x21e49
movl $0x8, %r8d
jmp 0x21e49
movl $0xb, %r8d
jmp 0x21e49
movq 0x10(%rsp), %rbx
jmp 0x21f06
movq (%r15), %rdx
cmpq $0x10, %rdx
ja 0x21fff
movl $0x10116, %eax          # imm = 0x10116
btq %rdx, %rax
jae 0x21fff
movl 0x8(%rbx), %eax
cmpl $0x2, %eax
jae 0x21ff1
cmpq $0x10, %rdx
setne %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x21f4c
leaq 0xf192c(%rip), %rsi     # 0x11386e
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8faa
movq (%rbx), %rax
leaq (%rax,%rax,2), %rax
leaq 0x80(,%rax,2), %r14
leaq 0xf194b(%rip), %rsi     # 0x1138ad
movq %r12, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r12), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
jle 0x2204f
cmpl $0x0, 0x8(%rbx)
je 0x22012
movl $0x1, 0x28(%r15)
movq 0x18(%r15), %rsi
imulq 0x10(%r15), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x20(%r15)
movq %rax, %rdi
callq 0xf0855
movq 0x18(%r12), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
movq 0x20(%r15), %rcx
movq %r14, %rsi
xorl %r8d, %r8d
callq 0xfa81c
movq 0x20(%r15), %rdi
callq 0xf08ba
movq 0x20(%r15), %rax
movq 0x18(%rax), %rdx
leaq 0xf18e1(%rip), %rsi     # 0x1138c6
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x2201e
leaq 0xf185a(%rip), %rsi     # 0x113852
movq %r12, %rdi
movl %eax, %edx
jmp 0x22009
leaq 0xf1831(%rip), %rsi     # 0x113837
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x2204f
movq %r14, %rsi
callq 0xf1388
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r12, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, 0x38(%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0xf8b10
movq 0x38(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq 0x20(%r15), %rdi
callq 0xf052b
movq 0x38(%r15), %rdi
callq 0xf228d
movq %r12, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %r12, %rdi
movq %rbx, %rsi
callq 0xe8be2
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq