// File: /workspace/github_cpp_makefile_star2_O1/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 0x3fcde(%rip), %rsi     # 0xa25ae
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x62964
movq %rax, %r14
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x6295b
movq %r14, %rdi
callq 0xe720
cmpl $0x35, %eax
jne 0x6295b
movq %r14, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x6295b
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 0x62952
cmpl $0x20, %eax
je 0x62989
cmpl $0xa, %eax
je 0x62989
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x62a02
movslq %r12d, %rcx
incl %r12d
movb %al, 0x210(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x62913
jmp 0x62997
leaq 0x46bc3(%rip), %rdi     # 0xa9525
jmp 0x6296b
leaq 0x46ba0(%rip), %rdi     # 0xa950b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
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 0x629b2
leaq 0x46bac(%rip), %rdi     # 0xa955c
jmp 0x6296b
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 0x629f9
cmpl $0x20, %eax
je 0x62a0e
cmpl $0xa, %eax
je 0x62a0e
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x62a95
movslq %r12d, %rcx
incl %r12d
movb %al, 0x110(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x629ba
jmp 0x62a1c
leaq 0x46b35(%rip), %rdi     # 0xa953e
jmp 0x62a9c
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 0x62a3a
leaq 0x46b61(%rip), %rdi     # 0xa9596
jmp 0x6296b
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 0x62a8c
cmpl $0x20, %eax
je 0x62aaf
cmpl $0xa, %eax
je 0x62aaf
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x62c83
movslq %r12d, %rcx
incl %r12d
movb %al, 0x10(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x62a42
jmp 0x62aba
leaq 0x46adb(%rip), %rdi     # 0xa9577
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x62977
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 0x62ad8
leaq 0x46b01(%rip), %rdi     # 0xa95d4
jmp 0x6296b
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 0x485418(%rip), %r12    # 0x4e7f58
leaq 0x8(%r12), %r15
movq %r15, %rsi
callq 0x67944
movl 0x50(%r12), %eax
movl %eax, 0xa8(%rbx)
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x67944
movl 0x50(%r12), %eax
movl %eax, 0x50(%rbx)
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x62b7d
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 0x46a46(%rip), %rdi     # 0xa95f3
movl $0x1, %esi
xorl %eax, %eax
callq 0xe070
cmpl $0x0, 0xcc(%rbx)
jle 0x62c70
xorl %r12d, %r12d
leaq 0xc(%rsp), %r15
cmpl $0xff, %ebp
ja 0x62bf2
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movzbl 0xc(%rsp), %eax
jmp 0x62c20
cmpl $0xffff, %ebp           # imm = 0xFFFF
ja 0x62c29
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movzwl 0xc(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0xc(%rsp)
movzwl %ax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x62c50
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0xe6c0
movl 0xc(%rsp), %eax
bswapl %eax
movl %eax, 0xc(%rsp)
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 0x62bce
movq %r14, %rdi
callq 0xe430
movl 0xcc(%rbx), %ebx
jmp 0x62977
leaq 0x46928(%rip), %rdi     # 0xa95b2
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x62aa0
nop