// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/comicchat.c
// Function: read_dib_to_img
static void read_dib_to_img(deark *c, lctx *d, i64 pos1, UI magic,
	struct image_highlevel_ctx *ih, int itype, u8 direct_to_bmp)
{
	int ret;
	i64 pos;
	i64 cmpr_pos;
	i64 orig_len, cmpr_len;
	UI magic2;
	int saved_indent_level;
	struct image_extract_ctx *ic = NULL;
	struct de_dfilter_results dres;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_deflate_params deflateparams;

	de_dbg_indent_save(c, &saved_indent_level);
	ic = de_malloc(c, sizeof(struct image_extract_ctx));
	de_zeromem(&deflateparams, sizeof(struct de_deflate_params));
	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);

	pos = pos1;
	if(magic==0x0101) {
		i64 ck_len;

		// The palette is formatted like a standard chunk.
		// TODO: Ideally, we'd use shared chunk-reading code here.
		de_dbg(c, "palette chunk at %"I64_FMT, pos1);
		de_dbg_indent(c, 1);
		pos += 2; // 0x0101 tag, already read
		ck_len = de_getu16le_p(&pos);
		ic->pal_num_entries = de_getu16le_p(&pos);
		de_dbg(c, "num entries: %"I64_FMT, ic->pal_num_entries);
		if(ic->pal_num_entries>256) goto done;

		// Note that palette is RGB, not BGR as might be expected.
		if(ic->pal_num_entries>0) {
			de_read_simple_palette(c, c->infile, pos, ic->pal_num_entries, 3,
				ic->pal, 256, DE_RDPALTYPE_24BIT, 0);
		}
		pos = pos1 + 4 + ck_len;
		de_dbg_indent(c, -1);
	}

	ic->ihpos = pos;
	magic2 = (UI)de_getu16be(ic->ihpos);
	if(magic2 != 0x2800) {
		de_err(c, "Image not found at %"I64_FMT, ic->ihpos);
		goto done;
	}

	de_dbg(c, "infoheader at %"I64_FMT, ic->ihpos);
	de_dbg_indent(c, 1);
	ret = fmtutil_get_bmpinfo(c, c->infile, &ic->bi, ic->ihpos,
		c->infile->len - ic->ihpos, 0);
	de_dbg_indent(c, -1);
	if(!ret) goto done;
	if(ic->bi.infohdrsize != 40) goto done;
	if(ic->bi.pal_entries > 256) goto done;
	// We don't expect the zlib decompression to result in an image that is still
	// (RLE) compressed. We could support it, but we probably need samples.
	if(ic->bi.is_compressed) goto done;
	if(!de_good_image_dimensions(c, ic->bi.width, ic->bi.height)) goto done;

	if(ic->bi.bitcount==1 && itype>0) {
		ic->pal_num_entries = 2;
		// 0 is transparent, and the palette is undefined.
		// It'd be logical to make 0 black, but we make it white, so that masks
		// from all format versions are white-is-transparent.
		de_make_grayscale_palette(ic->pal, ic->pal_num_entries, 0x1);
	}
	else if(ic->bi.bitcount==2 && itype==0 && ic->pal_num_entries==0) {
		ih->special_2bpp_transparency = 1;
		ic->pal_num_entries = 4;
		ic->pal[0] = DE_MAKE_GRAY(128); // Arbitrary key color. Will be made transparent.
		// Sometimes pal[1] is used for the halo around the character, so it might
		// be interesting to make it partially transparent or something.
		// But unfortunately, other times the halo is pal[2].
		ic->pal[1] = DE_MAKE_GRAY(254); // alternate white foreground
		ic->pal[2] = DE_STOCKCOLOR_WHITE; // normal white foregreound
		ic->pal[3] = DE_STOCKCOLOR_BLACK; // normal black foreground
	}

	pos = ic->ihpos + ic->bi.infohdrsize;
	orig_len = de_getu32le_p(&pos);
	de_dbg(c, "orig len: %"I64_FMT, orig_len);
	cmpr_len = de_getu32le_p(&pos);
	de_dbg(c, "cmpr len: %"I64_FMT, cmpr_len);
	cmpr_pos = pos;
	de_dbg(c, "cmpr data at %"I64_FMT, cmpr_pos);
	if(cmpr_pos + cmpr_len > c->infile->len) goto done;

	ic->unc_pixels = dbuf_create_membuf(c, 0, 0);
	dcmpri.f = c->infile;
	dcmpri.pos = cmpr_pos;
	dcmpri.len = cmpr_len;
	dcmpro.f = ic->unc_pixels;
	dcmpro.expected_len = de_min_int(orig_len, ic->bi.foreground_size);
	dcmpro.len_known = 1;
	deflateparams.flags = DE_DEFLATEFLAG_ISZLIB;
	de_dbg(c, "[decompressing]");
	de_dbg_indent(c, 1);
	fmtutil_deflate_codectype1(c, &dcmpri, &dcmpro, &dres, (void*)&deflateparams);
	de_dbg_indent(c, -1);
	dbuf_flush(ic->unc_pixels);
	if(dres.errcode != 0) {
		de_err(c, "%s", de_dfilter_get_errmsg(c, &dres));
		goto done;
	}

	// This will ultimately result in the BMP header & palette being read
	// again (by the bmp module this time), dbg info printed again, etc.
	// It's messy, but it will do.
	reconstruct_bmp_and_cvt_to_img(c, d, ic, ih, itype, direct_to_bmp);

done:
	if(ic) {
		dbuf_close(ic->unc_pixels);
		de_free(c, ic);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movb 0x10(%rbp), %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x4b8, %esi            # imm = 0x4B8
callq 0x15b780
movq %rax, -0x60(%rbp)
leaq -0x108(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x19140
movq -0x8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
callq 0x1773f0
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x101, -0x1c(%rbp)     # imm = 0x101
jne 0x8b886
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x123928(%rip), %rsi    # 0x1af0a9
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x110(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0xb0(%rax), %rdx
leaq 0x143777(%rip), %rsi    # 0x1cef66
movb $0x0, %al
callq 0x15b0d0
movq -0x60(%rbp), %rax
cmpq $0x100, 0xb0(%rax) # imm = 0x100
jle 0x8b80c
jmp 0x8bc47
movq -0x60(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jle 0x8b865
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rax
movq 0xb0(%rax), %rcx
movq -0x60(%rbp), %r9
addq $0xb8, %r9
movl $0x3, %r8d
xorl %eax, %eax
movq $0x100, (%rsp)          # imm = 0x100
movl $0x1, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x18(%rbp), %rax
addq $0x4, %rax
addq -0x110(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x38(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rsi
callq 0x1651d0
movl %eax, -0x54(%rbp)
cmpl $0x2800, -0x54(%rbp)    # imm = 0x2800
je 0x8b8db
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rdx
leaq 0x123792(%rip), %rsi    # 0x1af061
movb $0x0, %al
callq 0x15bfb0
jmp 0x8bc47
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rdx
leaq 0x1237cd(%rip), %rsi    # 0x1af0be
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 -0x60(%rbp), %rdx
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %r8
movq -0x60(%rbp), %rax
subq 0xa0(%rax), %r8
xorl %r9d, %r9d
callq 0x172d70
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x0, -0x30(%rbp)
jne 0x8b95c
jmp 0x8bc47
movq -0x60(%rbp), %rax
cmpq $0x28, 0x18(%rax)
je 0x8b96c
jmp 0x8bc47
movq -0x60(%rbp), %rax
cmpq $0x100, 0x50(%rax)      # imm = 0x100
jle 0x8b97f
jmp 0x8bc47
movq -0x60(%rbp), %rax
cmpl $0x0, 0x98(%rax)
je 0x8b991
jmp 0x8bc47
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x8b9b4
jmp 0x8bc47
movq -0x60(%rbp), %rax
cmpq $0x1, 0x30(%rax)
jne 0x8b9f6
cmpl $0x0, -0x2c(%rbp)
jle 0x8b9f6
movq -0x60(%rbp), %rax
movq $0x2, 0xb0(%rax)
movq -0x60(%rbp), %rdi
addq $0xb8, %rdi
movq -0x60(%rbp), %rax
movq 0xb0(%rax), %rsi
movl $0x1, %edx
callq 0x16d580
jmp 0x8ba65
movq -0x60(%rbp), %rax
cmpq $0x2, 0x30(%rax)
jne 0x8ba63
cmpl $0x0, -0x2c(%rbp)
jne 0x8ba63
movq -0x60(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jne 0x8ba63
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
movq -0x60(%rbp), %rax
movq $0x4, 0xb0(%rax)
movq -0x60(%rbp), %rax
movl $0xff808080, 0xb8(%rax) # imm = 0xFF808080
movq -0x60(%rbp), %rax
movl $0xfffefefe, 0xbc(%rax) # imm = 0xFFFEFEFE
movq -0x60(%rbp), %rax
movl $0xffffffff, 0xc0(%rax) # imm = 0xFFFFFFFF
movq -0x60(%rbp), %rax
movl $0xff000000, 0xc4(%rax) # imm = 0xFF000000
jmp 0x8ba65
movq -0x60(%rbp), %rax
movq 0xa0(%rax), %rax
movq -0x60(%rbp), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x1154f7(%rip), %rsi    # 0x1a0f97
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x14ab96(%rip), %rsi    # 0x1d6661
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
leaq 0x125bca(%rip), %rsi    # 0x1b16b3
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x8bb0b
jmp 0x8bc47
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x60(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0xf8(%rbp)
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x68(%rax), %rsi
callq 0x15ccf0
movq %rax, -0xe8(%rbp)
movb $0x1, -0xf0(%rbp)
movl $0x1, -0x108(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x122ad4(%rip), %rsi    # 0x1ae667
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0xf8(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq -0x108(%rbp), %r8
callq 0x17f0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x60(%rbp), %rax
movq 0xa8(%rax), %rdi
callq 0x167c30
cmpl $0x0, -0xc0(%rbp)
je 0x8bc29
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x177360
movq -0x118(%rbp), %rdi
movq %rax, %rdx
leaq 0x114f0f(%rip), %rsi    # 0x1a0b2f
movb $0x0, %al
callq 0x15bfb0
jmp 0x8bc47
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movzbl 0x10(%rbp), %r9d
callq 0x8bd70
cmpq $0x0, -0x60(%rbp)
je 0x8bc6b
movq -0x60(%rbp), %rax
movq 0xa8(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
callq 0x15b5a0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq