// File: /workspace/github_c_makefile_star2_O1/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 0x10(%rdx), %rax
cmpq $0x0, 0x10(%rsi)
setg %cl
cmpq $0x2, %rax
setge %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x9d0ba
movq %rsi, %r15
movq %rdi, 0x18(%rsp)
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
movq %rcx, 0x10(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x8(%rsp)
cmpq $0x0, 0x8(%r15)
jle 0x9d07e
movq 0x10(%rsp), %rax
addq %r12, %rax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
cmpq 0x8(%r14), %rbx
jge 0x9d07e
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2bd3
movl %eax, %ebp
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2bd3
movl %eax, %r13d
movq %r14, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
callq 0xf2bd3
testl $0xff0000, %eax         # imm = 0xFF0000
je 0x9d033
testl $0xff0000, %r13d        # imm = 0xFF0000
je 0x9d03e
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 0x9d05f
cmpl $0x0, 0xc(%rsp)
je 0x9d048
movl %ebp, %ecx
jmp 0x9d05f
movl %ebp, %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
jmp 0x9d05f
andl $0xff0000, %r13d        # imm = 0xFF0000
negl %r13d
movl $0x0, %ecx
sbbl %ecx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
cmpl %ebp, %ecx
je 0x9d071
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xf2afd
incq %rbx
cmpq 0x8(%r15), %rbx
jl 0x9cfcb
incq %r12
cmpq 0x10(%r15), %r12
jge 0x9d092
cmpq 0x10(%rsp), %r12
jl 0x9cfb1
cmpl $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rdi
je 0x9d0ba
leaq 0x8b45f(%rip), %rsi     # 0x128504
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9b27
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq