// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_read_atari_palette
void fmtutil_read_atari_palette(deark *c, dbuf *f, i64 pos,
	de_color *dstpal, i64 ncolors_to_read, i64 ncolors_used, unsigned int flags)
{
	i64 i;
	unsigned int n;
	int pal_bits = 0; // 9, 12, or 15. 0 = not yet determined
	u8 cr, cg, cb;
	u8 cr1, cg1, cb1;
	char cbuf[32];
	char tmps[64];
	const char *s;

	s = de_get_ext_option(c, "atari:palbits");
	if(s) {
		pal_bits = de_atoi(s);
	}

	if(pal_bits==0 && (flags&DE_FLAG_ATARI_15BIT_PAL)) {
		pal_bits = 15;
	}

	if(pal_bits==0) {
		// Pre-scan the palette, and try to guess whether Atari STE-style 12-bit
		// colors are used, instead of the usual 9-bit colors.
		// I don't know the best way to do this. Sometimes the 4th bit in each
		// nibble is used for extra color detail, and sometimes it just seems to
		// contain garbage. Maybe the logic should also depend on the file
		// format, or the number of colors.
		int bit_3_used = 0;
		int nibble_3_used = 0;

		for(i=0; i<ncolors_to_read; i++) {
			n = (unsigned int)dbuf_getu16be(f, pos + i*2);
			if(n&0xf000) {
				nibble_3_used = 1;
			}
			if(n&0x0888) {
				bit_3_used = 1;
			}
		}

		if(bit_3_used && !nibble_3_used) {
			de_dbg(c, "12-bit palette colors detected");
			pal_bits = 12;
		}
	}

	if(pal_bits<12) { // Default to 9 if <12
		pal_bits = 9;
	}
	else if(pal_bits<15) {
		pal_bits = 12;
	}
	else {
		pal_bits = 15;
	}

	for(i=0; i<ncolors_to_read; i++) {
		n = (unsigned int)dbuf_getu16be(f, pos + 2*i);

		if(pal_bits==15) {
			cr1 = (u8)((n>>6)&0x1c);
			if(n&0x0800) cr1+=2;
			if(n&0x8000) cr1++;
			cg1 = (u8)((n>>2)&0x1c);
			if(n&0x0080) cg1+=2;
			if(n&0x4000) cg1++;
			cb1 = (u8)((n<<2)&0x1c);
			if(n&0x0008) cb1+=2;
			if(n&0x2000) cb1++;
			cr = de_scale_n_to_255(31, cr1);
			cg = de_scale_n_to_255(31, cg1);
			cb = de_scale_n_to_255(31, cb1);
			de_snprintf(cbuf, sizeof(cbuf), "%2d,%2d,%2d",
				(int)cr1, (int)cg1, (int)cb1);
		}
		else if(pal_bits==12) {
			cr1 = (u8)((n>>7)&0x0e);
			if(n&0x800) cr1++;
			cg1 = (u8)((n>>3)&0x0e);
			if(n&0x080) cg1++;
			cb1 = (u8)((n<<1)&0x0e);
			if(n&0x008) cb1++;
			cr = scale_15_to_255(cr1);
			cg = scale_15_to_255(cg1);
			cb = scale_15_to_255(cb1);
			de_snprintf(cbuf, sizeof(cbuf), "%2d,%2d,%2d",
				(int)cr1, (int)cg1, (int)cb1);
		}
		else {
			cr1 = (u8)((n>>8)&0x07);
			cg1 = (u8)((n>>4)&0x07);
			cb1 = (u8)(n&0x07);
			cr = scale_7_to_255(cr1);
			cg = scale_7_to_255(cg1);
			cb = scale_7_to_255(cb1);
			de_snprintf(cbuf, sizeof(cbuf), "%d,%d,%d",
				(int)cr1, (int)cg1, (int)cb1);
		}

		dstpal[i] = DE_MAKE_RGB(cr, cg, cb);
		de_snprintf(tmps, sizeof(tmps), "0x%04x (%s) "DE_CHAR_RIGHTARROW" ", n, cbuf);
		de_dbg_pal_entry2(c, i, dstpal[i], tmps, NULL,
			(i>=ncolors_used)?" [unused]":"");
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r13
movq %rcx, 0x28(%rsp)
movq %rdx, %rbp
movq %rsi, 0x20(%rsp)
movl 0xe0(%rsp), %ebx
leaq 0x5023d(%rip), %rsi     # 0x13d116
movq %rdi, 0x18(%rsp)
callq 0xdfa87
testq %rax, %rax
je 0xecef2
movq %rax, %rdi
callq 0xdfb36
jmp 0xecef4
xorl %eax, %eax
andl $0x2, %ebx
movl %eax, %ecx
orl %ebx, %ecx
je 0xecf27
testl %ebx, %ebx
pushq $0xf
popq %rcx
cmovel %ebx, %ecx
testl %eax, %eax
cmovnel %eax, %ecx
cmpl $0xc, %ecx
jge 0xecfa5
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
jmp 0xecfb5
xorl %r14d, %r14d
testq %r13, %r13
movl $0x0, %r15d
cmovgq %r13, %r15
pushq $0x1
popq %r12
movq %rbp, 0x10(%rsp)
movq %rbp, %rbx
xorl %ebp, %ebp
subq $0x1, %r15
jb 0xecf6e
movq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xe3f46
testw $0xf000, %ax            # imm = 0xF000
cmovnel %r12d, %ebp
testw $0x888, %ax             # imm = 0x888
cmovnel %r12d, %r14d
addq $0x2, %rbx
jmp 0xecf45
movl $0x0, 0x4(%rsp)
testl %r14d, %r14d
je 0xed219
movl $0x0, (%rsp)
testl %ebp, %ebp
movq 0x10(%rsp), %rbp
jne 0xecfb5
leaq 0x5018e(%rip), %rsi     # 0x13d124
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0xde896
pushq $0xc
popq %rcx
cmpl $0xf, %ecx
setb %al
movl %eax, (%rsp)
setae %al
movl %eax, 0x4(%rsp)
xorl %eax, %eax
testq %r13, %r13
cmovleq %rax, %r13
movq %r13, 0x38(%rsp)
xorl %r14d, %r14d
cmpq %r14, 0x38(%rsp)
je 0xed207
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0xe3f46
movq %rax, %r13
cmpb $0x0, 0x4(%rsp)
movq %rbp, 0x10(%rsp)
je 0xed083
movl %r13d, %eax
shrl $0x6, %eax
andb $0x1c, %al
movl %r13d, %ecx
shrl $0xa, %ecx
andb $0x2, %cl
orb %al, %cl
movl %r13d, %r12d
shrl $0xf, %r12d
andb $0x1, %r12b
orb %cl, %r12b
movl %r13d, %eax
shrb $0x2, %al
movl %eax, %ecx
andb $0x1c, %cl
movl %r13d, %edx
shrb $0x6, %dl
andb $0x2, %dl
orb %cl, %dl
movl %r13d, %ebp
shrl $0xe, %ebp
andb $0x1, %bpl
orb %dl, %bpl
leal (,%r13,4), %ecx
andb $0x1c, %cl
andb $0x2, %al
orb %cl, %al
movl %r13d, %ebx
shrl $0xd, %ebx
andb $0x1, %bl
orb %al, %bl
movzbl %r12b, %esi
pushq $0x1f
popq %r15
movq %r15, %rdi
callq 0xe1d3e
movl %eax, 0xc(%rsp)
movzbl %bpl, %esi
movq %r15, %rdi
callq 0xe1d3e
movl %eax, 0x8(%rsp)
movzbl %bl, %esi
movq %r15, %rdi
callq 0xe1d3e
movl %eax, %r15d
jmp 0xed0e1
cmpb $0x0, (%rsp)
je 0xed0ea
movl %r13d, %eax
shrl $0x7, %eax
andb $0xe, %al
movl %r13d, %r12d
shrl $0xb, %r12d
andb $0x1, %r12b
orb %al, %r12b
movzbl %r12b, %eax
movl %r13d, %ebx
shrb $0x3, %bl
movl %ebx, %ecx
andb $0xe, %cl
movl %r13d, %ebp
shrb $0x7, %bpl
orb %cl, %bpl
movzbl %bpl, %ecx
leal (,%r13,2), %edx
andb $0xe, %dl
andb $0x1, %bl
orb %dl, %bl
movzbl %bl, %edx
imull $0x11, %eax, %eax
movl %eax, 0xc(%rsp)
imull $0x11, %ecx, %eax
movl %eax, 0x8(%rsp)
imull $0x11, %edx, %r15d
leaq 0x5005b(%rip), %rdx     # 0x13d143
jmp 0xed165
movl %r13d, %eax
shrl $0x8, %eax
movl %eax, %r12d
andb $0x7, %r12b
movl %r13d, %ebp
shrb $0x4, %bpl
andb $0x7, %bpl
movl %r13d, %ebx
andb $0x7, %bl
andl $0x7, %eax
cvtsi2sd %eax, %xmm0
movsd 0x4fd29(%rip), %xmm1    # 0x13ce40
mulsd %xmm1, %xmm0
movsd 0x1d61d(%rip), %xmm2    # 0x10a740
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movzbl %bpl, %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x8(%rsp)
movl %r13d, %eax
andl $0x7, %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %r15d
leaq 0x3aea6(%rip), %rdx     # 0x12800b
movzbl %r12b, %ecx
movzbl %bpl, %r8d
movzbl %bl, %r9d
leaq 0x40(%rsp), %r12
movq %r12, %rdi
pushq $0x20
popq %rsi
xorl %eax, %eax
callq 0xde40c
movzbl 0xc(%rsp), %eax
shll $0x10, %eax
movzbl 0x8(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl %r15b, %eax
orl %ecx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movq 0x28(%rsp), %rbx
movl %eax, (%rbx,%r14,4)
leaq 0x60(%rsp), %r15
movq %r15, %rdi
pushq $0x40
popq %rsi
leaq 0x4ff94(%rip), %rdx     # 0x13d14f
movl %r13d, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0xde40c
movl (%rbx,%r14,4), %edx
cmpq 0x30(%rsp), %r14
leaq 0x16db0(%rip), %r9      # 0x103f88
leaq 0x4aa81(%rip), %rax     # 0x137c60
cmovlq %rax, %r9
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xdf153
incq %r14
movq 0x10(%rsp), %rbp
addq $0x2, %rbp
jmp 0xecfc6
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%rsp)
movq 0x10(%rsp), %rbp
jmp 0xecfb5