// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/epocimage.c
// Function: do_read_phys_image
static void do_read_phys_image(deark *c, lctx *d, struct phys_image_ctx *pi,
	dbuf *unc_pixels)
{
	de_bitmap *img = NULL;
	i64 pdwidth;
	int bypp;

	if(pi->img) goto done;
	pdwidth = pi->width;

	if(pi->bits_per_pixel==24) {
		// 24-bit images seem to be 12-byte aligned
		pi->src_rowspan = ((pi->bits_per_pixel*pi->width +95)/96)*12;
	}
	else if(pi->bits_per_pixel==12) {
		// Our decompression algorithm expands RLE12 to an RGB24 format.
		// Apparently, rows with an odd number of pixels have one pixel of
		// padding, which at this stage is 3 bytes.
		pi->src_rowspan = 3*pi->width;
		if(pi->width%2) pi->src_rowspan += 3;
	}
	else {
		i64 bits_per_row;
		// Rows are 4-byte aligned

		bits_per_row = de_pad_to_n(pi->bits_per_pixel*pi->width, 32);
		pi->src_rowspan = bits_per_row / 8;
		pdwidth = bits_per_row / pi->bits_per_pixel;
	}

	bypp = pi->color_type ? 3 : 1;
	img = de_bitmap_create2(c, pi->width, pdwidth, pi->height, bypp);
	pi->img = img;

	if(pi->bits_per_pixel==1 || pi->bits_per_pixel==2 || pi->bits_per_pixel==4 ||
		pi->bits_per_pixel==8)
	{
		if(pi->bits_per_pixel==4 && pi->color_type) {
			de_memcpy(pi->pal, g_stdpal16, sizeof(g_stdpal16));
		}
		else if(pi->bits_per_pixel==8 && pi->color_type) {
			populate_stdpal256(d);
			de_memcpy(pi->pal, d->stdpal256, sizeof(d->stdpal256));
		}
		else {
			// I have no 8-bit grayscale sample files, so I don't know if this is
			// correct for them.
			de_make_grayscale_palette(pi->pal, 1LL<<pi->bits_per_pixel, 0);
		}

		de_convert_image_paletted(unc_pixels, 0, pi->bits_per_pixel, pi->src_rowspan,
			pi->pal, img, 0x1);
	}
	else if(pi->bits_per_pixel==12 || pi->bits_per_pixel==24) {
		de_convert_image_rgb(unc_pixels, 0, pi->src_rowspan, 3, img, 0);
	}
	else if(pi->bits_per_pixel==16) {
		decode_image_16bit(c, d, pi, unc_pixels, img);
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x5dcf0
jmp 0x5df62
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x18, 0x18(%rax)
jne 0x5dd35
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
imulq (%rcx), %rax
addq $0x5f, %rax
movl $0x60, %ecx
cqto
idivq %rcx
imulq $0xc, %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
jmp 0x5ddc4
movq -0x18(%rbp), %rax
cmpq $0xc, 0x18(%rax)
jne 0x5dd79
movq -0x18(%rbp), %rax
imulq $0x3, (%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x5dd77
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x3, %rcx
movq %rcx, 0x28(%rax)
jmp 0x5ddc2
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
imulq (%rax), %rdi
movl $0x20, %esi
callq 0x15ce90
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
cqto
idivq 0x18(%rcx)
movq %rax, -0x30(%rbp)
jmp 0x5ddc4
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movl $0x1, %eax
movl $0x3, %ecx
cmpq $0x0, %rdx
cmovnel %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movl -0x34(%rbp), %r8d
callq 0x16b7f0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x18(%rax)
je 0x5de40
movq -0x18(%rbp), %rax
cmpq $0x2, 0x18(%rax)
je 0x5de40
movq -0x18(%rbp), %rax
cmpq $0x4, 0x18(%rax)
je 0x5de40
movq -0x18(%rbp), %rax
cmpq $0x8, 0x18(%rax)
jne 0x5deff
movq -0x18(%rbp), %rax
cmpq $0x4, 0x18(%rax)
jne 0x5de71
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x5de71
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
leaq 0x149d6b(%rip), %rsi    # 0x1a7bd0
movl $0x40, %edx
callq 0x191e0
jmp 0x5decd
movq -0x18(%rbp), %rax
cmpq $0x8, 0x18(%rax)
jne 0x5deac
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x5deac
movq -0x10(%rbp), %rdi
callq 0x5df70
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movq -0x10(%rbp), %rsi
addq $0x2c, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x191e0
jmp 0x5decb
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0x16d580
jmp 0x5decd
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x40, %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x1, (%rsp)
callq 0x16c520
jmp 0x5df60
movq -0x18(%rbp), %rax
cmpq $0xc, 0x18(%rax)
je 0x5df15
movq -0x18(%rbp), %rax
cmpq $0x18, 0x18(%rax)
jne 0x5df38
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
movq -0x28(%rbp), %r8
xorl %eax, %eax
movl %eax, %esi
movl $0x3, %ecx
xorl %r9d, %r9d
callq 0x16c8b0
jmp 0x5df5e
movq -0x18(%rbp), %rax
cmpq $0x10, 0x18(%rax)
jne 0x5df5c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x5dfd0
jmp 0x5df5e
jmp 0x5df60
jmp 0x5df62
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)