// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mbk.c
// Function: do_pkscreen_bank
static void do_pkscreen_bank(deark *c, lctx *d, i64 pos1)
{
	struct pkpic_ctx *pp = NULL;
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	const char *tname = NULL;
	i64 pos;
	UI num_planes;
	UI bits_per_pixel;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	pp = de_malloc(c, sizeof(struct pkpic_ctx));
	de_zeromem(d->pal, sizeof(d->pal));

	pos = pos1+4;
	pp->res_code = (UI)de_getu16be_p(&pos);
	de_dbg(c, "res: %u", pp->res_code);

	switch(pp->res_code) {
	case 0:
		if(d->imgtype_of_res0==PKPIC_IMGTYPE_UNKNOWN) {
			pp->imgtype = PKPIC_IMGTYPE_LOW;
		}
		else {
			pp->imgtype = d->imgtype_of_res0;
		}
		break;
	case 1:
		if(d->imgtype_of_res1==PKPIC_IMGTYPE_UNKNOWN) {
			const char *tmps;

			if(d->is_container_fmt) {
				pp->imgtype = PKPIC_IMGTYPE_MED;
				tmps = "l|h";
			}
			else {
				// Unfortunately, a file with res=1 (that couldn't be identified by
				// its extension) seems to be more likely to be the oddball PP1
				// format than the nice standard STOS medium-resolution format.
				pp->imgtype = PKPIC_IMGTYPE_PP1;
				tmps = "m|h";
			}
			de_warn(c, "Ambiguous image. If it looks wrong, try \"-opt stos:res1=<%s>\".", tmps);
		}
		else {
			pp->imgtype = d->imgtype_of_res1;
		}
		break;
	case 2: pp->imgtype = PKPIC_IMGTYPE_HIGH; break;
	default: pp->imgtype = PKPIC_IMGTYPE_UNKNOWN; break;
	}

	switch(pp->imgtype) {
	case PKPIC_IMGTYPE_LOW:
		num_planes = 4;
		bits_per_pixel = 4;
		tname = "4-plane low res";
		break;
	case PKPIC_IMGTYPE_M4P:
		num_planes = 4;
		bits_per_pixel = 2;
		tname = "DAJ 4-plane med res";
		break;
	case PKPIC_IMGTYPE_MED:
		num_planes = 2;
		bits_per_pixel = 2;
		tname = "2-plane med res";
		break;
	case PKPIC_IMGTYPE_HIGH:
		num_planes = 1;
		bits_per_pixel = 1;
		tname = "1-plane high res";
		break;
	case PKPIC_IMGTYPE_PP1:
		num_planes = 2;
		bits_per_pixel = 4;
		tname = "PP1 2-plane low res";
		break;
	case PKPIC_IMGTYPE_PP3:
		num_planes = 2;
		bits_per_pixel = 1;
		tname = "PP3 2-plane high res";
		break;
	default:
		de_err(c, "Unsupported picture resolution: %u", pp->res_code);
		goto done;
	}
	if(tname) {
		de_dbg(c, "interpreted image type: %s", tname);
	}

	pos += 4;
	pp->width_in_words = de_getu16be_p(&pos);
	pp->pseudowidth = pp->width_in_words * 16;
	if(pp->imgtype==PKPIC_IMGTYPE_PP1) {
		pp->w = pp->width_in_words * 8;
	}
	else if(pp->imgtype==PKPIC_IMGTYPE_M4P) {
		pp->w = pp->width_in_words * 32;
	}
	else {
		pp->w = pp->pseudowidth;
	}
	de_dbg(c, "width in words: %"I64_FMT, pp->width_in_words);
	pp->height_in_lumps = de_getu16be_p(&pos);
	de_dbg(c, "height in lumps: %"I64_FMT, pp->height_in_lumps);
	pos += 2; // unknown
	pp->lines_per_lump = de_getu16be_p(&pos);
	de_dbg(c, "lines per lump: %"I64_FMT, pp->lines_per_lump);
	pp->pseudoheight = pp->height_in_lumps * pp->lines_per_lump;
	if(pp->imgtype==PKPIC_IMGTYPE_PP3) {
		pp->h = pp->pseudoheight * 2;
	}
	else {
		pp->h = pp->pseudoheight;
	}
	de_dbg_dimensions(c, pp->w, pp->h);
	pos += 2; // flags
	pp->picdata_rel = 70;
	pp->rledata_rel = de_getu32be_p(&pos);
	pp->pointdata_rel = de_getu32be_p(&pos);
	pp->picdata_abs = pos1 + pp->picdata_rel;
	pp->rledata_abs = pos1 + pp->rledata_rel;
	pp->pointdata_abs = pos1 + pp->pointdata_rel;
	de_dbg(c, "picdata: %"I64_FMT, pp->picdata_abs);
	de_dbg(c, "rledata: %"I64_FMT, pp->rledata_abs);
	de_dbg(c, "pointdata: %"I64_FMT, pp->pointdata_abs);

	pos = pos1+38;
	de_dbg(c, "palette at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	fmtutil_read_atari_palette(c, c->infile, pos, d->pal, 16, 16, 0);
	de_dbg_indent(c, -1);

	if(bits_per_pixel==1) {
		if((d->pal[0] & 0xffffff)==0) {
			d->pal[0] = DE_STOCKCOLOR_BLACK;
			d->pal[1] = DE_STOCKCOLOR_WHITE;
		}
		else {
			d->pal[0] = DE_STOCKCOLOR_WHITE;
			d->pal[1] = DE_STOCKCOLOR_BLACK;
		}
	}

	pp->unc_image_size = pp->width_in_words*2 * (i64)num_planes * pp->h;

	unc_pixels = dbuf_create_membuf(c, 0, 0);

	fmtutil_decompress_stos_pictbank(c, c->infile, pp->picdata_abs, pp->rledata_abs,
		pp->pointdata_abs, unc_pixels, pp->unc_image_size);

	img = de_bitmap_create(c, pp->w, pp->h, ((bits_per_pixel==1)?1:3));
	render_stos_pictbank1(c, d, pp, unc_pixels, img, num_planes);

	fi = de_finfo_create(c);
	adata = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata->bpp = (i64)bits_per_pixel;
	fmtutil_atari_set_standard_density(c, adata, fi);
	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_bitmap_destroy(img);
	dbuf_close(unc_pixels);
	if(pp) {
		de_free(c, pp);
	}
	de_free(c, adata);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x78, %esi
callq 0x15b780
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2c, %rdi
xorl %esi, %esi
movl %esi, -0x70(%rbp)
movl $0x400, %edx            # imm = 0x400
callq 0x19140
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movl -0x70(%rbp), %eax
movl %ecx, %edx
movq -0x20(%rbp), %rcx
movl %edx, (%rcx)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl (%rcx), %edx
leaq 0xdfd42(%rip), %rsi     # 0x1bbfdb
callq 0x15b0d0
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
testl %eax, %eax
je 0xdc2c8
jmp 0xdc2ad
movl -0x6c(%rbp), %eax
subl $0x1, %eax
je 0xdc2f2
jmp 0xdc2b7
movl -0x6c(%rbp), %eax
subl $0x2, %eax
je 0xdc35b
jmp 0xdc368
movq -0x10(%rbp), %rax
cmpl $0x63, 0x4(%rax)
jne 0xdc2df
movq -0x20(%rbp), %rax
movl $0x64, 0x4(%rax)
jmp 0xdc2ed
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xdc373
movq -0x10(%rbp), %rax
cmpl $0x63, 0x8(%rax)
jne 0xdc34b
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0xdc31d
movq -0x20(%rbp), %rax
movl $0x65, 0x4(%rax)
leaq 0xdfccc(%rip), %rax     # 0x1bbfe3
movq %rax, -0x68(%rbp)
jmp 0xdc333
movq -0x20(%rbp), %rax
movl $0x6e, 0x4(%rax)
leaq 0xdfcb8(%rip), %rax     # 0x1bbfe7
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0xdfca9(%rip), %rsi     # 0x1bbfeb
movb $0x0, %al
callq 0x15bc10
jmp 0xdc359
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0xdc373
movq -0x20(%rbp), %rax
movl $0x66, 0x4(%rax)
jmp 0xdc373
movq -0x20(%rbp), %rax
movl $0x63, 0x4(%rax)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addl $-0x64, %eax
movl %eax, %ecx
movq %rcx, -0x78(%rbp)
subl $0x14, %eax
ja 0xdc448
movq -0x78(%rbp), %rax
leaq 0xdf8a9(%rip), %rcx     # 0x1bbc40
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, -0x54(%rbp)
movl $0x4, -0x58(%rbp)
leaq 0xdfc75(%rip), %rax     # 0x1bc02a
movq %rax, -0x48(%rbp)
jmp 0xdc465
movl $0x4, -0x54(%rbp)
movl $0x2, -0x58(%rbp)
leaq 0xdfc67(%rip), %rax     # 0x1bc03a
movq %rax, -0x48(%rbp)
jmp 0xdc465
movl $0x2, -0x54(%rbp)
movl $0x2, -0x58(%rbp)
leaq 0xdfc5d(%rip), %rax     # 0x1bc04e
movq %rax, -0x48(%rbp)
jmp 0xdc465
movl $0x1, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
leaq 0xdfc52(%rip), %rax     # 0x1bc05e
movq %rax, -0x48(%rbp)
jmp 0xdc465
movl $0x2, -0x54(%rbp)
movl $0x4, -0x58(%rbp)
leaq 0xdfc48(%rip), %rax     # 0x1bc06f
movq %rax, -0x48(%rbp)
jmp 0xdc465
movl $0x2, -0x54(%rbp)
movl $0x1, -0x58(%rbp)
leaq 0xdfc41(%rip), %rax     # 0x1bc083
movq %rax, -0x48(%rbp)
jmp 0xdc465
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq 0xdfc3f(%rip), %rsi     # 0x1bc098
movb $0x0, %al
callq 0x15bfb0
jmp 0xdc89d
cmpq $0x0, -0x48(%rbp)
je 0xdc482
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0xdfc40(%rip), %rsi     # 0x1bc0bb
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shlq $0x4, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rax
cmpl $0x6e, 0x4(%rax)
jne 0xdc4de
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shlq $0x3, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xdc510
movq -0x20(%rbp), %rax
cmpl $0x78, 0x4(%rax)
jne 0xdc4fe
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shlq $0x5, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xdc50e
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x30(%rax)
jmp 0xdc510
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xdfbb3(%rip), %rsi     # 0x1bc0d6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0xdfb91(%rip), %rsi     # 0x1bc0ea
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x165210
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xdfb64(%rip), %rsi     # 0x1bc0ff
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rax
cmpl $0x70, 0x4(%rax)
jne 0xdc5da
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
shlq %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0xdc5ea
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdx
callq 0x15bba0
movq -0x50(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq $0x46, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x1654b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
addq 0x40(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
addq 0x50(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
addq 0x60(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
leaq 0xdfa71(%rip), %rsi     # 0x1bc113
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rdx
leaq 0xdfa64(%rip), %rsi     # 0x1bc120
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rdx
leaq 0xdfa57(%rip), %rsi     # 0x1bc12d
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x26, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0xe57f5(%rip), %rsi     # 0x1c1eed
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x50(%rbp), %rdx
movq -0x10(%rbp), %rcx
addq $0x2c, %rcx
movl $0x10, %r9d
xorl %eax, %eax
movq %r9, %r8
movl $0x0, (%rsp)
callq 0x174460
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x1, -0x58(%rbp)
jne 0xdc791
movq -0x10(%rbp), %rax
movl 0x2c(%rax), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
cmpl $0x0, %eax
jne 0xdc779
movq -0x10(%rbp), %rax
movl $0xff000000, 0x2c(%rax) # imm = 0xFF000000
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
jmp 0xdc78f
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movl $0xff000000, 0x30(%rax) # imm = 0xFF000000
jmp 0xdc791
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
shlq %rcx
movl -0x54(%rbp), %eax
imulq %rax, %rcx
movq -0x20(%rbp), %rax
imulq 0x38(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x58(%rax), %rcx
movq -0x20(%rbp), %rax
movq 0x68(%rax), %r8
movq -0x28(%rbp), %r9
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, (%rsp)
callq 0x17a430
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rdx
movl -0x58(%rbp), %r8d
movl $0x3, %ecx
movl $0x1, %eax
cmpl $0x1, %r8d
cmovel %eax, %ecx
callq 0x16b750
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %r8
movl -0x54(%rbp), %r9d
callq 0xdd260
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movl -0x58(%rbp), %eax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x174df0
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x30(%rbp), %rdi
callq 0x16af60
movq -0x28(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x20(%rbp)
je 0xdc8c3
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movl -0x5c(%rbp), %esi
callq 0x15b5a0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)