// File: /workspace/github_c_makefile_star2_O1/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 0x24(%rsp), %rsi
callq 0xe958c
leaq 0xc(%rbp), %rax
movq 0x18(%r14), %rcx
cmpq 0x18(%rcx), %rax
jle 0x77838
xorl %r15d, %r15d
jmp 0x77b88
movq 0x8(%rbx), %rdx
movq 0x8(%rsp), %rcx
leaq 0xb8fda(%rip), %rsi     # 0x130822
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, 0x18(%rsp)
cmpl $0x0, 0x4(%rbx)
movl $0x0, %r13d
movl $0x0, %ebx
je 0x778b4
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xef1ca
movq %rax, %r13
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
addq $0x2, %rsi
callq 0xef1ca
movq %rax, %rbx
leaq 0xa59e2(%rip), %rsi     # 0x11d287
movq %r14, %rdi
movl %r13d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0xe9226
movq %rbx, 0x60(%rsp)
leaq 0x8(%rsp), %rbx
addq $0x4, (%rbx)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xef1ed
movq %rax, %r12
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xef1ed
movq %rax, %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe9b05
movq %r14, %rdi
movq %r12, 0x40(%rsp)
movq %r12, %rsi
movq %rbx, 0x38(%rsp)
movq %rbx, %rdx
callq 0xf241d
testl %eax, %eax
je 0x77b88
movq %rbp, 0x28(%rsp)
movq %r13, 0x58(%rsp)
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef1ed
movq %rax, %rbx
leaq 0xbe636(%rip), %rsi     # 0x135f66
movq %r14, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, 0x30(%rsp)
leaq (,%rbx,8), %r15
movq 0x18(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x77975
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0xef1ca
leaq 0xb0c28(%rip), %rsi     # 0x128591
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
addq $0x2, 0x8(%rsp)
movq %r14, %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq 0x38(%rsp), %r12
movq %r12, %rcx
movl $0x1, %r8d
callq 0xf2ce6
movq %rax, %r13
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
movl $0x4, %r8d
callq 0xf2ce6
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rdx
leaq 0xc2c02(%rip), %rsi     # 0x13a5c9
movq %r15, 0x50(%rsp)
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rbp
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0xf3081
movq %rbp, %rbx
imulq %r12, %rbx
movq 0x8(%rsp), %rdx
addq %rbx, %rdx
movq %rdx, 0x8(%rsp)
leaq 0xb71c9(%rip), %rsi     # 0x12ebd9
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0xf3081
addq %rbx, 0x8(%rsp)
testq %r12, %r12
movq 0x28(%rsp), %rbp
jle 0x77b18
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 0x77aea
xorl %r12d, %r12d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2bd3
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x77ae2
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0xf2bd3
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x77ae2
leal (%rbx,%r12), %r14d
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x7f, %ecx
callq 0xf2a4a
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 0xf2afd
cmpq 0x40(%rsp), %r12
movl $0x1, %eax
cmovll %eax, %r15d
incq %r12
cmpq %r12, %rbp
jne 0x77a6f
incq %rbx
cmpq 0x38(%rsp), %rbx
jne 0x77a64
testl %r15d, %r15d
movq 0x48(%rsp), %r14
movq 0x28(%rsp), %rbp
je 0x77b18
leaq 0xb09f6(%rip), %rsi     # 0x128504
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
movl $0x1, %r15d
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0xf3e1c
movq %r14, %rdi
callq 0xea9a9
movq %rax, %rbx
movq 0x18(%rsp), %rax
cmpl $0x0, 0x4(%rax)
je 0x77b60
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 0xf2917
movq 0x8(%rsp), %rax
subq %rbp, %rax
movq 0x18(%rsp), %rcx
movq %rax, 0x10(%rcx)
jmp 0x77b90
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r12, %rdi
callq 0xf28d4
movq %r13, %rdi
callq 0xf28d4
movq %r14, %rdi
movq %rbx, %rsi
callq 0xea9b3
movl 0x24(%rsp), %esi
movq %r14, %rdi
callq 0xe9595
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)