// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/reko.c
// Function: convert_image_planar_HAM
static void convert_image_planar_HAM(dbuf *f, i64 fpos, i64 nplanes,
	i64 row_stride, i64 plane_stride, const de_color *pal, de_bitmap *img)
{
	i64 ypos;
	u8 pbit[8];
	i64 units_per_row; // num bytes per row per plane that we will process
	UI pixshift1;

	de_zeromem(pbit, sizeof(pbit));
	if(nplanes==6) {
		pixshift1 = 4;
	}
	else if(nplanes==8) {
		pixshift1 = 2;
	}
	else goto done;

	units_per_row = (img->width + 7)/8;

	for(ypos=0; ypos<img->height; ypos++) {
		i64 n;
		u8 cr, cg, cb;

		cr = DE_COLOR_R(pal[0]) >> pixshift1;
		cg = DE_COLOR_G(pal[0]) >> pixshift1;
		cb = DE_COLOR_B(pal[0]) >> pixshift1;

		// Read 8 bits from each plane, then rearrange 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);
				pbit[pn] = b;
			}

			for(k=0; k<8; k++) {
				de_color clr;
				u8 pixval;
				u8 pixval_code;
				u8 pixval_color;
				u8 cr2, cg2, cb2;

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

				if(nplanes==6) {
					pixval_code = pixval >> 4;
					pixval_color = pixval & 0x0f;
				}
				else {
					pixval_code = pixval >> 6;
					pixval_color = pixval & 0x3f;
				}

				switch(pixval_code) {
				case 0x1: // Modify blue value
					cb = pixval_color;
					break;
				case 0x2: // Modify red value
					cr = pixval_color;
					break;
				case 0x3: // Modify green value
					cg = pixval_color;
					break;
				default: // 0: Use colormap value
					clr = pal[(UI)pixval_color];
					cr = DE_COLOR_R(clr) >> pixshift1;
					cg = DE_COLOR_G(clr) >> pixshift1;
					cb = DE_COLOR_B(clr) >> pixshift1;
					break;
				}

				if(nplanes==6) {
					cr2 = (cr<<4) | cr;
					cg2 = (cg<<4) | cg;
					cb2 = (cb<<4) | cb;
				}
				else {
					cr2 = (cr<<2) | (cr>>4);
					cg2 = (cg<<2) | (cg>>4);
					cb2 = (cb<<2) | (cb>>4);
				}
				xpos = n*8 + (i64)k;
				de_bitmap_setpixel_rgba(img, xpos, ypos, DE_MAKE_RGB(cr2, cg2, cb2));
			}
		}
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
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)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x19140
cmpq $0x6, -0x18(%rbp)
jne 0x156597
movl $0x4, -0x4c(%rbp)
jmp 0x1565ae
cmpq $0x8, -0x18(%rbp)
jne 0x1565a7
movl $0x2, -0x4c(%rbp)
jmp 0x1565ac
jmp 0x15689c
jmp 0x1565ae
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x15689a
movq -0x30(%rbp), %rax
movl (%rax), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x59(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x5a(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x5b(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0x156887
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jae 0x156692
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rax
imulq -0x20(%rbp), %rax
addq %rax, %rsi
movl -0x64(%rbp), %eax
imulq -0x28(%rbp), %rax
addq %rax, %rsi
addq -0x58(%rbp), %rsi
callq 0x165060
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %cl
movl -0x64(%rbp), %eax
movb %cl, -0x40(%rbp,%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x156647
movl $0x0, -0x60(%rbp)
cmpl $0x8, -0x60(%rbp)
jae 0x156874
movb $0x0, -0x75(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl %ecx, %eax
jae 0x1566fb
movl -0x64(%rbp), %eax
movzbl -0x40(%rbp,%rax), %eax
movl $0x7, %ecx
subl -0x60(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x1566ee
movl -0x64(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movzbl -0x75(%rbp), %eax
orl %ecx, %eax
movb %al, -0x75(%rbp)
jmp 0x1566f0
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1566ae
cmpq $0x6, -0x18(%rbp)
jne 0x156718
movzbl -0x75(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x76(%rbp)
movzbl -0x75(%rbp), %eax
andl $0xf, %eax
movb %al, -0x77(%rbp)
jmp 0x15672c
movzbl -0x75(%rbp), %eax
sarl $0x6, %eax
movb %al, -0x76(%rbp)
movzbl -0x75(%rbp), %eax
andl $0x3f, %eax
movb %al, -0x77(%rbp)
movzbl -0x76(%rbp), %eax
movl %eax, -0x80(%rbp)
subl $0x1, %eax
je 0x15674e
jmp 0x15673a
movl -0x80(%rbp), %eax
subl $0x2, %eax
je 0x156756
jmp 0x156744
movl -0x80(%rbp), %eax
subl $0x3, %eax
je 0x15675e
jmp 0x156766
movb -0x77(%rbp), %al
movb %al, -0x5b(%rbp)
jmp 0x1567b5
movb -0x77(%rbp), %al
movb %al, -0x59(%rbp)
jmp 0x1567b5
movb -0x77(%rbp), %al
movb %al, -0x5a(%rbp)
jmp 0x1567b5
movq -0x30(%rbp), %rax
movzbl -0x77(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x59(%rbp)
movl -0x74(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x5a(%rbp)
movl -0x74(%rbp), %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x4c(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x5b(%rbp)
cmpq $0x6, -0x18(%rbp)
jne 0x1567ee
movzbl -0x59(%rbp), %eax
shll $0x4, %eax
movzbl -0x59(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x78(%rbp)
movzbl -0x5a(%rbp), %eax
shll $0x4, %eax
movzbl -0x5a(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x79(%rbp)
movzbl -0x5b(%rbp), %eax
shll $0x4, %eax
movzbl -0x5b(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x7a(%rbp)
jmp 0x156827
movzbl -0x59(%rbp), %eax
shll $0x2, %eax
movzbl -0x59(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
movb %al, -0x78(%rbp)
movzbl -0x5a(%rbp), %eax
shll $0x2, %eax
movzbl -0x5a(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
movb %al, -0x79(%rbp)
movzbl -0x5b(%rbp), %eax
shll $0x2, %eax
movzbl -0x5b(%rbp), %ecx
sarl $0x4, %ecx
orl %ecx, %eax
movb %al, -0x7a(%rbp)
movq -0x58(%rbp), %rax
shlq $0x3, %rax
movl -0x60(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq 0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x38(%rbp), %rdx
movzbl -0x78(%rbp), %ecx
shll $0x10, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movzbl -0x79(%rbp), %eax
shll $0x8, %eax
orl %eax, %ecx
movzbl -0x7a(%rbp), %eax
orl %eax, %ecx
callq 0x16b350
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x156699
jmp 0x156876
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x156632
jmp 0x156889
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1565d0
jmp 0x15689c
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)