// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %r15
cmpl $0x0, 0x1c(%rsi)
je 0x7b1e9
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xf228d
movq %r15, %rdi
movq %rbx, %rsi
callq 0xea50e
movq %r15, %rdi
movq %r12, %rsi
callq 0xe8be2
movq %r15, %rdi
movq %r14, %rsi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2
movq %rcx, %rbp
movq %rsi, %rbx
cmpq $0x0, 0x20(%rcx)
je 0x7b20b
movq %rdx, %r13
cmpl $0x1, 0x58(%rdx)
movq 0x10(%rdx), %rax
jne 0x7b214
cmpq $0x18, %rax
jne 0x7b1a7
jmp 0x7b21e
movl $0x1, 0x1c(%rbx)
jmp 0x7b1a7
addq $-0x9, %rax
cmpq $-0x8, %rax
jb 0x7b1a7
cmpl $0x0, 0x28(%rbx)
je 0x7b27b
movq %r15, %rdi
callq 0xea504
movq %rax, %r14
leaq 0x9e60b(%rip), %rdx     # 0x119841
movq %r15, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xea78f
movq 0x20(%rbp), %rdi
movq 0x8(%r13), %rcx
movq 0x18(%r13), %rdx
imulq 0x38(%r13), %rdx
movq 0x48(%r13), %r8
xorl %esi, %esi
xorl %r9d, %r9d
pushq $0x0
pushq %r14
callq 0xf2a98
addq $0x10, %rsp
movq %r15, %rdi
movq %r14, %rsi
callq 0xea50e
movl (%r13), %r14d
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xe96f7
movq %rax, %r12
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xe96f7
movq %rax, 0x20(%rsp)
movl $0x3, %ecx
cmpb $0x0, 0x61(%r13)
je 0x7b32b
cmpb $0x0, 0x22(%r13)
movq %rbp, 0x28(%rsp)
jne 0x7b2c6
cmpb $0x1, 0x21(%r13)
jne 0x7b2c8
incl %ecx
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %r15, %rdi
callq 0xf266a
cmpb $0x0, 0x60(%r13)
movq %rbx, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq %r15, 0x30(%rsp)
je 0x7b367
movq %rax, %rbp
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x48(%r13), %rcx
leaq 0x4d4(%rbx), %r8
subq $0x8, %rsp
movl $0x8, %edx
xorl %esi, %esi
movq %rbp, %r9
pushq $0x0
callq 0xf2f79
addq $0x10, %rsp
movq %rbp, %r15
jmp 0x7b759
cmpb $0x0, 0x33(%rbx)
je 0x7b2b3
cmpl $0x0, 0xd0(%rbx)
je 0x7b2b3
cmpq $0x8, 0x60(%rbx)
jg 0x7b2b3
cmpb $0x0, 0x39(%rbx)
jne 0x7b2b3
cmpb $0x0, 0x3a(%rbx)
jne 0x7b2b3
movl $0x1, %ecx
jmp 0x7b2b3
cmpb $0x0, 0x61(%r13)
je 0x7b397
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x40(%r13), %rdx
movl $0x3, %ecx
xorl %esi, %esi
movq 0x8(%rsp), %r15
movq %r15, %r8
xorl %r9d, %r9d
callq 0xf3279
jmp 0x7b759
cmpq $0x0, 0x8(%r13)
movq 0x8(%rsp), %r15
jle 0x7b759
xorl %r12d, %r12d
cmpq $0x0, 0x40(%r13)
jle 0x7b48f
xorl %r15d, %r15d
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
cmpq $0x0, 0x18(%r13)
jle 0x7b43d
xorl %ebp, %ebp
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x48(%r13), %rsi
imulq %r12, %rsi
movq 0x40(%r13), %rax
imulq %rbp, %rax
addq %rbx, %rsi
addq %rax, %rsi
callq 0xeead3
movl $0x1, %edx
movl %ebp, %ecx
shll %cl, %edx
movzbl %al, %eax
movl $0x7, %ecx
xorl %esi, %esi
btl %ecx, %eax
jae 0x7b429
cmpq 0x10(%r13), %rbp
jge 0x7b424
orl %edx, 0x40(%rsp,%rsi,4)
jmp 0x7b429
movb $0x1, 0x38(%rsp,%rsi)
incq %rsi
decl %ecx
cmpq $0x8, %rsi
jne 0x7b413
incq %rbp
cmpq 0x18(%r13), %rbp
jl 0x7b3dc
movl %r15d, %eax
shrl $0x3, %eax
shlq $0x5, %rax
addq 0x10(%rsp), %rax
movl %r15d, %ecx
andl $-0x8, %ecx
movq 0x20(%rsp), %rdx
addq %rcx, %rdx
xorl %esi, %esi
leaq (%rcx,%rsi), %rdi
cmpq %r14, %rdi
jae 0x7b475
movl 0x40(%rsp,%rsi,4), %edi
movl %edi, (%rax,%rsi,4)
movb 0x38(%rsp,%rsi), %dil
movb %dil, (%rdx,%rsi)
incq %rsi
cmpq $0x8, %rsi
jne 0x7b45c
incq %rbx
addq $0x8, %r15
cmpq 0x40(%r13), %rbx
jl 0x7b3ba
cmpl $0x1, 0x58(%r13)
jne 0x7b4e5
testq %r14, %r14
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r15
je 0x7b702
xorl %ebp, %ebp
movl (%rbx,%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 %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbp
cmpq %rbp, %r14
jne 0x7b4ab
jmp 0x7b702
movq 0x18(%rsp), %rax
cmpb $0x0, 0x39(%rax)
je 0x7b5de
testq %r14, %r14
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r15
je 0x7b702
movq 0x18(%rsp), %rax
movl 0x4d4(%rax), %eax
movl %eax, %edx
shrl $0x8, %edx
movl %edx, 0x4(%rsp)
movl %eax, %ebx
shrl $0x10, %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
movl (%rcx,%rbp,4), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0x3, %ecx
leaq 0xad539(%rip), %rdx     # 0x128a70
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x18(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movl %ecx, %esi
shrl $0x10, %esi
movl %ecx, %edx
shrl $0x8, %edx
jmp 0x7b592
andb $0xf, %al
movzbl %al, %eax
movl %eax, %esi
shll $0x4, %esi
addl %eax, %esi
movl %ebx, %ecx
movl 0x4(%rsp), %edx
jmp 0x7b592
andb $0xf, %al
movzbl %al, %eax
movl %eax, %edx
shll $0x4, %edx
addl %eax, %edx
movl %ebx, %ecx
jmp 0x7b58f
andb $0xf, %al
movzbl %al, %eax
movl %eax, %ecx
shll $0x4, %ecx
addl %eax, %ecx
movl 0x4(%rsp), %edx
movl (%rsp), %esi
movl %esi, (%rsp)
movzbl %sil, %eax
shll $0x10, %eax
movl %edx, 0x4(%rsp)
movzbl %dl, %edx
shll $0x8, %edx
orl %eax, %edx
movl %ecx, %ebx
movzbl %cl, %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbp
cmpq %rbp, %r14
movq 0x10(%rsp), %rcx
jne 0x7b525
jmp 0x7b702
cmpb $0x0, 0x3a(%rax)
je 0x7b6cb
testq %r14, %r14
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r15
je 0x7b702
movq 0x18(%rsp), %rax
movl 0x4d4(%rax), %eax
movl %eax, %edx
shrl $0x8, %edx
movl %edx, 0x4(%rsp)
movl %eax, %ebx
shrl $0x10, %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
movl (%rcx,%rbp,4), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3, %ecx
leaq 0xad455(%rip), %rdx     # 0x128a80
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x18(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movl %ecx, %eax
shrl $0x10, %eax
movl %ecx, %edx
shrl $0x8, %edx
jmp 0x7b683
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %eax
movl %ebx, %ecx
movl 0x4(%rsp), %edx
jmp 0x7b683
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %edx
movl %ebx, %ecx
jmp 0x7b680
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %ecx
movl 0x4(%rsp), %edx
movl (%rsp), %eax
movl %eax, (%rsp)
movzbl %al, %eax
shll $0x10, %eax
movl %edx, 0x4(%rsp)
movzbl %dl, %edx
shll $0x8, %edx
orl %eax, %edx
movl %ecx, %ebx
movzbl %cl, %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbp
cmpq %rbp, %r14
movq 0x10(%rsp), %rcx
jne 0x7b619
jmp 0x7b702
testq %r14, %r14
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r15
je 0x7b702
xorl %ebp, %ebp
movzbl (%rbx,%rbp,4), %eax
movq 0x18(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %rbp
cmpq %rbp, %r14
jne 0x7b6dc
testq %r14, %r14
sete %al
cmpb $0x1, 0x21(%r13)
setne %cl
orb %al, %cl
movq 0x20(%rsp), %rbx
jne 0x7b74c
xorl %ebp, %ebp
cmpb $0x0, (%rbx,%rbp)
jne 0x7b744
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2592
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %eax, %ecx
callq 0xf24bf
incq %rbp
cmpq %rbp, %r14
jne 0x7b71b
incq %r12
cmpq 0x8(%r13), %r12
jl 0x7b3aa
movq 0x18(%rsp), %r14
movzbl 0x2c(%r14), %eax
testl %eax, %eax
je 0x7b789
cmpl $0x2, %eax
movq 0x30(%rsp), %r15
jne 0x7b796
cmpb $0x1, 0x21(%r13)
jne 0x7b796
movq 0x8(%rsp), %rdi
movl $0x6, %esi
callq 0xf38c4
jmp 0x7b796
movq %r15, %rdi
callq 0xf383e
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0xea504
movq %rax, %rbx
cmpl $0x0, 0x5c(%r13)
movq 0x10(%rsp), %r12
je 0x7b7c7
leaq 0xaf962(%rip), %rdx     # 0x12b116
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xea78f
movq 0x70(%r14), %rcx
testq %rcx, %rcx
jle 0x7b7da
cmpq $0x0, 0x78(%r14)
setle %dl
jmp 0x7b7dc
movb $0x1, %dl
movl 0x5c(%r13), %eax
testl %eax, %eax
je 0x7b817
testb %dl, %dl
jne 0x7b80d
movl $0x1, 0x78(%rbx)
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x88(%rbx)
xorps %xmm0, %xmm0
cvtsi2sdq 0x78(%r14), %xmm0
movsd %xmm0, 0x80(%rbx)
testl %eax, %eax
jne 0x7b895
jmp 0x7b879
movq 0x80(%r14), %rsi
testq %rsi, %rsi
jle 0x7b7e4
movq 0x88(%r14), %rdi
testq %rdi, %rdi
jle 0x7b7e4
movq %rdi, %xmm0
movq %rsi, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0x9bf2b(%rip), %xmm0    # 0x117770
pand %xmm1, %xmm0
por 0x9bf2f(%rip), %xmm0    # 0x117780
movl $0x2, 0x78(%rbx)
psrlq $0x20, %xmm1
por 0x9bf2b(%rip), %xmm1    # 0x117790
subpd 0x9bf33(%rip), %xmm1    # 0x1177a0
addpd %xmm0, %xmm1
movupd %xmm1, 0x80(%rbx)
cmpb $0x0, 0xa0(%r14)
je 0x7b895
movb $0x1, 0xe(%rbx)
movq 0xa4(%r14), %rcx
movq %rcx, 0x98(%rbx)
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal 0x2(%rcx), %edx
cmpl $0x0, (%r14)
cmovnel %ecx, %edx
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf22d0
movq 0x20(%rsp), %r14
jmp 0x7b1b2