// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x61bfe(%rip), %rsi     # 0x1d6096
callq 0x15cb30
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x1744bd
movq -0xb8(%rbp), %rdi
callq 0x15cca0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x1744d5
movl 0x10(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1744d5
movl $0xf, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x17458a
movl $0x0, -0xbc(%rbp)
movl $0x0, -0xc0(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x17455d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x1651d0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x0, %eax
je 0x174536
movl $0x1, -0xc0(%rbp)
movl -0x3c(%rbp), %eax
andl $0x888, %eax            # imm = 0x888
cmpl $0x0, %eax
je 0x17454d
movl $0x1, -0xbc(%rbp)
jmp 0x17454f
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1744fb
cmpl $0x0, -0xbc(%rbp)
je 0x174588
cmpl $0x0, -0xc0(%rbp)
jne 0x174588
movq -0x8(%rbp), %rdi
leaq 0x61b2a(%rip), %rsi     # 0x1d60a4
movb $0x0, %al
callq 0x15b0d0
movl $0xc, -0x40(%rbp)
jmp 0x17458a
cmpl $0xc, -0x40(%rbp)
jge 0x174599
movl $0x9, -0x40(%rbp)
jmp 0x1745b1
cmpl $0xf, -0x40(%rbp)
jge 0x1745a8
movl $0xc, -0x40(%rbp)
jmp 0x1745af
movl $0xf, -0x40(%rbp)
jmp 0x1745b1
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x1748af
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x1651d0
movl %eax, -0x3c(%rbp)
cmpl $0xf, -0x40(%rbp)
jne 0x1746f4
movl -0x3c(%rbp), %eax
shrl $0x6, %eax
andl $0x1c, %eax
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x17460e
movzbl -0x44(%rbp), %eax
addl $0x2, %eax
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x174623
movb -0x44(%rbp), %al
addb $0x1, %al
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x2, %eax
andl $0x1c, %eax
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x174646
movzbl -0x45(%rbp), %eax
addl $0x2, %eax
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x17465b
movb -0x45(%rbp), %al
addb $0x1, %al
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
shll $0x2, %eax
andl $0x1c, %eax
movb %al, -0x46(%rbp)
movl -0x3c(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x17467c
movzbl -0x46(%rbp), %eax
addl $0x2, %eax
movb %al, -0x46(%rbp)
movl -0x3c(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x174691
movb -0x46(%rbp), %al
addb $0x1, %al
movb %al, -0x46(%rbp)
movzbl -0x44(%rbp), %eax
movl %eax, %esi
movl $0x1f, %edi
callq 0x1617b0
movb %al, -0x41(%rbp)
movzbl -0x45(%rbp), %eax
movl %eax, %esi
movl $0x1f, %edi
callq 0x1617b0
movb %al, -0x42(%rbp)
movzbl -0x46(%rbp), %eax
movl %eax, %esi
movl $0x1f, %edi
callq 0x1617b0
movb %al, -0x43(%rbp)
leaq -0x70(%rbp), %rdi
movzbl -0x44(%rbp), %ecx
movzbl -0x45(%rbp), %r8d
movzbl -0x46(%rbp), %r9d
movl $0x20, %esi
leaq 0x619db(%rip), %rdx     # 0x1d60c3
movb $0x0, %al
callq 0x15a980
jmp 0x174815
cmpl $0xc, -0x40(%rbp)
jne 0x1747a9
movl -0x3c(%rbp), %eax
shrl $0x7, %eax
andl $0xe, %eax
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x17471f
movb -0x44(%rbp), %al
addb $0x1, %al
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x3, %eax
andl $0xe, %eax
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x174740
movb -0x45(%rbp), %al
addb $0x1, %al
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
shll %eax
andl $0xe, %eax
movb %al, -0x46(%rbp)
movl -0x3c(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x17475e
movb -0x46(%rbp), %al
addb $0x1, %al
movb %al, -0x46(%rbp)
movzbl -0x44(%rbp), %edi
callq 0x1748c0
movb %al, -0x41(%rbp)
movzbl -0x45(%rbp), %edi
callq 0x1748c0
movb %al, -0x42(%rbp)
movzbl -0x46(%rbp), %edi
callq 0x1748c0
movb %al, -0x43(%rbp)
leaq -0x70(%rbp), %rdi
movzbl -0x44(%rbp), %ecx
movzbl -0x45(%rbp), %r8d
movzbl -0x46(%rbp), %r9d
movl $0x20, %esi
leaq 0x61923(%rip), %rdx     # 0x1d60c3
movb $0x0, %al
callq 0x15a980
jmp 0x174813
movl -0x3c(%rbp), %eax
shrl $0x8, %eax
andl $0x7, %eax
movb %al, -0x44(%rbp)
movl -0x3c(%rbp), %eax
shrl $0x4, %eax
andl $0x7, %eax
movb %al, -0x45(%rbp)
movl -0x3c(%rbp), %eax
andl $0x7, %eax
movb %al, -0x46(%rbp)
movzbl -0x44(%rbp), %edi
callq 0x1748e0
movb %al, -0x41(%rbp)
movzbl -0x45(%rbp), %edi
callq 0x1748e0
movb %al, -0x42(%rbp)
movzbl -0x46(%rbp), %edi
callq 0x1748e0
movb %al, -0x43(%rbp)
leaq -0x70(%rbp), %rdi
movzbl -0x44(%rbp), %ecx
movzbl -0x45(%rbp), %r8d
movzbl -0x46(%rbp), %r9d
movl $0x20, %esi
leaq 0x4c52f(%rip), %rdx     # 0x1c0d3b
movb $0x0, %al
callq 0x15a980
jmp 0x174815
movzbl -0x41(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x42(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x43(%rbp), %eax
orl %eax, %edx
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
leaq -0xb0(%rbp), %rdi
movl -0x3c(%rbp), %ecx
leaq -0x70(%rbp), %r8
movl $0x40, %esi
leaq 0x61879(%rip), %rdx     # 0x1d60cf
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq -0xb0(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x30(%rbp), %r10
leaq 0x5c34a(%rip), %r9      # 0x1d0bd0
leaq 0x286f7(%rip), %rax     # 0x19cf84
cmpq %r10, %r8
cmovgeq %rax, %r9
xorl %eax, %eax
movl %eax, %r8d
callq 0x15bdb0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1745b9
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)