// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movq 0x10(%rdx), %rax
addq 0x18(%rdx), %rax
movq %rax, 0x30(%rsp)
movq 0xc8(%rsi), %rsi
callq 0xe969b
movq %rax, 0x10(%rsp)
movq 0xd0(%r13), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq %r14, 0x18(%rsp)
movq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x80(%r13)
jle 0x7f3d7
xorl %eax, %eax
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x20(%rsp)
jle 0x7f3c2
xorl %r14d, %r14d
movq 0x8(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
jge 0x7f3fd
testq %r14, %r14
je 0x7f38f
movq 0x18(%rbx), %rdi
movq 0xd0(%r13), %rcx
movq %r15, %rsi
callq 0xeeee8
movq 0xd0(%r13), %rax
addq %rax, 0x8(%rsp)
movq 0x18(%r12), %rdx
movq 0xc8(%r13), %rcx
subq %rcx, %rdx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0xeeee8
cmpq $0x0, 0xc8(%r13)
jle 0x7f3b4
xorl %ebp, %ebp
movq %rbp, %rax
shrq $0x3, %rax
movl %ebp, %ecx
notb %cl
movzbl (%r15,%rax), %eax
movzbl %cl, %ecx
andl $0x7, %ecx
btl %ecx, %eax
jae 0x7f36e
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef14d
jmp 0x7f376
movq 0x10(%rsp), %rax
movb (%rax,%rbp), %al
movzbl %al, %esi
movq %r12, %rdi
callq 0xf11be
incq %rbp
cmpq 0xc8(%r13), %rbp
jl 0x7f343
jmp 0x7f3b4
movq 0x18(%rbx), %rdi
movq 0xc8(%r13), %rax
movq %rdx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xefabc
movq 0xc8(%r13), %rax
addq %rax, 0x8(%rsp)
incq %r14
cmpq 0x20(%rsp), %r14
jne 0x7f2e3
movq 0x28(%rsp), %rax
incq %rax
cmpq 0x80(%r13), %rax
jl 0x7f2cf
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rax
subq 0x18(%rax), %rdx
movq 0x18(%r12), %rcx
leaq 0x93897(%rip), %rsi     # 0x112c88
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x7f416
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0xaa61d(%rip), %rsi     # 0x129a29
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)