// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xeeb67
movq %rax, %r12
leaq 0xf0818(%rip), %rsi     # 0x113650
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
testl %r12d, %r12d
je 0x22e66
leaq 0xf0c6b(%rip), %rsi     # 0x113abc
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9295
jmp 0x22f45
movq $0x4, (%r14)
movq $0x140, 0x10(%r14)      # imm = 0x140
movq $0xc8, 0x18(%r14)
movl $0x4, %edi
callq 0xe9c9b
movq %rax, 0x8(%r14)
movl 0x10(%r14), %edx
movl 0x18(%r14), %ecx
leaq 0xf07f1(%rip), %rsi     # 0x11368b
movq %rbx, %rdi
movl %eax, %r8d
xorl %eax, %eax
callq 0xe8617
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 0xf876e
movq 0x18(%rbx), %rdi
movl $0x80, %esi
movl $0x7d00, %edx           # imm = 0x7D00
callq 0xf1388
movq %rax, 0x20(%r14)
movq 0x8(%r14), %rsi
movq 0x30(%r14), %rdi
callq 0xf1ed8
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 0xf266a
movq %rax, 0x38(%r14)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0xf8e1c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf8b10
movq 0x38(%r14), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xf22d0
movq 0x20(%r14), %rdi
callq 0xf052b
movq 0x38(%r14), %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea50e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq