// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %rbx
movl $0x40, %esi
movq %r12, %rdi
callq 0xe969b
movq %rax, %r15
leaq 0x18(%rbx), %r14
movq %r14, 0x30(%rax)
movq 0x18(%r12), %rdi
movl $0x6, %esi
callq 0xef169
movq %rax, (%rbx)
leaq 0xf1b7b(%rip), %rsi     # 0x11374b
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movl $0x8, %esi
callq 0xef169
movq %rax, 0x10(%r15)
movq 0x18(%r12), %rdi
movl $0xa, %esi
callq 0xef169
movq %rax, 0x18(%r15)
movq 0x10(%r15), %rsi
movq %r12, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r12, %rdi
callq 0xf241d
testl %eax, %eax
je 0x21ffa
movq 0x18(%r12), %rdi
movl $0xc, %esi
callq 0xef169
movq %rax, (%r15)
leaq 0x124002(%rip), %rsi    # 0x145c44
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movl $0xe, %esi
callq 0xef169
movl %eax, 0x8(%rbx)
leaq 0xf1b47(%rip), %rsi     # 0x1137ae
movq %r12, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movl $0x10, %esi
callq 0xef32a
movq %rax, 0x10(%rbx)
leaq 0xf1acb(%rip), %rsi     # 0x113758
movq %r12, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
leaq 0x50(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
movq (%rbx), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xea792
testq %rax, %rax
jle 0x21eae
movq %r14, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movl $0x84, %r13d
xorl %r15d, %r15d
movq %r12, (%rsp)
movq %rax, 0x20(%rsp)
movq 0x18(%r12), %rdi
leaq -0x4(%r13), %rsi
callq 0xef169
movq %rax, %rbx
movq %rax, 0x28(%rsp)
movq 0x18(%r12), %rdi
leaq -0x2(%r13), %rsi
callq 0xef169
movq %rax, %r12
movq (%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rsi
callq 0xef169
movq %rax, %r14
movq %rbx, %rdi
callq 0xece08
movl %eax, %ebx
movq %r12, %rdi
callq 0xece08
movl %eax, %ebp
movq %r14, %rdi
callq 0xece08
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 0xf1aca(%rip), %rdx     # 0x113830
movq 0x28(%rsp), %rcx
movl %r12d, %r8d
movq (%rsp), %r12
movl %r14d, %r9d
xorl %eax, %eax
callq 0xe8d67
movq %r12, %rdi
movq %r15, %rsi
movl %ebx, %edx
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
movq 0x20(%rsp), %rax
movl %ebx, 0x50(%rsp,%r15,4)
incq %r15
addq $0x6, %r13
cmpq %r15, %rax
jne 0x21ce2
testq %rax, %rax
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r9
jle 0x21eae
movq (%r15), %r10
cmpq $0x8, %r10
movl $0xff, %edx
movl $0x1, %ecx
cmoveq %rdx, %rcx
xorl %edx, %edx
cmpq $0x3, %r10
setge %dl
leaq (%rdx,%rdx,4), %rdx
incq %rdx
xorl %esi, %esi
leaq 0xf145e(%rip), %rdi     # 0x113250
movq %rsi, %r8
cmpq $0x1, %r10
jne 0x21e14
movl %r8d, %r8d
movl 0x50(%rsp,%r8,4), %r8d
movl %r8d, (%r9,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x21df2
jmp 0x21eae
leal -0x1(%rsi), %r8d
cmpl $0xe, %r8d
ja 0x21e30
movslq (%rdi,%r8,4), %r8
addq %rdi, %r8
jmpq *%r8
movl $0x2, %r8d
jmp 0x21dfb
cmpl $0xff, %esi
jne 0x21e40
movl $0x1, %r8d
jmp 0x21dfb
movq %rsi, %r8
jmp 0x21dfb
movq %rcx, %r8
jmp 0x21dfb
movl $0xd, %r8d
jmp 0x21dfb
movl $0xf, %r8d
jmp 0x21dfb
movl $0x3, %r8d
jmp 0x21dfb
movl $0x5, %r8d
jmp 0x21dfb
movl $0xa, %r8d
jmp 0x21dfb
movq %rdx, %r8
jmp 0x21dfb
movl $0xe, %r8d
jmp 0x21dfb
movl $0x7, %r8d
jmp 0x21dfb
movl $0x9, %r8d
jmp 0x21dfb
movl $0x8, %r8d
jmp 0x21dfb
movl $0xb, %r8d
jmp 0x21dfb
movq (%r15), %rdx
cmpq $0x10, %rdx
ja 0x21faa
movl $0x10116, %eax          # imm = 0x10116
btq %rdx, %rax
jae 0x21faa
movl 0x8(%rbx), %eax
cmpl $0x2, %eax
jae 0x21f9c
cmpq $0x10, %rdx
setne %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x21ef7
leaq 0xf18d1(%rip), %rsi     # 0x1137be
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9b27
movq (%rbx), %rax
leaq (%rax,%rax,2), %rax
leaq 0x80(,%rax,2), %r14
leaq 0xf18f0(%rip), %rsi     # 0x1137fd
movq %r12, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r12), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
jle 0x21ffa
cmpl $0x0, 0x8(%rbx)
je 0x21fbd
movl $0x1, 0x28(%r15)
movq 0x18(%r15), %rsi
imulq 0x10(%r15), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x20(%r15)
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%r12), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
movq 0x20(%r15), %rcx
movq %r14, %rsi
xorl %r8d, %r8d
callq 0xfad1a
movq 0x20(%r15), %rdi
callq 0xf0ea4
movq 0x20(%r15), %rax
movq 0x18(%rax), %rdx
leaq 0xf1886(%rip), %rsi     # 0x113816
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x21fc9
leaq 0xf17ff(%rip), %rsi     # 0x1137a2
movq %r12, %rdi
movl %eax, %edx
jmp 0x21fb4
leaq 0xf17d6(%rip), %rsi     # 0x113787
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x21ffa
movq %r14, %rsi
callq 0xf1988
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %r12, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, 0x38(%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0xf900c
movq 0x38(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
testq %r15, %r15
je 0x2201c
movq 0x20(%r15), %rdi
callq 0xf0b13
movq 0x38(%r15), %rdi
callq 0xf28d4
movq %r12, %rdi
movq %r15, %rsi
callq 0xe9752
movq %r12, %rdi
movq %rbx, %rsi
callq 0xe9752
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq