// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/storyboard.c
// Function: do_newfmt_main
static void do_newfmt_main(deark *c, struct storyboard_ctx *d)
{
	dbuf *unc_data = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	UI i;
	char tmps[32];
	// I can't figure out how the image bits are mapped to palette indices.
	// These maps make no sense to me, but it's one way to do it.
	static const u8 palmap_cga[4] = { 3, 1, 2, 0 };
	static const u8 palmap_ega[16] = {
		0, 11,  6,  4, 13, 15,  2, 9, 8,  3, 14, 12,  5,  7, 10, 1 };
	static const u8 palmap_16[16] = {
		0, 11, 13, 15,  6,  4,  2, 9, 8,  3,  5,  7, 14, 12, 10, 1 };

	de_dbg(c, "screen mode: %u"DE_CHAR_TIMES"%u %u-color", (UI)d->sm_width,
		(UI)d->sm_height, (UI)(1U<<d->bpp));
	// The SHOWPIC utility from SB-Live ignores the reported dimensions, but
	// the Picture Maker utility respects them.
	d->width = de_getu16le(5);
	d->height = de_getu16le(7);
	de_dbg_dimensions(c, d->width, d->height);
	if(!de_good_image_dimensions(c, d->width, d->height)) goto done;

	if(d->bpp==2) {
		u8 x;
		u8 bg;

		x = de_getbyte(13) & 0x0f;
		if(x==0x06) {
			de_copy_std_palette(DE_PALID_CGA, 4, 0, d->paltmp, 4, 0);
		}
		else {
			de_copy_std_palette(DE_PALID_CGA, 3, 0, d->paltmp, 4, 0);
		}
		bg = de_getbyte(14) & 0x0f;
		d->paltmp[0] = de_get_std_palette_entry(DE_PALID_PC16, 0, (int)bg);

		for(i=0; i<4; i++) {
			d->pal[i] = d->paltmp[(UI)palmap_cga[i]];
		}
	}
	else if(d->bpp==4) {
		for(i=0; i<16; i++) {
			int idx, idx_adj;

			idx = (int)de_getbyte(13+(i64)i);
			if(idx==6 && d->sm_height==200) {
				idx_adj = 20; // ?? hack - dark yellow vs. brown issue
			}
			else {
				idx_adj = idx;
			}
			d->paltmp[i] = de_get_std_palette_entry(DE_PALID_EGA64, 0, idx_adj);
			de_snprintf(tmps, sizeof(tmps), "%2d ", (int)idx);
			de_dbg_pal_entry2(c, i, d->paltmp[i], tmps, NULL, NULL);
		}
		for(i=0; i<16; i++) {
			if(d->sm_height==200) {
				d->pal[i] = d->paltmp[(UI)palmap_16[i]];
			}
			else {
				d->pal[i] = d->paltmp[(UI)palmap_ega[i]];
			}
		}
	}
	else if(d->bpp==8) {
		newfmt_readpal256(c, d);
	}

	if(d->bpp<8) {
		d->nstrips_per_plane = de_pad_to_n(d->width, 8) / 8;
		d->planespan = d->nstrips_per_plane * d->height;
		d->max_unc_size = d->planespan*d->bpp;
	}
	else {
		d->max_unc_size = d->width * d->height * (d->bpp/8);
	}

	unc_data = dbuf_create_membuf(c, d->max_unc_size, 0x1);
	dbuf_enable_wbuffer(unc_data);

	if(!do_decompress_newfmt(c, d, unc_data)) goto done;
	dbuf_flush(unc_data);

	fi = de_finfo_create(c);
	img = de_bitmap_create(c, d->width, d->height, 3);

	if(d->bpp<8) {
		do_newfmt_render_planar(c, d, unc_data, img);
	}
	else {
		de_convert_image_paletted(unc_data, 0, 8, d->width, d->pal, img, 0);
	}

	if(d->sm_width==320 && d->sm_height==200) {
		fi->density.code = DE_DENSITY_UNK_UNITS;
		fi->density.xdens = 6.0;
		fi->density.ydens = 5.0;
	}
	else if(d->sm_width==640 && d->sm_height!=480) {
		fi->density.code = DE_DENSITY_UNK_UNITS;
		fi->density.xdens = 480.0;
		fi->density.ydens = (double)d->sm_height;
	}

	de_bitmap_write_to_file_finfo(img, fi, 0);

done:
	dbuf_close(unc_data);
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
movl $0x1, %r8d
shll %cl, %r8d
movl -0x60(%rbp), %ecx
leaq 0x79240(%rip), %rsi     # 0x1d19ce
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x5, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x7, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x15880b
jmp 0x158c7f
movq -0x10(%rbp), %rax
cmpq $0x2, 0x8(%rax)
jne 0x15890a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xd, %esi
callq 0x165060
movzbl %al, %eax
andl $0xf, %eax
movb %al, -0x51(%rbp)
movzbl -0x51(%rbp), %eax
cmpl $0x6, %eax
jne 0x158867
movq -0x10(%rbp), %rcx
addq $0x47c, %rcx            # imm = 0x47C
movl $0xa, %edi
movl $0x4, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x161320
jmp 0x15888e
movq -0x10(%rbp), %rcx
addq $0x47c, %rcx            # imm = 0x47C
movl $0xa, %edi
movl $0x3, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x161320
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xe, %esi
callq 0x165060
movzbl %al, %eax
andl $0xf, %eax
movb %al, -0x52(%rbp)
movzbl -0x52(%rbp), %edx
movl $0x14, %edi
xorl %esi, %esi
callq 0x161150
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x47c(%rax)
movl $0x0, -0x2c(%rbp)
cmpl $0x4, -0x2c(%rbp)
jae 0x158905
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7913e(%rip), %rcx     # 0x1d1a20
movzbl (%rcx,%rdx), %ecx
movl %ecx, %ecx
movl 0x47c(%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, 0x7c(%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1588cc
jmp 0x158a70
movq -0x10(%rbp), %rax
cmpq $0x4, 0x8(%rax)
jne 0x158a54
movl $0x0, -0x2c(%rbp)
cmpl $0x10, -0x2c(%rbp)
jae 0x1589d8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
addq $0xd, %rsi
callq 0x165060
movzbl %al, %eax
movl %eax, -0x58(%rbp)
cmpl $0x6, -0x58(%rbp)
jne 0x158963
movq -0x10(%rbp), %rax
cmpq $0xc8, 0x58(%rax)
jne 0x158963
movl $0x14, -0x5c(%rbp)
jmp 0x158969
movl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %edx
movl $0x1e, %edi
xorl %esi, %esi
callq 0x161150
movl %eax, %edx
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, 0x47c(%rax,%rcx,4)
leaq -0x50(%rbp), %rdi
movl -0x58(%rbp), %ecx
movl $0x20, %esi
leaq 0x51689(%rip), %rdx     # 0x1aa024
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl 0x47c(%rax,%rcx,4), %edx
leaq -0x50(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x158920
movl $0x0, -0x2c(%rbp)
cmpl $0x10, -0x2c(%rbp)
jae 0x158a52
movq -0x10(%rbp), %rax
cmpq $0xc8, 0x58(%rax)
jne 0x158a1d
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7903d(%rip), %rcx     # 0x1d1a40
movzbl (%rcx,%rdx), %ecx
movl %ecx, %ecx
movl 0x47c(%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, 0x7c(%rax,%rcx,4)
jmp 0x158a45
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x79003(%rip), %rcx     # 0x1d1a30
movzbl (%rcx,%rdx), %ecx
movl %ecx, %ecx
movl 0x47c(%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, 0x7c(%rax,%rcx,4)
jmp 0x158a47
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1589df
jmp 0x158a6e
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jne 0x158a6c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x158cb0
jmp 0x158a6e
jmp 0x158a70
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jge 0x158ad6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x8, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x158b05
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x8, %esi
cqto
idivq %rsi
imulq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x158df0
cmpl $0x0, %eax
jne 0x158b43
jmp 0x158c7f
movq -0x18(%rbp), %rdi
callq 0x167c30
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x8, 0x8(%rax)
jge 0x158b9d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x1590e0
jmp 0x158bcc
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %r8
addq $0x7c, %r8
movq -0x20(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x10(%rbp), %rax
cmpq $0x140, 0x50(%rax)      # imm = 0x140
jne 0x158c1d
movq -0x10(%rbp), %rax
cmpq $0xc8, 0x58(%rax)
jne 0x158c1d
movq -0x28(%rbp), %rax
movl $0x1, 0x78(%rax)
movq -0x28(%rbp), %rax
movsd 0x55c89(%rip), %xmm0    # 0x1ae888
movsd %xmm0, 0x80(%rax)
movq -0x28(%rbp), %rax
movsd 0x55c6d(%rip), %xmm0    # 0x1ae880
movsd %xmm0, 0x88(%rax)
jmp 0x158c70
movq -0x10(%rbp), %rax
cmpq $0x280, 0x50(%rax)      # imm = 0x280
jne 0x158c6e
movq -0x10(%rbp), %rax
cmpq $0x1e0, 0x58(%rax)      # imm = 0x1E0
je 0x158c6e
movq -0x28(%rbp), %rax
movl $0x1, 0x78(%rax)
movq -0x28(%rbp), %rax
movsd 0x4d688(%rip), %xmm0    # 0x1a62d8
movsd %xmm0, 0x80(%rax)
movq -0x10(%rbp), %rax
cvtsi2sdq 0x58(%rax), %xmm0
movq -0x28(%rbp), %rax
movsd %xmm0, 0x88(%rax)
jmp 0x158c70
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15d260
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)