// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/rsc.c
// Function: do_color_icon
static void do_color_icon(deark *c, lctx *d, struct iconinfo *ii, i64 fg_pos,
	i64 mask_pos, const char *token, int is_sel)
{
	de_bitmap *img = NULL;
	de_bitmap *mask = NULL;
	de_finfo *fi = NULL;
	i64 planespan;
	const de_color *pal_to_use;

	// TODO: Images with 5, 6, or 7 planes exist, but I don't know what palettes
	// they use.

	if(ii->nplanes!=1 && ii->nplanes!=4 && ii->nplanes!=8) {
		// The non-selected and selected images have the same # of planes, so
		// suppress the duplicate warning message.
		if(!is_sel) {
			de_warn(c, "%d-plane icons not supported", (int)ii->nplanes);
		}
		goto done;
	}

	if(d->pal16[0]==0) {
		construct_palettes(c, d);
	}

	switch(ii->nplanes) {
	case 1: pal_to_use = d->pal2; break;
	case 4: pal_to_use = d->pal16; break;
	default: pal_to_use = d->pal256;
	}

	img = de_bitmap_create(c, ii->width, ii->height, 4);
	mask = de_bitmap_create(c, ii->width, ii->height, 1);

	planespan = ii->mono_rowspan * ii->height;
	de_convert_image_paletted_planar(c->infile, fg_pos, ii->nplanes,
		ii->mono_rowspan, planespan, pal_to_use, img, 0x2);

	do_decode_bilevel_image(c, d, mask, mask_pos, ii->mono_rowspan);
	de_bitmap_apply_mask(img, mask, DE_BITMAPFLAG_WHITEISTRNS);

	fi = de_finfo_create(c);
	set_icon_finfo(c, d, fi, ii, token);
	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_bitmap_destroy(img);
	de_bitmap_destroy(mask);
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r15
movq %rdi, %rbx
movl 0x50(%rsp), %eax
movq 0x18(%rdx), %rdx
cmpq $0x8, %rdx
ja 0xc6500
movq %rsi, %r13
movl $0x112, %esi            # imm = 0x112
btq %rdx, %rsi
jae 0xc6500
movq %rcx, (%rsp)
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
leaq 0x98(%r13), %r14
cmpl $0x0, 0x98(%r13)
jne 0xc64eb
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x90(%r13)
xorl %eax, %eax
leaq 0x749da(%rip), %rcx     # 0x13ade0
movl $0xff000000, %esi       # imm = 0xFF000000
movl (%rax,%rcx), %edi
orl %esi, %edi
movl %edi, (%r14,%rax)
addq $0x4, %rax
cmpq $0x40, %rax
jne 0xc640b
movl $0xfffffff1, %eax       # imm = 0xFFFFFFF1
movl $0xfffffd03, %ecx       # imm = 0xFFFFFD03
leaq 0x74a61(%rip), %rsi     # 0x13ae90
xorl %edi, %edi
movl $0xaaaaaaab, %r8d       # imm = 0xAAAAAAAB
leaq 0x749e2(%rip), %r9      # 0x13ae20
cmpq $0xf, %rdi
ja 0xc6449
movl (%rsi), %r10d
jmp 0xc64c3
cmpq $0xe5, %rdi
ja 0xc64bb
movl %eax, %r10d
imulq $0x38e38e39, %r10, %r11 # imm = 0x38E38E39
shrq $0x23, %r11
shll $0x2, %r11d
leal (%r11,%r11,8), %r11d
negl %r11d
imulq %r8, %r10
shrq $0x22, %r10
imull $0x132, %r10d, %r10d    # imm = 0x132
movl %ecx, %ebp
subl %r10d, %ebp
leal -0xf(%rdi), %r10d
imulq $0x38e38e39, %r10, %r10 # imm = 0x38E38E39
shrq $0x23, %r10
addl %edi, %r11d
addl $-0xf, %r11d
imulq %r8, %r11
shrq $0x22, %r11
imull $0x330000, %r10d, %r10d # imm = 0x330000
imull $0x3300, %r11d, %r11d   # imm = 0x3300
movzwl %r11w, %r11d
orl %r10d, %r11d
movzbl %bpl, %r10d
orl %r11d, %r10d
jmp 0xc64c3
movl -0x398(%r9,%rdi,4), %r10d
orl $0xff000000, %r10d      # imm = 0xFF000000
movl %r10d, 0xd8(%r13,%rdi,4)
incq %rdi
incl %eax
addl $0x33, %ecx
addq $0x4, %rsi
cmpq $0x100, %rdi            # imm = 0x100
jne 0xc643e
cmpq $0x4, %rdx
je 0xc6517
cmpq $0x1, %rdx
jne 0xc6510
leaq 0x90(%r13), %r14
jmp 0xc6517
testl %eax, %eax
je 0xc65c0
xorl %r12d, %r12d
jmp 0xc65d4
leaq 0xd8(%r13), %r14
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
movl $0x4, %ecx
callq 0xf2cab
movq %rax, %rbp
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf2cab
movq %rax, %r12
movq 0x10(%r15), %rcx
movq 0x18(%r15), %rdx
movq 0x8(%r15), %r8
imulq %rcx, %r8
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq %r14, %r9
pushq $0x2
pushq %rbp
callq 0xf36f9
addq $0x10, %rsp
movq 0x10(%r15), %rcx
movq %r13, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0xc6007
movq %rbp, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf3e1c
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
callq 0xc630c
movq %rbp, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0xf2917
jmp 0xc65d9
leaq 0x747ca(%rip), %rsi     # 0x13ad91
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xf28d4
movq %r12, %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea9b3
nop