// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq %rcx, 0x20(%rsp)
movl $0x0, (%rsp)
movl (%rsi), %ecx
cmpl $0x7, %ecx
jne 0xb77bc
movl 0x24(%r14), %ecx
movl 0x28(%r14), %eax
cmpl $0x2, %eax
je 0xb7a74
cmpl $0x1, %eax
jne 0xb7aa2
movl 0x2c(%r14), %edx
movl %ecx, %esi
xorl $0x1, %esi
movl $0x1, %eax
orl %edx, %esi
je 0xb77ce
testl %edx, %edx
setne %dl
cmpl $0x2, %ecx
sete %cl
movl $0x2, %eax
jmp 0xb7a9a
xorl %eax, %eax
cmpl $0x6, %ecx
sete %al
cmpl $0x3, %ecx
leal 0x1(%rax,%rax), %eax
cmovel %ecx, %eax
movl 0x20(%r14), %ebp
cmpl $0x100, %ebp            # imm = 0x100
setge %cl
movl %eax, %r12d
movq 0x10(%r14), %rsi
movq %rsi, %rdi
imulq %r12, %rdi
shlq %cl, %rdi
movq 0x18(%r14), %rdx
imulq %rdx, %rdi
movq %rdi, 0x38(%r14)
movq %r15, %rdi
movl %eax, %ecx
callq 0xf266a
movq %rax, %r13
cmpl $0x100, %ebp            # imm = 0x100
jl 0xb7827
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %r15, %rdi
movl %r12d, %ecx
callq 0xf266a
movq %rax, 0x8(%rsp)
jmp 0xb7830
movq $0x0, 0x8(%rsp)
movq 0x18(%r14), %rcx
testq %rcx, %rcx
jle 0xb7a56
movq 0x10(%r14), %rax
xorl %edx, %edx
movq %r13, 0x10(%rsp)
testq %rax, %rax
jle 0xb7a4a
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
movq %rdx, %r15
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
cmpl $0xff, %ebp
jle 0xb788e
callq 0xeeb91
movl 0x20(%r14), %edi
leaq (%rsp,%r13), %rdx
addq $0x4, %rdx
leaq (%rsp,%r13), %rcx
movl %eax, %esi
callq 0xec812
jmp 0xb78a4
callq 0xeeb4b
movslq 0x20(%r14), %rdi
movzbl %al, %esi
callq 0xec7dc
movb %al, 0x4(%rsp,%r13)
incq %r13
cmpq %r13, %r12
movq %r15, %rdx
jne 0xb785d
cmpq $0x2, %r12
je 0xb799b
cmpl $0x3, %r12d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r15
je 0xb7936
cmpl $0x4, %r12d
jne 0xb7a00
movzbl 0x7(%rsp), %eax
shll $0x18, %eax
movzbl 0x4(%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x5(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
movzbl 0x6(%rsp), %ecx
orl %eax, %ecx
movq %r15, %rsi
movq %rdx, %r13
callq 0xf24bf
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb7a29
movzbl 0x3(%rsp), %eax
shll $0x18, %eax
movzbl (%rsp), %ecx
shll $0x10, %ecx
orl %eax, %ecx
movzbl 0x1(%rsp), %eax
shll $0x8, %eax
orl %ecx, %eax
movzbl 0x2(%rsp), %ecx
orl %eax, %ecx
jmp 0xb79f3
movzbl 0x4(%rsp), %eax
shll $0x10, %eax
movzbl 0x5(%rsp), %esi
shll $0x8, %esi
orl %eax, %esi
movzbl 0x6(%rsp), %ecx
orl %esi, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq %r15, %rsi
movq %rdx, %r13
callq 0xf24ba
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb7a29
movl (%rsp), %eax
shll $0x10, %eax
movzbl 0x1(%rsp), %edx
shll $0x8, %edx
orl %eax, %edx
movzbl 0x2(%rsp), %ecx
orl %edx, %ecx
orl $0xff000000, %ecx       # imm = 0xFF000000
movq %r15, %rsi
movq %r13, %rdx
callq 0xf24ba
jmp 0xb7a29
movzbl 0x5(%rsp), %eax
shll $0x18, %eax
movzbl 0x4(%rsp), %edi
movl %edi, %esi
shll $0x10, %esi
orl %eax, %esi
movl %edi, %ecx
shll $0x8, %ecx
orl %esi, %ecx
orl %edi, %ecx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %rdx, %r13
callq 0xf24bf
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb7a29
movzbl 0x1(%rsp), %eax
shll $0x18, %eax
movzbl (%rsp), %edx
movl %edx, %esi
shll $0x10, %esi
orl %eax, %esi
movl %edx, %ecx
shll $0x8, %ecx
orl %esi, %ecx
orl %edx, %ecx
movq %r15, %rsi
movq %r13, %rdx
callq 0xf24bf
jmp 0xb7a29
movzbl 0x4(%rsp), %ecx
movq %r15, %rsi
movq %rdx, %r13
callq 0xf2405
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb7a29
movzbl (%rsp), %ecx
movq %r15, %rsi
movq %r13, %rdx
callq 0xf2405
incq %r15
movq 0x10(%r14), %rax
movq %r15, 0x18(%rsp)
cmpq %rax, %r15
movq %r13, %rdx
movq 0x10(%rsp), %r13
jl 0xb785a
movq 0x18(%r14), %rcx
incq %rdx
cmpq %rcx, %rdx
jl 0xb7848
movq %r13, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
movl $0x2, %ecx
callq 0xf1f1a
movl $0x1, %ebx
jmp 0xb7abb
movl 0x2c(%r14), %edx
movl %ecx, %esi
xorl $0x3, %esi
movl $0x3, %eax
orl %edx, %esi
je 0xb77ce
testl %edx, %edx
setne %dl
cmpl $0x4, %ecx
sete %cl
movl $0x4, %eax
testb %cl, %dl
jne 0xb77ce
leaq 0x8041d(%rip), %rsi     # 0x137ec6
xorl %r14d, %r14d
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r13, %rdi
callq 0xf228d
movq %r14, %rdi
callq 0xf228d
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq