// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/abk.c
// Function: picture_bank_read_picture
static void picture_bank_read_picture(deark *c, lctx *d, struct amosbank *bk, i64 pos)
{
	i64 bytes_per_row_per_plane;
	i64 height_in_lumps;
	i64 lines_per_lump;
	i64 width, height;
	de_bitmap *img = NULL;
	dbuf *unc_pixels = NULL;
	struct amos_pictbank_params *pb = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "picture header at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	// 24-byte "Picture header"

	bytes_per_row_per_plane = dbuf_getu16be(bk->f, pos+8);
	de_dbg(c, "bytes per row per plane: %d", (int)bytes_per_row_per_plane);
	width = bytes_per_row_per_plane * 8;

	height_in_lumps = dbuf_getu16be(bk->f, pos+10);
	de_dbg(c, "height in lumps: %d", (int)height_in_lumps);
	lines_per_lump = dbuf_getu16be(bk->f, pos+12);
	de_dbg(c, "lines per lump: %d", (int)lines_per_lump);
	height = height_in_lumps * lines_per_lump;

	de_dbg(c, "calculated dimensions: %d"DE_CHAR_TIMES"%d", (int)width, (int)height);

	bk->nplanes = dbuf_getu16be(bk->f, pos+14);
	de_dbg(c, "number of bitplanes: %d", (int)bk->nplanes);

	bk->pic_rledata_offset = dbuf_getu32be(bk->f, pos+16);
	de_dbg(c, "rledata offset: %"I64_FMT" (file offset: %"I64_FMT")",
		bk->pic_rledata_offset, (pos+bk->pic_rledata_offset));
	bk->pic_rledata_offset += pos; // Convert to absolute offset

	bk->pic_points_offset = dbuf_getu32be(bk->f, pos+20);
	de_dbg(c, "points offset: %"I64_FMT" (file offset: %"I64_FMT")",
		bk->pic_points_offset, (pos+bk->pic_points_offset));
	bk->pic_points_offset += pos; // Convert to absolute offset

	if(!de_good_image_dimensions(c, width, height)) goto done;
	if(bk->nplanes<1 || bk->nplanes>6) {
		de_err(c, "Unsupported number of planes: %d", (int)bk->nplanes);
		goto done;
	}

	de_dbg_indent(c, -1);

	bk->pic_picdata_offset = pos + 24;
	de_dbg(c, "picdata at %"I64_FMT, bk->pic_picdata_offset);

	bk->picdata_expected_unc_bytes = bytes_per_row_per_plane * bk->nplanes * height;
	unc_pixels = dbuf_create_membuf(c, bk->picdata_expected_unc_bytes, 0);
	fmtutil_decompress_stos_pictbank(c, c->infile, bk->pic_picdata_offset,
		bk->pic_rledata_offset, bk->pic_points_offset,
		unc_pixels, bk->picdata_expected_unc_bytes);
	img = de_bitmap_create(c, width, height, 3);

	pb = de_malloc(c, sizeof(struct amos_pictbank_params));
	pb->num_planes = (UI)bk->nplanes;
	pb->bits_per_pixel = pb->num_planes;
	pb->width_in_bytes = bytes_per_row_per_plane;
	pb->height_in_lumps = height_in_lumps;
	pb->lines_per_lump = lines_per_lump;
	pb->pseudoheight = height;
	pb->unc_pixels = unc_pixels;

	render_pictbank_to_bitmap(c, d, bk, pb, img);

	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);
done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
	de_free(c, pb);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1810a2(%rip), %rsi    # 0x19cf8e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x8, %rsi
callq 0x1651d0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x181079(%rip), %rsi    # 0x19cfa4
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
shlq $0x3, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xa, %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0x181058(%rip), %rsi    # 0x19cfc0
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xc, %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x18103b(%rip), %rsi    # 0x19cfd4
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
imulq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movl %eax, %ecx
leaq 0x181023(%rip), %rsi    # 0x19cfe7
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0xe, %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x480(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x480(%rax), %rax
movl %eax, %edx
leaq 0x180fff(%rip), %rsi    # 0x19d005
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x10, %rsi
callq 0x165470
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x490(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x490(%rax), %rdx
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x490(%rax), %rcx
leaq 0x180fc8(%rip), %rsi    # 0x19d01d
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x490(%rax), %rcx
movq %rcx, 0x490(%rax)
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x14, %rsi
callq 0x165470
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x498(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x498(%rax), %rdx
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x498(%rax), %rcx
leaq 0x180f8a(%rip), %rsi    # 0x19d044
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x498(%rax), %rcx
movq %rcx, 0x498(%rax)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1c0f2
jmp 0x1c2a4
movq -0x18(%rbp), %rax
cmpq $0x1, 0x480(%rax)
jl 0x1c10e
movq -0x18(%rbp), %rax
cmpq $0x6, 0x480(%rax)
jle 0x1c132
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x480(%rax), %rax
movl %eax, %edx
leaq 0x180f44(%rip), %rsi    # 0x19d06a
movb $0x0, %al
callq 0x15bfb0
jmp 0x1c2a4
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x20(%rbp), %rcx
addq $0x18, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x4a0(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x4a0(%rax), %rdx
leaq 0x180f22(%rip), %rsi    # 0x19d08b
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
imulq 0x480(%rax), %rcx
imulq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x4a8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x4a8(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x4a0(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x490(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x498(%rax), %r8
movq -0x58(%rbp), %r9
movq -0x18(%rbp), %rax
movq 0x4a8(%rax), %rax
movq %rax, (%rsp)
callq 0x17a430
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0x15b780
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq 0x480(%rax), %rax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x60(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x50(%rbp), %r8
callq 0x1c4e0
movq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x58(%rbp), %rdi
callq 0x1676a0
movq -0x50(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x64(%rbp), %esi
callq 0x15b5a0
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)