// File: /workspace/github_c_makefile_star2_O1/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 0xf049d(%rip), %rsi     # 0x113493
callq 0xeb6d6
leaq 0xf0a45(%rip), %rsi     # 0x113a47
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, (%rsp,%r15,4)
incq %r15
addl $0x111111, %eax         # imm = 0x111111
cmpq $0x10, %r15
jne 0x23024
movq %rsp, %rax
movq %rax, 0x30(%r14)
movq $0x4, (%r14)
movq $0x10, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef169
leaq 0x7(%rax), %rcx
addq $0xe, %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
addq %rax, %rax
andq $-0x10, %rax
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xef169
movq %rax, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x2318d
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xef169
movq %rax, %r12
leaq -0xa(%rax), %r15
leaq 0xf09b7(%rip), %rsi     # 0x113a80
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0xb, %r12
jl 0x2318d
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef169
movq %rax, %r12
leaq 0xf0998(%rip), %rsi     # 0x113a90
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf2509
testl %eax, %eax
sete %al
testq %r12, %r12
setle %cl
orb %al, %cl
jne 0x2318d
movl $0x16, %r13d
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xf1988
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf900c
movq 0x38(%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
movq 0x38(%r14), %rdi
callq 0xf28d4
movq %rbp, 0x38(%r14)
movq 0x20(%r14), %rdi
callq 0xf0b13
movq %rbp, 0x20(%r14)
addq %r15, %r13
decq %r12
jne 0x23127
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq