// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, (%rsp)
movq %rdi, %r15
movq %rcx, 0x20(%rsp)
movl (%rsi), %ecx
cmpl $0x7, %ecx
jne 0xb0661
movl 0x24(%r14), %eax
movl 0x28(%r14), %ecx
cmpl $0x2, %ecx
je 0xb0675
cmpl $0x1, %ecx
jne 0xb093b
movl 0x2c(%r14), %ecx
movl %eax, %edx
xorl $0x1, %edx
orl %ecx, %edx
jne 0xb0686
pushq $0x1
jmp 0xb06b2
xorl %eax, %eax
cmpl $0x6, %ecx
sete %al
cmpl $0x3, %ecx
leal 0x1(%rax,%rax), %eax
cmovel %ecx, %eax
jmp 0xb06b3
movl 0x2c(%r14), %ecx
movl %eax, %edx
xorl $0x3, %edx
orl %ecx, %edx
jne 0xb069d
pushq $0x3
jmp 0xb06b2
testl %ecx, %ecx
setne %cl
cmpl $0x2, %eax
sete %al
testb %al, %cl
je 0xb093b
pushq $0x2
jmp 0xb06b2
testl %ecx, %ecx
setne %cl
cmpl $0x4, %eax
sete %al
testb %al, %cl
je 0xb093b
pushq $0x4
popq %rax
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 0xe7437
movq %rax, %r13
cmpl $0x100, %ebp            # imm = 0x100
jl 0xb070c
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %r15, %rdi
movl %r12d, %ecx
callq 0xe7437
movq %rax, 0x8(%rsp)
jmp 0xb0715
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
movq %r13, 0x10(%rsp)
cmpq 0x18(%r14), %rdx
jge 0xb0921
xorl %r15d, %r15d
cmpq 0x10(%r14), %r15
jge 0xb0919
movq %r15, 0x18(%rsp)
xorl %r13d, %r13d
cmpq %r13, %r12
je 0xb078f
movq %rdx, %r15
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
cmpl $0xff, %ebp
jle 0xb0771
callq 0xe3f6e
movl 0x20(%r14), %edi
leaq (%rsp,%r13), %rdx
addq $0x4, %rdx
leaq (%rsp,%r13), %rcx
movl %eax, %esi
callq 0xe1d74
jmp 0xb0787
callq 0xe3f2a
movslq 0x20(%r14), %rdi
movzbl %al, %esi
callq 0xe1d3e
movb %al, 0x4(%rsp,%r13)
incq %r13
movq %r15, %rdx
jmp 0xb073b
cmpq $0x2, %r12
je 0xb087b
cmpl $0x3, %r12d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r15
je 0xb0816
cmpl $0x4, %r12d
jne 0xb08e0
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 0xe728c
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb0909
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 0xb08d3
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 0xe7287
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb0909
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 0xe7287
jmp 0xb0909
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 0xe728c
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb0909
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 0xe728c
jmp 0xb0909
movzbl 0x4(%rsp), %ecx
movq %r15, %rsi
movq %rdx, %r13
callq 0xe71da
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xb0909
movzbl (%rsp), %ecx
movq %r15, %rsi
movq %r13, %rdx
callq 0xe71da
incq %r15
movq %r13, %rdx
movq 0x10(%rsp), %r13
jmp 0xb0729
incq %rdx
jmp 0xb071c
pushq $0x2
popq %rcx
movq %r13, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0xe6d4b
pushq $0x1
popq %rbp
jmp 0xb0953
leaq 0x7a284(%rip), %rsi     # 0x12abc6
xorl %ebx, %ebx
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %r13, %rdi
callq 0xe7084
movq %rbx, %rdi
callq 0xe7084
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq