// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/palmbitmap.c
// Function: do_generate_img_from_unc_pixels
static void do_generate_img_from_unc_pixels(deark *c, lctx *d, struct page_ctx *pg,
	dbuf *unc_pixels)
{
	i64 i, j;
	i64 pdwidth;
	u32 clr;
	int has_color;
	de_bitmap *img = NULL;
	u32 pal[256];

	de_zeromem(pal, sizeof(pal));
	has_color = (pg->bitsperpixel>4 || pg->has_custom_pal);

	if(pg->bitsperpixel==1 && !has_color) {
		de_convert_and_write_image_bilevel2(unc_pixels, 0, pg->w, pg->h, pg->rowbytes,
			DE_CVTF_WHITEISZERO, NULL, 0);
		goto done;
	}

	pdwidth = (pg->rowbytes*8) / pg->bitsperpixel;
	if(pdwidth<pg->w) pdwidth = pg->w;

	img = de_bitmap_create2(c, pg->w, pdwidth, pg->h,
		(has_color?3:1) + (pg->has_trns?1:0));

	if(pg->bitsperpixel==16) {
		for(j=0; j<pg->h; j++) {
			for(i=0; i<pdwidth; i++) {
				u32 clr1;
				clr1 = (u32)dbuf_getu16be(unc_pixels, pg->rowbytes*j + 2*i);
				clr = de_rgb565_to_888(clr1);
				de_bitmap_setpixel_rgb(img, i, j, clr);
				if(pg->has_trns && clr1==pg->trns_value) {
					de_bitmap_setsample(img, i, j, 3, 0);
				}
			}
		}
	}
	else {
		if(!has_color) {
			if(pg->bitsperpixel>8) goto done;
			// TODO: What are the correct colors (esp. for 4bpp)?
			de_make_grayscale_palette(pal, 1ULL<<pg->bitsperpixel, 0x1);
		}
		else if(pg->has_custom_pal) {
			de_memcpy(pal, pg->custom_pal, sizeof(pg->custom_pal));
		}
		else {
			make_stdpal256(c, d, pal);
		}

		if(pg->has_trns && pg->trns_value<256) {
			pal[pg->trns_value] = DE_SET_ALPHA(pal[pg->trns_value], 0);
		}

		de_convert_image_paletted(unc_pixels, 0,pg->bitsperpixel, pg->rowbytes,
			pal, img, 0);
	}

	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0x450(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x19140
movq -0x18(%rbp), %rcx
movb $0x1, %al
cmpq $0x4, 0x10(%rcx)
movb %al, -0x455(%rbp)
jg 0xfde9a
movq -0x18(%rbp), %rax
cmpl $0x0, 0x30(%rax)
setne %al
movb %al, -0x455(%rbp)
movb -0x455(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jne 0xfdefa
cmpl $0x0, -0x40(%rbp)
jne 0xfdefa
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %r8
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %r9d
xorl %eax, %eax
movq $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16c000
jmp 0xfe155
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
shlq $0x3, %rax
movq -0x18(%rbp), %rcx
cqto
idivq 0x10(%rcx)
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jge 0xfdf2c
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x40(%rbp), %r9d
movl $0x1, %r8d
movl $0x3, %eax
cmpl $0x0, %r9d
cmovnel %eax, %r8d
movq -0x18(%rbp), %rax
movl 0x20(%rax), %r10d
xorl %eax, %eax
movl $0x1, %r9d
cmpl $0x0, %r10d
cmovnel %r9d, %eax
addl %eax, %r8d
callq 0x16b7f0
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x10, 0x10(%rax)
jne 0xfe062
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0xfe05d
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0xfe04a
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
imulq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x1651d0
movl %eax, -0x454(%rbp)
movl -0x454(%rbp), %edi
callq 0x1618a0
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl -0x3c(%rbp), %ecx
callq 0x16b310
movq -0x18(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0xfe037
movl -0x454(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0xfe037
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl $0x3, %ecx
xorl %r8d, %r8d
callq 0x16b060
jmp 0xfe039
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xfdfaf
jmp 0xfe04c
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfdf95
jmp 0xfe143
cmpl $0x0, -0x40(%rbp)
jne 0xfe09b
movq -0x18(%rbp), %rax
cmpq $0x8, 0x10(%rax)
jle 0xfe078
jmp 0xfe155
leaq -0x450(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x1, %esi
shlq %cl, %rsi
movl $0x1, %edx
callq 0x16d580
jmp 0xfe0d6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0xfe0c0
leaq -0x450(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x50, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x191e0
jmp 0xfe0d4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x450(%rbp), %rdx
callq 0xfe400
jmp 0xfe0d6
movq -0x18(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0xfe112
movq -0x18(%rbp), %rax
cmpl $0x100, 0x24(%rax)      # imm = 0x100
jae 0xfe112
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movl -0x450(%rbp,%rax,4), %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
orl $0x0, %ecx
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movl %ecx, -0x450(%rbp,%rax,4)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
leaq -0x450(%rbp), %r8
movq -0x48(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x48(%rbp), %rdi
callq 0x16af60
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
nopw (%rax,%rax)