// File: /workspace/github_c_makefile_star2_O1/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 %rcx, 0x38(%rsp)
movq %rsi, 0x8(%rsp)
leaq -0x9(%rdx), %rax
cmpq $-0x8, %rax
jb 0xf38ca
movq 0x90(%rsp), %rax
movq $0x0, 0x28(%rsp)
cmpq $0x0, 0x10(%rax)
jle 0xf38ca
movq %r8, %r14
movq %rdx, %r12
movq 0x8(%rax), %rcx
leaq 0x7(%rcx), %rax
movq %rcx, 0x30(%rsp)
addq $0xe, %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
movl 0x98(%rsp), %r8d
sarq $0x3, %rcx
cmpq $0x2, %rcx
movl $0x1, %eax
cmovgeq %rcx, %rax
movq %rax, 0x48(%rsp)
leaq -0x1(%rdx), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
movq %rdi, %r13
movq %r9, 0x50(%rsp)
cmpq $0x0, 0x30(%rsp)
jle 0xf389c
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
testq %r12, %r12
movq %r14, %rbx
je 0xf37f2
movq 0x20(%rsp), %r15
movq 0x40(%rsp), %rbp
xorl %r14d, %r14d
movq %r15, %rsi
callq 0xef0d5
movl 0x98(%rsp), %r8d
movq %r13, %rdi
testb $0x2, %r8b
movl %ebp, %ecx
cmovneq %r14, %rcx
movb %al, 0x28(%rsp,%rcx)
incq %r14
addq %rbx, %r15
addq $-0x1, %rbp
jb 0xf37c5
movq 0x10(%rsp), %rax
leaq (,%rax,8), %rbp
xorl %r15d, %r15d
movq %rbx, %r14
movq 0x50(%rsp), %rbx
testq %r12, %r12
je 0xf383e
movl $0x1, %eax
movl %r15d, %ecx
shll %cl, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movzbl 0x28(%rsp,%rcx), %esi
movl $0x1, %edi
shll %cl, %edi
andl %eax, %esi
testl %esi, %esi
cmovel %esi, %edi
orl %edi, %edx
incq %rcx
cmpq %rcx, %r12
jne 0xf381d
movl %edx, %eax
jmp 0xf3840
xorl %eax, %eax
leaq (%r15,%rbp), %rcx
movq %rbp, %rsi
subq %r15, %rsi
addq $0x7, %rsi
testb $0x1, %r8b
cmovneq %rcx, %rsi
movl (%rbx,%rax,4), %ecx
movq 0x90(%rsp), %rdi
movq 0x18(%rsp), %rdx
callq 0xf2b02
movl 0x98(%rsp), %r8d
incq %r15
cmpq $0x8, %r15
jne 0xf380a
movq %r13, %rdi
movq 0x10(%rsp), %rcx
incq %rcx
incq 0x20(%rsp)
movq %rcx, 0x10(%rsp)
cmpq 0x48(%rsp), %rcx
jne 0xf37b0
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rax
addq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq %rdx, 0x18(%rsp)
cmpq 0x10(%rax), %rdx
jl 0xf3791
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq