// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x19140
movq -0x18(%rbp), %rax
cmpl $0x7, (%rax)
jne 0x10ef68
movq -0x18(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, 0x28(%rax)
jne 0x10eeff
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0x10eeff
cmpl $0x1, -0x44(%rbp)
je 0x10ef4d
movq -0x18(%rbp), %rax
cmpl $0x1, 0x28(%rax)
jne 0x10ef19
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x10ef19
cmpl $0x2, -0x44(%rbp)
je 0x10ef4d
movq -0x18(%rbp), %rax
cmpl $0x2, 0x28(%rax)
jne 0x10ef33
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0x10ef33
cmpl $0x3, -0x44(%rbp)
je 0x10ef4d
movq -0x18(%rbp), %rax
cmpl $0x2, 0x28(%rax)
jne 0x10ef4f
movq -0x18(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x10ef4f
cmpl $0x4, -0x44(%rbp)
jne 0x10ef4f
jmp 0x10ef66
movq -0x8(%rbp), %rdi
leaq 0xb4820(%rip), %rsi     # 0x1c377a
movb $0x0, %al
callq 0x15bfb0
jmp 0x10f34d
jmp 0x10ef8a
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x10f370
cmpl $0x0, %eax
je 0x10ef81
movl $0x3, -0x44(%rbp)
jmp 0x10ef88
movl $0x1, -0x44(%rbp)
jmp 0x10ef8a
cmpl $0x1, -0x44(%rbp)
jl 0x10ef96
cmpl $0x4, -0x44(%rbp)
jle 0x10efab
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0xa1b40(%rip), %rsi     # 0x1b0ae4
movb $0x0, %al
callq 0x15bfb0
movq -0x18(%rbp), %rax
cmpl $0xff, 0x20(%rax)
jg 0x10efbe
movb $0x1, -0x45(%rbp)
jmp 0x10efc2
movb $0x2, -0x45(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x44(%rbp), %rcx
imulq %rcx, %rax
movzbl -0x45(%rbp), %ecx
imulq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movl -0x44(%rbp), %ecx
callq 0x16b750
movq %rax, -0x30(%rbp)
movzbl -0x45(%rbp), %eax
cmpl $0x1, %eax
je 0x10f03c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movl -0x44(%rbp), %ecx
callq 0x16b750
movq %rax, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x10f330
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x10f31d
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x10f144
movzbl -0x45(%rbp), %eax
cmpl $0x1, %eax
jne 0x10f0af
movq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x165170
movzbl %al, %ecx
movslq -0x84(%rbp), %rax
movl %ecx, -0x70(%rbp,%rax,4)
jmp 0x10f0c9
movq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x165210
movl %eax, %ecx
movslq -0x84(%rbp), %rax
movl %ecx, -0x70(%rbp,%rax,4)
movzbl -0x45(%rbp), %eax
cmpl $0x1, %eax
jne 0x10f0fb
movq -0x18(%rbp), %rax
movslq 0x20(%rax), %rdi
movslq -0x84(%rbp), %rax
movl -0x70(%rbp,%rax,4), %eax
movl %eax, %esi
callq 0x1617b0
movb %al, %cl
movslq -0x84(%rbp), %rax
movb %cl, -0x74(%rbp,%rax)
jmp 0x10f12e
movq -0x18(%rbp), %rax
movl 0x20(%rax), %edi
movslq -0x84(%rbp), %rax
movl -0x70(%rbp,%rax,4), %esi
movslq -0x84(%rbp), %rax
leaq -0x74(%rbp), %rdx
addq %rax, %rdx
movslq -0x84(%rbp), %rax
leaq -0x78(%rbp), %rcx
addq %rax, %rcx
callq 0x161810
jmp 0x10f130
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x10f07a
movl -0x44(%rbp), %eax
movl %eax, -0x88(%rbp)
subl $0x2, %eax
je 0x10f262
jmp 0x10f158
movl -0x88(%rbp), %eax
subl $0x3, %eax
je 0x10f1f2
jmp 0x10f169
movl -0x88(%rbp), %eax
subl $0x4, %eax
jne 0x10f2d7
jmp 0x10f17a
movzbl -0x71(%rbp), %eax
shll $0x18, %eax
movzbl -0x74(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x73(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x72(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b350
cmpq $0x0, -0x38(%rbp)
je 0x10f1ed
movzbl -0x75(%rbp), %eax
shll $0x18, %eax
movzbl -0x78(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x77(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x76(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b350
jmp 0x10f30a
movzbl -0x74(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0x73(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x72(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b310
cmpq $0x0, -0x38(%rbp)
je 0x10f25d
movzbl -0x78(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0x77(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x76(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b310
jmp 0x10f30a
movzbl -0x73(%rbp), %eax
shll $0x18, %eax
movzbl -0x74(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x74(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x74(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b350
cmpq $0x0, -0x38(%rbp)
je 0x10f2d5
movzbl -0x77(%rbp), %eax
shll $0x18, %eax
movzbl -0x78(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x78(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x78(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x7c(%rbp)
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl -0x7c(%rbp), %ecx
callq 0x16b350
jmp 0x10f30a
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movzbl -0x74(%rbp), %ecx
callq 0x16b1c0
cmpq $0x0, -0x38(%rbp)
je 0x10f308
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movzbl -0x78(%rbp), %ecx
callq 0x16b1c0
jmp 0x10f30a
jmp 0x10f30c
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x10f05e
jmp 0x10f31f
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x10f044
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x2, %ecx
callq 0x16aac0
movl $0x1, -0x80(%rbp)
movq -0x30(%rbp), %rdi
callq 0x16af60
movq -0x38(%rbp), %rdi
callq 0x16af60
movl -0x80(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)