// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/epocimage.c
// Function: do_read_paint_data_section
static void do_read_paint_data_section(deark *c, lctx *d,
	struct phys_image_ctx *pi, i64 pos1)
{
	i64 pixel_data_offset;
	i64 pos;
	dbuf *unc_pixels = NULL;
	i64 compression_type;
	i64 cmpr_pixels_size;

	pos = pos1;
	de_dbg(c, "paint data section at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	pi->paint_data_section_size = de_getu32le(pos);
	de_dbg(c, "paint data section size: %"I64_FMT, pi->paint_data_section_size);

	// offset within "paint data section"
	pixel_data_offset = de_getu32le(pos+4);
	de_dbg(c, "pixel data offset: %"I64_FMT, pixel_data_offset);

	pi->width = de_getu16le(pos+8);
	pi->height = de_getu16le(pos+12);
	de_dbg(c, "picture dimensions: %d"DE_CHAR_TIMES"%d", (int)pi->width, (int)pi->height);

	pi->bits_per_pixel = de_getu32le(pos+24);
	de_dbg(c, "bits/pixel: %d", (int)pi->bits_per_pixel);

	pi->color_type = de_getu32le(pos+28);
	// 0=grayscale  1=color
	de_dbg(c, "color type: %d", (int)pi->color_type);

	compression_type = de_getu32le(pos+36);
	// 0=uncompressed  1=8-bit RLE  2=12-bit RLE  3=16-bit RLE  4=24-bit RLE
	de_dbg(c, "compression type: %d (%s)", (int)compression_type,
		get_cmpr_type_name(compression_type));

	if(pi->color_type==0) {
		if(pi->bits_per_pixel!=1 && pi->bits_per_pixel!=2 && pi->bits_per_pixel!=4 &&
			pi->bits_per_pixel!=8)
		{
			de_err(c, "Unsupported bits/pixel (%d) for grayscale image", (int)pi->bits_per_pixel);
			goto done;
		}
	}
	else {
		if(pi->bits_per_pixel!=4 && pi->bits_per_pixel!=8 && pi->bits_per_pixel!=12 &&
			pi->bits_per_pixel!=16 && pi->bits_per_pixel!=24)
		{
			de_err(c, "Unsupported bits/pixel (%d) for color image", (int)pi->bits_per_pixel);
			goto done;
		}
		if(pi->bits_per_pixel==12 && compression_type!=2) {
			de_err(c, "12 bits/pixel images are not supported with this compression type (%d)",
				(int)compression_type);
		}
		if(pi->bits_per_pixel==16 && !d->warned_exp) {
			de_warn(c, "Support for this type of 16-bit image is experimental, and may not be correct.");
			d->warned_exp = 1;
		}
	}

	pos += 40;
	cmpr_pixels_size = pi->paint_data_section_size-40;
	de_dbg(c, "pixel data at %"I64_FMT, pos);

	switch(compression_type) {
	case 0: // uncompressed
		unc_pixels = dbuf_open_input_subfile(c->infile, pos, c->infile->len - pos);
		break;
	case 1: // RLE8
		unc_pixels = dbuf_create_membuf(c, 16384, 0);
		do_rle8(c, d, unc_pixels, pos, cmpr_pixels_size);
		break;
	case 2: // RLE12
		unc_pixels = dbuf_create_membuf(c, 16384, 0);
		do_rle12(c, d, unc_pixels, pos, cmpr_pixels_size);
		break;
	case 3: // RLE16
		unc_pixels = dbuf_create_membuf(c, 16384, 0);
		do_rle16_24(c, d, unc_pixels, pos, cmpr_pixels_size, 2);
		break;
	case 4: // RLE24
		unc_pixels = dbuf_create_membuf(c, 16384, 0);
		do_rle16_24(c, d, unc_pixels, pos, cmpr_pixels_size, 3);
		break;
	default:
		de_err(c, "Unsupported compression type: %d", (int)compression_type);
		goto done;
	}

	do_read_phys_image(c, d, pi, unc_pixels);

done:
	if(unc_pixels) dbuf_close(unc_pixels);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xcd3fe(%rip), %rsi     # 0x10f157
movq %rcx, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe409f
movq %rax, 0x38(%r14)
leaq 0xcd3ec(%rip), %rsi     # 0x10f171
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xe409f
leaq 0xcd3e8(%rip), %rsi     # 0x10f18e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x8(%r13), %rsi
callq 0xe3fa3
movq %rax, (%r14)
movq 0x18(%rbx), %rdi
leaq 0xc(%r13), %rsi
callq 0xe3fa3
movq %rax, 0x8(%r14)
movl (%r14), %edx
leaq 0xcd3c7(%rip), %rsi     # 0x10f1a5
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x18(%r13), %rsi
callq 0xe409f
movq %rax, 0x18(%r14)
leaq 0xf6edc(%rip), %rsi     # 0x138cde
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x1c(%r13), %rsi
callq 0xe409f
movq %rax, 0x10(%r14)
leaq 0xcd39a(%rip), %rsi     # 0x10f1c0
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x24(%r13), %rsi
callq 0xe409f
movq %rax, %r12
cmpq $0x4, %rax
ja 0x41e5d
leaq 0xcd17c(%rip), %rax     # 0x10efd0
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmp 0x41e64
leaq 0xdd07d(%rip), %rcx     # 0x11eee1
leaq 0xcd364(%rip), %rsi     # 0x10f1cf
movq 0x8(%rsp), %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x0, 0x10(%r14)
movq 0x18(%r14), %rdx
je 0x41ef7
leaq -0x4(%rdx), %rax
rolq $0x3e, %rax
cmpq $0x5, %rax
ja 0x42128
cmpq $0x4, %rax
je 0x42128
cmpq $0xc, %rdx
setne %al
cmpq $0x2, %r12
sete %cl
orb %al, %cl
jne 0x41ecd
leaq 0xcd38b(%rip), %rsi     # 0x10f245
movq 0x8(%rsp), %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xdf2a5
movq 0x18(%r14), %rdx
cmpq $0x10, %rdx
jne 0x41f10
cmpl $0x0, 0x10(%r15)
jne 0x41f10
leaq 0xcd3ab(%rip), %rsi     # 0x10f28c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xdf05a
movl $0x1, 0x10(%r15)
jmp 0x41f10
cmpq $0x8, %rdx
ja 0x4222a
movl $0x116, %eax            # imm = 0x116
btq %rdx, %rax
jae 0x4222a
leaq 0x28(%r13), %rbp
movq 0x38(%r14), %rbx
leaq 0xe2772(%rip), %rsi     # 0x124691
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
cmpq $0x4, %r12
ja 0x42114
movq %r15, 0x20(%rsp)
leaq -0x28(%rbx), %r15
leaq 0xccf28(%rip), %rax     # 0x10ee70
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x18(%rdi), %rdx
subq %rbp, %rdx
movq %rbp, %rsi
callq 0xe6248
movq %rax, %r12
jmp 0x42054
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
pushq $0x3
jmp 0x42041
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
leaq 0x18(%rsp), %rax
movq %rbp, (%rax)
addq %r13, %rbx
leaq 0x15(%rsp), %r13
pushq $0x3
popq %r15
cmpq %rbx, %rbp
jge 0x42054
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0xe3fc4
movl %eax, %ebp
shrl $0xc, %ebp
andl $0xf, %ebp
movl %eax, %ecx
shrl $0x8, %ecx
andb $0xf, %cl
movzbl %cl, %ecx
movl %eax, %edx
shrb $0x4, %dl
movzbl %dl, %edx
andb $0xf, %al
movzbl %al, %eax
imull $0x11, %ecx, %ecx
movb %cl, 0x15(%rsp)
imull $0x11, %edx, %ecx
movb %cl, 0x16(%rsp)
imull $0x11, %eax, %eax
movb %al, 0x17(%rsp)
incq %rbp
subq $0x1, %rbp
jb 0x42021
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xe46ba
jmp 0x4200b
movq 0x18(%rsp), %rbp
jmp 0x41fb7
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
pushq $0x2
popq %r8
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x423e6
cmpq $0x0, 0x30(%r14)
movq 0x20(%rsp), %rbx
jne 0x4221b
movq (%r14), %rsi
movq 0x18(%r14), %rdi
pushq $0x8
popq %rbp
cmpq $0xc, %rdi
je 0x42134
cmpq $0x18, %rdi
jne 0x4214f
imulq $0x18, %rsi, %rax
addq $0x5f, %rax
pushq $0x60
popq %rcx
cqto
idivq %rcx
imulq $0xc, %rax, %rax
jmp 0x42146
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
addq %r13, %rbx
cmpq %rbx, %rbp
jge 0x42054
movq 0x8(%rsp), %r13
movq 0x18(%r13), %rdi
movq %rbp, %rsi
callq 0xe3eb4
leaq 0x1(%rbp), %r15
movq 0x18(%r13), %rdi
testb %al, %al
js 0x420f6
incb %al
movzbl %al, %r13d
movq %r15, %rsi
callq 0xe3eb4
addq $0x2, %rbp
movzbl %al, %esi
movq %r12, %rdi
movq %r13, %rdx
callq 0xe5e40
jmp 0x420b2
movzbl %al, %eax
movl $0x100, %ebp            # imm = 0x100
subq %rax, %rbp
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xe45f4
addq %r15, %rbp
jmp 0x420b2
leaq 0xcd1c0(%rip), %rsi     # 0x10f2db
movq 0x8(%rsp), %rdi
movl %r12d, %edx
jmp 0x42236
leaq 0xcd0ea(%rip), %rsi     # 0x10f219
jmp 0x42231
leaq (%rsi,%rsi,2), %rax
movq %rax, 0x28(%r14)
testb $0x1, %sil
je 0x4214a
addq $0x3, %rax
movq %rax, 0x28(%r14)
movq %rsi, %rdx
jmp 0x42176
imulq %rsi, %rdi
pushq $0x20
popq %rsi
callq 0xdfbb6
movq %rax, %rcx
cqto
idivq %rbp
movq %rax, 0x28(%r14)
movq %rcx, %rax
cqto
idivq 0x18(%r14)
movq %rax, %rdx
movq (%r14), %rsi
xorl %eax, %eax
cmpq $0x0, 0x10(%r14)
setne %al
leal 0x1(,%rax,2), %r8d
movq 0x8(%r14), %rcx
movq 0x8(%rsp), %rdi
callq 0xe746f
movq %rax, %r13
movq %rax, 0x30(%r14)
movq 0x18(%r14), %rcx
leaq -0x1(%rcx), %rax
cmpq $0xb, %rax
ja 0x421f4
leaq 0xcccd2(%rip), %rdx     # 0x10ee84
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0x40(%r14), %rdi
pushq $0x1
popq %rsi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xe87a8
movq 0x18(%r14), %rbp
movq 0x28(%r14), %rcx
addq $0x40, %r14
movl $0x1, (%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movq %r14, %r8
movq %r13, %r9
callq 0xe7cbc
jmp 0x4221b
cmpq $0x10, %rcx
je 0x422cb
cmpq $0x18, %rcx
jne 0x4221b
movq 0x28(%r14), %rdx
pushq $0x3
popq %rcx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %r8
xorl %r9d, %r9d
callq 0xe7f67
testq %r12, %r12
je 0x4223d
movq %r12, %rdi
callq 0xe5591
jmp 0x4223d
leaq 0xccfb8(%rip), %rsi     # 0x10f1e9
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xdf2a5
pushq $-0x1
popq %rsi
movq 0x8(%rsp), %rdi
callq 0xdeba7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x10(%r14)
je 0x421bb
movaps 0xcccb5(%rip), %xmm0    # 0x10ef20
movups %xmm0, 0x70(%r14)
movaps 0xccc99(%rip), %xmm0    # 0x10ef10
movups %xmm0, 0x60(%r14)
movaps 0xccc7d(%rip), %xmm0    # 0x10ef00
movups %xmm0, 0x50(%r14)
movaps 0xccc61(%rip), %xmm0    # 0x10eef0
movups %xmm0, 0x40(%r14)
pushq $0x4
popq %rbp
jmp 0x421d0
cmpq $0x0, 0x10(%r14)
je 0x421bb
cmpb $0x0, 0x28(%rbx)
je 0x42331
leaq 0x40(%r14), %rdi
addq $0x2c, %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rsi
callq 0x181e0
jmp 0x421d0
xorl %r15d, %r15d
cmpq 0x8(%r14), %r15
jge 0x4221b
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq (%r14), %rbp
jge 0x4232c
movq 0x28(%r14), %rsi
imulq %r15, %rsi
addq %rbx, %rsi
movq %r12, %rdi
callq 0xe3fa3
cmpl $0x0, 0x20(%r14)
je 0x4230c
movzbl %ah, %eax
imull $0x10101, %eax, %ecx    # imm = 0x10101
orl $0xff000000, %ecx       # imm = 0xFF000000
jmp 0x42315
movl %eax, %edi
callq 0xe1da6
movl %eax, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xe7287
incq %rbp
addq $0x2, %rbx
jmp 0x422dc
incq %r15
jmp 0x422ce
movb $0x1, 0x28(%rbx)
xorl %edi, %edi
leaq 0xccbf2(%rip), %r8      # 0x10ef30
pushq $0x6
popq %r9
pushq $0x24
popq %r10
movb $0x6, %r11b
cmpq $0x100, %rdi            # imm = 0x100
je 0x422b1
leaq -0x6c(%rdi), %rax
cmpq $0x27, %rax
ja 0x4236a
movl -0x1b0(%r8,%rdi,4), %eax
jmp 0x423ad
leal -0x28(%rdi), %ecx
cmpq $0x6c, %rdi
cmovbl %edi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
movl %edx, %esi
movl %ecx, %eax
xorl %edx, %edx
divl %r10d
movl %eax, %ecx
movzbl %dl, %eax
divb %r11b
movzbl %al, %eax
imull $0x33, %ecx, %ecx
imull $0x330000, %esi, %edx   # imm = 0x330000
imull $0x3300, %eax, %eax     # imm = 0x3300
movzwl %ax, %esi
orl %edx, %esi
movzbl %cl, %eax
orl %esi, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x2c(%rbx,%rdi,4)
incq %rdi
jmp 0x42349