// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, 0x10(%rsp)
movq 0xd0(%r13), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq %r14, 0x18(%rsp)
movq 0x18(%r14), %rcx
movq %rcx, 0x8(%rsp)
movq 0x80(%r13), %rax
movq %rcx, %rdx
testq %rax, %rax
jle 0x7ee3b
xorl %ecx, %ecx
cmpq $0x0, 0x20(%rsp)
jle 0x7ee21
movq %rcx, 0x28(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
jge 0x7ee56
movq 0x18(%rbx), %rdi
testq %r14, %r14
je 0x7ede6
movq 0xd0(%r13), %rcx
movq %r15, %rsi
callq 0xee8d4
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 0xee8d4
cmpq $0x0, 0xc8(%r13)
jle 0x7ee07
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 0x7edc5
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeeb4b
jmp 0x7edcd
movq 0x10(%rsp), %rax
movb (%rax,%rbp), %al
movzbl %al, %esi
movq %r12, %rdi
callq 0xf0bd6
incq %rbp
cmpq 0xc8(%r13), %rbp
jl 0x7ed9a
jmp 0x7ee07
movq 0xc8(%r13), %rax
movq %rdx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xef481
movq 0xc8(%r13), %rax
addq %rax, 0x8(%rsp)
incq %r14
cmpq 0x20(%rsp), %r14
jne 0x7ed3a
movq 0x80(%r13), %rax
movq 0x28(%rsp), %rcx
incq %rcx
cmpq %rax, %rcx
jl 0x7ed26
movq 0x8(%rsp), %rdx
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rcx
subq %rcx, %rdx
movq 0x18(%r12), %rcx
leaq 0x93e4e(%rip), %rsi     # 0x112c98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x7ee6f
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edx
leaq 0xaae34(%rip), %rsi     # 0x129c99
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe8be2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop