// File: 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:
subq $0x388, %rsp            # imm = 0x388
movq %rdi, 0x378(%rsp)
movq %rsi, 0x370(%rsp)
movq 0x378(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x370(%rsp), %rdi
leaq 0x7cb66(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x368(%rsp)
cmpq $0x0, 0x368(%rsp)
jne 0xb6ab1
movq 0x370(%rsp), %rsi
leaq 0x7f37f(%rip), %rdi     # 0x135e19
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0x50, 0x364(%rsp)
je 0xb6af5
movq 0x370(%rsp), %rsi
leaq 0x7f355(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0x35, 0x364(%rsp)
je 0xb6b39
movq 0x370(%rsp), %rsi
leaq 0x7f311(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0xa, 0x364(%rsp)
je 0xb6b7d
movq 0x370(%rsp), %rsi
leaq 0x7f2cd(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl $0x0, 0x360(%rsp)
movl $0x0, 0x25c(%rsp)
cmpl $0x100, 0x25c(%rsp) # imm = 0x100
jge 0xb6c76
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0x20, 0x364(%rsp)
jne 0xb6bd1
cmpl $0x0, 0x360(%rsp)
jne 0xb6bd1
jmp 0xb6c60
cmpl $0x20, 0x364(%rsp)
je 0xb6be5
cmpl $0xa, 0x364(%rsp)
jne 0xb6bf7
movslq 0x360(%rsp), %rax
movb $0x0, 0x260(%rsp,%rax)
jmp 0xb6c76
movl 0x364(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb6c35
movl 0x364(%rsp), %esi
movq 0x370(%rsp), %rdx
leaq 0x7f22e(%rip), %rdi     # 0x135e4c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl 0x364(%rsp), %eax
movb %al, %cl
movl 0x360(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x360(%rsp)
cltq
movb %cl, 0x260(%rsp,%rax)
jmp 0xb6c5c
jmp 0xb6c5e
jmp 0xb6c60
movl 0x25c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x25c(%rsp)
jmp 0xb6b93
cmpl $0x0, 0x360(%rsp)
je 0xb6c8d
cmpl $0x80, 0x360(%rsp)
jle 0xb6cb3
movq 0x370(%rsp), %rsi
leaq 0x7f1ce(%rip), %rdi     # 0x135e6a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl $0x0, 0x258(%rsp)
movl $0x0, 0x14c(%rsp)
cmpl $0x100, 0x14c(%rsp) # imm = 0x100
jge 0xb6dac
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0x20, 0x364(%rsp)
jne 0xb6d07
cmpl $0x0, 0x258(%rsp)
jne 0xb6d07
jmp 0xb6d96
cmpl $0x20, 0x364(%rsp)
je 0xb6d1b
cmpl $0xa, 0x364(%rsp)
jne 0xb6d2d
movslq 0x258(%rsp), %rax
movb $0x0, 0x150(%rsp,%rax)
jmp 0xb6dac
movl 0x364(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb6d6b
movl 0x364(%rsp), %esi
movq 0x370(%rsp), %rdx
leaq 0x7f131(%rip), %rdi     # 0x135e85
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl 0x364(%rsp), %eax
movb %al, %cl
movl 0x258(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x258(%rsp)
cltq
movb %cl, 0x150(%rsp,%rax)
jmp 0xb6d92
jmp 0xb6d94
jmp 0xb6d96
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0xb6cc9
cmpl $0x0, 0x258(%rsp)
je 0xb6dc3
cmpl $0x80, 0x258(%rsp)
jle 0xb6de9
movq 0x370(%rsp), %rsi
leaq 0x7f0d2(%rip), %rdi     # 0x135ea4
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl $0x0, 0x148(%rsp)
movl $0x0, 0x3c(%rsp)
cmpl $0x100, 0x3c(%rsp)      # imm = 0x100
jge 0xb6ee3
movq 0x368(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x364(%rsp)
cmpl $0x20, 0x364(%rsp)
jne 0xb6e37
cmpl $0x0, 0x148(%rsp)
jne 0xb6e37
jmp 0xb6ed3
cmpl $0x20, 0x364(%rsp)
je 0xb6e4b
cmpl $0xa, 0x364(%rsp)
jne 0xb6e5d
movslq 0x148(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
jmp 0xb6ee3
jmp 0xb6e5f
movl 0x364(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb6eb1
cmpl $0x2e, 0x364(%rsp)
je 0xb6eb1
cmpl $0x2d, 0x364(%rsp)
je 0xb6eb1
movl 0x364(%rsp), %esi
movq 0x370(%rsp), %rdx
leaq 0x7f026(%rip), %rdi     # 0x135ec0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movl 0x364(%rsp), %eax
movb %al, %cl
movl 0x148(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x148(%rsp)
cltq
movb %cl, 0x40(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0xb6dfc
cmpl $0x0, 0x148(%rsp)
je 0xb6efa
cmpl $0x80, 0x148(%rsp)
jle 0xb6f20
movq 0x370(%rsp), %rsi
leaq 0x7efd9(%rip), %rdi     # 0x135ee2
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
leaq 0x260(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x38(%rsp)
leaq 0x150(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x34(%rsp)
leaq 0x40(%rsp), %rdi
callq 0xf7b0
movq 0x8(%rsp), %rdi
movl %eax, 0x30(%rsp)
movl 0x38(%rsp), %eax
movl %eax, 0xc0(%rdi)
movl 0x34(%rsp), %eax
movl %eax, 0xc4(%rdi)
movl 0x38(%rsp), %eax
movl %eax, 0xc8(%rdi)
movl 0x38(%rsp), %eax
imull 0x34(%rsp), %eax
movl %eax, 0xcc(%rdi)
movsd 0x7c07e(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rdi)
addq $0x58, %rdi
leaq 0x4c3acb(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x8(%rsp), %rdi
leaq 0x4c3aba(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x8(%rsp), %rax
cmpq $0x0, 0xb8(%rax)
je 0xb6fe3
movq 0x8(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xb6fe1
movq (%rsp), %rdi
callq 0xf670
jmp 0xb6fe3
movq 0x8(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0xb8(%rax)
cmpq $0x0, 0xb8(%rax)
jne 0xb705a
movq 0x8(%rsp), %rax
movl 0xcc(%rax), %esi
movq 0x370(%rsp), %rdx
leaq 0x7eebe(%rip), %rdi     # 0x135f01
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
movw $0x1, 0x2e(%rsp)
movzbl 0x2e(%rsp), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %esi
leaq 0x7eea4(%rip), %rdi     # 0x135f26
movb $0x0, %al
callq 0xf070
movl $0x0, 0x24(%rsp)
movq 0x8(%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl 0xcc(%rcx), %eax
jge 0xb721e
cmpl $0xff, 0x30(%rsp)
ja 0xb70ee
movq 0x368(%rsp), %rcx
leaq 0x23(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0xf780
movq 0x8(%rsp), %rax
movzbl 0x23(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0xb8(%rax), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb720c
cmpl $0xffff, 0x30(%rsp)     # imm = 0xFFFF
ja 0xb7160
movq 0x368(%rsp), %rcx
leaq 0x20(%rsp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0xf780
cmpl $0x0, 0x28(%rsp)
je 0xb713c
movw 0x20(%rsp), %ax
movw %ax, 0x1e(%rsp)
movzwl 0x1e(%rsp), %eax
shll $0x8, %eax
movzwl 0x1e(%rsp), %ecx
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, 0x20(%rsp)
movq 0x8(%rsp), %rax
movzwl 0x20(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0xb8(%rax), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb720a
cmpl $-0x1, 0x30(%rsp)
ja 0xb71e5
movq 0x368(%rsp), %rcx
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xf780
cmpl $0x0, 0x28(%rsp)
je 0xb71c4
movl 0x18(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
shll $0x18, %eax
movl 0x14(%rsp), %ecx
shll $0x8, %ecx
andl $0xff0000, %ecx         # imm = 0xFF0000
orl %ecx, %eax
movl 0x14(%rsp), %ecx
shrl $0x8, %ecx
andl $0xff00, %ecx           # imm = 0xFF00
orl %ecx, %eax
movl 0x14(%rsp), %ecx
shrl $0x18, %ecx
orl %ecx, %eax
movl %eax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movl 0x18(%rsp), %ecx
cvtsi2sd %rcx, %xmm0
movq 0xb8(%rax), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb7208
movq 0x370(%rsp), %rsi
leaq 0x7ed3f(%rip), %rdi     # 0x135f33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x384(%rsp)
jmp 0xb723d
jmp 0xb720a
jmp 0xb720c
jmp 0xb720e
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xb7091
movq 0x368(%rsp), %rdi
callq 0xf4b0
movq 0x8(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x384(%rsp)
movl 0x384(%rsp), %eax
addq $0x388, %rsp            # imm = 0x388
retq
nopl (%rax)