// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/insetpix.c
// Function: do_decompress_tile
static void do_decompress_tile(deark *c, lctx *d, struct insetpix_item_data *itd,
	dbuf *unc_pixels, i64 num_rows)
{
	u8 *rowbuf1 = NULL;
	u8 *compression_bytes = NULL;
	i64 pos;
	i64 i, j;
	i64 plane;
	i64 endpos = itd->loc + itd->len;

	// There are d->gfore planes (1-bpp images). The first row of each plane is
	// uncompressed. The rest are compressed with a delta compression algorithm.
	// There are d->page_rows rows in each plane.

	rowbuf1 = de_malloc(c, d->rowspan);
	compression_bytes = de_malloc(c, d->compression_bytes_per_row);

	pos = itd->loc;

	for(plane=0; plane<d->gfore; plane++) {
		for(j=0; j<num_rows; j++) {
			if(pos >= endpos) {
				de_warn(c, "Not enough data in tile %u", itd->tile_num);
				goto done;
			}

			if(j==0) {
				// First row is stored uncompressed
				dbuf_copy(c->infile, pos, d->rowspan, unc_pixels);
				pos += d->rowspan;
			}
			else {
				de_read(compression_bytes, pos, d->compression_bytes_per_row);
				pos += d->compression_bytes_per_row;

				// Read back a copy of the previous row
				dbuf_read(unc_pixels, rowbuf1, unc_pixels->len - d->rowspan, d->rowspan);

				// For every 1 bit in the compression_bytes array, read a byte from the file.
				// For every 0 bit, copy the byte from the previous row.
				for(i=0; i<d->rowspan; i++) {
					u8 b;

					if(getbit(compression_bytes, i)) {
						b =  de_getbyte_p(&pos);
					}
					else {
						b = rowbuf1[i];
					}
					dbuf_writebyte(unc_pixels, b);
				}
			}
		}
	}

	de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT, pos-itd->loc, unc_pixels->len);

done:
	de_free(c, compression_bytes);
	de_free(c, rowbuf1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq 0x10(%rdx), %rax
addq 0x18(%rdx), %rax
movq %rax, 0x28(%rsp)
movq 0xc8(%rsi), %rsi
callq 0xdecaf
movq %rax, 0x10(%rsp)
movq 0xd0(%r13), %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
movq %r14, 0x18(%rsp)
movq 0x18(%r14), %rax
movq %rsp, %rbp
movq %rax, (%rbp)
xorl %eax, %eax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
cmovleq %rax, %rcx
movq %rcx, 0x8(%rsp)
cmpq 0x80(%r13), %rax
jge 0x7a427
xorl %ecx, %ecx
movq %rax, 0x20(%rsp)
cmpq 0x8(%rsp), %rcx
je 0x7a3df
movq (%rsp), %rdx
cmpq 0x28(%rsp), %rdx
jge 0x7a3e7
movq 0x18(%rbx), %rdi
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
je 0x7a3ad
movq 0xd0(%r13), %rcx
movq %r15, %rsi
callq 0xe3cb7
movq 0xd0(%r13), %rax
addq %rax, (%rsp)
movq 0x18(%r12), %rdx
movq 0xc8(%r13), %rcx
subq %rcx, %rdx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0xe3cb7
xorl %r14d, %r14d
cmpq 0xc8(%r13), %r14
jge 0x7a3cd
movq %r14, %rax
shrq $0x3, %rax
movl %r14d, %ecx
notb %cl
movzbl (%r15,%rax), %eax
movzbl %cl, %ecx
andl $0x7, %ecx
btl %ecx, %eax
jae 0x7a394
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
jmp 0x7a39d
movq 0x10(%rsp), %rax
movb (%rax,%r14), %al
movzbl %al, %esi
movq %r12, %rdi
callq 0xe5bc5
incq %r14
jmp 0x7a361
movq 0xc8(%r13), %rax
movq %rdx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xe45f4
movq 0xc8(%r13), %rax
addq %rax, (%rsp)
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rax
jmp 0x7a2fc
incq %rax
jmp 0x7a2e8
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0xa2653(%rip), %rsi     # 0x11ca49
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq %rbx, %rdi
movq %r15, %rsi
callq 0xded32
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xded32
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdx
movq 0x18(%rsp), %rax
subq 0x18(%rax), %rdx
movq 0x18(%r12), %rcx
leaq 0x8b838(%rip), %rsi     # 0x105c78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x7a400