// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/fli.c
// Function: do_chunk_delta_flc
static void do_chunk_delta_flc(deark *c, lctx *d, struct chunk_info_type *ci)
{
	i64 ypos = 0;
	i64 num_encoded_lines;
	i64 line_idx;
	i64 pos = ci->dpos;
	struct image_ctx_type *ictx;

	if(!ci->ictx) goto done;
	de_dbg(c, "doing delta (FLC-style) decompression");
	ictx = ci->ictx;
	ictx->use_count++;

	num_encoded_lines = de_getu16le_p(&pos);

	for(line_idx=0; line_idx<num_encoded_lines; line_idx++) {
		UI npackets = 0;
		UI pkidx;
		UI wcode, wcode_hi;
		i64 xpos = 0;

		// Read code words until we find one the high 2 bits == 0.
		while(1) {
			if(pos >= ci->pos + ci->len) goto done;

			wcode = (UI)de_getu16le_p(&pos);
			wcode_hi = wcode>>14;

			if(wcode_hi==0) {
				npackets = wcode;
				break;
			}
			else if(wcode_hi==3) {
				ypos += (i64)65536 - (i64)wcode;
			}
			else if(wcode_hi==2) {
				// Set the "last byte of current line".
				// (UNTESTED) This feature is only expected to be used if the
				// screen width is odd, and I haven't found such a file.
				de_bitmap_setpixel_rgb(ictx->img, ictx->w-1, ypos, ictx->pal[wcode & 0x00ff]);
			}
		}

		for(pkidx=0; pkidx<npackets; pkidx++) {
			UI clridx, clridx2;
			u8 code;
			i64 count;
			i64 k;
			i64 skip_count;

			if(pos >= ci->pos + ci->len) goto done;
			skip_count = (i64)de_getbyte_p(&pos);
			xpos += skip_count;
			code = de_getbyte_p(&pos);
			if(code<128) { // "positive" = run of uncompressed pixels
				count = 2 * (i64)code;
				for(k=0; k<count; k++) {
					clridx = (UI)de_getbyte_p(&pos);
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx]);
					xpos++;
				}
			}
			else { // "negative" = RLE
				count = (i64)256 - (i64)code;
				clridx = (UI)de_getbyte_p(&pos);
				clridx2 = (UI)de_getbyte_p(&pos);
				for(k=0; k<count; k++) {
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx]);
					xpos++;
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx2]);
					xpos++;
				}
			}
		}

		ypos++;
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x20(%rdx), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x48(%rdx)
je 0x5b218
movq %rdx, %r13
movq %rdi, %rbp
leaq 0xc7338(%rip), %rsi     # 0x122384
xorl %eax, %eax
callq 0xe8617
movq 0x48(%r13), %rbx
incl 0x10(%rbx)
movq 0x18(%rbp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xeebeb
movq %rax, 0x28(%rsp)
testq %rax, %rax
jle 0x5b218
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
movq $0x0, 0x20(%rsp)
movq 0x18(%r13), %rax
addq 0x10(%r13), %rax
cmpq %rax, 0x10(%rsp)
jge 0x5b218
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeebeb
cmpl $0x3fff, %eax           # imm = 0x3FFF
jbe 0x5b0e5
movl %eax, %ecx
shrl $0xe, %ecx
cmpl $0x2, %ecx
je 0x5b0ca
cmpl $0x3, %ecx
jne 0x5b087
movl %eax, %eax
subq %rax, %r12
addq $0x10000, %r12          # imm = 0x10000
jmp 0x5b087
movq (%rbx), %rsi
movq 0x30(%rbx), %rdi
decq %rsi
movzbl %al, %eax
movl 0x38(%rbx,%rax,4), %ecx
movq %r12, %rdx
callq 0xf24ba
jmp 0x5b087
testl %eax, %eax
je 0x5b1fd
xorl %r15d, %r15d
xorl %edx, %edx
movq %r13, 0x40(%rsp)
movq %rbp, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x18(%r13), %rcx
addq 0x10(%r13), %rcx
cmpq %rcx, 0x10(%rsp)
jge 0x5b218
movl %edx, 0x1c(%rsp)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %eax
addq %rax, %r15
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeeb4b
testb %al, %al
js 0x5b177
je 0x5b1dd
addb %al, %al
movzbl %al, %r14d
leaq 0x10(%rsp), %r13
movq 0x18(%rbp), %rdi
movq %r13, %rsi
callq 0xeeb4b
movq 0x30(%rbx), %rdi
movzbl %al, %eax
movl 0x38(%rbx,%rax,4), %ecx
movq %r15, %rsi
movq %r12, %rdx
callq 0xf24ba
incq %r15
decq %r14
jne 0x5b14b
jmp 0x5b1e2
movzbl %al, %r13d
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeeb4b
movb %al, 0xf(%rsp)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl 0xf(%rsp), %r14d
movzbl %al, %ebp
orq $-0x100, %r13
movq 0x30(%rbx), %rdi
movl 0x38(%rbx,%r14,4), %ecx
movq %r15, %rsi
movq %r12, %rdx
callq 0xf24ba
movq 0x30(%rbx), %rdi
movl 0x38(%rbx,%rbp,4), %ecx
leaq 0x1(%r15), %rsi
movq %r12, %rdx
callq 0xf24ba
addq $0x2, %r15
incq %r13
jne 0x5b1a7
movq 0x30(%rsp), %rbp
leaq 0x10(%rsp), %r13
movl 0x1c(%rsp), %edx
incl %edx
movq 0x38(%rsp), %rax
cmpl %eax, %edx
movq %r13, %r14
movq 0x40(%rsp), %r13
jne 0x5b101
incq %r12
movq 0x20(%rsp), %rcx
incq %rcx
movq %rcx, 0x20(%rsp)
cmpq 0x28(%rsp), %rcx
jne 0x5b087
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq