// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/bsave.c
// Function: do_4color
static void do_4color(deark *c, lctx *d)
{
	// TODO: This may not be the right palette.
	static const de_color default_palette[4] = { 0x000000, 0x55ffff, 0xff55ff, 0xffffff };
	de_color palette[4];
	i64 w, h;
	i64 i,j;
	i64 pos;
	i64 src_rowspan;
	de_bitmap *img = NULL;

	pos = BSAVE_HDRSIZE;

	if(d->has_dimension_fields) {
		// 11-byte header that includes width & height
		w = (d->metrics.maybe_bitsperrow + 1)/2; // width = number of bits??
		h = d->metrics.maybe_nrows;
		pos+=4;
	}
	else if(!d->interlaced) {
		w = get_width(c, d, 320);
		src_rowspan = (w+3)/4;
		h = d->data_size / src_rowspan;
		h = get_height(c, d, h);
	}
	else {
		w = get_width(c, d, 320);
		h = get_height(c, d, 200);	// TODO: Calculate this?
	}

	// Set the palette
	if(d->have_pcpaint_palinfo) {
		de_copy_std_palette(DE_PALID_CGA, (int)d->pcpaint_pal_num, 0, palette, 4, 0);
		palette[0] = de_get_std_palette_entry(DE_PALID_PC16, 0, (int)d->pcpaint_border_col);
	}
	else {
		for(i=0; i<4; i++) {
			palette[i] = default_palette[i];
		}
	}

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

	if(d->interlaced) {
		dbuf *tmpf;

		tmpf = dbuf_create_membuf(c, h*src_rowspan, 0x1);
		for(j=0; j<h; j++) {
			// interlaced -- Odd rows appear offset by 8192 bytes
			dbuf_copy(c->infile, pos + ((j%2)*8192) +
				(j/2)*src_rowspan, src_rowspan, tmpf);
		}

		de_convert_image_paletted(tmpf, 0, 2, src_rowspan, palette, img, 0);
		dbuf_close(tmpf);
	}
	else {
		de_convert_image_paletted(c->infile, pos, 2, src_rowspan, palette, img, 0);
	}

	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 $0x38, %rsp
movq %rsi, %r12
movq %rdi, %r15
cmpb $0x0, 0x2d(%rsi)
pushq $0x4
popq %r14
je 0x37d51
movq 0x440(%r12), %rax
movq 0x448(%r12), %rbx
incq %rax
pushq $0x2
popq %rcx
cqto
idivq %rcx
movq %rax, %r13
pushq $0xb
jmp 0x37d9d
movb 0x2c(%r12), %bl
movl $0x140, %esi            # imm = 0x140
movq %r15, %rdi
callq 0x3843b
movq %rax, %r13
testb %bl, %bl
je 0x37d74
movl $0xc8, %esi
movq %r15, %rdi
jmp 0x37d93
movq %r13, %rax
addq $0x3, %rax
cqto
idivq %r14
movq %rax, %rcx
movq 0x10(%r12), %rax
cqto
idivq %rcx
movq %r15, %rdi
movq %rax, %rsi
callq 0x38484
movq %rax, %rbx
pushq $0x7
popq %rax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x18(%r12)
je 0x37ecc
movzbl 0x1c(%r12), %esi
pushq $0xa
popq %rdi
leaq 0x10(%rsp), %rbp
pushq $0x4
popq %r8
xorl %edx, %edx
movq %rbp, %rcx
xorl %r9d, %r9d
callq 0xe1b11
movzbl 0x1d(%r12), %edx
pushq $0x14
popq %rdi
xorl %esi, %esi
callq 0xe1a23
movl %eax, (%rbp)
leaq 0x3(%r13), %rax
cqto
idivq %r14
movq %rax, %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xdf038
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x37eec
pushq $0x3
popq %rcx
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xe7437
movq %rax, %r14
cmpb $0x0, 0x2c(%r12)
je 0x37ef1
movq %r14, 0x28(%rsp)
movq %rbp, %rsi
imulq %rbx, %rsi
pushq $0x1
popq %rdx
movq %r15, 0x30(%rsp)
movq %r15, %rdi
callq 0xe59cd
movq %rax, %r14
xorl %r13d, %r13d
testq %rbx, %rbx
cmovleq %r13, %rbx
movq %rbx, %r12
movl $0x2000, %r15d          # imm = 0x2000
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x37e9e
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rax
andq %r15, %rax
addq 0x8(%rsp), %rax
movq %rbx, %rsi
shrq %rsi
imulq %rbp, %rsi
addq %rax, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0xe45f4
incq %rbx
addq %r15, %r13
jmp 0x37e65
andl $0x0, (%rsp)
pushq $0x2
popq %rdx
leaq 0x10(%rsp), %r8
movq %r14, %rbx
movq %r14, %rdi
xorl %esi, %esi
movq %rbp, %rcx
movq 0x28(%rsp), %r14
movq %r14, %r9
callq 0xe7cbc
movq %rbx, %rdi
callq 0xe5591
jmp 0x37f11
xorl %eax, %eax
leaq 0xd3f0b(%rip), %rcx     # 0x10bde0
cmpq $0x10, %rax
je 0x37de1
movl (%rax,%rcx), %edx
movl %edx, 0x10(%rsp,%rax)
addq $0x4, %rax
jmp 0x37ed5
xorl %r14d, %r14d
jmp 0x37f1d
movq 0x18(%r15), %rdi
andl $0x0, (%rsp)
pushq $0x2
popq %rdx
leaq 0x10(%rsp), %r8
movq 0x8(%rsp), %rsi
movq %rbp, %rcx
movq %r14, %r9
callq 0xe7cbc
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq %r14, %rdi
callq 0xe7084
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq