// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, 0x8(%rsp)
leaq 0x2c(%rsp), %rsi
callq 0xe8a1c
leaq 0xc(%rbp), %rax
movq 0x18(%r14), %rcx
cmpq 0x18(%rcx), %rax
jle 0x774b5
xorl %r15d, %r15d
jmp 0x777ff
movq 0x8(%rbx), %rdx
leaq 0xb969d(%rip), %rsi     # 0x130b5d
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, 0x20(%rsp)
cmpl $0x0, 0x4(%rbx)
movl $0x0, %r13d
movl $0x0, %ebx
je 0x77528
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xeebc8
movq %rax, %r13
movq 0x18(%r14), %rdi
leaq 0x2(%rbp), %rsi
callq 0xeebc8
movq %rax, %rbx
leaq 0xa5e7e(%rip), %rsi     # 0x11d397
movq %r14, %rdi
movl %r13d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xe8617
movq %rbx, 0x60(%rsp)
leaq 0x4(%rbp), %rax
leaq 0x8(%rsp), %rbx
movq %rax, (%rbx)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeebeb
movq %rax, %r12
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeebeb
movq %rax, %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe8f88
movq %r14, %rdi
movq %r12, 0x38(%rsp)
movq %r12, %rsi
movq %rbx, 0x18(%rsp)
movq %rbx, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x777ff
movq %r13, 0x58(%rsp)
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeebeb
movq %rax, %rbx
leaq 0xbee07(%rip), %rsi     # 0x1363a9
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, 0x30(%rsp)
leaq (,%rbx,8), %r15
movq 0x20(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x775e7
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xeebc8
leaq 0xb11e6(%rip), %rsi     # 0x1287c1
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
addq $0x2, 0x8(%rsp)
movq %r14, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %r12
movq %r12, %rcx
movl $0x1, %r8d
callq 0xf26a5
movq %rax, %r13
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl $0x4, %r8d
callq 0xf26a5
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdx
leaq 0xc3310(%rip), %rsi     # 0x13a949
movq %r15, 0x50(%rsp)
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %r12
movq %r12, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xf2a40
movq %r12, %rbx
imulq 0x18(%rsp), %rbx
movq 0x8(%rsp), %rdx
addq %rbx, %rdx
movq %rdx, 0x8(%rsp)
leaq 0xb7865(%rip), %rsi     # 0x12eee9
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0xf2a40
addq %rbx, 0x8(%rsp)
cmpq $0x0, 0x18(%rsp)
jle 0x7778f
movq %rbp, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movq 0x50(%rsp), %rax
cmpq $0x2, %rax
movl $0x1, %ebp
cmovgeq %rax, %rbp
xorl %ebx, %ebx
movq 0x10(%rsp), %r14
cmpq $0x0, 0x30(%rsp)
jle 0x77761
xorl %r12d, %r12d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2592
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x77759
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2592
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x77759
leal (%rbx,%r12), %r14d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x7f, %ecx
callq 0xf2405
testb $0x1, %r14b
movq 0x10(%rsp), %r14
movl $0xffff0080, %ecx       # imm = 0xFFFF0080
movl $0xff8000ff, %eax       # imm = 0xFF8000FF
cmovel %eax, %ecx
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf24ba
cmpq 0x38(%rsp), %r12
movl $0x1, %eax
cmovll %eax, %r15d
incq %r12
cmpq %r12, %rbp
jne 0x776e6
incq %rbx
cmpq 0x18(%rsp), %rbx
jne 0x776db
testl %r15d, %r15d
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %rbp
je 0x7778f
leaq 0xb0faf(%rip), %rsi     # 0x128734
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8faa
movl $0x1, %r15d
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0xf37bd
movq %r14, %rdi
callq 0xea504
movq %rax, %rbx
movq 0x20(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x777d7
movb $0x1, 0xe(%rbx)
movq 0x58(%rsp), %rax
movl %eax, 0x98(%rbx)
movq 0x60(%rsp), %rax
movl %eax, 0x9c(%rbx)
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl $0x2, %edx
callq 0xf22d0
movq 0x8(%rsp), %rax
subq %rbp, %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x10(%rcx)
jmp 0x77807
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r12, %rdi
callq 0xf228d
movq %r13, %rdi
callq 0xf228d
movq %r14, %rdi
movq %rbx, %rsi
callq 0xea50e
movl 0x2c(%rsp), %esi
movq %r14, %rdi
callq 0xe8a25
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq