// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rdi, %rbx
leaq 0xd8521(%rip), %rsi     # 0x11c137
movq %rcx, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef37e
movq %rax, 0x38(%r14)
leaq 0xd850d(%rip), %rsi     # 0x11c151
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xef37e
leaq 0xd8509(%rip), %rsi     # 0x11c16e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x8(%r13), %rsi
callq 0xef1ca
movq %rax, (%r14)
movq 0x18(%rbx), %rdi
leaq 0xc(%r13), %rsi
callq 0xef1ca
movq %rax, 0x8(%r14)
movl (%r14), %edx
leaq 0xd84e8(%rip), %rsi     # 0x11c185
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x18(%r13), %rsi
callq 0xef37e
movq %rax, 0x18(%r14)
leaq 0x101f83(%rip), %rsi    # 0x145c44
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
leaq 0x1c(%r13), %rsi
callq 0xef37e
movq %rax, 0x10(%r14)
leaq 0xd84bb(%rip), %rsi     # 0x11c1a0
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x24(%r13), %rsi
callq 0xef37e
movq %rax, %r15
cmpq $0x4, %rax
ja 0x43d1c
leaq 0xd829d(%rip), %rax     # 0x11bfb0
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmp 0x43d23
leaq 0xe8146(%rip), %rcx     # 0x12be69
leaq 0xd8485(%rip), %rsi     # 0x11c1af
movq 0x8(%rsp), %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x10(%r14)
movq 0x18(%r14), %rdx
je 0x43db1
leaq -0x4(%rdx), %rax
rolq $0x3e, %rax
cmpq $0x4, %rax
jb 0x43d5c
cmpq $0x5, %rax
jne 0x44018
cmpq $0xc, %rdx
setne %al
cmpq $0x2, %r15
sete %cl
orb %al, %cl
jne 0x43d84
leaq 0xd84b0(%rip), %rsi     # 0x11c225
movq 0x8(%rsp), %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9df7
cmpq $0x10, 0x18(%r14)
jne 0x43dca
cmpl $0x0, 0x10(%r12)
jne 0x43dca
leaq 0xd84d2(%rip), %rsi     # 0x11c26c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe9b27
movl $0x1, 0x10(%r12)
jmp 0x43dca
cmpq $0x8, %rdx
ja 0x440f8
movl $0x116, %eax            # imm = 0x116
btq %rdx, %rax
jae 0x440f8
leaq 0x28(%r13), %rbp
movq 0x38(%r14), %rbx
leaq 0xed820(%rip), %rsi     # 0x1315f9
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
cmpq $0x4, %r15
ja 0x44001
movq %r12, 0x18(%rsp)
leaq -0x28(%rbx), %rdx
leaq 0xd804e(%rip), %rax     # 0x11be50
movslq (%rax,%r15,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 0xf1988
movq %rax, %r12
jmp 0x43f25
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, %r15
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movl $0x3, %r8d
jmp 0x43f14
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movq %rbp, 0x20(%rsp)
cmpq $0x29, %rbx
jl 0x43f25
addq %r13, %rbx
leaq 0x20(%rsp), %r15
leaq 0x15(%rsp), %r13
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r15, %rsi
callq 0xef1ed
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
movl %ecx, %esi
shll $0x4, %esi
addl %ecx, %esi
movb %sil, 0x15(%rsp)
movl %edx, %ecx
shll $0x4, %ecx
addl %edx, %ecx
movb %cl, 0x16(%rsp)
movl %eax, %ecx
shll $0x4, %ecx
addl %eax, %ecx
movb %cl, 0x17(%rsp)
incq %rbp
movl $0x3, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0xefb82
decq %rbp
jne 0x43ed6
cmpq %rbx, 0x20(%rsp)
jl 0x43e80
jmp 0x43f25
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, %r15
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movl $0x2, %r8d
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x4432a
cmpq $0x0, 0x30(%r14)
jne 0x442cf
movq (%r14), %r9
movq 0x18(%r14), %rdi
cmpq $0xc, %rdi
je 0x44024
cmpq $0x18, %rdi
jne 0x4403c
leaq (%r9,%r9,2), %rax
leaq 0x5f(,%rax,8), %rax
movabsq $0x2aaaaaaaaaaaaaab, %rcx # imm = 0x2AAAAAAAAAAAAAAB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x4, %rdx
addq %rax, %rdx
shlq $0x2, %rdx
leaq (%rdx,%rdx,2), %rax
jmp 0x44036
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
cmpq $0x29, %rbx
jl 0x43f25
addq %r13, %rbx
movq 0x8(%rsp), %r13
movq 0x18(%r13), %rdi
movq %rbp, %rsi
callq 0xef0d5
leaq 0x1(%rbp), %r15
movq 0x18(%r13), %rdi
testb %al, %al
js 0x43fdb
incb %al
movzbl %al, %r13d
movq %r15, %rsi
callq 0xef0d5
addq $0x2, %rbp
movzbl %al, %esi
movq %r12, %rdi
movq %r13, %rdx
callq 0xf14bd
jmp 0x43ff7
movzbl %al, %eax
movl $0x100, %ebp            # imm = 0x100
subq %rax, %rbp
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xefabc
addq %r15, %rbp
cmpq %rbx, %rbp
jl 0x43f9c
jmp 0x43f25
leaq 0xd82b3(%rip), %rsi     # 0x11c2bb
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
movl %r15d, %edx
jmp 0x44107
leaq 0xd81da(%rip), %rsi     # 0x11c1f9
jmp 0x440ff
leaq (%r9,%r9,2), %rax
movq %rax, 0x28(%r14)
testb $0x1, %r9b
je 0x44066
addq $0x3, %rax
movq %rax, 0x28(%r14)
jmp 0x44066
imulq %r9, %rdi
movl $0x20, %esi
callq 0xea80b
leaq 0x7(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x3, %rcx
movq %rcx, 0x28(%r14)
cqto
idivq 0x18(%r14)
movq %rax, %r9
xorl %eax, %eax
cmpq $0x0, 0x10(%r14)
setne %al
leal 0x1(%rax,%rax), %r8d
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movq 0x8(%rsp), %rdi
movq %r9, %rdx
callq 0xf2ce6
movq %rax, %r13
movq %rax, 0x30(%r14)
movq 0x18(%r14), %rcx
leaq -0x1(%rcx), %rax
cmpq $0xb, %rax
ja 0x440c6
leaq 0xd7dbf(%rip), %rdx     # 0x11be64
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0x40(%r14), %rdi
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xf41f4
jmp 0x442ac
cmpq $0x10, %rcx
je 0x44153
cmpq $0x18, %rcx
jne 0x442cf
movq 0x28(%r14), %rdx
movl $0x3, %ecx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %r8
xorl %r9d, %r9d
callq 0xf38d9
jmp 0x442cf
leaq 0xd80ca(%rip), %rsi     # 0x11c1c9
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x442cf
cmpq $0x0, 0x10(%r14)
je 0x441c7
movaps 0xd7ddb(%rip), %xmm0    # 0x11bf00
movups %xmm0, 0x70(%r14)
movaps 0xd7dbf(%rip), %xmm0    # 0x11bef0
movups %xmm0, 0x60(%r14)
movaps 0xd7da3(%rip), %xmm0    # 0x11bee0
movups %xmm0, 0x50(%r14)
movaps 0xd7d87(%rip), %xmm0    # 0x11bed0
movups %xmm0, 0x40(%r14)
jmp 0x442ac
cmpq $0x0, 0x8(%r14)
jle 0x442cf
xorl %r15d, %r15d
cmpq $0x0, (%r14)
jle 0x441b9
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x28(%r14), %rsi
imulq %r15, %rsi
addq %rbx, %rsi
movq %r12, %rdi
callq 0xef1ca
cmpl $0x0, 0x20(%r14)
je 0x44196
movzbl %ah, %eax
imull $0x10101, %eax, %ecx    # imm = 0x10101
orl $0xff000000, %ecx       # imm = 0xFF000000
jmp 0x4419f
movl %eax, %edi
callq 0xece9d
movl %eax, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf2afd
incq %rbp
addq $0x2, %rbx
cmpq (%r14), %rbp
jl 0x4416b
incq %r15
cmpq 0x8(%r14), %r15
jl 0x44161
jmp 0x442cf
cmpq $0x8, %rcx
jne 0x440ae
cmpq $0x0, 0x10(%r14)
je 0x440ae
movq 0x18(%rsp), %rax
cmpb $0x0, 0x28(%rax)
jne 0x44295
movq 0x18(%rsp), %rax
movb $0x1, 0x28(%rax)
xorl %eax, %eax
leaq 0xd7d13(%rip), %rcx     # 0x11bf10
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
leaq -0x6c(%rax), %rsi
cmpq $0x27, %rsi
ja 0x44215
movl -0x1b0(%rcx,%rax,4), %esi
jmp 0x4427d
leal -0x28(%rax), %esi
cmpq $0x6c, %rax
cmovbl %eax, %esi
imulq $0x38e38e39, %rsi, %rdi # imm = 0x38E38E39
movq %rsi, %r8
imulq %rdx, %r8
shrq $0x22, %r8
addl %r8d, %r8d
leal (%r8,%r8,2), %r8d
movl %esi, %r9d
subl %r8d, %r9d
shrq $0x23, %rdi
leal (,%rdi,4), %r8d
leal (%r8,%r8,8), %r8d
subl %r8d, %esi
imulq %rdx, %rsi
shrq $0x22, %rsi
imull $0x33, %edi, %edi
imull $0x330000, %r9d, %r8d   # imm = 0x330000
imull $0x3300, %esi, %esi     # imm = 0x3300
movzwl %si, %r9d
orl %r8d, %r9d
movzbl %dil, %esi
orl %r9d, %esi
orl $0xff000000, %esi       # imm = 0xFF000000
movq 0x18(%rsp), %rdi
movl %esi, 0x2c(%rdi,%rax,4)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x44202
leaq 0x40(%r14), %rdi
movq 0x18(%rsp), %rsi
addq $0x2c, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x181e0
movq 0x18(%r14), %rdx
movq 0x28(%r14), %rcx
addq $0x40, %r14
movl $0x1, (%rsp)
movq %r12, %rdi
xorl %esi, %esi
movq %r14, %r8
movq %r13, %r9
callq 0xf35b8
testq %r12, %r12
je 0x442dc
movq %r12, %rdi
callq 0xf0b13
movq 0x8(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq