// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdi, %r13
leaq 0xd3d0d(%rip), %rsi     # 0x10bc5c
callq 0xe07f7
movl $0xa0, %esi
movq %r13, %rdi
callq 0x3843b
movq %rax, %r15
pushq $0x64
popq %rsi
movq %r13, %rdi
callq 0x38484
movq %rax, %rcx
movq %rbx, %r14
movq 0x10(%rbx), %rax
movq %r15, %rbx
addq %r15, %rax
decq %rax
cqto
idivq %r15
movq %rax, %r15
cmpq %rax, %rcx
cmovlq %rcx, %r15
testq %r15, %r15
jle 0x38128
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xdf038
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x3813d
pushq $0x3
popq %rcx
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xe7437
movq %rax, 0x8(%rsp)
movq %r14, %rcx
addq $0x3c, %rcx
pushq $0x14
popq %rdi
pushq $0x10
popq %r8
xorl %esi, %esi
xorl %edx, %edx
movq %rcx, 0x38(%rsp)
xorl %r9d, %r9d
callq 0xe1b11
pushq $0x8
popq %rbp
xorl %eax, %eax
movl $0x0, 0x4(%rsp)
movq %r15, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq %r13, 0x18(%rsp)
movq 0x8(%rsp), %r12
cmpq %r15, %rax
je 0x38115
movq %rax, 0x10(%rsp)
movq %rbp, 0x28(%rsp)
xorl %r15d, %r15d
cmpq %rbx, %r15
jge 0x380fb
movq 0x18(%r13), %rdi
leaq -0x1(%rbp), %rsi
callq 0xe3eb4
movl %eax, %r14d
movq 0x18(%r13), %rdi
movq %rbp, %rsi
callq 0xe3eb4
movl %eax, %r13d
cmpl $0x0, 0x4(%rsp)
movq %rbp, 0x40(%rsp)
jne 0x38084
leal 0x21(%r14), %eax
cmpb $-0x3, %al
ja 0x38084
movzbl %r14b, %edx
movq 0x18(%rsp), %rdi
leaq 0xd3c2c(%rip), %rsi     # 0x10bca2
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movl %r13d, %eax
shrb $0x4, %al
andb $0xf, %r13b
cmpb $-0x22, %r14b
movzbl %al, %r14d
movzbl %r13b, %eax
movl %eax, %ecx
cmovel %r14d, %ecx
cmovel %eax, %r14d
movl $0xff, %ebx
andl %ebx, %ecx
movq 0x38(%rsp), %r13
movl (%r13,%rcx,4), %ecx
movq %r12, %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rbp
movq %rbp, %rdx
callq 0xe7287
andl %ebx, %r14d
movl (%r13,%r14,4), %ecx
leaq 0x1(%r15), %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0xe7287
movq 0x40(%rsp), %rbp
addq $0x2, %rbp
addq $0x2, %r15
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rbx
jmp 0x3802a
movq 0x10(%rsp), %rax
incq %rax
movq 0x28(%rsp), %rbp
addq %rbx, %rbp
movq 0x20(%rsp), %r15
jmp 0x38014
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
jmp 0x3813f
leaq 0xd3b53(%rip), %rsi     # 0x10bc82
xorl %ebx, %ebx
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x3813f
xorl %ebx, %ebx
movq %rbx, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe7084