// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %r8, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %r14
incq 0x428(%rsi)
leaq 0xa(%rcx), %rbx
xorl %eax, %eax
cmpl $0x14, %edx
movq %rcx, 0x48(%rsp)
cmovneq %rcx, %rbx
sete %al
incl %eax
cmpl 0x424(%rsi), %eax
jle 0xd4795
movl %eax, 0x424(%r12)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xef1ca
movq %rax, %rbp
leaq 0x2(%rbx), %rsi
movq 0x18(%r14), %rdi
callq 0xef1ca
leaq 0x4(%rbx), %r15
movq %r14, %rdi
movq %rbp, %rsi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0xef1ca
movq %rax, %r13
leaq 0x71466(%rip), %rsi     # 0x145c44
movq %r14, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x6(%rbx), %rsi
movq 0x18(%r14), %rdi
callq 0xef1ca
movq %rax, %r15
leaq 0x8(%rbx), %rsi
movq 0x18(%r14), %rdi
callq 0xef1ca
leaq 0x6c868(%rip), %rsi     # 0x141077
movq %r14, %rdi
movq %r15, 0x58(%rsp)
movl %r15d, %edx
movq %rax, 0x50(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
cmpq $0x1, 0x428(%r12)
jne 0xd4850
movq %r13, 0x430(%r12)
movq %rbp, 0x438(%r12)
movq 0x18(%rsp), %rax
movq %rax, 0x440(%r12)
cmpq $0x8, %r13
movq %r14, 0x10(%rsp)
ja 0xd4939
movl $0x116, %eax            # imm = 0x116
btq %r13, %rax
jae 0xd4939
movq %r14, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq 0xf241d
testl %eax, %eax
je 0xd4935
addq $0xa, %rbx
movl $0x1, %eax
cmpq $0x1, %r13
je 0xd4a76
cmpq $0x2, %r13
jne 0xd48ab
cmpl $0x0, 0x10(%r12)
je 0xd4a23
cmpq $0x4, %r13
sete %cl
cmpl $0x0, 0xc(%r12)
setne %al
andb %cl, %al
xorl %ecx, %ecx
movl %eax, %esi
xorb $0x1, %sil
movl $0x1, %edi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl 0x14(%r12,%r9,4), %r10d
movl %r10d, 0x60(%rsp,%r9,4)
cmpq $0x10, %r9
setae %r11b
orb %sil, %r11b
jne 0xd4908
testl $0xf0f0f, %r10d         # imm = 0xF0F0F
cmovnel %edi, %edx
testl $0x3f3f3f, %r10d        # imm = 0x3F3F3F
cmovnel %edi, %r8d
testl $0xffffff, %r10d        # imm = 0xFFFFFF
cmovnel %edi, %ecx
incq %r9
cmpq $0x100, %r9             # imm = 0x100
jne 0xd48d2
movq %rbp, %r15
testl %r8d, %r8d
sete %sil
andb %al, %sil
testl %ecx, %ecx
setne %bpl
andb %sil, %bpl
je 0xd495a
leaq 0x6c7db(%rip), %rsi     # 0x14110e
jmp 0xd4972
xorl %ebp, %ebp
jmp 0xd494f
leaq 0x6c74b(%rip), %rsi     # 0x14108b
xorl %ebp, %ebp
movq %r14, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9df7
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0xd4c93
testl %edx, %edx
sete %dl
andb %dl, %al
movb $0x1, %dl
cmpb $0x1, %al
jne 0xd497e
testl %ecx, %ecx
je 0xd497e
leaq 0x542f3(%rip), %rsi     # 0x128c65
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movl %ebp, %edx
testb %bpl, %bpl
je 0xd49d0
xorl %eax, %eax
movq %r15, %rbp
movl 0x60(%rsp,%rax,4), %ecx
movl %ecx, %edx
shrl $0x16, %edx
movl %ecx, %esi
shrl $0xe, %esi
andl $0x3, %edx
andl $0x3, %esi
shrb $0x6, %cl
movzbl %cl, %ecx
imull $0x55, %ecx, %ecx
imull $0x550000, %edx, %edx   # imm = 0x550000
imull $0x5500, %esi, %esi     # imm = 0x5500
orl %edx, %esi
movzbl %cl, %ecx
addl %esi, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, 0x60(%rsp,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0xd4988
jmp 0xd4a53
testb %dl, %dl
movq %r15, %rbp
jne 0xd4a53
xorl %eax, %eax
movl 0x60(%rsp,%rax,4), %ecx
movl %ecx, %edx
shrl $0x14, %edx
movl %ecx, %esi
shrl $0xc, %esi
andl $0xf, %edx
andl $0xf, %esi
shrb $0x4, %cl
movzbl %cl, %ecx
movl %ecx, %edi
shll $0x4, %edi
addl %ecx, %edi
imull $0x110000, %edx, %ecx   # imm = 0x110000
imull $0x1100, %esi, %edx     # imm = 0x1100
orl %ecx, %edx
movzbl %dil, %ecx
addl %edx, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, 0x60(%rsp,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0xd49d9
jmp 0xd4a53
leaq 0x6c6b0(%rip), %rsi     # 0x1410da
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
leaq 0x60(%rsp), %rcx
movl $0x4, %r8d
movl $0xa, %edi
movl $0x2, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xecbbc
movl $0x1, %esi
movl %r13d, %ecx
shlq %cl, %rsi
leaq 0x60(%rsp), %rdi
callq 0xf2544
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %eax
movl %eax, 0x34(%rsp)
movq %r12, 0x38(%rsp)
movq %rbp, 0x40(%rsp)
movq %rbp, %r15
movq %r13, 0x28(%rsp)
imulq %r13, %r15
leaq 0x7(%r15), %rax
addq $0xe, %r15
testq %rax, %rax
cmovnsq %rax, %r15
sarq $0x3, %r15
movq 0x18(%rsp), %rsi
imulq %r15, %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %rbp
movq 0x20(%rsp), %r14
addq 0x48(%rsp), %r14
cmpq %rbx, %r14
jle 0xd4b9f
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r12
movq %r14, 0x20(%rsp)
leaq 0x1(%rbx), %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0xef0d5
testb %al, %al
je 0xd4b11
js 0xd4b4c
movzbl %al, %ebx
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xefabc
addq %rbx, %r13
movq %r13, %rbx
jmp 0xd4b96
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xef0d5
testb %al, %al
je 0xd4b96
movzbl %al, %r14d
movq 0x18(%rbp), %r13
subq %r15, %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xefabc
decq %r14
jne 0xd4b2f
movq 0x20(%rsp), %r14
jmp 0xd4b96
movl %eax, %ecx
negb %cl
jno 0xd4b6e
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xef0d5
movzbl %al, %edx
movq %rbp, %rdi
movl $0xff, %esi
jmp 0xd4b91
andb $0x7f, %al
movzbl %al, %r14d
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xef0d5
movzbl %al, %esi
movq %rbp, %rdi
movq %r14, %rdx
movq 0x20(%rsp), %r14
callq 0xf14bd
cmpq %r14, %rbx
jl 0xd4add
leaq (,%r15,8), %rax
cqto
idivq 0x28(%rsp)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %rax, %rdx
movq 0x18(%rsp), %rcx
movl 0x34(%rsp), %r8d
callq 0xf2ce6
movq %rax, %r12
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r13
movq 0x58(%rsp), %rdx
testq %rdx, %rdx
setle %al
movq 0x50(%rsp), %rsi
testq %rsi, %rsi
setle %cl
orb %al, %cl
jne 0xd4c1c
movl $0x2, 0x78(%r13)
cvtsi2sd %rdx, %xmm0
movsd %xmm0, 0x80(%r13)
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
movsd %xmm0, 0x88(%r13)
movq 0x28(%rsp), %rax
cmpq $0x1, %rax
jne 0xd4c3c
movq %rbp, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf3081
jmp 0xd4c83
cmpq $0x2, %rax
je 0xd4c62
movq 0x38(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xd4c62
leaq 0x6c454(%rip), %rsi     # 0x1410a8
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xd4c93
movl $0x0, (%rsp)
leaq 0x60(%rsp), %r8
movq %rbp, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
movq %r15, %rcx
movq %r12, %r9
callq 0xf35b8
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf2917
movq %r12, %rdi
callq 0xf28d4
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xea9b3
movq %rbp, %rdi
callq 0xf0b13
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq