// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadPNMFile(char const*)
int R2Grid::
ReadPNMFile(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open image: %s\n", filename);
    return 0;
  }

  // Read magic header
  int c;
  c = fgetc(fp); if (c != 'P') { RNFail("Bad magic keyword in %s\n", filename); return 0; }
  c = fgetc(fp); if (c != '5') { RNFail("Bad magic keyword in %s\n", filename); return 0; }
  c = fgetc(fp); if (c != '\n') { RNFail("Bad magic keyword in %s\n", filename); return 0; }

  // Read width
  int width_count = 0;
  char width_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (width_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { width_string[width_count] = '\0'; break; }
    else if (!isdigit(c)) { RNFail("Bad width character %c in %s\n", c, filename); return 0; }
    else width_string[width_count++] = c;
  }

  // Check width
  if ((width_count == 0) || (width_count > 128)) {
    RNFail("Error reading width in %s\n", filename); 
    return 0; 
  }

  // Read height
  int height_count = 0;
  char height_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (height_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { height_string[height_count] = '\0'; break; }
    else if (!isdigit(c)) { RNFail("Bad height character %c in %s\n", c, filename); return 0; }
    else height_string[height_count++] = c;
  }

  // Check height
  if ((height_count == 0) || (height_count > 128)) {
    RNFail("Error reading height in %s\n", filename); 
    return 0; 
  }

  // Read max_value
  int max_value_count = 0;
  char max_value_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (max_value_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { max_value_string[max_value_count] = '\0'; break; }
    if (!isdigit(c) && (c != '.') && (c != '-')) { RNFail("Bad max_value character %c in %s\n", c, filename); return 0; }
    max_value_string[max_value_count++] = c;
  }

  // Check max_value
  if ((max_value_count == 0) || (max_value_count > 128)) {
    RNFail("Error reading max_value in %s\n", filename); 
    return 0; 
  }

  // Parse values
  int width = atoi(width_string);
  int height = atoi(height_string);
  unsigned int max_value = atoi(max_value_string);

  // Fill in grid info
  grid_resolution[0] = width;
  grid_resolution[1] = height;
  grid_row_size = width;
  grid_size = width*height;
  world_to_grid_scale_factor = 1;
  world_to_grid_transform = R2identity_affine;
  grid_to_world_transform = R2identity_affine;
  if (grid_values) delete [] grid_values;
  grid_values = new RNScalar [ grid_size ];
  if (!grid_values) {
    RNFail("Unable to allocate %d pixels for %s\n", grid_size, filename);
    return 0;
  }

  // Check endian of this computer
  // PNM files are big endian
  const unsigned short endian_test = 1;
  RNBoolean little_endian = (*((unsigned char *) &endian_test) == '\0') ? FALSE : TRUE;
  printf("Endian = %d\n", little_endian);
  
  // Read pixels
  for (int i = 0; i < grid_size; i++) {
    if (max_value <= 0xFF) {
      unsigned char value;
      fread(&value, sizeof(unsigned char), 1, fp);
      grid_values[i] = value;
    }
    else if (max_value <= 0xFFFF) {
      unsigned short value;
      fread(&value, sizeof(unsigned short), 1, fp);
      if (little_endian) { unsigned short x = value; value = (x<<8) | (x>>8); }
      grid_values[i] = value;
    }
    else if (max_value <= 0xFFFFFFFF) {
      unsigned int value;
      fread(&value, sizeof(unsigned int), 1, fp);
      if (little_endian) { unsigned int x = value; value = (x<<24) | ((x<<8) & 0x00FF0000) | ((x>>8) & 0x0000FF00) | (x>>24); }
      grid_values[i] = value;
    }
    else {
      RNFail("Unrecognized maximum value in %s\n", filename);
      return 0;
    }
  }

  // Close image
  fclose(fp);

  // Return success
  return grid_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x310, %rsp            # imm = 0x310
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x40b25(%rip), %rsi     # 0xa25ab
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x61b1a
movq %rax, %r14
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x61b11
movq %r14, %rdi
callq 0xe720
cmpl $0x35, %eax
jne 0x61b11
movq %r14, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x61b11
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x61b08
cmpl $0x20, %eax
je 0x61b3f
cmpl $0xa, %eax
je 0x61b3f
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x61bb8
movslq %r12d, %rcx
incl %r12d
movb %al, 0x210(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x61ac9
jmp 0x61b4d
leaq 0x47a0d(%rip), %rdi     # 0xa9525
jmp 0x61b21
leaq 0x479ea(%rip), %rdi     # 0xa950b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebx, %eax
addq $0x310, %rsp            # imm = 0x310
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movslq %r12d, %rax
movb $0x0, 0x210(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x61b68
leaq 0x479f6(%rip), %rdi     # 0xa955c
jmp 0x61b21
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x61baf
cmpl $0x20, %eax
je 0x61bc4
cmpl $0xa, %eax
je 0x61bc4
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x61c4b
movslq %r12d, %rcx
incl %r12d
movb %al, 0x110(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x61b70
jmp 0x61bd2
leaq 0x4797f(%rip), %rdi     # 0xa953e
jmp 0x61c52
movslq %r12d, %rax
movb $0x0, 0x110(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x61bf0
leaq 0x479ab(%rip), %rdi     # 0xa9596
jmp 0x61b21
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x61c42
cmpl $0x20, %eax
je 0x61c65
cmpl $0xa, %eax
je 0x61c65
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x61e30
movslq %r12d, %rcx
incl %r12d
movb %al, 0x10(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x61bf8
jmp 0x61c70
leaq 0x47925(%rip), %rdi     # 0xa9577
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x61b2d
movslq %r12d, %rax
movb $0x0, 0x10(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x61c8e
leaq 0x4794b(%rip), %rdi     # 0xa95d4
jmp 0x61b21
leaq 0x210(%rsp), %rdi
callq 0xe700
movl %eax, %r15d
leaq 0x110(%rsp), %rdi
callq 0xe700
movl %eax, %r12d
leaq 0x10(%rsp), %rdi
callq 0xe700
movl %eax, %ebp
movl %r15d, 0xc0(%rbx)
movl %r12d, 0xc4(%rbx)
movl %r15d, 0xc8(%rbx)
imull %r15d, %r12d
movl %r12d, 0xcc(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%rbx)
leaq 0x60(%rbx), %rdi
leaq 0x486262(%rip), %r12    # 0x4e7f58
leaq 0x8(%r12), %r15
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0xa8(%rbx)
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0x50(%rbx)
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x61d33
callq 0xe5b0
movslq 0xcc(%rbx), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
leaq 0x47890(%rip), %rdi     # 0xa95f3
movl $0x1, %esi
xorl %eax, %eax
callq 0xe070
cmpl $0x0, 0xcc(%rbx)
jle 0x61e1d
xorl %r12d, %r12d
leaq 0xc(%rsp), %r15
cmpl $0xff, %ebp
ja 0x61da8
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movzbl 0xc(%rsp), %eax
jmp 0x61dd1
cmpl $0xffff, %ebp           # imm = 0xFFFF
ja 0x61dda
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movzwl 0xc(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x61dfd
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movl 0xc(%rsp), %eax
bswapl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movq 0xb8(%rbx), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
movslq 0xcc(%rbx), %rax
cmpq %rax, %r12
jl 0x61d84
movq %r14, %rdi
callq 0xe430
movl 0xcc(%rbx), %ebx
jmp 0x61b2d
leaq 0x4777b(%rip), %rdi     # 0xa95b2
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x61c56