// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/atari-img.c
// Function: de_run_neochrome
static void de_run_neochrome(deark *c, de_module_params *mparams)
{
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	unsigned int resolution_code;
	int is_grayscale;
	u32 pal[16];

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata->pal = pal;

	resolution_code = (unsigned int)de_getu16be(2);
	de_dbg(c, "resolution code: %u", resolution_code);
	if(resolution_code!=0) {
		de_err(c, "Invalid or unsupported NEOchrome image (resolution=%d)", (int)resolution_code);
		goto done;
	}

	// TODO: Warn about palette animation settings.
	// TODO: (Maybe) Use the embedded filename, if it seems valid.

	adata->bpp = 4;
	adata->w = 320;
	adata->h = 200;
	adata->ncolors = de_pow2(adata->bpp);
	de_dbg(c, "dimensions: %d"DE_CHAR_TIMES"%d, colors: %d", (int)adata->w, (int)adata->h, (int)adata->ncolors);

	fmtutil_read_atari_palette(c, c->infile, 4, adata->pal, 16, adata->ncolors, 0);
	adata->unc_pixels = dbuf_open_input_subfile(c->infile, 128, 32000);
	is_grayscale = de_is_grayscale_palette(adata->pal, adata->ncolors);
	adata->img = de_bitmap_create(c, adata->w, adata->h, is_grayscale?1:3);
	fi = de_finfo_create(c);
	fmtutil_atari_set_standard_density(c, adata, fi);
	fmtutil_atari_decode_image(c, adata);
	de_bitmap_write_to_file_finfo(adata->img, fi, 0);

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

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movl $0x40, %esi
callq 0xe969b
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xef169
movq %rax, %r12
leaq 0xf076f(%rip), %rsi     # 0x1135a0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
testl %r12d, %r12d
je 0x22e5f
leaq 0xf0bc2(%rip), %rsi     # 0x113a0c
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9df7
jmp 0x22f3e
movq $0x4, (%r14)
movq $0x140, 0x10(%r14)      # imm = 0x140
movq $0xc8, 0x18(%r14)
movl $0x4, %edi
callq 0xea7f2
movq %rax, 0x8(%r14)
movl 0x10(%r14), %edx
movl 0x18(%r14), %ecx
leaq 0xf0748(%rip), %rsi     # 0x1135db
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rsi
movq 0x8(%r14), %r9
movq 0x30(%r14), %rcx
movl $0x0, (%rsp)
movl $0x4, %edx
movl $0x10, %r8d
movq %rbx, %rdi
callq 0xf8c62
movq 0x18(%rbx), %rdi
movl $0x80, %esi
movl $0x7d00, %edx           # imm = 0x7D00
callq 0xf1988
movq %rax, 0x20(%r14)
movq 0x8(%r14), %rsi
movq 0x30(%r14), %rdi
callq 0xf2544
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
movq %rbx, %rdi
callq 0xf2cab
movq %rax, 0x38(%r14)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xf9323
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf900c
movq 0x38(%r14), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xf2917
testq %r14, %r14
je 0x22f60
movq 0x20(%r14), %rdi
callq 0xf0b13
movq 0x38(%r14), %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea9b3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq