// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %r9, 0x48(%rsp)
movq %r8, %r12
movq %rcx, 0x40(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movl 0xf0(%rsp), %r14d
leaq 0x513a1(%rip), %rsi     # 0x14a036
movq %rdi, 0x38(%rsp)
callq 0xea6cb
testq %rax, %rax
je 0xf8cae
movq %rax, %rdi
callq 0xea781
jmp 0xf8cb0
xorl %eax, %eax
movl %r14d, %ecx
andl $0x2, %ecx
shll $0x1e, %r14d
sarl $0x1f, %r14d
andl $0xf, %r14d
testl %eax, %eax
cmovnel %eax, %r14d
movq %r14, 0x10(%rsp)
orl %eax, %ecx
movq %r13, 0x28(%rsp)
je 0xf8f5c
testq %r12, %r12
jle 0xf8f4a
movq 0x10(%rsp), %rax
addl $-0xc, %eax
movl %eax, 0x34(%rsp)
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq %r13, %rdi
movq %rbx, 0x20(%rsp)
movq %rbx, %rsi
callq 0xef169
movq %rax, %rbx
cmpl $0xf, 0x10(%rsp)
jl 0xf8da7
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 0xece35
movl %eax, 0xc(%rsp)
movzbl %bpl, %esi
movl $0x1f, %edi
callq 0xece35
movl %eax, 0x8(%rsp)
movzbl %r13b, %esi
movl $0x1f, %edi
callq 0xece35
movl %eax, %r15d
jmp 0xf8e12
cmpl $0x2, 0x34(%rsp)
ja 0xf8e1b
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 0x5124a(%rip), %rdx     # 0x14a063
jmp 0xf8e9a
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 0x50f29(%rip), %xmm1    # 0x149d70
mulsd %xmm1, %xmm0
movsd 0x1e87d(%rip), %xmm2    # 0x1176d0
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 0x3c0c5(%rip), %rdx     # 0x134f5f
movzbl %r12b, %ecx
movzbl %bpl, %r8d
movzbl %r13b, %r9d
movl $0x20, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0xe8d67
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 0x40(%rsp), %r15
movl %eax, (%r15,%r14,4)
movl $0x40, %esi
leaq 0x70(%rsp), %r13
movq %r13, %rdi
leaq 0x5117b(%rip), %rdx     # 0x14a06f
movl %ebx, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0xe8d67
movl (%r15,%r14,4), %edx
cmpq 0x48(%rsp), %r14
leaq 0x1808c(%rip), %r9      # 0x110f9c
leaq 0x4bcb9(%rip), %rax     # 0x144bd0
cmovlq %rax, %r9
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xe9c1e
incq %r14
movq 0x20(%rsp), %rbx
addq $0x2, %rbx
cmpq %r14, 0x18(%rsp)
movq 0x28(%rsp), %r13
jne 0xf8cf7
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r12, %r12
jle 0xf8fea
xorl %ebp, %ebp
movl $0x1, %r14d
movq %r12, 0x18(%rsp)
movq %r12, %r15
movq %rbx, 0x20(%rsp)
xorl %r12d, %r12d
movq %r13, %rdi
movq %rbx, %rsi
callq 0xef169
testl $0xf000, %eax           # imm = 0xF000
cmovnel %r14d, %ebp
testl $0x888, %eax            # imm = 0x888
cmovnel %r14d, %r12d
addq $0x2, %rbx
decq %r15
jne 0xf8f7d
movq $0x0, 0x10(%rsp)
testl %r12d, %r12d
je 0xf8ff8
testl %ebp, %ebp
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r13
movq 0x18(%rsp), %r12
jne 0xf8cda
leaq 0x51075(%rip), %rsi     # 0x14a044
movq 0x38(%rsp), %rdi
xorl %eax, %eax
callq 0xe9226
movl $0xc, %eax
movq %rax, 0x10(%rsp)
jmp 0xf8cda
movq $0x0, 0x10(%rsp)
jmp 0xf8cda
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r13
movq 0x18(%rsp), %r12
jmp 0xf8cda