// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pcx.c
// Function: do_palette_stuff
static void do_palette_stuff(deark *c, lctx *d)
{
	i64 k;

	if(d->ncolors>256) {
		return;
	}

	if(d->ncolors==256) {
		// For 256-color images, start with a default grayscale palette.
		for(k=0; k<256; k++) {
			d->pal[k] = DE_MAKE_GRAY((UI)k);
		}
	}

	if(do_read_alt_palette_file(c, d)) {
		return;
	}

	if(d->ncolors==2) {
		// TODO: Allegedly, some 2-color PCXs are not simply white-on-black,
		// and at least the foreground color can be something other than white.
		// The color information would be stored in the palette area, but
		// different files use different ways of conveying that information,
		// and it seems hopeless to reliably determine the correct format.
		return;
	}

	if(d->version==3 && d->ncolors>=8 && d->ncolors<=16) {
		// Come up with a 16-color palette, if there is no palette in the file.
		// (8-color version-3 PCXs apparently use only the first 8 colors of the
		// palette.)

		if(!d->default_pal_set) {
			de_info(c, "Note: This paletted PCX file does not contain a palette. "
				"If it is not decoded correctly, try \"-opt pcx:pal=1\".");
		}
		de_dbg(c, "using a default EGA palette");
		if(d->default_pal_num==1) {
			// This is the "default EGA palette" used by several PCX viewers.
			// I don't know its origin.
			de_memcpy(d->pal, ega16pal_1, sizeof(ega16pal_1));
		}
		else {
			// This palette seems to be correct for at least some files.
			de_copy_std_palette(DE_PALID_WIN16, 2, 0, d->pal, 16, 0);
		}
		return;
	}

	if(d->version>=5 && d->ncolors==256) {
		if(do_read_vga_palette(c, d)) {
			return;
		}
		de_warn(c, "Expected VGA palette was not found");
		// (Use the grayscale palette created earlier, as a last resort.)
		return;
	}

	if(d->ncolors==4) {
		u8 p0, p3;
		UI bgcolor;
		UI fgpal;
		int pal_subid;

		de_warn(c, "4-color PCX images might not be supported correctly");

		p0 = de_getbyte(16);
		p3 = de_getbyte(19);
		bgcolor = p0>>4;
		fgpal = p3>>5;
		de_dbg(c, "using a CGA palette: palette #%d, bkgd color %d", (int)fgpal, (int)bgcolor);

		// Set first pal entry to background color
		d->pal[0] = de_get_std_palette_entry(DE_PALID_PC16, 0, (int)bgcolor);

		// TODO: These palettes are quite possibly incorrect. I can't find good
		// information about them.
		switch(fgpal) {
		case 1: case 3:
			pal_subid = 5; break; // C=0 P=? I=1
		case 4:
			pal_subid = 1; break; // C=1 P=0 I=0
		case 5:
			pal_subid = 4; break; // C=1 P=0 I=1
		case 6:
			pal_subid = 0; break; // C=1 P=1 I=0
		case 7:
			pal_subid = 3; break; // C=1 P=1 I=1
		default: // 0, 2
			pal_subid = 2; break; // C=0 P=? I=0
		}
		de_copy_std_palette(DE_PALID_CGA, pal_subid, 1, &d->pal[1], 3, 0);
		return;
	}

	if(d->ncolors>16 && d->ncolors<=256) {
		de_warn(c, "%u-color image format with 16-color palette", (UI)d->ncolors);
	}

	de_dbg(c, "using 16-color palette from header");

	de_dbg_indent(c, 1);
	de_read_palette_rgb(c->infile, 16, 16, 3, d->pal, 256, 0);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x100, 0x60(%rax)      # imm = 0x100
jle 0x104e33
jmp 0x10516f
movq -0x10(%rbp), %rax
cmpq $0x100, 0x60(%rax)      # imm = 0x100
jne 0x104e90
movq $0x0, -0x18(%rbp)
cmpq $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x104e8e
movq -0x18(%rbp), %rax
movl %eax, %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movq -0x18(%rbp), %rax
shll $0x8, %eax
orl %eax, %edx
movq -0x18(%rbp), %rax
orl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movl %edx, 0xb8(%rax,%rcx,4)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x104e49
jmp 0x104e90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x105770
cmpl $0x0, %eax
je 0x104ea7
jmp 0x10516f
movq -0x10(%rbp), %rax
cmpq $0x2, 0x60(%rax)
jne 0x104eb7
jmp 0x10516f
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
jne 0x104f6d
movq -0x10(%rbp), %rax
cmpq $0x8, 0x60(%rax)
jl 0x104f6d
movq -0x10(%rbp), %rax
cmpq $0x10, 0x60(%rax)
jg 0x104f6d
movq -0x10(%rbp), %rax
cmpl $0x0, 0xa0(%rax)
jne 0x104f04
movq -0x8(%rbp), %rdi
leaq 0xbce36(%rip), %rsi     # 0x1c1d33
movb $0x0, %al
callq 0x15c0c0
movq -0x8(%rbp), %rdi
leaq 0xbce93(%rip), %rsi     # 0x1c1da2
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpl $0x1, 0x9c(%rax)
jne 0x104f41
movq -0x10(%rbp), %rdi
addq $0xb8, %rdi
leaq 0xbcb9b(%rip), %rsi     # 0x1c1ad0
movl $0x40, %edx
callq 0x191e0
jmp 0x104f68
movq -0x10(%rbp), %rcx
addq $0xb8, %rcx
movl $0x32, %edi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
xorl %r9d, %r9d
callq 0x161320
jmp 0x10516f
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x5, %eax
jl 0x104fb5
movq -0x10(%rbp), %rax
cmpq $0x100, 0x60(%rax)      # imm = 0x100
jne 0x104fb5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x105990
cmpl $0x0, %eax
je 0x104f9e
jmp 0x10516f
movq -0x8(%rbp), %rdi
leaq 0xbce15(%rip), %rsi     # 0x1c1dbe
movb $0x0, %al
callq 0x15bc10
jmp 0x10516f
movq -0x10(%rbp), %rax
cmpq $0x4, 0x60(%rax)
jne 0x1050d8
movq -0x8(%rbp), %rdi
leaq 0xbce12(%rip), %rsi     # 0x1c1de1
xorl %eax, %eax
movl %eax, -0x34(%rbp)
movb %al, -0x35(%rbp)
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x165060
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x13, %esi
callq 0x165060
movb %al, %cl
movb -0x35(%rbp), %al
movb %cl, -0x1a(%rbp)
movzbl -0x19(%rbp), %ecx
shrl $0x4, %ecx
movl %ecx, -0x20(%rbp)
movzbl -0x1a(%rbp), %ecx
shrl $0x5, %ecx
movl %ecx, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %edx
movl -0x20(%rbp), %ecx
leaq 0xbcde5(%rip), %rsi     # 0x1c1e15
callq 0x15b0d0
movl -0x34(%rbp), %esi
movl -0x20(%rbp), %edx
movl $0x14, %edi
callq 0x161150
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xb8(%rax)
movl -0x24(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x6, %eax
ja 0x1050a2
movq -0x30(%rbp), %rax
leaq 0xbca44(%rip), %rcx     # 0x1c1ab0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x5, -0x28(%rbp)
jmp 0x1050a9
movl $0x1, -0x28(%rbp)
jmp 0x1050a9
movl $0x4, -0x28(%rbp)
jmp 0x1050a9
movl $0x0, -0x28(%rbp)
jmp 0x1050a9
movl $0x3, -0x28(%rbp)
jmp 0x1050a9
movl $0x2, -0x28(%rbp)
movl -0x28(%rbp), %esi
movq -0x10(%rbp), %rcx
addq $0xb8, %rcx
addq $0x4, %rcx
movl $0xa, %edi
movl $0x1, %edx
movl $0x3, %r8d
xorl %r9d, %r9d
callq 0x161320
jmp 0x10516f
movq -0x10(%rbp), %rax
cmpq $0x10, 0x60(%rax)
jle 0x10510d
movq -0x10(%rbp), %rax
cmpq $0x100, 0x60(%rax)      # imm = 0x100
jg 0x10510d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %edx
leaq 0xbcd3f(%rip), %rsi     # 0x1c1e45
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
leaq 0xbcd59(%rip), %rsi     # 0x1c1e71
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %r8
addq $0xb8, %r8
movl $0x10, %edx
movl $0x3, %ecx
movl $0x100, %r9d            # imm = 0x100
xorl %eax, %eax
movq %rdx, %rsi
movl $0x0, (%rsp)
callq 0x16c0c0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)