// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rsi, %r13
movq %rdi, %r12
incq 0x428(%rsi)
leaq 0xa(%rcx), %rbx
xorl %eax, %eax
cmpl $0x14, %edx
movq %rcx, 0x18(%rsp)
cmovneq %rcx, %rbx
sete %al
incl %eax
cmpl 0x424(%rsi), %eax
jle 0xd3b33
movl %eax, 0x424(%r13)
movq 0x18(%r12), %rdi
movq %rbx, %rsi
callq 0xeebc8
movq %rax, %rbp
leaq 0x2(%rbx), %rsi
movq 0x18(%r12), %rdi
callq 0xeebc8
leaq 0x4(%rbx), %r15
movq %r12, %rdi
movq %rbp, 0x28(%rsp)
movq %rbp, %rsi
movq %rax, 0x20(%rsp)
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xeebc8
movq %rax, %rbp
leaq 0x725aa(%rip), %rsi     # 0x14612e
movq %r12, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x6(%rbx), %rsi
movq 0x18(%r12), %rdi
callq 0xeebc8
movq %rax, %r15
leaq 0x8(%rbx), %rsi
movq 0x18(%r12), %rdi
callq 0xeebc8
leaq 0x6d9a1(%rip), %rsi     # 0x141557
movq %r12, %rdi
movq %r15, 0x48(%rsp)
movl %r15d, %edx
movq %rax, 0x40(%rsp)
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
cmpq $0x1, 0x428(%r13)
jne 0xd3bf8
movq %rbp, 0x430(%r13)
movq 0x28(%rsp), %rax
movq %rax, 0x438(%r13)
movq 0x20(%rsp), %rax
movq %rax, 0x440(%r13)
cmpq $0x8, %rbp
movq %r12, 0x10(%rsp)
ja 0xd3e87
movl $0x116, %eax            # imm = 0x116
btq %rbp, %rax
jae 0xd3e87
movq %r12, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xf1db1
testl %eax, %eax
je 0xd3e83
addq $0xa, %rbx
cmpq $0x1, %rbp
movq %r13, 0x38(%rsp)
je 0xd400e
cmpq $0x2, %rbp
jne 0xd3c54
cmpl $0x0, 0x10(%r13)
je 0xd3fbc
cmpq $0x4, %rbp
sete %cl
cmpl $0x0, 0xc(%r13)
setne %al
andb %cl, %al
movzbl %al, %ecx
movd %ecx, %xmm0
pshufd $0x0, %xmm0, %xmm1      # xmm1 = xmm0[0,0,0,0]
movdqa 0x5e936(%rip), %xmm9    # 0x1325b0
movdqa 0x5ca4e(%rip), %xmm3    # 0x1306d0
pxor %xmm4, %xmm4
xorl %ecx, %ecx
movdqa 0x5ca50(%rip), %xmm5    # 0x1306e0
movaps 0x6d578(%rip), %xmm10   # 0x141210
movaps %xmm10, %xmm6
shufps $0x88, %xmm10, %xmm6    # xmm6 = xmm6[0,2],xmm10[0,2]
movdqa 0x6d596(%rip), %xmm12   # 0x141240
movdqa 0x56b4d(%rip), %xmm13   # 0x12a800
pxor %xmm11, %xmm11
pxor %xmm0, %xmm0
pxor %xmm8, %xmm8
movdqa %xmm3, %xmm15
pxor %xmm5, %xmm15
movdqa %xmm9, %xmm2
pxor %xmm5, %xmm2
movaps %xmm10, %xmm14
pcmpeqd %xmm15, %xmm14
shufps $0x88, %xmm2, %xmm15    # xmm15 = xmm15[0,2],xmm2[0,2]
movaps %xmm6, %xmm7
pcmpgtd %xmm15, %xmm7
pcmpeqd %xmm10, %xmm2
shufps $0xdd, %xmm2, %xmm14    # xmm14 = xmm14[1,3],xmm2[1,3]
movdqu 0x14(%r13,%rcx,4), %xmm15
andps %xmm7, %xmm14
andps %xmm1, %xmm14
movdqa %xmm15, %xmm2
pand 0x6d520(%rip), %xmm2    # 0x141230
pcmpgtd %xmm4, %xmm2
pand %xmm14, %xmm2
por %xmm2, %xmm8
movdqa %xmm15, %xmm2
pand 0x6d4f5(%rip), %xmm2    # 0x141220
pcmpgtd %xmm4, %xmm2
pand %xmm14, %xmm2
por %xmm2, %xmm0
movdqa %xmm15, 0x50(%rsp,%rcx,4)
pand %xmm12, %xmm15
pcmpgtd %xmm4, %xmm15
pand %xmm14, %xmm15
por %xmm15, %xmm11
addq $0x4, %rcx
paddq %xmm13, %xmm3
paddq %xmm13, %xmm9
cmpq $0x100, %rcx            # imm = 0x100
jne 0xd3cc1
movq %r14, %r15
pslld $0x1f, %xmm11
movmskps %xmm11, %ecx
pslld $0x1f, %xmm8
movmskps %xmm8, %edx
testl %edx, %edx
sete %dl
andb %al, %dl
cmpb $0x1, %dl
jne 0xd3ea8
testb %cl, %cl
je 0xd3ea8
leaq 0x6d84a(%rip), %rsi     # 0x1415ee
xorl %r14d, %r14d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
movdqa 0x6d4e7(%rip), %xmm0    # 0x1412a0
movdqa 0x43a2f(%rip), %xmm1    # 0x1177f0
movdqa 0x6d4e7(%rip), %xmm2    # 0x1412b0
movdqa 0x6d4ef(%rip), %xmm3    # 0x1412c0
movdqa 0x6d4f7(%rip), %xmm4    # 0x1412d0
movdqa 0x6d4ff(%rip), %xmm5    # 0x1412e0
movdqa 0x3cf97(%rip), %xmm6    # 0x110d80
movdqa 0x50(%rsp,%r14,4), %xmm7
movdqa %xmm7, %xmm9
psrld $0x16, %xmm9
movdqa %xmm7, %xmm8
psrld $0xe, %xmm8
pand %xmm0, %xmm9
pand %xmm0, %xmm8
pmaddwd %xmm5, %xmm8
pand %xmm1, %xmm7
packuswb %xmm7, %xmm7
packuswb %xmm7, %xmm7
psrlw $0x6, %xmm7
pand %xmm2, %xmm7
punpcklbw %xmm7, %xmm7    # xmm7 = xmm7[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pmullw %xmm3, %xmm7
punpcklwd %xmm7, %xmm7    # xmm7 = xmm7[0,0,1,1,2,2,3,3]
pshufd $0xf5, %xmm9, %xmm10    # xmm10 = xmm9[1,1,3,3]
pmuludq %xmm4, %xmm9
pshufd $0xe8, %xmm9, %xmm9     # xmm9 = xmm9[0,2,2,3]
pmuludq %xmm4, %xmm10
pshufd $0xe8, %xmm10, %xmm10   # xmm10 = xmm10[0,2,2,3]
punpckldq %xmm10, %xmm9   # xmm9 = xmm9[0],xmm10[0],xmm9[1],xmm10[1]
por %xmm8, %xmm9
pand %xmm1, %xmm7
por %xmm9, %xmm7
por %xmm6, %xmm7
movdqa %xmm7, 0x50(%rsp,%r14,4)
addq $0x4, %r14
cmpq $0x10, %r14
jne 0xd3de9
jmp 0xd3fef
xorl %ebp, %ebp
jmp 0xd3e9d
leaq 0x6d6dd(%rip), %rsi     # 0x14156b
movq %rbp, %rdx
xorl %ebp, %ebp
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r12d, %r12d
xorl %r13d, %r13d
jmp 0xd4247
pslld $0x1f, %xmm0
movmskps %xmm0, %edx
testl %edx, %edx
sete %dl
andb %dl, %al
cmpb $0x1, %al
jne 0xd3fef
testb %cl, %cl
je 0xd3fef
leaq 0x54fc7(%rip), %rsi     # 0x128e95
xorl %r14d, %r14d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8617
movdqa 0x6d36d(%rip), %xmm0    # 0x141250
movdqa 0x43905(%rip), %xmm1    # 0x1177f0
movdqa 0x6d36d(%rip), %xmm2    # 0x141260
movdqa 0x6d375(%rip), %xmm3    # 0x141270
movdqa 0x6d37d(%rip), %xmm4    # 0x141280
movdqa 0x6d385(%rip), %xmm5    # 0x141290
xorps %xmm6, %xmm6
movdqa 0x3ce6a(%rip), %xmm7    # 0x110d80
movdqa 0x50(%rsp,%r14,4), %xmm8
movdqa %xmm8, %xmm9
psrld $0x14, %xmm9
movdqa %xmm8, %xmm10
psrld $0xc, %xmm10
pand %xmm0, %xmm9
pand %xmm0, %xmm10
pmaddwd %xmm5, %xmm10
pand %xmm1, %xmm8
packuswb %xmm8, %xmm8
packuswb %xmm8, %xmm8
movdqa %xmm8, %xmm11
psrlw $0x4, %xmm11
pand %xmm2, %xmm11
pand %xmm3, %xmm8
por %xmm11, %xmm8
pshufd $0xf5, %xmm9, %xmm11    # xmm11 = xmm9[1,1,3,3]
pmuludq %xmm4, %xmm9
pshufd $0xe8, %xmm9, %xmm9     # xmm9 = xmm9[0,2,2,3]
pmuludq %xmm4, %xmm11
pshufd $0xe8, %xmm11, %xmm11   # xmm11 = xmm11[0,2,2,3]
punpckldq %xmm11, %xmm9   # xmm9 = xmm9[0],xmm11[0],xmm9[1],xmm11[1]
por %xmm10, %xmm9
punpcklbw %xmm6, %xmm8    # xmm8 = xmm8[0],xmm6[0],xmm8[1],xmm6[1],xmm8[2],xmm6[2],xmm8[3],xmm6[3],xmm8[4],xmm6[4],xmm8[5],xmm6[5],xmm8[6],xmm6[6],xmm8[7],xmm6[7]
punpcklwd %xmm6, %xmm8    # xmm8 = xmm8[0],xmm6[0],xmm8[1],xmm6[1],xmm8[2],xmm6[2],xmm8[3],xmm6[3]
por %xmm9, %xmm8
por %xmm7, %xmm8
movdqa %xmm8, 0x50(%rsp,%r14,4)
addq $0x4, %r14
cmpq $0x10, %r14
jne 0xd3f16
jmp 0xd3fef
movq %r14, %r15
leaq 0x6d5f4(%rip), %rsi     # 0x1415ba
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8faa
leaq 0x50(%rsp), %rcx
movl $0x4, %r8d
movl $0xa, %edi
movl $0x2, %esi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xec55a
movl $0x1, %esi
movl %ebp, %ecx
shlq %cl, %rsi
leaq 0x50(%rsp), %rdi
callq 0xf1ed8
testl %eax, %eax
movq %r15, %r14
je 0xd4276
movl $0x1, 0xc(%rsp)
movq 0x28(%rsp), %r15
movq %rbp, 0x30(%rsp)
imulq %rbp, %r15
leaq 0x7(%r15), %rax
addq $0xe, %r15
testq %rax, %rax
cmovnsq %rax, %r15
sarq $0x3, %r15
movq 0x20(%rsp), %rsi
imulq %r15, %rsi
movq %r12, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %rbp
addq 0x18(%rsp), %r14
cmpq %rbx, %r14
jle 0xd412e
movq 0x10(%rsp), %rax
movq 0x18(%rax), %r12
movq %r14, 0x18(%rsp)
leaq 0x1(%rbx), %r13
movq %r12, %rdi
movq %rbx, %rsi
callq 0xeead3
testb %al, %al
je 0xd40a0
js 0xd40db
movzbl %al, %ebx
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xef481
addq %rbx, %r13
movq %r13, %rbx
jmp 0xd4125
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xeead3
testb %al, %al
je 0xd4125
movzbl %al, %r14d
movq 0x18(%rbp), %r13
subq %r15, %r13
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xef481
decq %r14
jne 0xd40be
movq 0x18(%rsp), %r14
jmp 0xd4125
movl %eax, %ecx
negb %cl
jno 0xd40fd
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xeead3
movzbl %al, %edx
movq %rbp, %rdi
movl $0xff, %esi
jmp 0xd4120
andb $0x7f, %al
movzbl %al, %r14d
addq $0x2, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0xeead3
movzbl %al, %esi
movq %rbp, %rdi
movq %r14, %rdx
movq 0x18(%rsp), %r14
callq 0xf0eba
cmpq %r14, %rbx
jl 0xd406c
leaq (,%r15,8), %rax
cqto
idivq 0x30(%rsp)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rax, %rdx
movq 0x20(%rsp), %rcx
movl 0xc(%rsp), %r8d
callq 0xf26a5
movq %rax, %r12
movq %rbx, %rdi
callq 0xea504
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 0xd41d0
movq %rsi, %xmm0
movq %rdx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0x435d6(%rip), %xmm0    # 0x117770
pand %xmm1, %xmm0
por 0x435da(%rip), %xmm0    # 0x117780
movl $0x2, 0x78(%r13)
psrlq $0x20, %xmm1
por 0x435d5(%rip), %xmm1    # 0x117790
subpd 0x435dd(%rip), %xmm1    # 0x1177a0
addpd %xmm0, %xmm1
movupd %xmm1, 0x80(%r13)
movq 0x30(%rsp), %rax
cmpq $0x1, %rax
jne 0xd41f0
movq %rbp, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0xf2a40
jmp 0xd4237
cmpq $0x2, %rax
je 0xd4216
movq 0x38(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xd4216
leaq 0x6d380(%rip), %rsi     # 0x141588
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xd4247
movl $0x0, (%rsp)
leaq 0x50(%rsp), %r8
movq %rbp, %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %r12, %r9
callq 0xf2f79
movq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0xf22d0
movq %r12, %rdi
callq 0xf228d
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xea50e
movq %rbp, %rdi
callq 0xf052b
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3, 0xc(%rsp)
jmp 0xd4016