// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/atari-img.c
// Function: de_run_neochrome_ani
static void de_run_neochrome_ani(deark *c, de_module_params *mparams)
{
	struct atari_img_decode_data *adata = NULL;
	i64 width_in_bytes;
	i64 nframes;
	i64 bytes_per_frame;
	i64 frame;
	i64 k;
	u32 pal[16];

	de_declare_fmt(c, "NEOchrome Animation");

	de_warn(c, "NEOchrome Animation images may not be decoded correctly.");

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));

	// TODO: What palette should we use?
	for(k=0; k<16; k++) {
		pal[k] = DE_MAKE_GRAY((unsigned int)(k*17));
	}
	adata->pal = pal;
	adata->bpp = 4;
	adata->ncolors = 16;

	width_in_bytes = de_getu16be(4); // Always a multiple of 8
	adata->w = ((width_in_bytes+7)/8)*16;
	adata->h = de_getu16be(6);
	de_dbg_dimensions(c, adata->w, adata->h);
	if(!de_good_image_dimensions(c, adata->w, adata->h)) goto done;

	bytes_per_frame = de_getu16be(8);
	bytes_per_frame -= 10;
	de_dbg(c, "bytes/frame: %d", (int)bytes_per_frame);
	if(bytes_per_frame<1) goto done;

	nframes = de_getu16be(14);
	de_dbg(c, "number of frames: %d", (int)nframes);
	if(!de_good_image_count(c, nframes)) goto done;

	for(frame=0; frame<nframes; frame++) {
		adata->unc_pixels = dbuf_open_input_subfile(c->infile, 22 + frame*bytes_per_frame, bytes_per_frame);
		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);

		de_bitmap_destroy(adata->img);
		adata->img = NULL;

		dbuf_close(adata->unc_pixels);
		adata->unc_pixels = NULL;
	}

done:
	de_free(c, adata);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0xe3c0c(%rip), %rsi     # 0x1064e3
callq 0xe07f7
leaq 0xe41b4(%rip), %rsi     # 0x106a97
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
movl $0xff000000, %eax       # imm = 0xFF000000
cmpq $0x10, %r15
je 0x22917
movl %eax, (%rsp,%r15,4)
incq %r15
addl $0x111111, %eax         # imm = 0x111111
jmp 0x22903
movq %rsp, %rax
movq %rax, 0x30(%r14)
movq $0x4, (%r14)
movq $0x10, 0x8(%r14)
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3f46
addq $0x7, %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
shlq $0x4, %rax
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rdi
pushq $0x6
popq %rsi
callq 0xe3f46
movq %rax, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x22a5f
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0xe3f46
movq %rax, %r12
leaq -0xa(%rax), %r15
leaq 0xe4132(%rip), %rsi     # 0x106ad0
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0xb, %r12
jl 0x22a5f
movq 0x18(%rbx), %rdi
pushq $0xe
popq %rsi
callq 0xe3f46
movq %rax, %r12
leaq 0xe4115(%rip), %rsi     # 0x106ae0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe6ccb
testl %eax, %eax
je 0x22a5f
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
pushq $0x16
popq %r13
pushq $0x3
popq %rbp
subq $0x1, %r12
jb 0x22a5f
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xe6248
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl %ebp, %ecx
callq 0xe7437
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xed22a
movq 0x38(%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq 0x38(%r14), %rdi
callq 0xe7084
andq $0x0, 0x38(%r14)
movq 0x20(%r14), %rdi
callq 0xe5591
andq $0x0, 0x20(%r14)
addq %r15, %r13
jmp 0x229f7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq