// File: /workspace/github_c_makefile_star2_O3/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, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movl 0xe0(%rsp), %ebx
leaq 0x51e44(%rip), %rsi     # 0x14a5e6
movq %rdi, 0x20(%rsp)
callq 0xe9b74
testq %rax, %rax
movq %r15, 0x18(%rsp)
je 0xf87f3
movq %rax, %rdi
callq 0xe9c2a
andl $0x2, %ebx
movl %eax, %ecx
orl %ebx, %ecx
je 0xf87fd
testl %ebx, %ebx
movl $0xf, %ecx
cmovel %ebx, %ecx
testl %eax, %eax
cmovnel %eax, %ecx
cmpl $0xc, %ecx
jge 0xf887c
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
jmp 0xf888c
movl $0xf, %ecx
testb $0x2, %bl
jne 0xf887c
cmpq $0x0, 0x10(%rsp)
jle 0xf8aed
xorl %ebp, %ebp
movq %r15, %r14
movq 0x10(%rsp), %r15
movq %r13, %rbx
xorl %r12d, %r12d
movq %r14, %rdi
movq %rbx, %rsi
callq 0xeeb67
testl $0xf000, %eax           # imm = 0xF000
movl $0x1, %ecx
cmovnel %ecx, %ebp
testl $0x888, %eax            # imm = 0x888
cmovnel %ecx, %r12d
addq $0x2, %rbx
decq %r15
jne 0xf8819
movl $0x0, 0x4(%rsp)
testl %r12d, %r12d
je 0xf8aff
movl $0x0, (%rsp)
testl %ebp, %ebp
movq 0x18(%rsp), %r15
jne 0xf888c
leaq 0x51d89(%rip), %rsi     # 0x14a5f4
movq 0x20(%rsp), %rdi
xorl %eax, %eax
callq 0xe8617
movl $0xc, %ecx
cmpl $0xf, %ecx
setb %al
movl %eax, (%rsp)
setae %al
movl %eax, 0x4(%rsp)
cmpq $0x0, 0x10(%rsp)
jle 0xf8aed
xorl %r14d, %r14d
movq %r15, %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rax, %rbx
cmpb $0x0, 0x4(%rsp)
movq %r13, 0x38(%rsp)
je 0xf894b
movl %ebx, %eax
shrl $0x6, %eax
andb $0x1c, %al
movl %ebx, %ecx
shrl $0xa, %ecx
andb $0x2, %cl
orb %al, %cl
movl %ebx, %r12d
shrl $0xf, %r12d
andb $0x1, %r12b
orb %cl, %r12b
movl %ebx, %eax
shrb $0x2, %al
movl %eax, %ecx
andb $0x1c, %cl
movl %ebx, %edx
shrb $0x6, %dl
andb $0x2, %dl
orb %cl, %dl
movl %ebx, %ebp
shrl $0xe, %ebp
andb $0x1, %bpl
orb %dl, %bpl
leal (,%rbx,4), %ecx
andb $0x1c, %cl
andb $0x2, %al
orb %cl, %al
movl %ebx, %r13d
shrl $0xd, %r13d
andb $0x1, %r13b
orb %al, %r13b
movzbl %r12b, %esi
movl $0x1f, %edi
callq 0xec7dc
movl %eax, 0xc(%rsp)
movzbl %bpl, %esi
movl $0x1f, %edi
callq 0xec7dc
movl %eax, 0x8(%rsp)
movzbl %r13b, %esi
movl $0x1f, %edi
callq 0xec7dc
movl %eax, %r15d
jmp 0xf89b5
cmpb $0x0, (%rsp)
je 0xf89be
movl %ebx, %eax
shrl $0x7, %eax
andb $0xe, %al
movl %ebx, %r12d
shrl $0xb, %r12d
andb $0x1, %r12b
orb %al, %r12b
movzbl %r12b, %eax
movl %ebx, %r13d
shrb $0x3, %r13b
movl %r13d, %ecx
andb $0xe, %cl
movl %ebx, %ebp
shrb $0x7, %bpl
orb %cl, %bpl
movzbl %bpl, %ecx
leal (%rbx,%rbx), %edx
andb $0xe, %dl
andb $0x1, %r13b
orb %dl, %r13b
movzbl %r13b, %edx
movl %eax, %esi
shll $0x4, %esi
addl %eax, %esi
movl %esi, 0xc(%rsp)
movl %ecx, %eax
shll $0x4, %eax
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movl %edx, %r15d
shll $0x4, %r15d
addl %edx, %r15d
leaq 0x51c57(%rip), %rdx     # 0x14a613
jmp 0xf8a3d
movl %ebx, %eax
shrl $0x8, %eax
movl %eax, %r12d
andb $0x7, %r12b
movl %ebx, %ebp
shrb $0x4, %bpl
andb $0x7, %bpl
movl %ebx, %r13d
andb $0x7, %r13b
andl $0x7, %eax
cvtsi2sd %eax, %xmm0
movsd 0x51926(%rip), %xmm1    # 0x14a310
mulsd %xmm1, %xmm0
movsd 0x1ee3a(%rip), %xmm2    # 0x117830
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc(%rsp)
movzbl %bpl, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x8(%rsp)
movl %ebx, %eax
andl $0x7, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %r15d
leaq 0x3c8ae(%rip), %rdx     # 0x1352eb
movzbl %r12b, %ecx
movzbl %bpl, %r8d
movzbl %r13b, %r9d
movl $0x20, %esi
leaq 0x40(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8193
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), %r15
movl %eax, (%r15,%r14,4)
movl $0x40, %esi
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x51b88(%rip), %rdx     # 0x14a61f
movl %ebx, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0xe8193
movl (%r15,%r14,4), %edx
cmpq 0x30(%rsp), %r14
leaq 0x184f8(%rip), %r9      # 0x110fab
leaq 0x4c5f6(%rip), %rax     # 0x1450b0
cmovlq %rax, %r9
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xe90a3
incq %r14
movq 0x38(%rsp), %r13
addq $0x2, %r13
cmpq %r14, 0x10(%rsp)
movq 0x18(%rsp), %r15
jne 0xf889b
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%rsp)
movq 0x18(%rsp), %r15
jmp 0xf888c