// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/epocimage.c
// Function: do_combine_and_write_images
static void do_combine_and_write_images(deark *c, lctx *d,
	struct phys_image_ctx *pi_fg, struct phys_image_ctx *pi_mask)
{
	de_bitmap *fg_img;
	de_bitmap *mask_img;
	de_bitmap *img = NULL; // The combined image
	i64 i, j;

	if(!pi_fg || !pi_fg->img) goto done;
	if(!pi_mask || !pi_mask->img) {
		de_bitmap_write_to_file(pi_fg->img, NULL, 0);
		goto done;
	}

	fg_img = pi_fg->img;
	mask_img = pi_mask->img;

	// Create a new image (which supports transparency).
	img = de_bitmap_create2(c, fg_img->unpadded_width, fg_img->width, fg_img->height,
		(fg_img->bytes_per_pixel<=2 ? 2 : 4));

	for(j=0; j<img->height; j++) {
		for(i=0; i<img->width; i++) {
			de_color clr;
			de_colorsample a;

			clr = de_bitmap_getpixel(fg_img, i, j);

			if(i>=fg_img->unpadded_width) {
				// Make all padding pixels opaque. (We don't preserve the mask's padding pixels.)
				a = 0;
			}
			else if(i<mask_img->unpadded_width && j<mask_img->height) {
				de_color clrm;
				i64 a1;

				clrm = de_bitmap_getpixel(mask_img, i, j);

				// Some masks have colors that are not quite grayscale.
				// Guess we'll use the average of the sample values.
				a1 = (i64)DE_COLOR_R(clrm) + (i64)DE_COLOR_G(clrm) + (i64)DE_COLOR_B(clrm);
				a = de_scale_n_to_255(255*3, a1);

				if(pi_mask->color_type==0 && pi_mask->bits_per_pixel==8) {
					a = 255-a;
				}
			}
			else {
				// Apparently, some masks are smaller than the image, and undefined
				// pixels should be transparent.
				a = 0xff;
			}

			de_bitmap_setpixel_rgba(img, i, j, DE_SET_ALPHA(clr, 255-a));
		}
	}
	de_bitmap_write_to_file(img, NULL, 0);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x5e3a2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x5e3a7
jmp 0x5e58b
cmpq $0x0, -0x20(%rbp)
je 0x5e3b9
movq -0x20(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0x5e3d1
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
jmp 0x5e58b
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movl 0x24(%rax), %r9d
movl $0x4, %r8d
movl $0x2, %eax
cmpl $0x2, %r9d
cmovlel %eax, %r8d
callq 0x16b7f0
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x5e57c
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jge 0x5e569
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x16b540
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jl 0x5e488
movb $0x0, -0x4d(%rbp)
jmp 0x5e52c
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x5e526
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x5e526
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x16b540
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %eax
movl -0x54(%rbp), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movzbl %cl, %ecx
addq %rcx, %rax
movl -0x54(%rbp), %ecx
andl $0xff, %ecx
movzbl %cl, %ecx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
movl $0x2fd, %edi            # imm = 0x2FD
callq 0x1617b0
movb %al, -0x4d(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x5e524
movq -0x20(%rbp), %rax
cmpq $0x8, 0x18(%rax)
jne 0x5e524
movzbl -0x4d(%rbp), %ecx
movl $0xff, %eax
subl %ecx, %eax
movb %al, -0x4d(%rbp)
jmp 0x5e52a
movb $-0x1, -0x4d(%rbp)
jmp 0x5e52c
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x4c(%rbp), %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movzbl -0x4d(%rbp), %r8d
movl $0xff, %eax
subl %r8d, %eax
shll $0x18, %eax
orl %eax, %ecx
callq 0x16b350
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x5e44b
jmp 0x5e56b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x5e431
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x38(%rbp), %rdi
callq 0x16af60
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)