// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ico.c
// Function: decode_win1_icon
static int decode_win1_icon(deark *c, win1ctx *d, i64 pos1)
{
	de_bitmap *mask = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	i64 npwidth, h;
	i64 pdwidth;
	i64 rowspan;
	i64 i, j;
	i64 pos = pos1;
	int has_inv_bkgd = 0;
	int hotspot_x = 0;
	int hotspot_y = 0;
	int retval = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	if(pos1+12 > c->infile->len) goto done;

	de_dbg(c, "%s at %"I64_FMT, d->type_name, pos);
	de_dbg_indent(c, 1);

	if(d->is_cur) {
		hotspot_x = (int)de_getu16le(pos);
		hotspot_y = (int)de_getu16le(pos+2);
		de_dbg(c, "hotspot: %d,%d", hotspot_x, hotspot_y);
	}
	pos += 4;

	npwidth = de_getu16le_p(&pos);
	h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, npwidth, h);
	if(!de_good_image_dimensions(c, npwidth, h)) goto done;

	rowspan = de_getu16le_p(&pos);
	de_dbg(c, "bytes/row: %d", (int)rowspan);
	pdwidth = rowspan*8;

	if(d->is_cur) {
		unsigned int csColor;
		csColor = (unsigned int)de_getu16le(pos);
		de_dbg(c, "csColor: 0x%04x", csColor);
	}
	pos += 2;

	mask = de_bitmap_create2(c, npwidth, pdwidth, h, 1);
	img = de_bitmap_create2(c, npwidth, pdwidth, h, 4);
	de_dbg(c, "mask at %"I64_FMT, pos);
	de_convert_image_bilevel(c->infile, pos, rowspan, mask, 0);
	pos += rowspan*h;
	de_dbg(c, "foreground at %"I64_FMT, pos);
	de_convert_image_bilevel(c->infile, pos, rowspan, img, 0);
	pos += rowspan*h;

	// This whole loop does nothing, except handle inverse-background-color
	// pixels. But we have to do something, because such pixels are not
	// uncommon.
	for(j=0; j<h; j++) {
		for(i=0; i<pdwidth; i++) {
			u8 fgclr, maskclr;
			u32 newclr;

			maskclr = DE_COLOR_K(de_bitmap_getpixel(mask, i, j));
			if(maskclr==0) continue;
			fgclr = DE_COLOR_K(de_bitmap_getpixel(img, i, j));
			if(fgclr==0) continue;

			newclr = get_inv_bkgd_replacement_clr(i, j);
			de_bitmap_setpixel_gray(mask, i, j, 255-DE_COLOR_A(newclr));
			de_bitmap_setpixel_rgb(img, i, j, DE_MAKE_OPAQUE(newclr));
			if(i<npwidth) {
				has_inv_bkgd = 1;
			}
		}
	}
	if(has_inv_bkgd) {
		warn_inv_bkgd(c);
	}

	de_bitmap_apply_mask(img, mask, DE_BITMAPFLAG_WHITEISTRNS);

	fi = de_finfo_create(c);

	if(d->is_cur) {
		fi->has_hotspot = 1;
		fi->hotspot_x = hotspot_x;
		fi->hotspot_y = hotspot_y;
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);
	d->bytes_consumed = pos - pos1;
	retval = 1;

done:
	de_bitmap_destroy(img);
	de_bitmap_destroy(mask);
	de_finfo_destroy(c, fi);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movq %rdx, 0x8(%rsp)
leaq 0x24(%rsp), %rsi
callq 0xdebae
leaq 0xc(%r12), %rax
movq 0x18(%r13), %rcx
cmpq 0x18(%rcx), %rax
jle 0x73125
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0x7344c
movq 0x8(%r15), %rdx
leaq 0xb074a(%rip), %rsi     # 0x12387a
movq %r13, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0xdeba7
cmpl $0x0, 0x4(%r15)
movl $0x0, %ebp
movl $0x0, %r14d
je 0x73190
movq 0x18(%r13), %rdi
movq %r12, %rsi
callq 0xe3fa3
movq %rax, %rbp
movq 0x18(%r13), %rdi
leaq 0x2(%r12), %rsi
callq 0xe3fa3
movq %rax, %r14
leaq 0x9d126(%rip), %rsi     # 0x1102a7
movq %r13, %rdi
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0xde896
movq %r14, 0x50(%rsp)
leaq 0x4(%r12), %rax
leaq 0x8(%rsp), %r14
movq %rax, (%r14)
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xe3fc4
movq %rax, %rbx
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xe3fc4
movq %rax, %r14
movq %r13, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xdf038
movq %r13, %rdi
movq %rbx, 0x28(%rsp)
movq %rbx, %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x73441
movq %rbp, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq 0x18(%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3fc4
movq %rax, %r12
leaq 0xb5e99(%rip), %rsi     # 0x1290a9
movq %r13, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
leaq (,%r12,8), %rbx
movq %r15, 0x40(%rsp)
cmpl $0x0, 0x4(%r15)
je 0x73252
movq 0x18(%r13), %rdi
movq 0x8(%rsp), %rsi
callq 0xe3fa3
leaq 0xa833b(%rip), %rsi     # 0x11b581
movq %r13, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
addq $0x2, 0x8(%rsp)
movq %r13, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %rbp
movq %rbp, %rcx
pushq $0x1
popq %r8
callq 0xe746f
movq %rax, %r14
pushq $0x4
popq %r8
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xe746f
movq %rax, %r15
movq 0x8(%rsp), %rdx
leaq 0xba3bb(%rip), %rsi     # 0x12d659
movq %r13, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%r13), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0xe77b7
movq %r12, 0x18(%rsp)
imulq %rbp, %r12
movq 0x8(%rsp), %rdx
addq %r12, %rdx
movq %rdx, 0x8(%rsp)
leaq 0xae985(%rip), %rsi     # 0x121c61
movq %r13, %rdi
xorl %eax, %eax
callq 0xde896
movq %r13, 0x48(%rsp)
movq 0x18(%r13), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xe77b7
addq %r12, 0x8(%rsp)
testq %rbx, %rbx
movl $0x0, %r13d
cmovleq %r13, %rbx
testq %rbp, %rbp
cmovleq %r13, %rbp
movq %rbp, 0x10(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
cmpq 0x10(%rsp), %rbp
je 0x733cb
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x733c3
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xe735f
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x733bb
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xe735f
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x733bb
movq %r15, %rbx
movq %r13, %r15
leal (%r12,%rbp), %r13d
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
pushq $0x7f
popq %rcx
callq 0xe71da
testb $0x1, %r13b
movq %r15, %r13
movq %rbx, %r15
movq 0x18(%rsp), %rbx
movl $0xffff0080, %ecx       # imm = 0xFFFF0080
movl $0xff8000ff, %eax       # imm = 0xFF8000FF
cmovel %eax, %ecx
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xe7287
cmpq 0x28(%rsp), %r12
pushq $0x1
popq %rax
cmovll %eax, %r13d
incq %r12
jmp 0x73337
incq %rbp
jmp 0x73329
testl %r13d, %r13d
movq 0x48(%rsp), %r13
je 0x733dd
movq %r13, %rdi
callq 0x730d2
pushq $0x1
popq %rbx
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0xe83f8
movq %r13, %rdi
callq 0xdfd1a
movq %rax, %r12
movq 0x40(%rsp), %rbp
cmpl $0x0, 0x4(%rbp)
je 0x73423
movb $0x1, 0xe(%r12)
movq 0x30(%rsp), %rax
movl %eax, 0x98(%r12)
movq 0x50(%rsp), %rax
movl %eax, 0x9c(%r12)
pushq $0x2
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0xe70ab
movq 0x8(%rsp), %rax
subq 0x38(%rsp), %rax
movq %rax, 0x10(%rbp)
jmp 0x7344c
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r15, %rdi
callq 0xe7084
movq %r14, %rdi
callq 0xe7084
movq %r13, %rdi
movq %r12, %rsi
callq 0xdfd24
movl 0x24(%rsp), %esi
movq %r13, %rdi
callq 0xdebb7
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq