// File: /workspace/github_c_makefile_star2_O2/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 0xbdaaf
movq %rsi, %r13
movl $0x112, %esi            # imm = 0x112
btq %rdx, %rsi
jae 0xbdaaf
movq %rcx, (%rsp)
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
leaq 0x98(%r13), %r14
cmpl $0x0, 0x98(%r13)
je 0xbd9af
cmpq $0x4, %rdx
je 0xbdac6
cmpq $0x1, %rdx
jne 0xbdabf
leaq 0x90(%r13), %r14
jmp 0xbdac6
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x90(%r13)
xorl %eax, %eax
leaq 0x704c7(%rip), %rcx     # 0x12de90
movl $0xff000000, %esi       # imm = 0xFF000000
cmpq $0x40, %rax
je 0xbd9e3
movl (%rax,%rcx), %edi
orl %esi, %edi
movl %edi, (%r14,%rax)
addq $0x4, %rax
jmp 0xbd9ce
leaq 0x70556(%rip), %rdi     # 0x12df40
movb $-0xf, %r8b
xorl %r9d, %r9d
movb $0x6, %r10b
movb $0x24, %r11b
leaq 0x704d3(%rip), %r12     # 0x12ded0
movzbl %r8b, %esi
movl %esi, %eax
divb %r10b
movl %eax, %ecx
movl %esi, %eax
divb %r11b
cmpq $0x100, %r9             # imm = 0x100
je 0xbd98f
cmpq $0xf, %r9
ja 0xbda24
movl (%rdi), %eax
jmp 0xbda93
cmpq $0xe5, %r9
ja 0xbda8b
movzbl %cl, %ecx
imull $0x6, %ecx, %ecx
movb $-0x10, %sil
subb %cl, %sil
movzbl %sil, %ecx
incq %rcx
movzbl %al, %eax
imull $0x24, %eax, %eax
movb $-0x10, %sil
subb %al, %sil
movzbl %sil, %esi
incq %rsi
leal -0xf(%r9), %eax
movzbl %al, %eax
divb %r11b
movzbl %al, %ebp
addl %r9d, %esi
movzbl %sil, %eax
divb %r10b
movzbl %al, %eax
addl %r9d, %ecx
movzbl %cl, %ecx
imull $0x33, %ecx, %ecx
imull $0x330000, %ebp, %esi   # imm = 0x330000
orl %ecx, %esi
imull $0x3300, %eax, %eax     # imm = 0x3300
movzwl %ax, %eax
addl %esi, %eax
jmp 0xbda93
movl -0x398(%r12,%r9,4), %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0xd8(%r13,%r9,4)
incq %r9
addq $0x4, %rdi
incb %r8b
jmp 0xbd9fd
testl %eax, %eax
je 0xbdb65
xorl %r12d, %r12d
jmp 0xbdb79
leaq 0xd8(%r13), %r14
movq (%r15), %rsi
movq 0x8(%r15), %rdx
pushq $0x4
popq %rcx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %rbp
movq (%r15), %rsi
movq 0x8(%r15), %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
callq 0xe7437
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 0xe7de1
popq %rax
popq %rcx
movq 0x10(%r15), %rcx
movq %r13, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq 0xbd5fb
movq %rbp, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0xe83f8
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
callq 0xbd8b7
pushq $0x2
popq %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0xe70ab
jmp 0xbdb7e
leaq 0x702cd(%rip), %rsi     # 0x12de39
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %rbp, %rdi
callq 0xe7084
movq %r12, %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdfd24
nop