// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ilbm.c
// Function: write_frame
static void write_frame(deark *c, lctx *d, struct imgbody_info *ibi, struct frame_ctx *frctx)
{
	de_bitmap *img = NULL;
	i64 j;
	u32 *rowbuf = NULL; // The current row of pixel (palette or RGB) values
	u8 *rowbuf_trns = NULL; // The current row's 1-bit transparency mask values
	UI rowbuf_size;
	int bypp;
	de_finfo *fi = NULL;
	UI createflags = 0;
	u32 pixelval[8];
	u8 pixeltrnsval[8];

	if(d->errflag) goto done;
	if(!frctx) goto done;
	if(!frctx->frame_buffer) {
		d->errflag = 1;
		goto done;
	}
	if(ibi->colortype==COLORTYPE_RGB24) {
		if(ibi->planes_fg!=24) goto done;
	}
	else if(ibi->planes_fg<1 || ibi->planes_fg>8) {
		goto done;
	}

	if(d->debug_frame_buffer) {
		de_finfo *fi_fb;

		fi_fb = de_finfo_create(c);
		de_finfo_set_name_from_sz(c, fi_fb, "fb", 0, DE_ENCODING_LATIN1);
		de_convert_and_write_image_bilevel(frctx->frame_buffer, 0,
			ibi->bits_per_row_per_plane * ibi->planes_total,
			ibi->height, ibi->frame_buffer_rowspan, 0, fi_fb, 0);
		de_finfo_destroy(c, fi_fb);
	}

	rowbuf_size = (UI)ibi->width;
	rowbuf = de_mallocarray(c, rowbuf_size, sizeof(rowbuf[0]));
	rowbuf_trns = de_mallocarray(c, rowbuf_size, sizeof(rowbuf_trns[0]));

	if(ibi->is_rgb24) {
		bypp = 3;
	}
	else if(d->found_cmap && d->pal_is_grayscale && d->planes_raw<=8 && !d->is_ham6 && !d->is_ham8) {
		bypp = 1;
	}
	else {
		bypp = 3;
	}

	if(ibi->use_colorkey_transparency || ibi->masking_code==MASKINGTYPE_1BITMASK) {
		bypp++;
	}

	img = de_bitmap_create(c, ibi->width, ibi->height, bypp);

	if(ibi->is_pbm) {
		de_convert_image_paletted(frctx->frame_buffer, 0, 8, ibi->frame_buffer_rowspan,
			d->pal, img, 0);
		goto after_render;
	}
	else if(ibi->is_rgb24) {
		de_convert_image_rgb(frctx->frame_buffer, 0, ibi->bytes_per_row_per_plane, 3,
			img, 0);
		goto after_render;
	}

	for(j=0; j<ibi->height; j++) {
		i64 z;
		i64 plane;
		UI k;

		// Process 8 pixels at a time
		for(z=0; z<ibi->bytes_per_row_per_plane; z++) {
			de_zeromem(pixelval, sizeof(pixelval));
			de_zeromem(pixeltrnsval, sizeof(pixeltrnsval));

			// Read the zth byte in each plane
			for(plane=0; plane<ibi->planes_total; plane++) {
				u8 b;

				b = dbuf_getbyte(frctx->frame_buffer,
					j*ibi->frame_buffer_rowspan +
					plane*ibi->bytes_per_row_per_plane + z);

				for(k=0; k<8; k++) {
					if(b & (1U<<(7-k))) {
						if(plane < ibi->planes_fg) {
							pixelval[k] |= 1U<<(UI)plane;
						}
						else {
							// The only way this can happen is if this plane is a
							// 1-bit transparency mask.
							pixeltrnsval[k] = 1;
						}
					}
				}
			}

			for(k=0; k<8; k++) {
				UI idx;

				idx = (UI)z*8+k;
				if(idx < rowbuf_size) {
					rowbuf[idx] = pixelval[k];
					rowbuf_trns[idx] = pixeltrnsval[k];
				}
			}
		}

		if(ibi->colortype==COLORTYPE_RGB24) {
			render_pixel_row_rgb24(c, d, ibi, j, rowbuf, rowbuf_size, img);
		}
		else if(d->is_ham6) {
			render_pixel_row_ham6(c, d, j, rowbuf, rowbuf_size, img);
		}
		else if(d->is_ham8) {
			render_pixel_row_ham8(c, d, j, rowbuf, rowbuf_size, img);
		}
		else {
			render_pixel_row_normal(c, d, ibi, j, rowbuf, rowbuf_size, img);
		}

		// Handle 1-bit transparency masks here, for all color types.
		if(ibi->masking_code==MASKINGTYPE_1BITMASK) {
			i64 i;

			for(i=0; i<rowbuf_size; i++) {
				u32 clr;

				if(rowbuf_trns[i]==0) {
					clr = de_bitmap_getpixel(img, i, j);
					clr = DE_SET_ALPHA(clr, 0);
					de_bitmap_setpixel_rgba(img, i, j, clr);
				}
			}
		}
	}

after_render:
	if(d->trans_setting==TRANS_AUTO) {
		if(ibi->masking_code==MASKINGTYPE_1BITMASK) {
			de_bitmap_optimize_alpha(img, 0x2|0x4);
		}
	}
	else if(d->trans_setting==TRANS_REMOVE) {
		de_bitmap_remove_alpha(img);
	}

	fi = de_finfo_create(c);
	set_finfo_data(c, d, ibi, fi);
	if(ibi->is_thumb) {
		createflags |= DE_CREATEFLAG_IS_AUX;
	}
	if(!d->is_anim) {
		// TODO: Enable this for ANIM?
		createflags |= DE_CREATEFLAG_OPT_IMAGE;
	}

	de_bitmap_write_to_file_finfo(img, fi, createflags);

done:
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	de_free(c, rowbuf);
	de_free(c, rowbuf_trns);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r15
cmpl $0x0, 0x1c(%rsi)
je 0x776a9
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xe7084
movq %r15, %rdi
movq %rbx, %rsi
callq 0xdfd24
movq %r15, %rdi
movq %r12, %rsi
callq 0xded32
movq %r15, %rdi
movq %r14, %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32
movq %rcx, %rbx
movq %rsi, %r12
cmpq $0x0, 0x20(%rcx)
je 0x776cb
movq %rdx, %r13
cmpl $0x1, 0x58(%rdx)
movq 0x10(%rdx), %rax
jne 0x776d6
cmpq $0x18, %rax
jne 0x77664
jmp 0x776e0
movl $0x1, 0x1c(%r12)
jmp 0x77664
addq $-0x9, %rax
cmpq $-0x8, %rax
jb 0x77664
cmpl $0x0, 0x28(%r12)
je 0x7773e
movq %r15, %rdi
callq 0xdfd1a
movq %rax, %r14
leaq 0x95057(%rip), %rdx     # 0x10c751
pushq $0x5
popq %r8
movq %r15, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0xdffbd
movq 0x20(%rbx), %rdi
movq 0x8(%r13), %rcx
movq 0x18(%r13), %rdx
imulq 0x38(%r13), %rdx
movq 0x48(%r13), %r8
andl $0x0, 0x8(%rsp)
movq %r14, (%rsp)
xorl %esi, %esi
xorl %r9d, %r9d
callq 0xe7804
movq %r15, %rdi
movq %r14, %rsi
callq 0xdfd24
movl (%r13), %r14d
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0xdf66f
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0xdf66f
movq %rax, 0x38(%rsp)
pushq $0x3
popq %rax
cmpb $0x0, 0x61(%r13)
je 0x777c8
movl %eax, %ecx
cmpb $0x0, 0x22(%r13)
jne 0x77782
cmpb $0x1, 0x21(%r13)
jne 0x77784
incl %ecx
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %r15, %rdi
callq 0xe7437
cmpb $0x0, 0x60(%r13)
movq %rax, 0x18(%rsp)
movq %r15, 0x40(%rsp)
je 0x777ff
movq 0x20(%rbx), %rdi
movq 0x48(%r13), %rcx
leaq 0x4d4(%r12), %r8
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
xorl %esi, %esi
movq %rax, %r9
callq 0xe7cbc
jmp 0x77824
cmpb $0x0, 0x33(%r12)
je 0x77772
cmpl $0x0, 0xd0(%r12)
je 0x77772
cmpq $0x8, 0x60(%r12)
jg 0x77772
cmpb $0x0, 0x39(%r12)
jne 0x77772
cmpb $0x0, 0x3a(%r12)
pushq $0x1
popq %rcx
jne 0x77772
jmp 0x77774
cmpb $0x0, 0x61(%r13)
je 0x7798a
movq 0x20(%rbx), %rdi
movq 0x40(%r13), %rdx
pushq $0x3
popq %rcx
xorl %esi, %esi
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0xe7f67
movzbl 0x2c(%r12), %eax
testl %eax, %eax
je 0x7784e
cmpl $0x2, %eax
movq 0x40(%rsp), %r15
jne 0x7785d
cmpb $0x1, 0x21(%r13)
jne 0x7785d
pushq $0x6
popq %rsi
movq 0x18(%rsp), %rdi
callq 0xe84da
jmp 0x7785d
movq 0x18(%rsp), %rdi
callq 0xe8472
movq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0xdfd1a
movq %rax, %rbx
cmpl $0x0, 0x5c(%r13)
je 0x77887
leaq 0xa6630(%rip), %rdx     # 0x11dea6
pushq $0x5
popq %r8
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0xdffbd
movq 0x70(%r12), %rcx
testq %rcx, %rcx
jle 0x7789c
cmpq $0x0, 0x78(%r12)
setle %dl
jmp 0x7789e
movb $0x1, %dl
movl 0x5c(%r13), %eax
testl %eax, %eax
je 0x778d7
testb %dl, %dl
jne 0x778cd
movl $0x1, 0x78(%rbx)
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x88(%rbx)
cvtsi2sdq 0x78(%r12), %xmm0
movsd %xmm0, 0x80(%rbx)
testl %eax, %eax
jne 0x77959
jmp 0x7793b
movq 0x80(%r12), %rsi
testq %rsi, %rsi
jle 0x778a6
movq 0x88(%r12), %rdi
testq %rdi, %rdi
jle 0x778a6
movq %rdi, %xmm0
movq %rsi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0x92dd9(%rip), %xmm0    # 0x10a6e0
pand %xmm1, %xmm0
por 0x92ddd(%rip), %xmm0    # 0x10a6f0
movl $0x2, 0x78(%rbx)
psrlq $0x20, %xmm1
por 0x92dd9(%rip), %xmm1    # 0x10a700
subpd 0x92de1(%rip), %xmm1    # 0x10a710
addpd %xmm0, %xmm1
movupd %xmm1, 0x80(%rbx)
cmpb $0x0, 0xa0(%r12)
je 0x77959
movb $0x1, 0xe(%rbx)
movq 0xa4(%r12), %rcx
movq %rcx, 0x98(%rbx)
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal 0x2(%rcx), %edx
cmpl $0x0, (%r12)
cmovnel %ecx, %edx
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0xe70ab
movq 0x38(%rsp), %r14
movq 0x20(%rsp), %r12
jmp 0x7766f
xorl %r15d, %r15d
movq %rbx, 0x58(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
cmpq 0x8(%r13), %r15
jge 0x77824
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq 0x40(%r13), %r12
jge 0x77a86
movl %r14d, %ebx
shrl $0x3, %ebx
movq %r14, 0x50(%rsp)
andl $-0x8, %r14d
shlq $0x5, %rbx
addq 0x20(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
andq $0x0, 0x48(%rsp)
xorl %ebp, %ebp
cmpq 0x18(%r13), %rbp
jge 0x77a44
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x48(%r13), %rsi
imulq %r15, %rsi
movq 0x40(%r13), %rax
imulq %rbp, %rax
addq %r12, %rsi
addq %rax, %rsi
callq 0xe3eb4
pushq $0x1
popq %rdx
movl %ebp, %ecx
shll %cl, %edx
movzbl %al, %eax
pushq $0x7
popq %rcx
xorl %esi, %esi
cmpq $0x8, %rsi
je 0x77a3f
btl %ecx, %eax
jae 0x77a38
cmpq 0x10(%r13), %rbp
jge 0x77a33
orl %edx, 0x60(%rsp,%rsi,4)
jmp 0x77a38
movb $0x1, 0x48(%rsp,%rsi)
incq %rsi
decl %ecx
jmp 0x77a1c
incq %rbp
jmp 0x779e3
movq 0x38(%rsp), %rax
addq %r14, %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %rsi
cmpq $0x8, %rcx
je 0x77a75
leaq (%r14,%rcx), %rdx
cmpq %rsi, %rdx
jae 0x77a70
movl 0x60(%rsp,%rcx,4), %edx
movl %edx, (%rbx,%rcx,4)
movb 0x48(%rsp,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
jmp 0x77a53
incq %r12
movq 0x50(%rsp), %r14
addq $0x8, %r14
jmp 0x779ac
cmpl $0x1, 0x58(%r13)
jne 0x77ad3
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x77c8a
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %ecx
movl %ecx, %eax
andl $0xff00, %eax           # imm = 0xFF00
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
shll $0x10, %ecx
orl %eax, %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe7287
incq %rbp
jmp 0x77a8f
movq 0x30(%rsp), %rax
cmpb $0x0, 0x39(%rax)
je 0x77b93
movl 0x4d4(%rax), %ebx
movl %ebx, %r14d
shrl $0x10, %r14d
movl %ebx, %r12d
shrl $0x8, %r12d
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x77c8a
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0x3, %ecx
leaq 0xa3d26(%rip), %rdx     # 0x11b840
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x30(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ebx
movl %ebx, %r14d
shrl $0x10, %r14d
movl %ebx, %r12d
shrl $0x8, %r12d
jmp 0x77b60
andb $0xf, %al
movzbl %al, %eax
imull $0x11, %eax, %r14d
jmp 0x77b60
andb $0xf, %al
movzbl %al, %eax
imull $0x11, %eax, %r12d
jmp 0x77b60
andb $0xf, %al
movzbl %al, %eax
imull $0x11, %eax, %ebx
movzbl %r14b, %eax
shll $0x10, %eax
movzbl %r12b, %edx
shll $0x8, %edx
orl %eax, %edx
movzbl %bl, %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe7287
incq %rbp
jmp 0x77af8
cmpb $0x0, 0x3a(%rax)
je 0x77c57
movl 0x4d4(%rax), %r14d
movl %r14d, %r12d
shrl $0x10, %r12d
movl %r14d, %ebx
shrl $0x8, %ebx
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x77c8a
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3, %ecx
leaq 0xa3c7b(%rip), %rdx     # 0x11b850
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x30(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %r14d
movl %r14d, %r12d
shrl $0x10, %r12d
movl %r14d, %ebx
shrl $0x8, %ebx
jmp 0x77c24
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %r12d
jmp 0x77c24
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %ebx
jmp 0x77c24
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %r14d
movzbl %r12b, %eax
shll $0x10, %eax
movzbl %bl, %edx
shll $0x8, %edx
orl %eax, %edx
movzbl %r14b, %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe7287
incq %rbp
jmp 0x77bb3
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x77c8a
movq 0x20(%rsp), %rax
movzbl (%rax,%rbp,4), %eax
movq 0x30(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe7287
incq %rbp
jmp 0x77c59
cmpb $0x1, 0x21(%r13)
movq 0x38(%rsp), %rbx
movq 0x18(%rsp), %r14
jne 0x77cd2
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x77cd2
cmpb $0x0, (%rbx,%rbp)
jne 0x77ccd
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe735f
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movl %eax, %ecx
callq 0xe728c
incq %rbp
jmp 0x77c9d
incq %r15
movq 0x30(%rsp), %r12
jmp 0x7799c