// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-bitmap.c
// Function: de_convert_image_paletted_planar
void de_convert_image_paletted_planar(dbuf *f, i64 fpos, i64 nplanes,
	i64 row_stride, i64 plane_stride, const de_color *pal, de_bitmap *img, UI flags)
{
	i64 ypos;
	u8 bit_order_is_lsb = 0;
	u8 plane_order_is_lsb = 0;
	u8 pbit[8]; // [0] is for bits from the least-significant plane, etc.
	i64 units_per_row; // num bytes per row per plane that we will process

	if(nplanes<1 || nplanes>8) goto done;
	de_zeromem(pbit, sizeof(pbit));

	if(flags & 0x01) bit_order_is_lsb = 1;
	if(flags & 0x02) plane_order_is_lsb = 1;
	units_per_row = (img->width + 7)/8;

	for(ypos=0; ypos<img->height; ypos++) {
		i64 n;

		// Read 8 bits from each plane, then rearrange them to make 8
		// output pixels.
		for(n=0; n<units_per_row; n++) {
			UI k;
			UI pn;
			u8 b;
			i64 xpos;

			for(pn=0; pn<(UI)nplanes; pn++) {
				b = dbuf_getbyte(f, fpos + ypos*row_stride + pn*plane_stride + n);
				if(plane_order_is_lsb) {
					pbit[pn] = b;
				}
				else {
					pbit[(UI)nplanes-1-pn] = b;
				}
			}

			for(k=0; k<8; k++) {
				UI palent;

				palent = 0;
				for(pn=0; pn<(UI)nplanes; pn++) {
					if((pbit[pn] & (1U<<k))!=0) {
						palent |= 1U<<pn;
					}
				}

				if(bit_order_is_lsb) {
					xpos = n*8 + (i64)k;
				}
				else {
					xpos = n*8 + (i64)(7-k);
				}
				de_bitmap_setpixel_rgba(img, xpos, ypos, pal[palent]);
			}
		}
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movb $0x0, -0x39(%rbp)
movb $0x0, -0x3a(%rbp)
cmpq $0x1, -0x18(%rbp)
jl 0x16c6d0
cmpq $0x8, -0x18(%rbp)
jle 0x16c6d5
jmp 0x16c8a2
leaq -0x42(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x19140
movl 0x18(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x16c6f4
movb $0x1, -0x39(%rbp)
movl 0x18(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x16c703
movb $0x1, -0x3a(%rbp)
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x16c8a0
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x16c88d
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jae 0x16c7bc
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rax
imulq -0x20(%rbp), %rax
addq %rax, %rsi
movl -0x60(%rbp), %eax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq -0x58(%rbp), %rsi
callq 0x165060
movb %al, -0x61(%rbp)
cmpb $0x0, -0x3a(%rbp)
je 0x16c79c
movb -0x61(%rbp), %cl
movl -0x60(%rbp), %eax
movb %cl, -0x42(%rbp,%rax)
jmp 0x16c7af
movb -0x61(%rbp), %cl
movq -0x18(%rbp), %rax
subl $0x1, %eax
subl -0x60(%rbp), %eax
movl %eax, %eax
movb %cl, -0x42(%rbp,%rax)
jmp 0x16c7b1
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x16c754
movl $0x0, -0x5c(%rbp)
cmpl $0x8, -0x5c(%rbp)
jae 0x16c87a
movl $0x0, -0x74(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jae 0x16c81e
movl -0x60(%rbp), %eax
movzbl -0x42(%rbp,%rax), %eax
movl -0x5c(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x16c811
movl -0x60(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
orl -0x74(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x16c813
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x16c7db
cmpb $0x0, -0x39(%rbp)
je 0x16c838
movq -0x58(%rbp), %rax
shlq $0x3, %rax
movl -0x5c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0x16c851
movq -0x58(%rbp), %rax
shlq $0x3, %rax
movl $0x7, %ecx
subl -0x5c(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq 0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rax
movl -0x74(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
callq 0x16b350
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x16c7c3
jmp 0x16c87c
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x16c73f
jmp 0x16c88f
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x16c725
jmp 0x16c8a2
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)