// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x39eaf(%rip), %rsi     # 0x9355b
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x59737
movq %rax, %r14
movq %rax, %rdi
callq 0xe750
cmpl $0x50, %eax
jne 0x5972e
movq %r14, %rdi
callq 0xe750
cmpl $0x35, %eax
jne 0x5972e
movq %r14, %rdi
callq 0xe750
cmpl $0xa, %eax
jne 0x5972e
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x59766
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x59729
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x5975e
cmpl $0xa, %eax
je 0x5975e
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x597ce
leal 0x1(%rcx), %edx
movb %al, 0x210(%rsp,%rcx)
movl %edx, %r12d
jmp 0x596eb
leaq 0x40d80(%rip), %rdi     # 0x9a4b5
jmp 0x5973e
leaq 0x40d5d(%rip), %rdi     # 0x9a49b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, 0x210(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x59783
leaq 0x40d6b(%rip), %rdi     # 0x9a4ec
jmp 0x5973e
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x597e2
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x597c9
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x597da
cmpl $0xa, %eax
je 0x597da
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x5985a
leal 0x1(%rcx), %edx
movb %al, 0x110(%rsp,%rcx)
movl %edx, %r12d
jmp 0x5978b
leaq 0x40cf9(%rip), %rdi     # 0x9a4ce
jmp 0x59861
movb $0x0, 0x110(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x59802
leaq 0x40d29(%rip), %rdi     # 0x9a526
jmp 0x5973e
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x59879
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x59855
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x59874
cmpl $0xa, %eax
je 0x59874
leal -0x3a(%rax), %edx
cmpl $-0xa, %edx
setae %dl
movl %eax, %esi
addl $-0x2f, %esi
cmpl $-0x2, %esi
setae %sil
orb %dl, %sil
je 0x599f9
leal 0x1(%rcx), %edx
movb %al, 0x10(%rsp,%rcx)
movl %edx, %r12d
jmp 0x5980a
leaq 0x40ca6(%rip), %rdi     # 0x9a507
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x5974a
movb $0x0, 0x10(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x59899
leaq 0x40cd0(%rip), %rdi     # 0x9a564
jmp 0x5973e
leaq 0x210(%rsp), %rdi
callq 0xe730
movl %eax, %r15d
leaq 0x110(%rsp), %rdi
callq 0xe730
movl %eax, %r12d
leaq 0x10(%rsp), %rdi
callq 0xe730
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 0x58(%rbx), %rdi
leaq 0x483667(%rip), %r15    # 0x4dcf68
movq %r15, %rsi
callq 0x5ac32
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5ac32
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x59925
callq 0xe5d0
movslq 0xcc(%rbx), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
leaq 0x40c32(%rip), %rdi     # 0x9a583
xorl %r13d, %r13d
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0xe070
leaq 0xc(%rsp), %r15
pushq $0x1
popq %r12
movslq 0xcc(%rbx), %rax
cmpq %rax, %r13
jge 0x599e6
movq %r15, %rdi
cmpl $0xff, %ebp
ja 0x59993
movq %r12, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe6f0
movzbl 0xc(%rsp), %eax
jmp 0x599b5
cmpl $0xffff, %ebp           # imm = 0xFFFF
ja 0x599bb
pushq $0x2
popq %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe6f0
movzwl 0xc(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cvtsi2sd %eax, %xmm0
jmp 0x599d4
pushq $0x4
popq %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xe6f0
movl 0xc(%rsp), %eax
bswapl %eax
cvtsi2sd %rax, %xmm0
movq 0xb8(%rbx), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
jmp 0x59967
movq %r14, %rdi
callq 0xe440
movl 0xcc(%rbx), %ebx
jmp 0x5974a
leaq 0x40b42(%rip), %rdi     # 0x9a542
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x59865
nop