// File: /workspace/github_c_makefile_star2_O3/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 0xf054b(%rip), %rsi     # 0x113543
callq 0xeb125
leaq 0xf0af3(%rip), %rsi     # 0x113af7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movaps 0xf026b(%rip), %xmm0    # 0x113290
movq %rsp, %rax
movaps %xmm0, (%rax)
movaps 0xf026e(%rip), %xmm0    # 0x1132a0
movaps %xmm0, 0x10(%rax)
movaps 0xf0273(%rip), %xmm0    # 0x1132b0
movaps %xmm0, 0x20(%rax)
movaps 0xf0278(%rip), %xmm0    # 0x1132c0
movaps %xmm0, 0x30(%rax)
movq %rax, 0x30(%r14)
movq $0x4, (%r14)
movq $0x10, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeeb67
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 0xeeb67
movq %rax, 0x18(%r14)
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x231a0
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xeeb67
movq %rax, %r12
leaq -0xa(%rax), %r15
leaq 0xf0a54(%rip), %rsi     # 0x113b30
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0xb, %r12
jl 0x231a0
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeeb67
movq %rax, %r12
leaq 0xf0a35(%rip), %rsi     # 0x113b40
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf1e9d
testl %eax, %eax
sete %al
testq %r12, %r12
setle %cl
orb %al, %cl
jne 0x231a0
movl $0x16, %r13d
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xf1388
movq %rax, 0x20(%r14)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, 0x38(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf8b10
movq 0x38(%r14), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq 0x38(%r14), %rdi
callq 0xf228d
movq %rbp, 0x38(%r14)
movq 0x20(%r14), %rdi
callq 0xf052b
movq %rbp, 0x20(%r14)
addq %r15, %r13
decq %r12
jne 0x2313a
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq