// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/os2bmp.c
// Function: do_apply_os2bmp_mask
static void do_apply_os2bmp_mask(deark *c, de_bitmap *fg, de_bitmap *mask, int is_color)
{
	i64 i, j;
	i64 mask_adj_height;
	int inverse_used = 0;

	mask_adj_height = mask->height / 2;

	for(j=0; j<fg->height && j<mask_adj_height; j++) {
		for(i=0; i<fg->width && i<mask->width; i++) {
			u8 andmaskclr;
			u8 xormaskclr;
			u32 oldclr;
			u32 newclr;

			oldclr = de_bitmap_getpixel(fg, i, j);
			newclr = oldclr;
			xormaskclr = DE_COLOR_K(de_bitmap_getpixel(mask, i, j));
			andmaskclr =  DE_COLOR_K(de_bitmap_getpixel(mask, i, mask_adj_height+j));

			if(andmaskclr==0) {
				if(is_color) {
					// For color bitmaps, the XOR bit is not used when the AND bit is 0.
					// Always use foreground color.
					;
				}
				else {
					if(xormaskclr==0) {
						newclr = DE_STOCKCOLOR_BLACK;
					}
					else {
						newclr = DE_STOCKCOLOR_WHITE;
					}
				}
			}
			else {
				if(xormaskclr==0) { // transparent
					newclr = DE_SET_ALPHA(oldclr, 0);
				}
				else { // inverse background
					newclr = get_inv_bkgd_replacement_clr(i, j);
					inverse_used = 1;
				}
			}

			if(newclr!=oldclr) {
				de_bitmap_setpixel_rgb(fg, i, j, newclr);
			}
		}
	}

	if(inverse_used) {
		de_warn(c, "This image contains inverse background pixels, which are not fully supported.");
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq 0x10(%rdx), %rax
movq 0x10(%rsi), %rcx
testq %rcx, %rcx
setg %dl
cmpq $0x2, %rax
setge %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x9c8b1
movq %rdi, 0x10(%rsp)
movq %rax, %rdx
shrq $0x3f, %rdx
addq %rax, %rdx
sarq %rdx
movq 0x8(%r15), %rax
xorl %r12d, %r12d
movl $0x0, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
testq %rax, %rax
jle 0x9c878
cmpq $0x0, 0x8(%r14)
jle 0x9c86f
leaq (%r12,%rdx), %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2592
movl %eax, %ebp
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2592
movl %eax, %r13d
movq %r14, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0xf2592
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x9c81f
testl $0xff0000, %r13d        # imm = 0xFF0000
je 0x9c83f
leal (%r12,%rbx), %eax
testb $0x1, %al
movl $0x80ff0080, %ecx       # imm = 0x80FF0080
movl $0x808000ff, %eax       # imm = 0x808000FF
cmovel %eax, %ecx
movl $0x1, 0x8(%rsp)
jmp 0x9c847
cmpl $0x0, 0xc(%rsp)
jne 0x9c859
andl $0xff0000, %r13d        # imm = 0xFF0000
negl %r13d
movl $0x0, %ecx
sbbl %ecx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
jmp 0x9c847
movl %ebp, %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
cmpl %ebp, %ecx
je 0x9c859
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbx
movq 0x8(%r15), %rax
cmpq %rax, %rbx
jge 0x9c86f
cmpq 0x8(%r14), %rbx
jl 0x9c7c1
movq 0x10(%r15), %rcx
movq 0x18(%rsp), %rdx
incq %r12
cmpq %rcx, %r12
jge 0x9c889
cmpq %rdx, %r12
jl 0x9c7a2
cmpl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
je 0x9c8b1
leaq 0x8be98(%rip), %rsi     # 0x128734
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8faa
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq