// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, 0x28(%rsp)
leaq -0x9(%rdx), %rax
cmpq $-0x8, %rax
jb 0xf326a
movq 0xa0(%rsp), %rcx
movq $0x0, 0x18(%rsp)
movq 0x10(%rcx), %rax
testq %rax, %rax
jle 0xf326a
movq %rdx, %r12
movq %rsi, %r15
movq 0x8(%rcx), %rdx
leaq 0x7(%rdx), %rcx
movq %rdx, 0x20(%rsp)
addq $0xe, %rdx
testq %rcx, %rcx
cmovnsq %rcx, %rdx
sarq $0x3, %rdx
cmpq $0x2, %rdx
movl $0x1, %ecx
cmovgeq %rdx, %rcx
movq %rcx, 0x40(%rsp)
leaq -0x1(%r12), %rcx
movq %rcx, 0x38(%rsp)
movq $0x0, 0x10(%rsp)
movq %r9, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
cmpq $0x0, 0x20(%rsp)
jle 0xf324f
movq %r15, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, 0x60(%rsp)
movq 0x38(%rsp), %r14
xorl %ebp, %ebp
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0xeead3
movl 0xa8(%rsp), %ecx
testb $0x2, %cl
movl %r14d, %ecx
cmovneq %rbp, %rcx
movb %al, 0x18(%rsp,%rcx)
incq %rbp
addq %rbx, %r15
addq $-0x1, %r14
jb 0xf3178
movq 0x8(%rsp), %rax
leaq (,%rax,8), %r14
xorl %r15d, %r15d
movq 0x58(%rsp), %rbp
movq 0xa0(%rsp), %rbx
movl 0xa8(%rsp), %r13d
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
xorl %ecx, %ecx
xorl %eax, %eax
movzbl 0x18(%rsp,%rcx), %esi
movl $0x1, %edi
shll %cl, %edi
andl %edx, %esi
testl %esi, %esi
cmovel %esi, %edi
orl %edi, %eax
incq %rcx
cmpq %rcx, %r12
jne 0xf31d7
leaq (%r14,%r15), %rcx
movq %r14, %rsi
subq %r15, %rsi
addq $0x7, %rsi
testb $0x1, %r13b
movl %eax, %eax
cmovneq %rcx, %rsi
movl (%rbp,%rax,4), %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
callq 0xf24bf
incq %r15
cmpq $0x8, %r15
jne 0xf31c9
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x60(%rsp), %r15
incq %r15
movq %rcx, 0x8(%rsp)
cmpq 0x40(%rsp), %rcx
jne 0xf3162
movq 0x10(%rbx), %rax
movq 0x30(%rsp), %r15
movq 0x10(%rsp), %rdx
incq %rdx
addq 0x28(%rsp), %r15
movq %rdx, 0x10(%rsp)
cmpq %rax, %rdx
jl 0xf3148
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq