// File: /workspace/github_c_makefile_star2_O2/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 0x58260
movq %rdx, %r15
movq %rdi, %rbp
leaq 0xbd12e(%rip), %rsi     # 0x1151c4
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xde896
movq 0x48(%r15), %rbx
incl 0x10(%rbx)
movq 0x18(%rbp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3fc4
testq %rax, %rax
cmovleq %r14, %rax
xorl %r13d, %r13d
movq %rax, 0x18(%rsp)
movq %r14, 0x20(%rsp)
cmpq %rax, %r14
leaq 0x10(%rsp), %r14
je 0x58260
movq 0x18(%r15), %rax
addq 0x10(%r15), %rax
cmpq %rax, 0x10(%rsp)
jge 0x58260
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xe3fc4
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x58135
movl %eax, %ecx
shrl $0xe, %ecx
cmpl $0x2, %ecx
je 0x5811a
cmpl $0x3, %ecx
jne 0x580d7
movl %eax, %eax
subq %rax, %r13
addq $0x10000, %r13          # imm = 0x10000
jmp 0x580d7
movq (%rbx), %rsi
movq 0x30(%rbx), %rdi
decq %rsi
movzbl %al, %eax
movl 0x38(%rbx,%rax,4), %ecx
movq %r13, %rdx
callq 0xe7287
jmp 0x580d7
xorl %edx, %edx
xorl %r12d, %r12d
movq %r15, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movq %rax, 0x38(%rsp)
cmpl %eax, %edx
je 0x5824b
movq 0x18(%r15), %rcx
addq 0x10(%r15), %rcx
cmpq %rcx, 0x10(%rsp)
jge 0x58260
movq %rdx, 0x40(%rsp)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xe3f2a
movzbl %al, %eax
addq %rax, %r12
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xe3f2a
testb %al, %al
js 0x581c4
addb %al, %al
movzbl %al, %r14d
subq $0x1, %r14
jb 0x58235
movq 0x18(%rbp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3f2a
movq 0x30(%rbx), %rdi
movzbl %al, %eax
movl 0x38(%rbx,%rax,4), %ecx
movq %r12, %rsi
movq %r13, %rdx
callq 0xe7287
incq %r12
jmp 0x58191
movzbl %al, %r15d
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xe3f2a
movb %al, 0xf(%rsp)
movq 0x18(%rbp), %rdi
movq %r14, %rsi
callq 0xe3f2a
movzbl 0xf(%rsp), %ebp
movzbl %al, %r14d
orq $-0x100, %r15
testq %r15, %r15
je 0x5822b
movq 0x30(%rbx), %rdi
movl 0x38(%rbx,%rbp,4), %ecx
movq %r12, %rsi
movq %r13, %rdx
callq 0xe7287
leaq 0x1(%r12), %rsi
movq 0x30(%rbx), %rdi
movl 0x38(%rbx,%r14,4), %ecx
movq %r13, %rdx
callq 0xe7287
addq $0x2, %r12
incq %r15
jmp 0x581f4
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %rbp
movq 0x40(%rsp), %rdx
incl %edx
leaq 0x10(%rsp), %r14
movq 0x38(%rsp), %rax
jmp 0x58149
incq %r13
movq 0x20(%rsp), %r14
incq %r14
movq 0x18(%rsp), %rax
jmp 0x580c4
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq