// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r8, 0x50(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x48(%rsp)
leaq -0x9(%rdx), %rax
cmpq $-0x8, %rax
jae 0xe7e1c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
andq $0x0, 0x18(%rsp)
movq 0x90(%rsp), %rax
movq 0x8(%rax), %rax
addq $0x7, %rax
pushq $0x8
popq %rcx
cqto
idivq %rcx
xorl %ebx, %ebx
testq %rax, %rax
cmovleq %rbx, %rax
movq %rax, 0x38(%rsp)
leaq -0x1(%r12), %rax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %r14
movq %r9, 0x30(%rsp)
movq 0x90(%rsp), %rax
cmpq 0x10(%rax), %rbx
jge 0xe7e0d
movq 0x8(%rsp), %r15
xorl %eax, %eax
cmpq 0x38(%rsp), %rax
je 0xe7f50
movq %rax, 0x10(%rsp)
movq %r15, 0x40(%rsp)
movq 0x28(%rsp), %rbp
xorl %r13d, %r13d
cmpq $-0x1, %rbp
je 0xe7ec4
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0xe3eb4
movl 0x98(%rsp), %ecx
testb $0x2, %cl
movl %ebp, %ecx
cmovneq %r13, %rcx
movb %al, 0x18(%rsp,%rcx)
incq %r13
decq %rbp
addq 0x50(%rsp), %r15
jmp 0xe7e90
movq 0x10(%rsp), %rax
leaq (,%rax,8), %rbp
xorl %r15d, %r15d
movq 0x30(%rsp), %r13
cmpq $0x8, %r15
je 0xe7f3b
movl %r14d, %edx
movl %r15d, %ecx
shll %cl, %edx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq %rcx, %r12
je 0xe7f08
movzbl 0x18(%rsp,%rcx), %esi
andl %edx, %esi
movl %r14d, %edi
shll %cl, %edi
testl %esi, %esi
cmovel %esi, %edi
orl %edi, %eax
incq %rcx
jmp 0xe7eeb
pushq $0x7
popq %rsi
subq %r15, %rsi
movl 0x98(%rsp), %ecx
testb $0x1, %cl
cmovneq %r15, %rsi
orq %rbp, %rsi
movl %eax, %eax
movl (%r13,%rax,4), %ecx
movq 0x90(%rsp), %rdi
movq %rbx, %rdx
callq 0xe728c
incq %r15
jmp 0xe7ed9
movq 0x10(%rsp), %rax
incq %rax
movq 0x40(%rsp), %r15
incq %r15
jmp 0xe7e73
incq %rbx
movq 0x8(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0xe7e5e