// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc2.c
// Function: de_run_cserve_rle
static void de_run_cserve_rle(deark *c, de_module_params *mparams)
{
	int fmt;
	i64 w, expected_h, actual_h;
	i64 max_npixels;
	i64 npixels_expected;
	i64 npixels_found;
	i64 pos;
	u8 next_color;
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_color pal[256];

	fmt = get_cserve_rle_fmt(c);
	if(fmt==1) {
		w = 128;
		expected_h = 96;
	}
	else if(fmt==2) {
		w = 256;
		expected_h = 192;
	}
	else {
		de_err(c, "Not a CompuServe RLE file");
		goto done;
	}

	de_dbg(c, "width: %"I64_FMT, w);
	npixels_expected = w*expected_h;
#define CSRLE_MAX_H 1920 // arbitrary
	max_npixels = w*CSRLE_MAX_H;
	unc_pixels = dbuf_create_membuf(c, max_npixels, 0x1);
	dbuf_enable_wbuffer(unc_pixels);
	pos = 3;
	npixels_found = 0;
	next_color = 1; // 1=black, 2=white, 0=unset
	while(1) {
		u8 x;

		if(npixels_found >= max_npixels) {
			break;
		}
		if(pos >= c->infile->len) break;

		x = de_getbyte_p(&pos);
		if(x>=0x20) {
			i64 count;

			count = (i64)x - 0x20;
			dbuf_write_run(unc_pixels, next_color, count);
			npixels_found += count;
			next_color = (next_color==1)?2:1;
		}
		else if(x==0x1b) {
			break;
		}
		// TODO: Could do more error checking
	}

	dbuf_flush(unc_pixels);
	actual_h = expected_h;
	if(npixels_found != npixels_expected) {
		de_warn(c, "Expected %"I64_FMT" pixels, found %"I64_FMT"%s", npixels_expected,
			npixels_found,
			((npixels_found>npixels_expected && !c->padpix)?" (try -padpix)":""));
		if(npixels_found>npixels_expected && c->padpix) {
			actual_h = de_pad_to_n(npixels_found, w)/w;
		}
	}

	img = de_bitmap_create(c, w, actual_h, 3);
	de_zeromem(pal, sizeof(pal));
	// Images that don't end cleanly are common enough that we go to
	// the trouble of making missing pixels a special color.
	pal[0] = DE_MAKE_RGB(255,0,255);
	pal[1] = DE_STOCKCOLOR_BLACK;
	pal[2] = DE_STOCKCOLOR_WHITE;
	de_convert_image_paletted(unc_pixels, 0, 8, w, pal, img, 0);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rdi, %r15
callq 0x6d185
cmpl $0x1, %eax
je 0x6a86c
cmpl $0x2, %eax
jne 0x6a99c
movl $0x100, %ebx            # imm = 0x100
movl $0xc0, %eax
jmp 0x6a876
movl $0x80, %ebx
movl $0x60, %eax
movq %rax, 0x10(%rsp)
leaq 0xbaca1(%rip), %rsi     # 0x125523
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, 0x20(%rsp)
imulq $0x780, %rbx, %rbp      # imm = 0x780
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xf0faf
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0xf0e3f
leaq 0x28(%rsp), %r12
movq $0x3, (%r12)
movb $0x1, %bl
cmpq %rbp, %r13
jge 0x6a914
movq 0x28(%rsp), %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6a914
movq %r12, %rsi
callq 0xef14d
cmpb $0x20, %al
jb 0x6a91c
movzbl %al, %r14d
leaq -0x20(%r14), %rdx
movzbl %bl, %ebx
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0xf14bd
addq %r14, %r13
addq $-0x20, %r13
cmpb $0x1, %bl
sete %bl
incb %bl
movb $0x1, %al
jmp 0x6a916
xorl %eax, %eax
testb %al, %al
jne 0x6a8ca
jmp 0x6a923
cmpb $0x1b, %al
setne %al
jmp 0x6a916
movq 0x20(%rsp), %r14
movq %r14, %r12
movq 0x10(%rsp), %rbx
imulq %rbx, %r12
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0xf0ea4
cmpq %r12, %r13
je 0x6a9d5
jle 0x6a9b7
cmpb $0x0, 0xd6(%r15)
leaq 0xbabf4(%rip), %rax     # 0x12554f
leaq 0xda26e(%rip), %r8      # 0x144bd0
cmoveq %rax, %r8
leaq 0xbabc1(%rip), %rsi     # 0x12552e
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9b27
cmpb $0x0, 0xd6(%r15)
je 0x6a9d5
movq %r13, %rdi
movq %r14, %rsi
callq 0xea80b
cqto
idivq %r14
movq %rax, %rbx
jmp 0x6a9d5
leaq 0xbab66(%rip), %rsi     # 0x125509
xorl %ebp, %ebp
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r15d, %r15d
jmp 0x6aa41
leaq 0xbab70(%rip), %rsi     # 0x12552e
leaq 0xda20b(%rip), %r8      # 0x144bd0
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9b27
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r15
leaq 0x30(%rsp), %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x18140
movabsq $-0xffffff0000ff01, %rax # imm = 0xFF000000FFFF00FF
movq %rax, (%rbx)
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
movl $0x0, (%rsp)
movl $0x8, %edx
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rcx
movq %rbx, %r8
movq %r15, %r9
callq 0xf35b8
movq %r15, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf2923
movq %rbp, %rdi
callq 0xf0b13
movq %r15, %rdi
callq 0xf28d4
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq