// File: /workspace/github_c_makefile_star2_O3/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 0xd8616(%rip), %rsi     # 0x11c247
movq %rcx, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeed61
movq %rax, 0x38(%r14)
leaq 0xd8602(%rip), %rsi     # 0x11c261
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x4(%r13), %rsi
callq 0xeed61
leaq 0xd85fe(%rip), %rsi     # 0x11c27e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x8(%r13), %rsi
callq 0xeebc8
movq %rax, (%r14)
movq 0x18(%rbx), %rdi
leaq 0xc(%r13), %rsi
callq 0xeebc8
movq %rax, 0x8(%r14)
movl (%r14), %edx
leaq 0xd85dd(%rip), %rsi     # 0x11c295
movq %rbx, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x18(%r13), %rsi
callq 0xeed61
movq %rax, 0x18(%r14)
leaq 0x102452(%rip), %rsi    # 0x14612e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
leaq 0x1c(%r13), %rsi
callq 0xeed61
movq %rax, 0x10(%r14)
leaq 0xd85b0(%rip), %rsi     # 0x11c2b0
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, 0x8(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x24(%r13), %rsi
callq 0xeed61
movq %rax, %r12
cmpq $0x4, %rax
ja 0x43d37
leaq 0xd8392(%rip), %rax     # 0x11c0c0
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmp 0x43d3e
leaq 0xe8413(%rip), %rcx     # 0x12c151
leaq 0xd857a(%rip), %rsi     # 0x11c2bf
movq 0x8(%rsp), %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x10(%r14)
movq 0x18(%r14), %rdx
je 0x43dcd
leaq -0x4(%rdx), %rax
rolq $0x3e, %rax
cmpq $0x4, %rax
jb 0x43d77
cmpq $0x5, %rax
jne 0x44033
cmpq $0xc, %rdx
setne %al
cmpq $0x2, %r12
sete %cl
orb %al, %cl
jne 0x43da3
leaq 0xd85a5(%rip), %rsi     # 0x11c335
movq 0x8(%rsp), %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9295
movq 0x18(%r14), %rdx
cmpq $0x10, %rdx
jne 0x43de6
cmpl $0x0, 0x10(%r15)
jne 0x43de6
leaq 0xd85c5(%rip), %rsi     # 0x11c37c
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe8faa
movl $0x1, 0x10(%r15)
jmp 0x43de6
cmpq $0x8, %rdx
ja 0x44139
movl $0x116, %eax            # imm = 0x116
btq %rdx, %rax
jae 0x44139
leaq 0x28(%r13), %rbp
movq 0x38(%r14), %rbx
leaq 0xedb88(%rip), %rsi     # 0x13197d
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
cmpq $0x4, %r12
ja 0x4401f
movq %r15, 0x20(%rsp)
leaq -0x28(%rbx), %r15
leaq 0xd8142(%rip), %rax     # 0x11bf60
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 0xf1388
movq %rax, %r12
jmp 0x43f3e
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movl $0x3, %r8d
jmp 0x43f2d
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq %rbp, 0x18(%rsp)
cmpq $0x29, %rbx
jl 0x43f3e
addq %r13, %rbx
leaq 0x18(%rsp), %r13
leaq 0x15(%rsp), %rbp
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movq %r13, %rsi
callq 0xeebeb
movl %eax, %r15d
shrl $0xc, %r15d
andl $0xf, %r15d
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 %r15
movl $0x3, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0xef547
decq %r15
jne 0x43ef2
cmpq %rbx, 0x18(%rsp)
jl 0x43e99
jmp 0x43f3e
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movl $0x2, %r8d
movq %rbx, %rdi
movq %rax, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x4434f
cmpq $0x0, 0x30(%r14)
movq 0x20(%rsp), %rbx
jne 0x442f4
movq (%r14), %rsi
movq 0x18(%r14), %rdi
cmpq $0xc, %rdi
je 0x4403f
cmpq $0x18, %rdi
jne 0x4405a
leaq (%rsi,%rsi,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 0x44051
movl $0x4000, %esi           # imm = 0x4000
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
cmpq $0x29, %rbx
jl 0x43f3e
addq %r13, %rbx
movq 0x8(%rsp), %r13
movq 0x18(%r13), %rdi
movq %rbp, %rsi
callq 0xeead3
leaq 0x1(%rbp), %r15
movq 0x18(%r13), %rdi
testb %al, %al
js 0x43ff9
incb %al
movzbl %al, %r13d
movq %r15, %rsi
callq 0xeead3
addq $0x2, %rbp
movzbl %al, %esi
movq %r12, %rdi
movq %r13, %rdx
callq 0xf0eba
jmp 0x44015
movzbl %al, %eax
movl $0x100, %ebp            # imm = 0x100
subq %rax, %rbp
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0xef481
addq %r15, %rbp
cmpq %rbx, %rbp
jl 0x43fba
jmp 0x43f3e
leaq 0xd83a5(%rip), %rsi     # 0x11c3cb
movq 0x8(%rsp), %rdi
movl %r12d, %edx
jmp 0x44145
leaq 0xd82cf(%rip), %rsi     # 0x11c309
jmp 0x44140
leaq (%rsi,%rsi,2), %rax
movq %rax, 0x28(%r14)
testb $0x1, %sil
je 0x44055
addq $0x3, %rax
movq %rax, 0x28(%r14)
movq %rsi, %rdx
jmp 0x44087
imulq %rsi, %rdi
movl $0x20, %esi
callq 0xe9cb4
leaq 0x7(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x3, %rcx
movq %rcx, 0x28(%r14)
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 0xf26a5
movq %rax, %r13
movq %rax, 0x30(%r14)
movq 0x18(%r14), %rcx
leaq -0x1(%rcx), %rax
cmpq $0xb, %rax
ja 0x44107
leaq 0xd7eb1(%rip), %rdx     # 0x11bf74
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
leaq 0x40(%r14), %rdi
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xf3bdb
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 0xf2f79
jmp 0x442f4
cmpq $0x10, %rcx
je 0x4427f
cmpq $0x18, %rcx
jne 0x442f4
movq 0x28(%r14), %rdx
movl $0x3, %ecx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %r8
xorl %r9d, %r9d
callq 0xf3279
jmp 0x442f4
leaq 0xd8199(%rip), %rsi     # 0x11c2d9
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x44301
cmpq $0x0, 0x10(%r14)
je 0x440cc
movaps 0xd7ead(%rip), %xmm0    # 0x11c010
movups %xmm0, 0x70(%r14)
movaps 0xd7e91(%rip), %xmm0    # 0x11c000
movups %xmm0, 0x60(%r14)
movaps 0xd7e75(%rip), %xmm0    # 0x11bff0
movups %xmm0, 0x50(%r14)
movaps 0xd7e59(%rip), %xmm0    # 0x11bfe0
movups %xmm0, 0x40(%r14)
movl $0x4, %edx
jmp 0x440e3
cmpq $0x0, 0x10(%r14)
je 0x440cc
cmpb $0x0, 0x28(%rbx)
jne 0x44260
movb $0x1, 0x28(%rbx)
xorl %eax, %eax
leaq 0xd7e68(%rip), %rcx     # 0x11c020
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
movl $0xff, %esi
movl $0xffff, %edi           # imm = 0xFFFF
leaq -0x6c(%rax), %r8
cmpq $0x27, %r8
ja 0x441db
movl -0x1b0(%rcx,%rax,4), %r8d
jmp 0x4424c
leal -0x28(%rax), %r8d
cmpq $0x6c, %rax
cmovbl %eax, %r8d
imulq $0x38e38e39, %r8, %r9   # imm = 0x38E38E39
movq %r8, %r10
imulq %rdx, %r10
shrq $0x22, %r10
addl %r10d, %r10d
leal (%r10,%r10,2), %r10d
movl %r8d, %r11d
subl %r10d, %r11d
shrq $0x23, %r9
leal (,%r9,4), %r10d
leal (%r10,%r10,8), %r10d
subl %r10d, %r8d
andl %esi, %r8d
imull $0xab, %r8d, %r10d
shrl $0xa, %r10d
imull $0x33, %r9d, %r8d
imull $0x330000, %r11d, %r9d  # imm = 0x330000
imull $0x3300, %r10d, %r10d   # imm = 0x3300
andl %edi, %r10d
orl %r9d, %r10d
andl %esi, %r8d
orl %r10d, %r8d
orl $0xff000000, %r8d       # imm = 0xFF000000
movl %r8d, 0x2c(%rbx,%rax,4)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x441c7
leaq 0x40(%r14), %rdi
addq $0x2c, %rbx
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rsi
callq 0x181e0
movl $0x8, %edx
jmp 0x440e3
movq 0x8(%r14), %rcx
testq %rcx, %rcx
jle 0x442f4
movq (%r14), %rax
xorl %r15d, %r15d
testq %rax, %rax
jle 0x442ec
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x28(%r14), %rsi
imulq %r15, %rsi
addq %rbx, %rsi
movq %r12, %rdi
callq 0xeebc8
cmpl $0x0, 0x20(%r14)
je 0x442c2
movzbl %ah, %eax
imull $0x10101, %eax, %ecx    # imm = 0x10101
orl $0xff000000, %ecx       # imm = 0xFF000000
jmp 0x442cb
movl %eax, %edi
callq 0xec844
movl %eax, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf24ba
incq %rbp
movq (%r14), %rax
addq $0x2, %rbx
cmpq %rax, %rbp
jl 0x44297
movq 0x8(%r14), %rcx
incq %r15
cmpq %rcx, %r15
jl 0x4428e
testq %r12, %r12
je 0x44301
movq %r12, %rdi
callq 0xf052b
movq 0x8(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq