// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x40
popq %rsi
callq 0xdecaf
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rsi
callq 0xe3f46
movq %rax, %r12
leaq 0xe3ed3(%rip), %rsi     # 0x1065f0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
testl %r12d, %r12d
je 0x2274b
leaq 0xe4326(%rip), %rsi     # 0x106a5c
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x22825
movq $0x4, (%r14)
movq $0x140, 0x10(%r14)      # imm = 0x140
movq $0xc8, 0x18(%r14)
pushq $0x4
popq %r15
movq %r15, %rdi
callq 0xdfba1
movq %rax, 0x8(%r14)
movl 0x10(%r14), %edx
movl 0x18(%r14), %ecx
leaq 0xe3eaa(%rip), %rsi     # 0x10662b
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rsi
movq 0x8(%r14), %r9
movq 0x30(%r14), %rcx
andl $0x0, (%rsp)
pushq $0x10
popq %r8
movq %rbx, %rdi
movq %r15, %rdx
callq 0xecea5
movq 0x18(%rbx), %rdi
movl $0x80, %esi
movl $0x7d00, %edx           # imm = 0x7D00
callq 0xe6248
movq %rax, 0x20(%r14)
movq 0x8(%r14), %rsi
movq 0x30(%r14), %rdi
callq 0xe6d06
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 0xe7437
movq %rax, 0x38(%r14)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xed4e6
movq %rbx, %rdi
movq %r14, %rsi
callq 0xed22a
movq 0x38(%r14), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xe70ab
movq 0x20(%r14), %rdi
callq 0xe5591
movq 0x38(%r14), %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdfd24
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq