// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadPFMFile(char const*)
int R2Grid::
ReadPFMFile(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 != 'f') { 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 endian
  int endian_count = 0;
  char endian_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (endian_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { endian_string[endian_count] = '\0'; break; }
    if (!isdigit(c) && (c != '.') && (c != '-')) { RNFail("Bad endian character %c in %s\n", c, filename); return 0; }
    endian_string[endian_count++] = c;
  }

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

  // Parse values
  int width = atoi(width_string);
  int height = atoi(height_string);
  float endian = (float) atof(endian_string);
  if (endian == -999.0F) RNFail("Just trying to avoid compiler warning for unused variable\n");

  // Allocate pixels
  int npixels = width * height;
  float *pixels = new float [ npixels ];
  if (!pixels) {
    RNFail("Unable to allocate pixels for %s\n", filename);
    return 0;
  }

  // Read pixels
  int count = npixels;
  while (count > 0) {
    int n = fread(&pixels[npixels-count], sizeof(float), count, fp);
    if (n <= 0) { RNFail("Unable to read pixels from %s\n", filename); return 0; }
    count -= n;
  }

  // Fill in grid info
  grid_resolution[0] = width;
  grid_resolution[1] = height;
  grid_row_size = width;
  grid_size = npixels;
  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 ];
  for (int i = 0; i < grid_size; i++) {
    if (RNIsEqual(pixels[i], R2_GRID_UNKNOWN_VALUE)) grid_values[i] = R2_GRID_UNKNOWN_VALUE;
    else grid_values[i] = pixels[i];
  }

  // Delete pixels
  delete [] pixels;

  // Close image
  fclose(fp);

  // Return success
  return grid_size;
}

