// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/bsave.c
// Function: do_cga16
static void do_cga16(deark *c, lctx *d)
{
	de_bitmap *img = NULL;
	i64 w, h;
	i64 max_possible_height;
	i64 i, j;
	u8 charcode, colorcode;
	i64 src_rowspan;
	u8 color0, color1;
	int charwarning = 0;

	de_declare_fmt(c, "BSAVE-PC 16-color CGA pseudo-graphics");

	w = get_width(c, d, 160);
	h = get_height(c, d, 100);

	// Every pair of bytes codes for two pixels; i.e. one byte per pixel.
	src_rowspan = w;
	max_possible_height = (d->data_size+src_rowspan-1)/src_rowspan;
	if(h > max_possible_height)
		h = max_possible_height;

	if(h < 1) {
		de_err(c, "Not enough data for this format");
		goto done;
	}

	de_dbg_dimensions(c, w, h);
	if(!de_good_image_dimensions(c, w, h)) goto done;
	img = de_bitmap_create(c, w, h, 3);

	de_copy_std_palette(DE_PALID_PC16, 0, 0, d->pal, 16, 0);

	for(j=0; j<h; j++) {
		for(i=0; i<w; i+=2) {
			charcode = de_getbyte(BSAVE_HDRSIZE + j*src_rowspan + i);
			colorcode = de_getbyte(BSAVE_HDRSIZE + j*src_rowspan + i+1);

			if(charwarning==0 && charcode!=0xdd && charcode!=0xde) {
				// TODO: We could also handle space characters and full-block characters,
				// at least. But maybe not worth the trouble.
				de_warn(c, "Unexpected code found (0x%02x). Format may not be correct.", (UI)charcode);
				charwarning=1;
			}

			if(charcode==0xde) {
				color0 = colorcode>>4;
				color1 = colorcode&0x0f;
			}
			else {
				color1 = colorcode>>4;
				color0 = colorcode&0x0f;
			}

			de_bitmap_setpixel_rgb(img, i+0, j, d->pal[(UI)color0]);
			de_bitmap_setpixel_rgb(img, i+1, j, d->pal[(UI)color1]);
		}
	}

	de_bitmap_write_to_file(img, NULL, 0);
done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xdf511(%rip), %rsi     # 0x118c28
callq 0xeb6d6
movl $0xa0, %esi
movq %rbx, %rdi
callq 0x39c55
movq %rax, %r12
movl $0x64, %esi
movq %rbx, %rdi
callq 0x39c9e
movq %rax, %rcx
movq 0x10(%r14), %rax
addq %r12, %rax
decq %rax
cqto
idivq %r12
movq %rax, %rbp
cmpq %rax, %rcx
cmovlq %rcx, %rbp
testq %rbp, %rbp
jle 0x39906
movq %r14, %r15
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xe9b05
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xf241d
testl %eax, %eax
je 0x3991c
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movl $0x3, %ecx
callq 0xf2cab
movq %rax, 0x10(%rsp)
movq %r15, %rcx
addq $0x3c, %rcx
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
movq %rcx, 0x40(%rsp)
xorl %r9d, %r9d
callq 0xecbbc
movl $0x8, %eax
movq %rax, 0x20(%rsp)
movl $0x7, %eax
movq %rax, 0x18(%rsp)
movl $0x0, 0x4(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbp, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq 0x10(%rsp), %r13
testq %r12, %r12
jle 0x398ce
xorl %ebp, %ebp
movq 0x28(%rsp), %r14
movq 0x18(%r14), %rdi
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %rsi
callq 0xef0d5
movl %eax, %r15d
movq 0x18(%r14), %rdi
movq 0x20(%rsp), %rax
leaq (%rax,%rbp), %rsi
callq 0xef0d5
movl %eax, %r14d
cmpl $0x0, 0x4(%rsp)
jne 0x39861
leal 0x21(%r15), %eax
cmpb $-0x3, %al
ja 0x39861
movzbl %r15b, %edx
movq 0x28(%rsp), %rdi
leaq 0xdf41c(%rip), %rsi     # 0x118c6e
xorl %eax, %eax
callq 0xe9b27
movl $0x1, 0x4(%rsp)
movl %r14d, %eax
shrb $0x4, %al
andb $0xf, %r14b
cmpb $-0x22, %r15b
movzbl %al, %r15d
movzbl %r14b, %eax
movl %eax, %ecx
cmovel %r15d, %ecx
cmovel %eax, %r15d
movl $0xff, %r12d
andl %r12d, %ecx
movq 0x40(%rsp), %r14
movl (%r14,%rcx,4), %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0xf2afd
andl %r12d, %r15d
movl (%r14,%r15,4), %ecx
leaq 0x1(%rbp), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0xf2afd
addq $0x2, %rbp
movq 0x38(%rsp), %r12
cmpq %r12, %rbp
jl 0x39804
movq 0x8(%rsp), %rcx
incq %rcx
addq %r12, 0x20(%rsp)
addq %r12, 0x18(%rsp)
movq 0x30(%rsp), %rbp
movq %rcx, 0x8(%rsp)
cmpq %rbp, %rcx
jne 0x397f9
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
jmp 0x3991f
leaq 0xdf341(%rip), %rsi     # 0x118c4e
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x3991f
xorl %r14d, %r14d
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf28d4