// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/insetpix.c
// Function: do_palette
static void do_palette(deark *c, lctx *d)
{
	i64 pos1;
	i64 pos;
	i64 pal_entries_in_file;
	i64 i;
	size_t k;
	u8 sm1[4]; // Original I, R, G, B
	u8 sm2[4]; // Intermediate
	u8 sm3[4]; // Post-processed samples
	u8 uses_intens = 0; // Special handling if we have RGB and 'I' values.
	double max_color_sample;
	double pal_sample_scalefactor[4];
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	pos1 = d->pal_pos;
	if(pos1==0 || d->pal_len<4) goto done;
	pos = pos1;
	de_dbg(c, "palette at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	pal_entries_in_file = d->pal_len/4;
	de_dbg(c, "number of palette colors: %d", (int)pal_entries_in_file);

	d->pal_entries_used = pal_entries_in_file; // default
	if(d->graphics_type==1) {
		d->pal_entries_used = d->max_sample_value+1;
	}
	if(d->pal_entries_used > pal_entries_in_file) d->pal_entries_used = pal_entries_in_file;


	// If intensity bits are used, make the initial colors darker, so that the
	// intensity bits can lighten them.
	uses_intens = (d->pal_sample_descriptor[0]!=0) && !d->is_grayscale;
	max_color_sample = uses_intens ? 170.0 : 255.0;

	for(k=0; k<4; k++) {
		if(d->pal_sample_descriptor[k]>=2)
			pal_sample_scalefactor[k] = max_color_sample / (double)(d->pal_sample_descriptor[k]-1);
		else
			pal_sample_scalefactor[k] = 0.0;
	}

	for(i=0; i<pal_entries_in_file; i++) {
		char tmps[64];

		if(i>255) break;

		for(k=0; k<4; k++) {
			sm1[k] = de_getbyte_p(&pos);
			sm2[k] = sm1[k];
		}

		for(k=1; k<4; k++) {
			// Best I can figure is that, in the palette definition, when there
			// are exactly 4 sample intensities, intensity 1 is brighter than
			// intensity 2. I don't know why I have to swap them like this.
			if(d->pal_sample_descriptor[k]==4) {
				if(sm2[k]==1) sm2[k] = 2;
				else if(sm2[k]==2) sm2[k] = 1;
			}
		}

		if(d->is_grayscale) {
			sm3[0] = (u8)(0.5+ pal_sample_scalefactor[0] * (double)sm2[0]);
			d->pal[i] = DE_MAKE_GRAY(sm3[0]);
		}
		else {
			sm3[1] = (u8)(0.5+ pal_sample_scalefactor[1] * (double)sm2[1]);
			sm3[2] = (u8)(0.5+ pal_sample_scalefactor[2] * (double)sm2[2]);
			sm3[3] = (u8)(0.5+ pal_sample_scalefactor[3] * (double)sm2[3]);
			if(uses_intens && sm2[0]) {
				// This is just a guess. The spec doesn't say what intensity bits do.
				// This is pretty much what old PC graphics cards do when the
				// intensity bit is set.
				sm3[1] += 85;
				sm3[2] += 85;
				sm3[3] += 85;
			}
			d->pal[i] = DE_MAKE_RGB(sm3[1],sm3[2],sm3[3]);
		}

		if(uses_intens) {
			de_snprintf(tmps, sizeof(tmps), "(%3u,%3u,%3u,intens=%u) "DE_CHAR_RIGHTARROW" ",
				(UI)sm1[1], (UI)sm1[2], (UI)sm1[3], (UI)sm1[0]);
		}
		else {
			de_snprintf(tmps, sizeof(tmps), "(%3u,%3u,%3u) "DE_CHAR_RIGHTARROW" ",
				(UI)sm1[1], (UI)sm1[2], (UI)sm1[3]);
		}
		de_dbg_pal_entry2(c, i, d->pal[i], tmps, NULL,
			i<d->pal_entries_used ? "":" [unused]");
	}

	d->have_pal_data = 1;
done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x45(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x15b580
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xb80c2
movq -0x10(%rbp), %rax
cmpq $0x4, 0x28(%rax)
jge 0xb80c7
jmp 0xb8517
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x109e0f(%rip), %rsi    # 0x1c1eed
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl $0x4, %ecx
cqto
idivq %rcx
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0xe5282(%rip), %rsi     # 0x19d39c
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %eax
cmpl $0x1, %eax
jne 0xb8157
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rcx
addq $0x1, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %rax
cmpq -0x28(%rbp), %rax
jle 0xb8177
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xe8(%rax)
movq -0x10(%rbp), %rax
movzbl 0x90(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xc1(%rbp)
je 0xb81a5
movq -0x10(%rbp), %rax
cmpl $0x0, 0xd8(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %eax
movsd 0xfd059(%rip), %xmm0    # 0x1b5218
movsd 0xeb469(%rip), %xmm1    # 0x1a3630
movsd %xmm1, -0xd8(%rbp)
cmpl $0x0, %eax
movsd %xmm0, -0xd0(%rbp)
jne 0xb81ec
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jae 0xb8266
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl 0x90(%rax,%rcx), %eax
cmpl $0x2, %eax
jl 0xb8249
movsd -0x50(%rbp), %xmm0
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl 0x90(%rax,%rcx), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movq -0x38(%rbp), %rax
movsd %xmm0, -0x70(%rbp,%rax,8)
jmp 0xb8256
movq -0x38(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp,%rax,8)
jmp 0xb8258
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb8201
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0xb850f
cmpq $0xff, -0x30(%rbp)
jle 0xb828b
jmp 0xb850f
movq $0x0, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jae 0xb82d3
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x38(%rbp), %rax
movb %cl, -0x3c(%rbp,%rax)
movq -0x38(%rbp), %rax
movb -0x3c(%rbp,%rax), %cl
movq -0x38(%rbp), %rax
movb %cl, -0x40(%rbp,%rax)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb8293
movq $0x1, -0x38(%rbp)
cmpq $0x4, -0x38(%rbp)
jae 0xb833b
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movzbl 0x90(%rax,%rcx), %eax
cmpl $0x4, %eax
jne 0xb832b
movq -0x38(%rbp), %rax
movzbl -0x40(%rbp,%rax), %eax
cmpl $0x1, %eax
jne 0xb8310
movq -0x38(%rbp), %rax
movb $0x2, -0x40(%rbp,%rax)
jmp 0xb8329
movq -0x38(%rbp), %rax
movzbl -0x40(%rbp,%rax), %eax
cmpl $0x2, %eax
jne 0xb8327
movq -0x38(%rbp), %rax
movb $0x1, -0x40(%rbp,%rax)
jmp 0xb8329
jmp 0xb832b
jmp 0xb832d
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xb82db
movq -0x10(%rbp), %rax
cmpl $0x0, 0xd8(%rax)
je 0xb839c
movsd -0x70(%rbp), %xmm0
movzbl -0x40(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movsd 0xeb2d7(%rip), %xmm1    # 0x1a3638
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x44(%rbp)
movzbl -0x44(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x44(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x44(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movl %edx, 0xf0(%rax,%rcx,4)
jmp 0xb8453
movsd -0x68(%rbp), %xmm0
movzbl -0x3f(%rbp), %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movsd 0xeb283(%rip), %xmm1    # 0x1a3638
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x43(%rbp)
movsd -0x60(%rbp), %xmm0
movzbl -0x3e(%rbp), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x42(%rbp)
movsd -0x58(%rbp), %xmm0
movzbl -0x3d(%rbp), %eax
cvtsi2sd %eax, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x41(%rbp)
movzbl -0x45(%rbp), %eax
cmpl $0x0, %eax
je 0xb8428
movzbl -0x40(%rbp), %eax
cmpl $0x0, %eax
je 0xb8428
movzbl -0x43(%rbp), %eax
addl $0x55, %eax
movb %al, -0x43(%rbp)
movzbl -0x42(%rbp), %eax
addl $0x55, %eax
movb %al, -0x42(%rbp)
movzbl -0x41(%rbp), %eax
addl $0x55, %eax
movb %al, -0x41(%rbp)
movzbl -0x43(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x42(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x41(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movl %edx, 0xf0(%rax,%rcx,4)
cmpb $0x0, -0x45(%rbp)
je 0xb848a
leaq -0xc0(%rbp), %rdi
movzbl -0x3b(%rbp), %ecx
movzbl -0x3a(%rbp), %r8d
movzbl -0x39(%rbp), %r9d
movzbl -0x3c(%rbp), %eax
movl $0x40, %esi
leaq 0xfd082(%rip), %rdx     # 0x1b5500
movl %eax, (%rsp)
movb $0x0, %al
callq 0x15a980
jmp 0xb84b2
leaq -0xc0(%rbp), %rdi
movzbl -0x3b(%rbp), %ecx
movzbl -0x3a(%rbp), %r8d
movzbl -0x39(%rbp), %r9d
movl $0x40, %esi
leaq 0xfd072(%rip), %rdx     # 0x1b551d
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0xf0(%rax,%rcx,4), %edx
leaq -0xc0(%rbp), %rcx
movq -0x30(%rbp), %r8
movq -0x10(%rbp), %rax
movq 0xe8(%rax), %r10
leaq 0xe4a9e(%rip), %r9      # 0x19cf84
leaq 0x1186e3(%rip), %rax    # 0x1d0bd0
cmpq %r10, %r8
cmovlq %rax, %r9
xorl %eax, %eax
movl %eax, %r8d
callq 0x15bdb0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xb826e
movq -0x10(%rbp), %rax
movb $0x1, 0x9(%rax)
movq -0x8(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x15b5a0
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)