// Assembly:
subq $0x398, %rsp            # imm = 0x398
movq %rdi, 0x388(%rsp)
movq %rsi, 0x380(%rsp)
movq 0x388(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x380(%rsp), %rdi
leaq 0x7d346(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x378(%rsp)
cmpq $0x0, 0x378(%rsp)
jne 0xb62d1
movq 0x380(%rsp), %rsi
leaq 0x7fb5f(%rip), %rdi     # 0x135e19
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0x50, 0x374(%rsp)
je 0xb6315
movq 0x380(%rsp), %rsi
leaq 0x7fb35(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0x66, 0x374(%rsp)
je 0xb6359
movq 0x380(%rsp), %rsi
leaq 0x7faf1(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0xa, 0x374(%rsp)
je 0xb639d
movq 0x380(%rsp), %rsi
leaq 0x7faad(%rip), %rdi     # 0x135e33
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl $0x0, 0x370(%rsp)
movl $0x0, 0x26c(%rsp)
cmpl $0x100, 0x26c(%rsp) # imm = 0x100
jge 0xb6496
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0x20, 0x374(%rsp)
jne 0xb63f1
cmpl $0x0, 0x370(%rsp)
jne 0xb63f1
jmp 0xb6480
cmpl $0x20, 0x374(%rsp)
je 0xb6405
cmpl $0xa, 0x374(%rsp)
jne 0xb6417
movslq 0x370(%rsp), %rax
movb $0x0, 0x270(%rsp,%rax)
jmp 0xb6496
movl 0x374(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb6455
movl 0x374(%rsp), %esi
movq 0x380(%rsp), %rdx
leaq 0x7fa0e(%rip), %rdi     # 0x135e4c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl 0x374(%rsp), %eax
movb %al, %cl
movl 0x370(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x370(%rsp)
cltq
movb %cl, 0x270(%rsp,%rax)
jmp 0xb647c
jmp 0xb647e
jmp 0xb6480
movl 0x26c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x26c(%rsp)
jmp 0xb63b3
cmpl $0x0, 0x370(%rsp)
je 0xb64ad
cmpl $0x80, 0x370(%rsp)
jle 0xb64d3
movq 0x380(%rsp), %rsi
leaq 0x7f9ae(%rip), %rdi     # 0x135e6a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl $0x0, 0x268(%rsp)
movl $0x0, 0x15c(%rsp)
cmpl $0x100, 0x15c(%rsp) # imm = 0x100
jge 0xb65cc
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0x20, 0x374(%rsp)
jne 0xb6527
cmpl $0x0, 0x268(%rsp)
jne 0xb6527
jmp 0xb65b6
cmpl $0x20, 0x374(%rsp)
je 0xb653b
cmpl $0xa, 0x374(%rsp)
jne 0xb654d
movslq 0x268(%rsp), %rax
movb $0x0, 0x160(%rsp,%rax)
jmp 0xb65cc
movl 0x374(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb658b
movl 0x374(%rsp), %esi
movq 0x380(%rsp), %rdx
leaq 0x7f911(%rip), %rdi     # 0x135e85
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl 0x374(%rsp), %eax
movb %al, %cl
movl 0x268(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x268(%rsp)
cltq
movb %cl, 0x160(%rsp,%rax)
jmp 0xb65b2
jmp 0xb65b4
jmp 0xb65b6
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x15c(%rsp)
jmp 0xb64e9
cmpl $0x0, 0x268(%rsp)
je 0xb65e3
cmpl $0x80, 0x268(%rsp)
jle 0xb6609
movq 0x380(%rsp), %rsi
leaq 0x7f8b2(%rip), %rdi     # 0x135ea4
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl $0x0, 0x158(%rsp)
movl $0x0, 0x4c(%rsp)
cmpl $0x100, 0x4c(%rsp)      # imm = 0x100
jge 0xb6703
movq 0x378(%rsp), %rdi
callq 0xf7d0
movl %eax, 0x374(%rsp)
cmpl $0x20, 0x374(%rsp)
jne 0xb6657
cmpl $0x0, 0x158(%rsp)
jne 0xb6657
jmp 0xb66f3
cmpl $0x20, 0x374(%rsp)
je 0xb666b
cmpl $0xa, 0x374(%rsp)
jne 0xb667d
movslq 0x158(%rsp), %rax
movb $0x0, 0x50(%rsp,%rax)
jmp 0xb6703
jmp 0xb667f
movl 0x374(%rsp), %edi
callq 0xf430
cmpl $0x0, %eax
jne 0xb66d1
cmpl $0x2e, 0x374(%rsp)
je 0xb66d1
cmpl $0x2d, 0x374(%rsp)
je 0xb66d1
movl 0x374(%rsp), %esi
movq 0x380(%rsp), %rdx
leaq 0x7f89b(%rip), %rdi     # 0x135f55
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl 0x374(%rsp), %eax
movb %al, %cl
movl 0x158(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x158(%rsp)
cltq
movb %cl, 0x50(%rsp,%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xb661c
cmpl $0x0, 0x158(%rsp)
je 0xb671a
cmpl $0x80, 0x158(%rsp)
jle 0xb6740
movq 0x380(%rsp), %rsi
leaq 0x7f84b(%rip), %rdi     # 0x135f74
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
leaq 0x270(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x48(%rsp)
leaq 0x160(%rsp), %rdi
callq 0xf7b0
movl %eax, 0x44(%rsp)
leaq 0x50(%rsp), %rdi
callq 0xf640
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x40(%rsp), %xmm0
movss 0x7f4fc(%rip), %xmm1    # 0x135c80
ucomiss %xmm1, %xmm0
jne 0xb6799
jp 0xb6799
leaq 0x7f7fe(%rip), %rdi     # 0x135f90
movb $0x0, %al
callq 0xd4f30
movl 0x48(%rsp), %eax
imull 0x44(%rsp), %eax
movl %eax, 0x3c(%rsp)
movslq 0x3c(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0xb67ff
movq 0x380(%rsp), %rsi
leaq 0x7f7e3(%rip), %rdi     # 0x135fcb
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl 0x3c(%rsp), %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jle 0xb687c
movq 0x30(%rsp), %rdi
movl 0x3c(%rsp), %eax
subl 0x2c(%rsp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movslq 0x2c(%rsp), %rdx
movq 0x378(%rsp), %rcx
movl $0x4, %esi
callq 0xf780
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x28(%rsp)
jg 0xb686c
movq 0x380(%rsp), %rsi
leaq 0x7f57d(%rip), %rdi     # 0x135dd2
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x394(%rsp)
jmp 0xb6a29
movl 0x28(%rsp), %ecx
movl 0x2c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x2c(%rsp)
jmp 0xb6807
movq 0x18(%rsp), %rdi
movl 0x48(%rsp), %eax
movl %eax, 0xc0(%rdi)
movl 0x44(%rsp), %eax
movl %eax, 0xc4(%rdi)
movl 0x48(%rsp), %eax
movl %eax, 0xc8(%rdi)
movl 0x3c(%rsp), %eax
movl %eax, 0xcc(%rdi)
movsd 0x7c757(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rdi)
addq $0x58, %rdi
leaq 0x4c41a4(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x18(%rsp), %rdi
leaq 0x4c4193(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x18(%rsp), %rax
cmpq $0x0, 0xb8(%rax)
je 0xb690c
movq 0x18(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xb690a
movq 0x10(%rsp), %rdi
callq 0xf670
jmp 0xb690c
movq 0x18(%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 0x18(%rsp), %rax
movq %rcx, 0xb8(%rax)
movl $0x0, 0x24(%rsp)
movq 0x18(%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl 0xcc(%rcx), %eax
jge 0xb69f0
movq 0x30(%rsp), %rax
movslq 0x24(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xc1b61(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
movsd 0x7d525(%rip), %xmm1    # 0x133eb0
callq 0x37170
cmpl $0x0, %eax
je 0xb69b5
movq 0x18(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x24(%rsp), %rcx
movsd 0x7d502(%rip), %xmm0    # 0x133eb0
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb69de
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0xb8(%rax), %rax
movslq 0x24(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xb69e0
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0xb6950
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb6a0a
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0x378(%rsp), %rdi
callq 0xf4b0
movq 0x18(%rsp), %rax
movl 0xcc(%rax), %eax
movl %eax, 0x394(%rsp)
movl 0x394(%rsp), %eax
addq $0x398, %rsp            # imm = 0x398
retq
nopl (%rax,%rax)