// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %rdi, %r15
cmpl $0x0, 0x1c(%rsi)
je 0x7b54f
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xf28d4
movq %r15, %rdi
movq %rbx, %rsi
callq 0xea9b3
movq %r15, %rdi
movq %r12, %rsi
callq 0xe9752
movq %r15, %rdi
movq %r14, %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752
movq %rcx, %rbp
movq %rsi, %rbx
cmpq $0x0, 0x20(%rcx)
je 0x7b571
movq %rdx, %r13
cmpl $0x1, 0x58(%rdx)
movq 0x10(%rdx), %rax
jne 0x7b57a
cmpq $0x18, %rax
jne 0x7b50d
jmp 0x7b584
movl $0x1, 0x1c(%rbx)
jmp 0x7b50d
addq $-0x9, %rax
cmpq $-0x8, %rax
jb 0x7b50d
cmpl $0x0, 0x28(%rbx)
je 0x7b5e1
movq %r15, %rdi
callq 0xea9a9
movq %rax, %r14
leaq 0x9e185(%rip), %rdx     # 0x119721
movq %r15, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xeac7c
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 0xf30d9
addq $0x10, %rsp
movq %r15, %rdi
movq %r14, %rsi
callq 0xea9b3
movq (%r13), %rax
movq %rax, 0x28(%rsp)
movl %eax, %r14d
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xea259
movq %rax, %r12
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0xea259
movq %rax, 0x30(%rsp)
movl $0x3, %ecx
cmpb $0x0, 0x61(%r13)
je 0x7b699
cmpb $0x0, 0x22(%r13)
movq %rbp, 0x38(%rsp)
jne 0x7b634
cmpb $0x1, 0x21(%r13)
jne 0x7b636
incl %ecx
movq (%r13), %rsi
movq 0x8(%r13), %rdx
movq %r15, %rdi
callq 0xf2cab
cmpb $0x0, 0x60(%r13)
movq %rbx, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq %r15, 0x40(%rsp)
je 0x7b6d5
movq %rax, %rbp
movq 0x38(%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 0xf35b8
addq $0x10, %rsp
movq %rbp, %r15
jmp 0x7bad5
cmpb $0x0, 0x33(%rbx)
je 0x7b621
cmpl $0x0, 0xd0(%rbx)
je 0x7b621
cmpq $0x8, 0x60(%rbx)
jg 0x7b621
cmpb $0x0, 0x39(%rbx)
jne 0x7b621
cmpb $0x0, 0x3a(%rbx)
jne 0x7b621
movl $0x1, %ecx
jmp 0x7b621
cmpb $0x0, 0x61(%r13)
je 0x7b705
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rdi
movq 0x40(%r13), %rdx
movl $0x3, %ecx
xorl %esi, %esi
movq 0x10(%rsp), %r15
movq %r15, %r8
xorl %r9d, %r9d
callq 0xf38d9
jmp 0x7bad5
cmpq $0x0, 0x8(%r13)
movq 0x10(%rsp), %r15
jle 0x7bad5
xorl %r12d, %r12d
cmpq $0x0, 0x40(%r13)
jle 0x7b7fd
xorl %r15d, %r15d
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
cmpq $0x0, 0x18(%r13)
jle 0x7b7ab
xorl %ebp, %ebp
movq 0x38(%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 0xef0d5
movl $0x1, %edx
movl %ebp, %ecx
shll %cl, %edx
movzbl %al, %eax
movl $0x7, %ecx
xorl %esi, %esi
btl %ecx, %eax
jae 0x7b797
cmpq 0x10(%r13), %rbp
jge 0x7b792
orl %edx, 0x50(%rsp,%rsi,4)
jmp 0x7b797
movb $0x1, 0x48(%rsp,%rsi)
incq %rsi
decl %ecx
cmpq $0x8, %rsi
jne 0x7b781
incq %rbp
cmpq 0x18(%r13), %rbp
jl 0x7b74a
movl %r15d, %eax
shrl $0x3, %eax
shlq $0x5, %rax
addq 0x18(%rsp), %rax
movl %r15d, %ecx
andl $-0x8, %ecx
movq 0x30(%rsp), %rdx
addq %rcx, %rdx
xorl %esi, %esi
leaq (%rcx,%rsi), %rdi
cmpq %r14, %rdi
jae 0x7b7e3
movl 0x50(%rsp,%rsi,4), %edi
movl %edi, (%rax,%rsi,4)
movb 0x48(%rsp,%rsi), %dil
movb %dil, (%rdx,%rsi)
incq %rsi
cmpq $0x8, %rsi
jne 0x7b7ca
incq %rbx
addq $0x8, %r15
cmpq 0x40(%r13), %rbx
jl 0x7b728
cmpl $0x1, 0x58(%r13)
jne 0x7b855
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r15
je 0x7ba7e
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 0xf2afd
incq %rbp
cmpq %rbp, %r14
jne 0x7b81b
jmp 0x7ba7e
movq 0x20(%rsp), %rax
cmpb $0x0, 0x39(%rax)
je 0x7b953
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r15
je 0x7ba7e
movq 0x20(%rsp), %rax
movl 0x4d4(%rax), %eax
movl %eax, %edx
shrl $0x8, %edx
movl %edx, 0xc(%rsp)
movl %eax, %ebx
shrl $0x10, %eax
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
movl (%rcx,%rbp,4), %eax
movl %eax, %ecx
shrl $0x4, %ecx
andl $0x3, %ecx
leaq 0xacf96(%rip), %rdx     # 0x128840
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x20(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movl %ecx, %esi
shrl $0x10, %esi
movl %ecx, %edx
shrl $0x8, %edx
jmp 0x7b906
andb $0xf, %al
movzbl %al, %eax
movl %eax, %esi
shll $0x4, %esi
addl %eax, %esi
movl %ebx, %ecx
movl 0xc(%rsp), %edx
jmp 0x7b906
andb $0xf, %al
movzbl %al, %eax
movl %eax, %edx
shll $0x4, %edx
addl %eax, %edx
movl %ebx, %ecx
jmp 0x7b902
andb $0xf, %al
movzbl %al, %eax
movl %eax, %ecx
shll $0x4, %ecx
addl %eax, %ecx
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %esi
movl %esi, 0x8(%rsp)
movzbl %sil, %eax
shll $0x10, %eax
movl %edx, 0xc(%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 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2afd
incq %rbp
cmpq %rbp, %r14
movq 0x18(%rsp), %rcx
jne 0x7b898
jmp 0x7ba7e
cmpb $0x0, 0x3a(%rax)
je 0x7ba45
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %r15
je 0x7ba7e
movq 0x20(%rsp), %rax
movl 0x4d4(%rax), %eax
movl %eax, %edx
shrl $0x8, %edx
movl %edx, 0xc(%rsp)
movl %eax, %ebx
shrl $0x10, %eax
movl %eax, 0x8(%rsp)
xorl %ebp, %ebp
movl (%rcx,%rbp,4), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3, %ecx
leaq 0xacead(%rip), %rdx     # 0x128850
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movzbl %al, %eax
movq 0x20(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movl %ecx, %eax
shrl $0x10, %eax
movl %ecx, %edx
shrl $0x8, %edx
jmp 0x7b9fc
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %eax
movl %ebx, %ecx
movl 0xc(%rsp), %edx
jmp 0x7b9fc
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %edx
movl %ebx, %ecx
jmp 0x7b9f8
movl %eax, %ecx
andl $0x30, %ecx
shrl $0x4, %ecx
leal (%rcx,%rax,4), %ecx
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
movzbl %al, %eax
shll $0x10, %eax
movl %edx, 0xc(%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 0x10(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2afd
incq %rbp
cmpq %rbp, %r14
movq 0x18(%rsp), %rcx
jne 0x7b991
jmp 0x7ba7e
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %r15
je 0x7ba7e
xorl %ebp, %ebp
movzbl (%rbx,%rbp,4), %eax
movq 0x20(%rsp), %rcx
movl 0x4d4(%rcx,%rax,4), %ecx
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2afd
incq %rbp
cmpq %rbp, %r14
jne 0x7ba58
testq %r14, %r14
sete %al
cmpb $0x1, 0x21(%r13)
setne %cl
orb %al, %cl
movq 0x30(%rsp), %rbx
jne 0x7bac8
xorl %ebp, %ebp
cmpb $0x0, (%rbx,%rbp)
jne 0x7bac0
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xf2bd3
andl $0xffffff, %eax         # imm = 0xFFFFFF
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %eax, %ecx
callq 0xf2b02
incq %rbp
cmpq %rbp, %r14
jne 0x7ba97
incq %r12
cmpq 0x8(%r13), %r12
jl 0x7b718
movq 0x20(%rsp), %r14
movzbl 0x2c(%r14), %eax
testl %eax, %eax
je 0x7bb05
cmpl $0x2, %eax
movq 0x40(%rsp), %r15
jne 0x7bb12
cmpb $0x1, 0x21(%r13)
jne 0x7bb12
movq 0x10(%rsp), %rdi
movl $0x6, %esi
callq 0xf3f11
jmp 0x7bb12
movq %r15, %rdi
callq 0xf3e98
movq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0xea9a9
movq %rax, %rbx
cmpl $0x0, 0x5c(%r13)
movq 0x18(%rsp), %r12
je 0x7bb43
leaq 0xaf316(%rip), %rdx     # 0x12ae46
movq %r15, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
movl $0x5, %r8d
callq 0xeac7c
movq 0x70(%r14), %rcx
testq %rcx, %rcx
jle 0x7bb56
cmpq $0x0, 0x78(%r14)
setle %dl
jmp 0x7bb58
movb $0x1, %dl
movl 0x5c(%r13), %eax
testl %eax, %eax
je 0x7bb86
testb %dl, %dl
jne 0x7bbc4
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movl $0x1, 0x78(%rbx)
movsd %xmm0, 0x88(%rbx)
movq 0x78(%r14), %rsi
movl $0x80, %ecx
jmp 0x7bbb7
movq 0x80(%r14), %rdi
testq %rdi, %rdi
jle 0x7bb60
movq 0x88(%r14), %rsi
testq %rsi, %rsi
jle 0x7bb60
movl $0x2, 0x78(%rbx)
cvtsi2sd %rdi, %xmm0
movsd %xmm0, 0x80(%rbx)
movl $0x88, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
movsd %xmm0, (%rbx,%rcx)
testl %eax, %eax
jne 0x7bbf0
cmpb $0x0, 0xa0(%r14)
je 0x7bbf0
movb $0x1, 0xe(%rbx)
movl 0xa4(%r14), %ecx
movl %ecx, 0x98(%rbx)
movl 0xa8(%r14), %ecx
movl %ecx, 0x9c(%rbx)
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal 0x2(%rcx), %edx
cmpl $0x0, (%r14)
cmovnel %ecx, %edx
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf2917
movq 0x30(%rsp), %r14
jmp 0x7b518