// File: /workspace/github_c_makefile_star2_O2/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 0xe3a16(%rip), %rsi     # 0x106500
callq 0xe07f7
pushq $0x40
popq %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xdecaf
movq %rax, %r15
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xe3f46
movq %rax, %r12
leaq 0xe3fca(%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 0x22c78
movq $0x4, (%r15)
movq $0x10, 0x8(%r15)
leaq 0x10(%rsp), %rax
movq %rax, 0x30(%r15)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
andl $0x0, (%rsp)
pushq $0x2
popq %rdx
pushq $0x10
popq %r13
movq %rbx, %rdi
movq %r13, %r8
callq 0xecea5
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
pushq $0x28
popq %rsi
callq 0xe3f46
movq %rax, 0x10(%r15)
movq 0x18(%rbx), %rdi
pushq $0x2a
popq %rsi
callq 0xe3f46
movq %rax, 0x18(%r15)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x22c78
movq 0x10(%r15), %rax
addq $0xf, %rax
cqto
idivq %r13
movq %rax, %r13
imulq (%r15), %r13
imulq 0x18(%r15), %r13
addq %r13, %r13
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x22c78
movq %rbx, %rdi
leaq 0xe3ef7(%rip), %rsi     # 0x106afa
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xe6248
movq %rax, 0x20(%r15)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
movq %rbx, %rdi
pushq $0x3
popq %rcx
callq 0xe7437
movq %rax, 0x38(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xed22a
movq 0x38(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq 0x38(%r15), %rdi
callq 0xe7084
andq $0x0, 0x38(%r15)
movq 0x20(%r15), %rdi
callq 0xe5591
andq $0x0, 0x20(%r15)
incq %rbp
addq %r13, %r14
jmp 0x22bf4
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq