// File: /workspace/github_c_makefile_star2_O3/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 0xc5996
movq %rsi, %r13
movl $0x112, %esi            # imm = 0x112
btq %rdx, %rsi
jae 0xc5996
movq %rcx, (%rsp)
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
leaq 0x98(%r13), %r14
cmpl $0x0, 0x98(%r13)
jne 0xc5981
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x90(%r13)
xorl %eax, %eax
leaq 0x75903(%rip), %rcx     # 0x13b180
movaps 0x4b4fc(%rip), %xmm0    # 0x110d80
movaps (%rax,%rcx), %xmm1
orps %xmm0, %xmm1
movups %xmm1, (%r14,%rax)
addq $0x10, %rax
cmpq $0x40, %rax
jne 0xc5884
leaq 0x7598f(%rip), %rax     # 0x13b230
movb $-0xf, %cl
xorl %esi, %esi
movl $0xff, %edi
movl $0xffff, %r8d           # imm = 0xFFFF
leaq 0x75909(%rip), %r9      # 0x13b1c0
cmpq $0xf, %rsi
ja 0xc58c5
movl (%rax), %r10d
jmp 0xc595c
cmpq $0xe5, %rsi
ja 0xc5954
movzbl %cl, %r11d
imull $0xab, %r11d, %r10d
shrl $0x9, %r10d
andl $-0x2, %r10d
leal (%r10,%r10,2), %r10d
movb $-0x10, %bpl
subb %r10b, %bpl
movzbl %bpl, %r10d
incq %r10
imull $0x39, %r11d, %r11d
shrl $0x9, %r11d
andl $-0x4, %r11d
leal (%r11,%r11,8), %r11d
movb $-0x10, %bpl
subb %r11b, %bpl
movzbl %bpl, %r11d
incq %r11
leal 0xf1(%rsi), %ebp
andl %edi, %ebp
imull $0x39, %ebp, %ebp
shrl $0xb, %ebp
addl %esi, %r11d
andl %edi, %r11d
imull $0xab, %r11d, %r11d
shrl $0xa, %r11d
addl %esi, %r10d
andl %edi, %r10d
imull $0x33, %r10d, %r10d
imull $0x330000, %ebp, %ebp   # imm = 0x330000
orl %r10d, %ebp
imull $0x3300, %r11d, %r10d   # imm = 0x3300
andl %r8d, %r10d
addl %ebp, %r10d
jmp 0xc595c
movl -0x398(%r9,%rsi,4), %r10d
orl $0xff000000, %r10d      # imm = 0xFF000000
movl %r10d, 0xd8(%r13,%rsi,4)
incq %rsi
addq $0x4, %rax
incb %cl
cmpq $0x100, %rsi            # imm = 0x100
jne 0xc58b7
cmpq $0x4, %rdx
je 0xc59ad
cmpq $0x1, %rdx
jne 0xc59a6
leaq 0x90(%r13), %r14
jmp 0xc59ad
testl %eax, %eax
je 0xc5a56
xorl %r12d, %r12d
jmp 0xc5a6a
leaq 0xd8(%r13), %r14
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
movl $0x4, %ecx
callq 0xf266a
movq %rax, %rbp
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf266a
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 0xf30b3
addq $0x10, %rsp
movq 0x10(%r15), %rcx
movq %r13, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0xc5468
movq %rbp, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf37bd
movq %rbx, %rdi
callq 0xea504
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
callq 0xc5783
movq %rbp, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0xf22d0
jmp 0xc5a6f
leaq 0x756cc(%rip), %rsi     # 0x13b129
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xf228d
movq %r12, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xea50e