// File: /workspace/github_c_makefile_star2_O2/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 0x69230
cmpl $0x1, %eax
je 0x66b4c
cmpl $0x2, %eax
jne 0x66c7e
movl $0x100, %ebp            # imm = 0x100
movl $0xc0, %ebx
jmp 0x66b54
movl $0x80, %ebp
pushq $0x60
popq %rbx
leaq 0xb19f8(%rip), %rsi     # 0x118553
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbp, %rax
movq %rbx, 0x8(%rsp)
imulq %rbx, %rax
movq %rax, 0x20(%rsp)
imulq $0x780, %rbp, %r12      # imm = 0x780
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0xe59cd
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0xe589c
leaq 0x28(%rsp), %rbx
movq $0x3, (%rbx)
movb $0x1, %r14b
cmpq %r12, %r13
jge 0x66c09
movq 0x28(%rsp), %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jge 0x66c09
movq %rbx, %rsi
callq 0xe3f2a
cmpb $0x20, %al
jae 0x66bd3
cmpb $0x1b, %al
jne 0x66bad
jmp 0x66c09
movq %rbp, 0x18(%rsp)
movzbl %al, %ebp
leaq -0x20(%rbp), %rdx
movzbl %r14b, %r14d
movq 0x10(%rsp), %rdi
movl %r14d, %esi
callq 0xe5e40
addq %rbp, %r13
addq $-0x20, %r13
movq 0x18(%rsp), %rbp
cmpb $0x1, %r14b
sete %r14b
incb %r14b
jmp 0x66bad
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0xe58e8
movq 0x20(%rsp), %rdx
cmpq %rdx, %r13
jne 0x66c2a
movq 0x8(%rsp), %rbx
jmp 0x66cb1
movq 0x8(%rsp), %rbx
jle 0x66c96
cmpb $0x0, 0xd6(%r15)
leaq 0xb193f(%rip), %rax     # 0x11857f
leaq 0xd1019(%rip), %r8      # 0x137c60
cmoveq %rax, %r8
leaq 0xb190c(%rip), %rsi     # 0x11855e
movq %r15, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xdf05a
cmpb $0x0, 0xd6(%r15)
je 0x66cb1
movq %r13, %rdi
movq %rbp, %rsi
callq 0xdfbb6
cqto
idivq %rbp
movq %rax, %rbx
jmp 0x66cb1
leaq 0xb18b4(%rip), %rsi     # 0x118539
xorl %r12d, %r12d
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %ebx, %ebx
jmp 0x66d12
leaq 0xb18c1(%rip), %rsi     # 0x11855e
leaq 0xd0fbc(%rip), %r8      # 0x137c60
movq %r15, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xdf05a
pushq $0x3
popq %rcx
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xe7437
movq %rax, %rbx
leaq 0x30(%rsp), %r14
movl $0x400, %edx            # imm = 0x400
movq %r14, %rdi
xorl %esi, %esi
callq 0x18140
movabsq $-0xffffff0000ff01, %rax # imm = 0xFF000000FFFF00FF
movq %rax, (%r14)
orl $-0x1, 0x8(%r14)
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rcx
movq %r14, %r8
movq %rbx, %r9
callq 0xe7cbc
pushq $0x2
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe70b7
movq %r12, %rdi
callq 0xe5591
movq %rbx, %rdi
callq 0xe7084
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq