// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/pnm.c
// Function: do_image_pgm_ppm_pam_binary
static int do_image_pgm_ppm_pam_binary(deark *c, lctx *d, struct page_ctx *pg,
	dbuf *inf, i64 pos1)
{
	de_bitmap *img = NULL;
	de_bitmap *imglo = NULL;
	i64 rowspan;
	int nsamples_per_pixel; // For both input and output
	u8 nbytes_per_sample;
	i64 i, j;
	i64 pos = pos1;
	UI samp_ori[4];
	u8 samp_adj[4]; // most significant 8 bits
	u8 samp_adj_lo[4];
	u32 clr;
	int retval = 0;

	de_zeromem(samp_adj_lo, sizeof(samp_adj_lo));

	if(pg->fmt==FMT_PAM) {
		nsamples_per_pixel = pg->pam_depth;

		if((pg->pam_subtype==PAMSUBTYPE_GRAY && !pg->has_alpha && nsamples_per_pixel==1) ||
			(pg->pam_subtype==PAMSUBTYPE_GRAY && pg->has_alpha && nsamples_per_pixel==2) ||
			(pg->pam_subtype==PAMSUBTYPE_RGB && !pg->has_alpha && nsamples_per_pixel==3) ||
			(pg->pam_subtype==PAMSUBTYPE_RGB && pg->has_alpha && nsamples_per_pixel==4))
		{
			;
		}
		else {
			de_err(c, "Unsupported PAM format");
			goto done;
		}
	}
	else if(fmt_is_ppm(pg->fmt)) {
		nsamples_per_pixel = 3;
	}
	else {
		nsamples_per_pixel = 1;
	}

	if(nsamples_per_pixel<1 || nsamples_per_pixel>4) {
		de_err(c, "Unsupported samples/pixel: %d", nsamples_per_pixel);
	}

	if(pg->maxval<=255) nbytes_per_sample = 1;
	else nbytes_per_sample = 2;

	rowspan = pg->width * (i64)nsamples_per_pixel * (i64)nbytes_per_sample;
	pg->image_data_len = rowspan * pg->height;

	img = de_bitmap_create(c, pg->width, pg->height, nsamples_per_pixel);
	if(nbytes_per_sample!=1) {
		imglo = de_bitmap_create(c, pg->width, pg->height, nsamples_per_pixel);
	}

	for(j=0; j<pg->height; j++) {
		for(i=0; i<pg->width; i++) {
			int k;

			for(k=0; k<nsamples_per_pixel; k++) {
				if(nbytes_per_sample==1) {
					samp_ori[k] = dbuf_getbyte_p(inf, &pos);
				}
				else {
					samp_ori[k] = (UI)dbuf_getu16be_p(inf, &pos);
				}

				if(nbytes_per_sample==1) {
					samp_adj[k] = de_scale_n_to_255(pg->maxval, samp_ori[k]);
				}
				else {
					de_scale_n_to_16bit(pg->maxval, (int)samp_ori[k], &samp_adj[k], &samp_adj_lo[k]);
				}
			}

			switch(nsamples_per_pixel) {
			case 4:
				clr = DE_MAKE_RGBA(samp_adj[0], samp_adj[1], samp_adj[2], samp_adj[3]);
				de_bitmap_setpixel_rgba(img, i, j, clr);
				if(imglo) {
					clr = DE_MAKE_RGBA(samp_adj_lo[0], samp_adj_lo[1], samp_adj_lo[2], samp_adj_lo[3]);
					de_bitmap_setpixel_rgba(imglo, i, j, clr);
				}
				break;
			case 3:
				clr = DE_MAKE_RGB(samp_adj[0], samp_adj[1], samp_adj[2]);
				de_bitmap_setpixel_rgb(img, i, j, clr);
				if(imglo) {
					clr = DE_MAKE_RGB(samp_adj_lo[0], samp_adj_lo[1], samp_adj_lo[2]);
					de_bitmap_setpixel_rgb(imglo, i, j, clr);
				}
				break;
			case 2:
				clr = DE_MAKE_RGBA(samp_adj[0], samp_adj[0], samp_adj[0], samp_adj[1]);
				de_bitmap_setpixel_rgba(img, i, j, clr);
				if(imglo) {
					clr = DE_MAKE_RGBA(samp_adj_lo[0], samp_adj_lo[0], samp_adj_lo[0], samp_adj_lo[1]);
					de_bitmap_setpixel_rgba(imglo, i, j, clr);
				}
				break;
			default: // Assuming nsamples==1
				de_bitmap_setpixel_gray(img, i, j, samp_adj[0]);
				if(imglo) {
					de_bitmap_setpixel_gray(imglo, i, j, samp_adj_lo[0]);
				}
			}
		}
	}

	de_bitmap16_write_to_file_finfo(img, imglo, NULL, DE_CREATEFLAG_OPT_IMAGE);
	retval = 1;

done:
	de_bitmap_destroy(img);
	de_bitmap_destroy(imglo);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rcx, 0x38(%rsp)
movl $0x0, 0x8(%rsp)
movl (%rsi), %eax
cmpl $0x7, %eax
jne 0xb83ca
movl 0x24(%r14), %eax
movl 0x28(%r14), %ecx
cmpl $0x1, %ecx
jne 0xb8379
movl %eax, %edx
xorl $0x1, %edx
movl $0x1, %r12d
orl 0x2c(%r14), %edx
je 0xb83de
cmpl $0x1, %ecx
jne 0xb8379
cmpl $0x0, 0x2c(%r14)
setne %dl
cmpl $0x2, %eax
sete %sil
movl $0x2, %r12d
testb %sil, %dl
jne 0xb83de
cmpl $0x2, %ecx
jne 0xb83ac
movl %eax, %edx
xorl $0x3, %edx
movl $0x3, %r12d
orl 0x2c(%r14), %edx
je 0xb83de
cmpl $0x2, %ecx
jne 0xb83ac
cmpl $0x0, 0x2c(%r14)
setne %cl
cmpl $0x4, %eax
sete %al
movl $0x4, %r12d
testb %al, %cl
jne 0xb83de
leaq 0x7f787(%rip), %rsi     # 0x137b3a
xorl %r12d, %r12d
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0xb86c0
xorl %ecx, %ecx
cmpl $0x6, %eax
sete %cl
cmpl $0x3, %eax
leal 0x1(%rcx,%rcx), %r12d
cmovel %eax, %r12d
movl 0x20(%r14), %ebp
cmpl $0x100, %ebp            # imm = 0x100
setge %cl
movl %r12d, %r13d
movq 0x10(%r14), %rsi
movq %rsi, %rax
imulq %r13, %rax
shlq %cl, %rax
movq 0x18(%r14), %rdx
imulq %rdx, %rax
movq %rax, 0x38(%r14)
movq %r15, %rdi
movl %r12d, %ecx
callq 0xf2cab
movq %rax, 0x20(%rsp)
cmpl $0x100, %ebp            # imm = 0x100
movl %r12d, 0x34(%rsp)
jl 0xb843d
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %r15, %rdi
movl %r12d, %ecx
callq 0xf2cab
movq %rax, %r8
jmp 0xb8440
xorl %r8d, %r8d
cmpq $0x0, 0x18(%r14)
jle 0xb86a1
xorl %eax, %eax
leaq 0x38(%rsp), %r15
movq %r8, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%r14)
jle 0xb868f
xorl %r12d, %r12d
movq %r12, 0x28(%rsp)
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r15, %rsi
cmpl $0xff, %ebp
jle 0xb8487
callq 0xef193
jmp 0xb848f
callq 0xef14d
movzbl %al, %eax
movl %eax, 0x40(%rsp,%r12,4)
movslq 0x20(%r14), %rdi
cmpl $0xff, %ebp
jle 0xb84bc
movl 0x40(%rsp,%r12,4), %esi
leaq (%rsp,%r12), %rdx
addq $0xc, %rdx
leaq (%rsp,%r12), %rcx
addq $0x8, %rcx
callq 0xece6b
jmp 0xb84cb
movl 0x40(%rsp,%r12,4), %esi
callq 0xece35
movb %al, 0xc(%rsp,%r12)
incq %r12
cmpq %r12, %r13
jne 0xb8472
movl 0x34(%rsp), %eax
cmpl $0x2, %eax
je 0xb85d5
cmpl $0x3, %eax
je 0xb855e
cmpl $0x4, %eax
jne 0xb8642
movzbl 0xf(%rsp), %eax
shll $0x18, %eax
movzbl 0xc(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0xd(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
movzbl 0xe(%rsp), %ecx
orl %eax, %ecx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2b02
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xb8682
movzbl 0xb(%rsp), %eax
shll $0x18, %eax
movzbl 0x8(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x9(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
movzbl 0xa(%rsp), %ecx
orl %eax, %ecx
jmp 0xb8630
movzbl 0xc(%rsp), %eax
shll $0x10, %eax
movzbl 0xd(%rsp), %edx
shll $0x8, %edx
orl %eax, %edx
movzbl 0xe(%rsp), %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2afd
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xb8682
movl 0x8(%rsp), %eax
shll $0x10, %eax
movzbl 0x9(%rsp), %edx
shll $0x8, %edx
orl %eax, %edx
movzbl 0xa(%rsp), %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq %r8, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2afd
jmp 0xb867d
movzbl 0xd(%rsp), %eax
shll $0x18, %eax
movzbl 0xc(%rsp), %edx
movl %edx, %esi
shll $0x10, %esi
orl %eax, %esi
movl %edx, %ecx
shll $0x8, %ecx
orl %esi, %ecx
orl %edx, %ecx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2b02
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xb8682
movzbl 0x9(%rsp), %eax
shll $0x18, %eax
movzbl 0x8(%rsp), %edx
movl %edx, %esi
shll $0x10, %esi
orl %eax, %esi
movl %edx, %ecx
shll $0x8, %ecx
orl %esi, %ecx
orl %edx, %ecx
movq %r8, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2b02
jmp 0xb867d
movzbl 0xc(%rsp), %ecx
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2a4a
movq 0x18(%rsp), %r8
testq %r8, %r8
je 0xb8682
movzbl 0x8(%rsp), %ecx
movq %r8, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0xf2a4a
movq 0x18(%rsp), %r8
incq %r12
cmpq 0x10(%r14), %r12
jl 0xb846a
movq 0x10(%rsp), %rax
incq %rax
cmpq 0x18(%r14), %rax
jl 0xb8457
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r8, %rsi
xorl %edx, %edx
movl $0x2, %ecx
movq %r8, %r12
callq 0xf2586
movl $0x1, %ebx
movq %r14, %rdi
callq 0xf28d4
movq %r12, %rdi
callq 0xf28d4
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq