// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x4011a(%rip), %rsi     # 0xa25ae
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x62528
movq %rax, %r14
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x6251f
movq %r14, %rdi
callq 0xe720
cmpl $0x66, %eax
jne 0x6251f
movq %r14, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x6251f
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 0x62516
cmpl $0x20, %eax
je 0x6254f
cmpl $0xa, %eax
je 0x6254f
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x625c8
movslq %r12d, %rcx
incl %r12d
movb %al, 0x210(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x624d7
jmp 0x6255d
leaq 0x46fff(%rip), %rdi     # 0xa9525
jmp 0x6252f
leaq 0x46fdc(%rip), %rdi     # 0xa950b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
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 0x62578
leaq 0x46fe6(%rip), %rdi     # 0xa955c
jmp 0x6252f
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 0x625bf
cmpl $0x20, %eax
je 0x625d4
cmpl $0xa, %eax
je 0x625d4
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x6265b
movslq %r12d, %rcx
incl %r12d
movb %al, 0x110(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x62580
jmp 0x625e2
leaq 0x46f6f(%rip), %rdi     # 0xa953e
jmp 0x62662
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 0x62600
leaq 0x46f9b(%rip), %rdi     # 0xa9596
jmp 0x6252f
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 0x62652
cmpl $0x20, %eax
je 0x62675
cmpl $0xa, %eax
je 0x62675
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x628a3
movslq %r12d, %rcx
incl %r12d
movb %al, 0x10(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x62608
jmp 0x62680
leaq 0x46f15(%rip), %rdi     # 0xa9577
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x6253b
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 0x6269e
leaq 0x46f86(%rip), %rdi     # 0xa961f
jmp 0x6252f
leaq 0x210(%rsp), %rdi
callq 0xe700
movl %eax, %ebp
leaq 0x110(%rsp), %rdi
callq 0xe700
movl %eax, %r13d
leaq 0x10(%rsp), %rdi
callq 0xe580
cvtsd2ss %xmm0, %xmm0
ucomiss 0x46cce(%rip), %xmm0    # 0xa93a0
jne 0x626e4
jp 0x626e4
leaq 0x46f5e(%rip), %rdi     # 0xa963b
xorl %eax, %eax
callq 0x72a1e
movl %r13d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
imull %ebp, %r13d
movslq %r13d, %rax
leaq (,%rax,4), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %rbp
movl %r13d, 0x4(%rsp)
testl %r13d, %r13d
jle 0x6276d
movl 0x4(%rsp), %eax
subl %r13d, %eax
cltq
movq %rbp, %r12
leaq (,%rax,4), %rdi
addq %rbp, %rdi
movl %r13d, %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0xe6c0
movq %rax, %rbp
testl %ebp, %ebp
jle 0x6274e
subl %ebp, %r13d
jmp 0x6275f
leaq 0x46d6f(%rip), %rdi     # 0xa94c4
movq %r15, %rsi
xorl %eax, %eax
callq 0x72a1e
testl %ebp, %ebp
movq %r12, %rbp
jg 0x62716
xorl %ebx, %ebx
jmp 0x6253b
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0xc4(%rbx)
movl %eax, 0xc8(%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0xcc(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%rbx)
leaq 0x60(%rbx), %rdi
leaq 0x4857ab(%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 0x627ea
callq 0xe5b0
movslq 0xcc(%rbx), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
testq %r15, %r15
jle 0x62888
leaq 0x83379(%rip), %rcx     # 0xe5b98
movsd (%rcx), %xmm0
movapd 0x3f7f5(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
movsd 0x46ae7(%rip), %xmm2    # 0xa9320
movsd 0x40777(%rip), %xmm3    # 0xa2fb8
xorps %xmm4, %xmm4
cvtss2sd (%rbp,%rcx,4), %xmm4
movaps %xmm4, %xmm5
addsd %xmm2, %xmm5
movapd %xmm1, %xmm6
cmpnlesd %xmm5, %xmm6
movapd %xmm6, %xmm7
andpd %xmm4, %xmm7
cmpnlesd %xmm0, %xmm5
andpd %xmm5, %xmm4
andnpd %xmm3, %xmm5
orpd %xmm4, %xmm5
andnpd %xmm5, %xmm6
orpd %xmm7, %xmm6
movlpd %xmm6, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r15
jne 0x62841
movq %rbp, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe430
movl 0xcc(%rbx), %ebx
jmp 0x6253b
leaq 0x46d56(%rip), %rdi     # 0xa9600
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x62666
nop