// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/atari-img.c
// Function: de_run_animatic
static void de_run_animatic(deark *c, de_module_params *mparams)
{
	struct atari_img_decode_data *adata = NULL;
	i64 nframes;
	i64 frame;
	i64 planespan, rowspan, framespan;
	i64 frame_bitmap_pos;
	u32 pal[16];

	de_declare_fmt(c, "Animatic Film");

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

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

	adata->bpp = 4;
	adata->ncolors = 16;
	adata->pal = pal;
	de_dbg_indent(c, 1);
	fmtutil_read_atari_palette(c, c->infile, 2, adata->pal, 16, adata->ncolors, 0);
	de_dbg_indent(c, -1);

	adata->w = de_getu16be(40);
	adata->h = de_getu16be(42);
	de_dbg_dimensions(c, adata->w, adata->h);
	if(!de_good_image_dimensions(c, adata->w, adata->h)) goto done;

	planespan = 2*((adata->w+15)/16);
	rowspan = planespan*adata->bpp;
	framespan = rowspan*adata->h;

	for(frame=0; frame<nframes; frame++) {
		frame_bitmap_pos = 64 + frame*framespan;
		de_dbg(c, "frame %d bitmap at %d", (int)frame, (int)frame_bitmap_pos);

		adata->unc_pixels = dbuf_open_input_subfile(c->infile, frame_bitmap_pos, framespan);
		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 $0x58, %rsp
movq %rdi, %rbx
leaq 0xf0331(%rip), %rsi     # 0x113560
callq 0xeb125
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeeb67
movq %rax, %r15
leaq 0xf08e7(%rip), %rsi     # 0x113b40
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rsi
callq 0xf1e9d
testl %eax, %eax
je 0x233e1
movq $0x4, (%r14)
movq $0x10, 0x8(%r14)
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r14)
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rsi
movq 0x8(%r14), %r9
movq 0x30(%r14), %rcx
movl $0x0, (%rsp)
movl $0x2, %edx
movl $0x10, %r8d
movq %rbx, %rdi
callq 0xf876e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl $0x28, %esi
callq 0xeeb67
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movl $0x2a, %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 0x233e1
movq 0x10(%r14), %r12
leaq 0xf(%r12), %rax
addq $0x1e, %r12
testq %rax, %rax
cmovnsq %rax, %r12
cmpq $0x0, 0x8(%rsp)
jle 0x233e1
sarq $0x4, %r12
imulq (%r14), %r12
imulq 0x18(%r14), %r12
addq %r12, %r12
movl $0x40, %r13d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %rbx, %rdi
leaq 0xf07f4(%rip), %rsi     # 0x113b5a
movl %ebp, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r12, %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 %r15, 0x38(%r14)
movq 0x20(%r14), %rdi
callq 0xf052b
movq %r15, 0x20(%r14)
incq %rbp
addq %r12, %r13
cmpq %rbp, 0x8(%rsp)
jne 0x2335c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq