// File: /workspace/github_c_makefile_star2_O1/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 0xf25b0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x20(%rdi)
je 0xf25b1
addq $0x58, %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, 0x40(%rsp)
cmpq $0x0, 0x28(%rbx)
jne 0xf25d3
movq %rbx, %rdi
callq 0xf27b5
testq %r12, %r12
je 0xf2630
cmpq $0x0, 0x28(%r12)
jne 0xf25e8
movq %r12, %rdi
callq 0xf27b5
cmpl $0x0, 0x20(%r12)
jne 0xf25a2
movl 0x24(%rbx), %eax
cmpl 0x24(%r12), %eax
jne 0xf25a2
movq 0x8(%rbx), %rax
cmpq 0x8(%r12), %rax
jne 0xf25a2
movq 0x10(%rbx), %rax
cmpq 0x10(%r12), %rax
jne 0xf25a2
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl %ebp, 0x28(%rsp)
testb $0x2, %bpl
jne 0xf271f
movq %r12, %r14
jmp 0xf2644
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl %ebp, 0x28(%rsp)
xorl %r14d, %r14d
testq %r14, %r14
jne 0xf26ab
testb $0x8, %bpl
jne 0xf26a6
testb $0x2, %bpl
je 0xf26ab
movq %r14, %r12
leaq 0x40(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf282e
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xf2688
movl 0x24(%rbx), %edx
movl 0x24(%rax), %ecx
leaq 0x569e1(%rip), %rsi     # 0x14905f
movq %r13, %rdi
xorl %eax, %eax
callq 0xe9461
cmpb $0x0, 0x43(%rsp)
movq %r12, %r14
jne 0xf26ab
movl 0x24(%rbx), %edx
leaq 0x569e1(%rip), %rsi     # 0x14907d
movq %r13, %rdi
xorl %eax, %eax
callq 0xe9461
orb $0x1, 0x2c(%rsp)
testq %r15, %r15
je 0xf26bc
cmpb $0x0, 0xf(%r15)
je 0xf26bc
orb $0x2, 0x2c(%rsp)
leaq 0x35e3d(%rip), %rsi     # 0x128500
movq %r13, %rdi
movq %r15, %rdx
movl %ebp, %ecx
callq 0xf037a
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %r15
testq %r15, %r15
je 0xf26e6
movq %r15, 0x10(%rsp)
jmp 0xf26f0
movq %rbx, 0x10(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x10(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x10f2e4
movq 0x10(%rbx), %rdi
callq 0xf0b13
testq %r15, %r15
je 0xf25a2
movq %r15, %rdi
callq 0xf28d4
jmp 0xf25a2
movq 0x10(%rbx), %rax
movq %rax, 0x50(%rsp)
testq %rax, %rax
jle 0xf2641
movq 0x8(%rbx), %rax
movq %rax, 0x38(%rsp)
movq $0x0, (%rsp)
movq %r13, 0x30(%rsp)
cmpq $0x0, 0x38(%rsp)
jle 0xf2786
xorl %r14d, %r14d
movq %rbx, %rdi
movq %r14, %rsi
movq (%rsp), %r13
movq %r13, %rdx
callq 0xf2bd3
movl %eax, 0xc(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xf2bd3
cmpl %eax, 0xc(%rsp)
jne 0xf27a8
incq %r14
cmpq %r14, 0x38(%rsp)
jne 0xf2752
movq (%rsp), %rcx
incq %rcx
movq %rcx, (%rsp)
cmpq 0x50(%rsp), %rcx
movq 0x30(%rsp), %r13
movl $0x0, %r14d
jne 0xf2747
jmp 0xf2644
movq %r12, %r14
movq 0x30(%rsp), %r13
jmp 0xf2644