// File: /workspace/github_c_makefile_star2_O3/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
movq 0x18(%rdi), %rdi
callq 0x6cf44
cmpl $0x1, %eax
je 0x6a61f
cmpl $0x2, %eax
jne 0x6a759
movl $0x100, %ebx            # imm = 0x100
movl $0xc0, %r14d
jmp 0x6a62a
movl $0x80, %ebx
movl $0x60, %r14d
leaq 0xbb122(%rip), %rsi     # 0x125753
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rax
movq %r14, 0x8(%rsp)
imulq %r14, %rax
movq %rax, 0x20(%rsp)
movq %rbx, 0x10(%rsp)
imulq $0x780, %rbx, %r12      # imm = 0x780
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf09c5
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0xf0855
leaq 0x28(%rsp), %rbx
movq $0x3, (%rbx)
movb $0x1, %bpl
movq 0x28(%rsp), %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6a6da
movq %rbx, %rsi
callq 0xeeb4b
cmpb $0x20, %al
jb 0x6a6d1
movzbl %al, %r14d
leaq -0x20(%r14), %rdx
movzbl %bpl, %ebp
movq 0x18(%rsp), %rdi
movl %ebp, %esi
callq 0xf0eba
addq %r14, %r13
addq $-0x20, %r13
cmpb $0x1, %bpl
sete %bpl
incb %bpl
jmp 0x6a6d5
cmpb $0x1b, %al
je 0x6a6da
cmpq %r12, %r13
jl 0x6a68a
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0xf08ba
movq 0x20(%rsp), %rdx
cmpq %rdx, %r13
jne 0x6a700
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
jmp 0x6a78f
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %rbx
jle 0x6a774
cmpb $0x0, 0xd6(%r15)
leaq 0xbb064(%rip), %rax     # 0x12577f
leaq 0xda98e(%rip), %r8      # 0x1450b0
cmoveq %rax, %r8
leaq 0xbb031(%rip), %rsi     # 0x12575e
movq %r15, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8faa
cmpb $0x0, 0xd6(%r15)
je 0x6a78f
movq %r13, %rdi
movq %r14, %rsi
callq 0xe9cb4
cqto
idivq %r14
movq %rax, %rbx
jmp 0x6a78f
leaq 0xbafd9(%rip), %rsi     # 0x125739
xorl %r12d, %r12d
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %ebx, %ebx
jmp 0x6a7fc
leaq 0xbafe3(%rip), %rsi     # 0x12575e
leaq 0xda92e(%rip), %r8      # 0x1450b0
movq %r15, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8faa
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0xf266a
movq %rax, %rbx
leaq 0x30(%rsp), %r15
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
callq 0x18140
movabsq $-0xffffff0000ff01, %rax # imm = 0xFF000000FFFF00FF
movq %rax, (%r15)
movl $0xffffffff, 0x8(%r15)  # imm = 0xFFFFFFFF
movl $0x0, (%rsp)
movl $0x8, %edx
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0xf2f79
movq %rbx, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf22dc
movq %r12, %rdi
callq 0xf052b
movq %rbx, %rdi
callq 0xf228d
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq