// File: /workspace/github_c_makefile_star2_O2/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, %r15
movl $0x418, %esi            # imm = 0x418
callq 0xdecaf
movq %rax, %r12
pushq $0x40
popq %rsi
movq %r15, %rdi
callq 0xdecaf
movq %rax, %r13
leaq 0x18(%r12), %rax
movq %rax, 0x30(%r13)
movq 0x18(%r15), %rdi
pushq $0x6
popq %rsi
callq 0xe3f46
movq %rax, (%r12)
leaq 0xe5211(%rip), %rsi     # 0x10679b
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
pushq $0x8
popq %rsi
callq 0xe3f46
movq %rax, 0x10(%r13)
movq 0x18(%r15), %rdi
pushq $0xa
popq %rsi
callq 0xe3f46
movq %rax, 0x18(%r13)
movq 0x10(%r13), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
movq %r15, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x21998
movq 0x18(%r15), %rdi
pushq $0xc
popq %rsi
callq 0xe3f46
movq %r13, 0x20(%rsp)
movq %rax, (%r13)
leaq 0x1176e5(%rip), %rsi    # 0x138cde
xorl %ebx, %ebx
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
pushq $0xe
popq %rsi
callq 0xe3f46
movl %eax, 0x8(%r12)
leaq 0xe51df(%rip), %rsi     # 0x1067fe
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
pushq $0x10
popq %rsi
callq 0xe404f
movq %rax, 0x10(%r12)
leaq 0xe5165(%rip), %rsi     # 0x1067a8
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x50(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x18140
movq %r12, 0x10(%rsp)
movq (%r12), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xdfb45
testq %rax, %rax
cmovleq %rbx, %rax
movq %rax, 0x18(%rsp)
movl $0x84, %r12d
xorl %r13d, %r13d
movq %r15, 0x8(%rsp)
movq %r15, %r14
cmpq %r13, 0x18(%rsp)
je 0x2175d
movq 0x18(%r14), %rdi
leaq -0x4(%r12), %rsi
callq 0xe3f46
movq %rax, %r15
movq %rax, 0x28(%rsp)
movq 0x18(%r14), %rdi
leaq -0x2(%r12), %rsi
callq 0xe3f46
movq %rax, %rbx
movq 0x18(%r14), %rdi
movq %r12, %rsi
callq 0xe3f46
movq %rax, %r14
movq %r15, %rdi
callq 0xe1d11
movl %eax, %ebp
movq %rbx, %rdi
callq 0xe1d11
movl %eax, %r15d
movq %r14, %rdi
callq 0xe1d11
movzbl %bpl, %ecx
shll $0x10, %ecx
movzbl %r15b, %edx
movq 0x8(%rsp), %r10
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %ebp
orl %edx, %ebp
orl $0xff000000, %ebp       # imm = 0xFF000000
leaq 0x30(%rsp), %r15
movq %r15, %rdi
pushq $0x20
popq %rsi
leaq 0xe515f(%rip), %rdx     # 0x106880
movq 0x28(%rsp), %rcx
movl %ebx, %r8d
movl %r14d, %r9d
movq %r10, %r14
xorl %eax, %eax
callq 0xde40c
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
movq %r15, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
movl %ebp, 0x50(%rsp,%r13,4)
incq %r13
addq $0x6, %r12
jmp 0x21691
xorl %eax, %eax
pushq $0x2
popq %rcx
pushq $0x1
popq %rdx
leaq 0xe4b34(%rip), %rsi     # 0x1062a0
pushq $0x3
popq %rdi
pushq $0x7
popq %r8
pushq $0x5
popq %r9
pushq $0x9
popq %r10
pushq $0xb
popq %r11
pushq $0xf
popq %rbx
pushq $0xd
popq %r14
movl $0xff, %r15d
movq 0x10(%rsp), %r12
cmpq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r13
je 0x21847
movq (%r13), %r13
movq %rax, %r12
cmpq $0x1, %r13
jne 0x217c5
movl %r12d, %r12d
movl 0x50(%rsp,%r12,4), %ebp
movq 0x10(%rsp), %r12
movl %ebp, 0x18(%r12,%rax,4)
incq %rax
jmp 0x21791
leal -0x1(%rax), %r12d
cmpl $0xe, %r12d
ja 0x217e0
movslq (%rsi,%r12,4), %rbp
addq %rsi, %rbp
movq %rcx, %r12
jmpq *%rbp
movq %rdi, %r12
jmp 0x217ae
cmpl $0xff, %eax
jne 0x217ec
movq %rdx, %r12
jmp 0x217ae
movq %rax, %r12
jmp 0x217ae
cmpq $0x8, %r13
movq %rdx, %r12
cmoveq %r15, %r12
jmp 0x217ae
movq %r14, %r12
jmp 0x217ae
movq %rbx, %r12
jmp 0x217ae
movq %r9, %r12
jmp 0x217ae
pushq $0xa
jmp 0x21838
xorl %r12d, %r12d
cmpq $0x3, %r13
setge %r12b
leaq (%r12,%r12,4), %r12
incq %r12
jmp 0x217ae
pushq $0xe
jmp 0x21838
movq %r8, %r12
jmp 0x217ae
movq %r10, %r12
jmp 0x217ae
pushq $0x8
popq %r12
jmp 0x217ae
movq %r11, %r12
jmp 0x217ae
movq (%r13), %rdx
cmpq $0x10, %rdx
ja 0x21945
movl $0x10116, %eax          # imm = 0x10116
btq %rdx, %rax
jae 0x21945
movl 0x8(%r12), %eax
cmpl $0x2, %eax
movq 0x8(%rsp), %r15
jae 0x21937
cmpq $0x10, %rdx
setne %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x21898
leaq 0xe4f80(%rip), %rsi     # 0x10680e
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf05a
imulq $0x6, (%r12), %r14
subq $-0x80, %r14
leaq 0xe4fa5(%rip), %rsi     # 0x10684d
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r15), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
jle 0x21998
cmpl $0x0, 0x8(%r12)
je 0x2195d
movl $0x1, 0x28(%r13)
movq 0x18(%r13), %rsi
imulq 0x10(%r13), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0x20(%r13)
movq %rax, %rdi
callq 0xe589c
movq 0x18(%r15), %rdi
movq 0x18(%rdi), %rdx
subq %r14, %rdx
movq 0x20(%r13), %rcx
movq %r14, %rsi
xorl %r8d, %r8d
callq 0xeed6a
movq 0x20(%r13), %rdi
callq 0xe58e8
movq 0x20(%r13), %rax
movq 0x18(%rax), %rdx
leaq 0xe4f3b(%rip), %rsi     # 0x106866
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x21969
leaq 0xe4eb4(%rip), %rsi     # 0x1067f2
movq %r15, %rdi
movl %eax, %edx
jmp 0x21954
leaq 0xe4e8b(%rip), %rsi     # 0x1067d7
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x21998
movq %r14, %rsi
callq 0xe6248
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdx
pushq $0x3
popq %rcx
movq %r15, %rdi
callq 0xe7437
movq %rax, 0x38(%r13)
movq %r15, %rdi
movq %r13, %rsi
callq 0xed22a
movq 0x38(%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq 0x20(%r13), %rdi
callq 0xe5591
movq 0x38(%r13), %rdi
callq 0xe7084
movq %r15, %rdi
movq %r13, %rsi
callq 0xded32
movq %r15, %rdi
movq %r12, %rsi
callq 0xded32
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq