// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/wpg.c
// Function: handler_bitmap
static void handler_bitmap(deark *c, lctx *d, u8 rectype, i64 dpos1, i64 dlen)
{
	i64 w, h;
	i64 xdens, ydens;
	i64 bpp;
	i64 pos = dpos1;
	i64 rowspan;
	int is_bilevel;
	int is_grayscale;
	int output_bypp;
	int record_version;
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	de_color finalpal[256];

	d->bitmap_count++;

	if(rectype==0x14) {
		record_version = 2;
		pos += 10;
	}
	else {
		record_version = 1;
	}

	// Keep track of the highest bitmap record version found.
	if(record_version > d->bitmap_record_ver) {
		d->bitmap_record_ver = record_version;
	}

	w = de_getu16le(pos);
	pos += 2;
	h = de_getu16le(pos);
	pos += 2;
	de_dbg_dimensions(c, w, h);

	bpp = de_getu16le(pos);
	de_dbg(c, "bits/pixel: %d", (int)bpp);
	pos += 2;

	xdens = de_getu16le(pos);
	pos += 2;
	ydens = de_getu16le(pos);
	pos += 2;
	de_dbg(c, "density: %d"DE_CHAR_TIMES"%d dpi", (int)xdens, (int)ydens);

	if(d->bitmap_count==1) {
		d->bpp_of_first_bitmap = bpp;
		d->width_of_first_bitmap = w;
		d->height_of_first_bitmap = h;
	}

	if(bpp!=1 && bpp!=2 && bpp!=4 && bpp!=8) {
		de_err(c, "Unsupported bitmap depth: %d", (int)bpp);
		goto done;
	}
	if(!de_good_image_dimensions(c, w, h)) goto done;

	// Evidence suggests the palette is to be ignored if bpp==1.
	// (Or maybe you're supposed to use pal[0] and pal[15]?)
	is_bilevel = (bpp==1);

	if(is_bilevel) {
		is_grayscale = 1;
	}
	else {
		get_final_palette(c, d, finalpal, bpp);
		is_grayscale = de_is_grayscale_palette(finalpal, (i64)1<<bpp);
	}

	if(is_bilevel || is_grayscale)
		output_bypp = 1;
	else
		output_bypp = 3;

	rowspan = (bpp * w + 7)/8;

	unc_pixels = dbuf_create_membuf(c, h*rowspan, 0x1);

	if(!do_uncompress_rle(c, d, c->infile, pos, dpos1+dlen-pos, rowspan, unc_pixels)) {
		goto done;
	}

	img = de_bitmap_create2(c, w, (rowspan*8)/bpp, h, output_bypp);

	fi = de_finfo_create(c);

	if(xdens>0 && ydens>0) {
		fi->density.code = DE_DENSITY_DPI;
		fi->density.xdens = (double)xdens;
		fi->density.ydens = (double)ydens;
	}

	if(is_bilevel) {
		de_convert_image_bilevel(unc_pixels, 0, rowspan, img, 0);
	}
	else {
		if(!d->has_pal && bpp!=2) {
			// TODO: Figure out what the default palette is.
			de_err(c, "Paletted images with no palette are not supported");
			goto done;
		}

		de_convert_image_paletted(unc_pixels, 0, bpp, rowspan, finalpal, img, 0);
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x428(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x428(%rax)
movzbl -0x11(%rbp), %eax
cmpl $0x14, %eax
jne 0x13b027
movl $0x2, -0x70(%rbp)
movq -0x58(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x58(%rbp)
jmp 0x13b02e
movl $0x1, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x424(%rcx), %eax
jle 0x13b04a
movl -0x70(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x424(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165280
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165280
movq %rax, -0x38(%rbp)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165280
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x96b31(%rip), %rsi     # 0x1d1bf4
movb $0x0, %al
callq 0x15b0d0
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165280
movq %rax, -0x40(%rbp)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x165280
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
movq -0x48(%rbp), %rax
movl %eax, %ecx
leaq 0x91f73(%rip), %rsi     # 0x1cd0a2
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x1, 0x428(%rax)
jne 0x13b171
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x430(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x438(%rax)
movq -0x38(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x440(%rax)
cmpq $0x1, -0x50(%rbp)
je 0x13b1aa
cmpq $0x2, -0x50(%rbp)
je 0x13b1aa
cmpq $0x4, -0x50(%rbp)
je 0x13b1aa
cmpq $0x8, -0x50(%rbp)
je 0x13b1aa
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x91f18(%rip), %rsi     # 0x1cd0b6
movb $0x0, %al
callq 0x15bfb0
jmp 0x13b3a7
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x13b1c5
jmp 0x13b3a7
cmpq $0x1, -0x50(%rbp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x13b1e4
movl $0x1, -0x68(%rbp)
jmp 0x13b217
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x490(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x13b610
leaq -0x490(%rbp), %rdi
movq -0x50(%rbp), %rcx
movl $0x1, %esi
shlq %cl, %rsi
callq 0x16aa20
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x64(%rbp)
jne 0x13b223
cmpl $0x0, -0x68(%rbp)
je 0x13b22c
movl $0x1, -0x6c(%rbp)
jmp 0x13b233
movl $0x3, -0x6c(%rbp)
movq -0x50(%rbp), %rax
imulq -0x30(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
imulq -0x60(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %r8
addq -0x28(%rbp), %r8
subq -0x58(%rbp), %r8
movq -0x60(%rbp), %r9
movq -0x78(%rbp), %rax
movq %rax, (%rsp)
callq 0x13b9b0
cmpl $0x0, %eax
jne 0x13b2a4
jmp 0x13b3a7
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x60(%rbp), %rax
shlq $0x3, %rax
cqto
idivq -0x50(%rbp)
movq %rax, %rdx
movq -0x38(%rbp), %rcx
movl -0x6c(%rbp), %r8d
callq 0x16b7f0
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x40(%rbp)
jle 0x13b324
cmpq $0x0, -0x48(%rbp)
jle 0x13b324
movq -0x88(%rbp), %rax
movl $0x2, 0x78(%rax)
cvtsi2sdq -0x40(%rbp), %xmm0
movq -0x88(%rbp), %rax
movsd %xmm0, 0x80(%rax)
cvtsi2sdq -0x48(%rbp), %xmm0
movq -0x88(%rbp), %rax
movsd %xmm0, 0x88(%rax)
cmpl $0x0, -0x64(%rbp)
je 0x13b344
movq -0x78(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0x80(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
xorl %r8d, %r8d
callq 0x16bee0
jmp 0x13b392
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x13b369
cmpq $0x2, -0x50(%rbp)
je 0x13b369
movq -0x8(%rbp), %rdi
leaq 0x91d73(%rip), %rsi     # 0x1cd0d3
movb $0x0, %al
callq 0x15bfb0
jmp 0x13b3a7
movq -0x78(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x60(%rbp), %rcx
leaq -0x490(%rbp), %r8
movq -0x80(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x80(%rbp), %rdi
movq -0x88(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x80(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x15d260
movq -0x78(%rbp), %rdi
callq 0x1676a0
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq
nopw %cs:(%rax,%rax)