// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/deark-bitmap.c
// Function: de_bitmap16_write_to_file_finfo
void de_bitmap16_write_to_file_finfo(de_bitmap *img, de_bitmap *imglo,
	de_finfo *fi, unsigned int createflags)
{
	deark *c;
	struct image_scan_opt_data optctx;
	struct de_write_image_params wp;

	if(!img) return;
	c = img->c;
	if(img->invalid_image_flag) return;
	de_zeromem(&optctx, sizeof(struct image_scan_opt_data));

	if(!img->bitmap) de_bitmap_alloc_pixels(img);
	if(imglo) {
		if(!imglo->bitmap) de_bitmap_alloc_pixels(imglo);
		if(!valid_imglo(img, imglo)) return;
	}

	de_zeromem(&wp, sizeof(struct de_write_image_params));
	wp.createflags = createflags;

	if(imglo && (createflags & DE_CREATEFLAG_OPT_IMAGE)) {
		// If the high and low bytes are the same in every sample, we don't need
		// the low byte.
		if(!bitmap16_low_bits_important(img, imglo)) {
			imglo = NULL;
		}
	}

	if(imglo) {
		; // The routines below don't support 16-bit images
	}
	else if(createflags & DE_CREATEFLAG_IS_BWIMG) {
		// The BWIMG flag/optimization has to be handled in a different way than the
		// other optimizations, because our de_bitmap object does not support a
		// 1 bit/pixel image type.
		wp.flags2 |= 0x1;
	}
	else if(createflags & DE_CREATEFLAG_OPT_IMAGE) {
		// This should probably be the default, but our optimization routine
		// isn't very efficient, and wouldn't change anything in most cases.
		get_optimized_image(img, &optctx);
		if(optctx.optimg) {
			de_dbg3(c, "reducing image depth (%d->%d)", img->bytes_per_pixel,
				optctx.optimg->bytes_per_pixel);
		}
		if(!optctx.is_nonbilevel) {
			de_dbg3(c, "reducing to bilevel (from %d samples)", img->bytes_per_pixel);
			wp.flags2 |= 0x1;
		}
	}

	if(fi && fi->linear_colorpace) {
		wp.flags2 |= 0x2;
	}

	wp.f = dbuf_create_output_file(c, "png", fi, createflags);
	if(optctx.optimg) {
		wp.img = optctx.optimg;
	}
	else {
		wp.img = img;
		wp.imglo = imglo;
	}
	de_write_png(c, &wp);
	dbuf_close(wp.f);

	if(optctx.optimg) de_bitmap_destroy(optctx.optimg);
}

// Assembly:
testq %rdi, %rdi
je 0xf1f44
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x20(%rdi)
je 0xf1f45
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r12
movq (%rbx), %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
cmpq $0x0, 0x28(%rbx)
jne 0xf1f67
movq %rbx, %rdi
callq 0xf216c
testq %r12, %r12
je 0xf1fce
cmpq $0x0, 0x28(%r12)
jne 0xf1f7c
movq %r12, %rdi
callq 0xf216c
cmpl $0x0, 0x20(%r12)
jne 0xf1f36
movl 0x24(%rbx), %eax
cmpl 0x24(%r12), %eax
jne 0xf1f36
movq 0x8(%rbx), %rax
movq %rax, 0x18(%rsp)
cmpq 0x8(%r12), %rax
jne 0xf1f36
movq 0x10(%rbx), %rcx
cmpq 0x10(%r12), %rcx
jne 0xf1f36
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %ebp, 0x38(%rsp)
movl $0x2, %eax
testb $0x2, %bpl
jne 0xf20e6
xorl %r14d, %r14d
jmp 0xf200c
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl %ebp, 0x38(%rsp)
testb $0x8, %bpl
jne 0xf1ff9
movl $0x2, %eax
testb $0x2, %bpl
jne 0xf2088
xorl %r14d, %r14d
jmp 0xf2009
xorl %r14d, %r14d
movl $0x1, 0x3c(%rsp)
movl $0x3, %eax
xorl %r12d, %r12d
testq %r15, %r15
je 0xf201c
cmpb $0x0, 0xf(%r15)
je 0xf201c
movl %eax, 0x3c(%rsp)
leaq 0x3670d(%rip), %rsi     # 0x128730
movq %r13, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xefda1
movq %rax, 0x30(%rsp)
testq %r14, %r14
je 0xf2063
leaq 0x20(%rsp), %rbx
movq %r14, (%rbx)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x10f28c
movq 0x10(%rbx), %rdi
callq 0xf052b
movq %r14, %rdi
callq 0xf228d
jmp 0xf1f36
leaq 0x20(%rsp), %r14
movq %r12, 0x8(%r14)
movq %rbx, (%r14)
movq %r13, %rdi
movq %r14, %rsi
callq 0x10f28c
movq 0x10(%r14), %rdi
callq 0xf052b
jmp 0xf1f36
leaq 0x50(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf21e7
movq 0x8(%r14), %r14
testq %r14, %r14
je 0xf20b9
movl 0x24(%rbx), %edx
movl 0x24(%r14), %ecx
leaq 0x57548(%rip), %rsi     # 0x1495f7
movq %r13, %rdi
xorl %eax, %eax
callq 0xe889f
cmpb $0x0, 0x53(%rsp)
je 0xf20cd
xorl %r12d, %r12d
movl $0x2, %eax
jmp 0xf200c
movl 0x24(%rbx), %edx
leaq 0x5753e(%rip), %rsi     # 0x149615
movq %r13, %rdi
xorl %eax, %eax
callq 0xe889f
jmp 0xf1ffc
testq %rcx, %rcx
jle 0xf1fdf
movq $0x0, (%rsp)
movq %r13, 0x10(%rsp)
movq %rcx, 0x48(%rsp)
cmpq $0x0, 0x18(%rsp)
jle 0xf2140
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r13, %rsi
movq (%rsp), %r14
movq %r14, %rdx
callq 0xf2592
movl %eax, 0xc(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xf2592
cmpl %eax, 0xc(%rsp)
jne 0xf215f
incq %r13
cmpq %r13, 0x18(%rsp)
jne 0xf210c
movq (%rsp), %rdx
incq %rdx
movq 0x48(%rsp), %rcx
movq %rdx, (%rsp)
cmpq %rcx, %rdx
movq 0x10(%rsp), %r13
jne 0xf2101
jmp 0xf1fdf
movq 0x10(%rsp), %r13
xorl %r14d, %r14d
jmp 0xf20c3