// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x15846e(%rip), %rsi    # 0x1a49e8
callq 0x15e380
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0xa0, %edx
callq 0x4cc30
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x64, %edx
callq 0x4cca0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq -0x50(%rbp), %rax
subq $0x1, %rax
cqto
idivq -0x50(%rbp)
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x30(%rbp), %rax
jle 0x4c5df
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x1, -0x28(%rbp)
jge 0x4c5fd
movq -0x8(%rbp), %rdi
leaq 0x15841d(%rip), %rsi    # 0x1a4a0e
movb $0x0, %al
callq 0x15bfb0
jmp 0x4c7be
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x4c629
jmp 0x4c7be
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rcx
addq $0x3c, %rcx
movl $0x14, %edi
xorl %r9d, %r9d
xorl %eax, %eax
movl %eax, %edx
movl $0x10, %r8d
movl %r9d, %esi
callq 0x161320
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x4c7af
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x4c79c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
imulq -0x50(%rbp), %rsi
addq $0x7, %rsi
addq -0x38(%rbp), %rsi
callq 0x165060
movb %al, -0x41(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
imulq -0x50(%rbp), %rsi
addq $0x7, %rsi
addq -0x38(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movb %al, -0x42(%rbp)
cmpl $0x0, -0x58(%rbp)
jne 0x4c710
movzbl -0x41(%rbp), %eax
cmpl $0xdd, %eax
je 0x4c710
movzbl -0x41(%rbp), %eax
cmpl $0xde, %eax
je 0x4c710
movq -0x8(%rbp), %rdi
movzbl -0x41(%rbp), %edx
leaq 0x15832c(%rip), %rsi    # 0x1a4a2e
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0x58(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0xde, %eax
jne 0x4c731
movzbl -0x42(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x51(%rbp)
movzbl -0x42(%rbp), %eax
andl $0xf, %eax
movb %al, -0x52(%rbp)
jmp 0x4c745
movzbl -0x42(%rbp), %eax
sarl $0x4, %eax
movb %al, -0x52(%rbp)
movzbl -0x42(%rbp), %eax
andl $0xf, %eax
movb %al, -0x51(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x0, %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movzbl -0x51(%rbp), %ecx
movl %ecx, %ecx
movl 0x3c(%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x1, %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movzbl -0x52(%rbp), %ecx
movl %ecx, %ecx
movl 0x3c(%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
jmp 0x4c683
jmp 0x4c79e
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x4c66d
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x18(%rbp), %rdi
callq 0x16af60
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)