// File: /workspace/github_c_makefile_star2_O2/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 $0x458, %rsp            # imm = 0x458
movq %r8, %r12
movq %rsi, %r14
movq %rdi, %rbp
incq 0x428(%rsi)
leaq 0xa(%rcx), %r15
xorl %eax, %eax
cmpl $0x14, %edx
movq %rcx, 0x28(%rsp)
cmovneq %rcx, %r15
sete %al
incl %eax
cmpl 0x424(%rsi), %eax
jle 0xcadce
movl %eax, 0x424(%r14)
movq 0x18(%rbp), %rdi
movq %r15, %rsi
callq 0xe3fa3
movq %rax, %r13
leaq 0x2(%r15), %rsi
movq 0x18(%rbp), %rdi
callq 0xe3fa3
leaq 0x4(%r15), %rbx
movq %rbp, %rdi
movq %r13, 0x20(%rsp)
movq %r13, %rsi
movq %rax, 0x18(%rsp)
movq %rax, %rdx
callq 0xdf038
movq 0x18(%rbp), %rdi
movq %rbx, %rsi
callq 0xe3fa3
movq %rax, %rbx
leaq 0x6dec2(%rip), %rsi     # 0x138cde
movq %rbp, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x6(%r15), %rsi
movq 0x18(%rbp), %rdi
callq 0xe3fa3
movq %rax, %r13
leaq 0x8(%r15), %rsi
movq 0x18(%rbp), %rdi
callq 0xe3fa3
leaq 0x692bb(%rip), %rsi     # 0x134107
movq %rbp, %rdi
movq %r13, 0x48(%rsp)
movl %r13d, %edx
movq %rax, 0x40(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
cmpq $0x1, 0x428(%r14)
jne 0xcae8e
movq %rbx, 0x430(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x438(%r14)
movq 0x18(%rsp), %rax
movq %rax, 0x440(%r14)
cmpq $0x8, %rbx
ja 0xcafc4
movl $0x116, %eax            # imm = 0x116
btq %rbx, %rax
jae 0xcafc4
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0xe6be1
testl %eax, %eax
je 0xcaf3b
pushq $0x1
popq %r13
cmpq $0x1, %rbx
movq %rbx, 0x10(%rsp)
movq %r14, 0x30(%rsp)
je 0xcb09f
cmpq $0x2, %rbx
jne 0xcaee6
cmpl $0x0, 0x10(%r14)
je 0xcb056
movl 0xc(%r14), %esi
xorl %edi, %edi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x100, %rdi            # imm = 0x100
je 0xcaf42
movl 0x14(%r14,%rdi,4), %r8d
movl %r8d, 0x50(%rsp,%rdi,4)
cmpq $0x4, %rbx
jne 0xcaf36
cmpq $0xf, %rdi
ja 0xcaf36
testl %esi, %esi
je 0xcaf36
testl $0xf0f0f, %r8d          # imm = 0xF0F0F
cmovnel %r13d, %ecx
testl $0x3f3f3f, %r8d         # imm = 0x3F3F3F
cmovnel %r13d, %edx
testl $0xffffff, %r8d         # imm = 0xFFFFFF
cmovnel %r13d, %eax
incq %rdi
jmp 0xcaef2
xorl %ebx, %ebx
jmp 0xcafda
testl %esi, %esi
setne %dil
cmpq $0x4, %rbx
sete %sil
andb %dil, %sil
cmpb $0x1, %sil
jne 0xcafe5
testl %edx, %edx
jne 0xcafe5
testl %eax, %eax
je 0xcafe5
leaq 0x6922e(%rip), %rsi     # 0x13419e
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xde896
cmpq $0x10, %rbx
je 0xcb080
movl 0x50(%rsp,%rbx,4), %eax
movl %eax, %ecx
shrl $0x16, %ecx
movl %eax, %edx
shrl $0xe, %edx
andl $0x3, %ecx
andl $0x3, %edx
shrb $0x6, %al
movzbl %al, %eax
imull $0x55, %eax, %eax
imull $0x550000, %ecx, %ecx   # imm = 0x550000
imull $0x5500, %edx, %edx     # imm = 0x5500
orl %ecx, %edx
movzbl %al, %eax
addl %edx, %eax
addl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x50(%rsp,%rbx,4)
incq %rbx
jmp 0xcaf7c
leaq 0x69150(%rip), %rsi     # 0x13411b
movq %rbx, %rdx
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0xcb2c7
testl %ecx, %ecx
sete %cl
andb %cl, %sil
cmpb $0x1, %sil
jne 0xcb080
testl %eax, %eax
je 0xcb080
leaq 0x50c4f(%rip), %rsi     # 0x11bc55
xorl %ebx, %ebx
movq %rbp, %rdi
xorl %eax, %eax
callq 0xde896
cmpq $0x10, %rbx
je 0xcb080
movl 0x50(%rsp,%rbx,4), %eax
movl %eax, %ecx
shrl $0x14, %ecx
movl %eax, %edx
shrl $0xc, %edx
andl $0xf, %ecx
andl $0xf, %edx
shrb $0x4, %al
movzbl %al, %eax
imull $0x11, %eax, %eax
imull $0x110000, %ecx, %ecx   # imm = 0x110000
imull $0x1100, %edx, %edx     # imm = 0x1100
orl %ecx, %edx
movzbl %al, %eax
addl %edx, %eax
addl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x50(%rsp,%rbx,4)
incq %rbx
jmp 0xcb012
leaq 0x6910d(%rip), %rsi     # 0x13416a
movq %rbp, %rdi
xorl %eax, %eax
callq 0xdf05a
pushq $0xa
popq %rdi
pushq $0x2
popq %rsi
leaq 0x50(%rsp), %rcx
pushq $0x4
popq %r8
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xe1b11
pushq $0x1
popq %rsi
movq 0x10(%rsp), %rbx
movl %ebx, %ecx
shlq %cl, %rsi
leaq 0x50(%rsp), %rdi
callq 0xe6d06
testl %eax, %eax
je 0xcb2f4
movl %r13d, 0xc(%rsp)
addq $0xa, %r15
movq 0x20(%rsp), %rax
imulq %rbx, %rax
addq $0x7, %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
movq %rax, %r14
movq 0x18(%rsp), %rsi
imulq %rax, %rsi
pushq $0x1
popq %rdx
movq %rbp, %rdi
callq 0xe59cd
movq %rax, %rbx
movq %rbp, 0x38(%rsp)
movq 0x18(%rbp), %rbp
addq 0x28(%rsp), %r12
movq %r12, 0x28(%rsp)
cmpq %r12, %r15
jge 0xcb1b6
leaq 0x1(%r15), %r13
movq %rbp, %rdi
movq %r15, %rsi
callq 0xe3eb4
testb %al, %al
je 0xcb125
js 0xcb15f
movzbl %al, %r15d
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xe45f4
addq %r15, %r13
movq %r13, %r15
jmp 0xcb0ea
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe3eb4
movzbl %al, %r12d
movq 0x18(%rbx), %r13
subq %r14, %r13
subq $0x1, %r12
jb 0xcb154
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xe45f4
jmp 0xcb13b
addq $0x2, %r15
movq 0x28(%rsp), %r12
jmp 0xcb0ea
movl %eax, %ecx
negb %cl
jno 0xcb189
addq $0x2, %r15
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe3eb4
movzbl %al, %edx
movq %rbx, %rdi
movl $0xff, %esi
callq 0xe5e40
jmp 0xcb0ea
andb $0x7f, %al
movzbl %al, %r12d
addq $0x2, %r15
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe3eb4
movzbl %al, %esi
movq %rbx, %rdi
movq %r12, %rdx
movq 0x28(%rsp), %r12
callq 0xe5e40
jmp 0xcb0ea
movq %r14, %rax
shlq $0x3, %rax
cqto
idivq 0x10(%rsp)
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0xe746f
movq %rax, %r12
movq %rbp, %rdi
callq 0xdfd1a
movq %rax, %r13
movq 0x48(%rsp), %rdx
testq %rdx, %rdx
setle %al
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
setle %cl
orb %al, %cl
jne 0xcb257
movq %rsi, %xmm0
movq %rdx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0x3f4bf(%rip), %xmm0    # 0x10a6e0
pand %xmm1, %xmm0
por 0x3f4c3(%rip), %xmm0    # 0x10a6f0
movl $0x2, 0x78(%r13)
psrlq $0x20, %xmm1
por 0x3f4be(%rip), %xmm1    # 0x10a700
subpd 0x3f4c6(%rip), %xmm1    # 0x10a710
addpd %xmm0, %xmm1
movupd %xmm1, 0x80(%r13)
movq 0x10(%rsp), %rax
cmpq $0x1, %rax
jne 0xcb277
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xe77b7
jmp 0xcb2b9
cmpq $0x2, %rax
je 0xcb29b
movq 0x30(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xcb29b
leaq 0x68ea9(%rip), %rsi     # 0x134138
movq %rbp, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0xcb2c7
andl $0x0, (%rsp)
leaq 0x50(%rsp), %r8
movq %rbx, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
movq %r12, %r9
callq 0xe7cbc
pushq $0x2
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0xe70ab
movq %r12, %rdi
callq 0xe7084
movq %rbp, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %rbx, %rdi
callq 0xe5591
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
popq %rax
movl %eax, 0xc(%rsp)
jmp 0xcb0a4