// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadPFMFile(char const*)
int R2Image::
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
  width = atoi(width_string);
  height = atoi(height_string);
  float endian = atof(endian_string);
  if (endian == -999.0F) RNFail("Just trying to avoid compiler warning for unused variable\n");

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

  // Read values
  int count = npixels;
  while (count > 0) {
    int n = (int) fread(&values[npixels-count], sizeof(float), count, fp);
    if (n <= 0) { RNFail("read error\n"); abort(); }
    count -= n;
  }

  // Close image
  fclose(fp);

  // Determine offset and scale
  float offset = 0;
  float scale = 1;
  if (npixels > 0) {
    float sum = 0;
    float min_value = FLT_MAX;
    float max_value = -FLT_MAX;
    for (int i = 0; i < npixels; i++) {
      if (values[i] == R2_GRID_UNKNOWN_VALUE) continue;
      if (values[i] > max_value) max_value = values[i];
      if (values[i] < min_value) min_value = values[i];
      sum += values[i];
    }
    float mean = sum / npixels;
    float ssd = 0;
    for (int i = 0; i < npixels; i++) {
      if (values[i] == R2_GRID_UNKNOWN_VALUE) continue;
      float delta = values[i] - mean;
      ssd += delta * delta;
    }
    float stddev = sqrt(ssd);
    float min_clamped = mean - 3 * stddev;
    if (min_clamped < min_value) min_clamped = min_value;
    float max_clamped = mean + 3 * stddev;
    if (max_clamped > max_value) max_clamped = max_value;
    float range = max_clamped - min_clamped;
    if (range > 0) {
      offset = min_clamped;
      scale = 1.0F / range;
    }
  }

  // Allocate pixels
  ncomponents = 3;
  rowsize = ncomponents * width;
  if ((rowsize % 4) != 0) rowsize = (rowsize / 4 + 1) * 4;
  int nbytes = rowsize * height;
  pixels = new unsigned char [nbytes];
  assert(pixels);

  // Fill in pixels
  float *valuesp = values;
  for (int j = 0; j < height; j++) {
    unsigned char *pixelsp = &pixels[j*rowsize];
    for (int i = 0; i < width; i++) {
      if (*valuesp == R2_GRID_UNKNOWN_VALUE) {
        *(pixelsp++) = 64;
        *(pixelsp++) = 32;
        *(pixelsp++) = 0;
      }
      else {
        int value = (int) ((*valuesp - offset) * 255 * scale);
        if (value < 0) value = 0;
        if (value > 255) value = 255;
        unsigned char p = (unsigned char) value;
        *(pixelsp++) = p;
        *(pixelsp++) = p;
        *(pixelsp++) = p;
      }
      valuesp++;
    }
  }

  // Return success
  return nbytes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x320, %rsp            # imm = 0x320
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x38303(%rip), %rsi     # 0xa25ab
movq %r14, %rdi
callq 0xe530
testq %rax, %rax
je 0x6a33c
movq %rax, %r15
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x6a333
movq %r15, %rdi
callq 0xe720
cmpl $0x66, %eax
jne 0x6a333
movq %r15, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x6a333
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r15, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x6a32a
cmpl $0x20, %eax
je 0x6a363
cmpl $0xa, %eax
je 0x6a363
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x6a3dc
movslq %r12d, %rcx
incl %r12d
movb %al, 0x220(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6a2eb
jmp 0x6a371
leaq 0x3f1eb(%rip), %rdi     # 0xa9525
jmp 0x6a343
leaq 0x3f1c8(%rip), %rdi     # 0xa950b
xorl %r15d, %r15d
movq %r14, %rsi
xorl %eax, %eax
callq 0x719d2
movl %r15d, %eax
addq $0x320, %rsp            # imm = 0x320
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movslq %r12d, %rax
movb $0x0, 0x220(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x6a38c
leaq 0x3f1d2(%rip), %rdi     # 0xa955c
jmp 0x6a343
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r15, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x6a3d3
cmpl $0x20, %eax
je 0x6a3e8
cmpl $0xa, %eax
je 0x6a3e8
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x6a46f
movslq %r12d, %rcx
incl %r12d
movb %al, 0x120(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6a394
jmp 0x6a3f6
leaq 0x3f15b(%rip), %rdi     # 0xa953e
jmp 0x6a476
movslq %r12d, %rax
movb $0x0, 0x120(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x6a414
leaq 0x3f187(%rip), %rdi     # 0xa9596
jmp 0x6a343
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r15, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x6a466
cmpl $0x20, %eax
je 0x6a48a
cmpl $0xa, %eax
je 0x6a48a
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x6a605
movslq %r12d, %rcx
incl %r12d
movb %al, 0x20(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6a41c
jmp 0x6a495
leaq 0x3f101(%rip), %rdi     # 0xa9577
xorl %r15d, %r15d
movl %eax, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x6a350
movslq %r12d, %rax
movb $0x0, 0x20(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x6a4b3
leaq 0x3f171(%rip), %rdi     # 0xa961f
jmp 0x6a343
leaq 0x220(%rsp), %rdi
callq 0xe700
movl %eax, %r14d
movl %eax, (%rbx)
leaq 0x120(%rsp), %rdi
callq 0xe700
movl %eax, %ebp
movl %eax, 0x4(%rbx)
leaq 0x20(%rsp), %rdi
callq 0xe580
cvtsd2ss %xmm0, %xmm0
ucomiss 0x3eeb4(%rip), %xmm0    # 0xa93a0
jne 0x6a504
jp 0x6a504
leaq 0x3f144(%rip), %rdi     # 0xa963b
xorl %eax, %eax
callq 0x719d2
movl (%rbx), %r14d
movl 0x4(%rbx), %ebp
imull %r14d, %ebp
movslq %ebp, %r12
leaq (,%r12,4), %rax
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r12d, %r12d
jle 0x6a616
movl %ebp, %r12d
movl %ebp, %eax
subl %r12d, %eax
cltq
leaq (%r14,%rax,4), %rdi
movl %r12d, %edx
movl $0x4, %esi
movq %r15, %rcx
callq 0xe6c0
testl %eax, %eax
jle 0x6a785
subl %eax, %r12d
jg 0x6a535
movq %r15, %rdi
callq 0xe430
testl %ebp, %ebp
jle 0x6a61e
movl %ebp, %eax
xorps %xmm7, %xmm7
movss 0x3f5da(%rip), %xmm5    # 0xa9b54
movss 0x3f5d6(%rip), %xmm6    # 0xa9b58
xorl %ecx, %ecx
movss 0x3f5d0(%rip), %xmm1    # 0xa9b5c
movss (%r14,%rcx,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x6a599
jnp 0x6a5ae
movaps %xmm0, %xmm2
maxss %xmm6, %xmm2
addss %xmm0, %xmm7
minss %xmm5, %xmm0
movaps %xmm0, %xmm5
movaps %xmm2, %xmm6
incq %rcx
cmpq %rcx, %rax
jne 0x6a58c
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
divss %xmm0, %xmm7
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movss (%r14,%rcx,4), %xmm2
movaps %xmm2, %xmm3
subss %xmm7, %xmm3
mulss %xmm3, %xmm3
addss %xmm0, %xmm3
cmpeqss %xmm1, %xmm2
movaps %xmm2, %xmm4
andnps %xmm3, %xmm4
andps %xmm0, %xmm2
orps %xmm4, %xmm2
movaps %xmm2, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x6a5c6
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jb 0x6a62b
sqrtss %xmm0, %xmm0
jmp 0x6a655
leaq 0x3eff4(%rip), %rdi     # 0xa9600
xorl %r15d, %r15d
movl %eax, %esi
jmp 0x6a47b
movq %r15, %rdi
callq 0xe430
movss 0x37cba(%rip), %xmm0    # 0xa22e0
xorps %xmm2, %xmm2
jmp 0x6a696
movaps %xmm5, 0x10(%rsp)
movss %xmm6, 0x8(%rsp)
movss %xmm7, 0xc(%rsp)
callq 0xe820
movss 0xc(%rsp), %xmm7
movss 0x8(%rsp), %xmm6
movaps 0x10(%rsp), %xmm5
xorps %xmm2, %xmm2
movss 0x3f503(%rip), %xmm1    # 0xa9b60
mulss %xmm0, %xmm1
addss %xmm7, %xmm1
maxss %xmm1, %xmm5
mulss 0x3f4f3(%rip), %xmm0    # 0xa9b64
addss %xmm7, %xmm0
minss %xmm0, %xmm6
subss %xmm5, %xmm6
ucomiss %xmm2, %xmm6
movss 0x37c58(%rip), %xmm0    # 0xa22e0
cmpltss %xmm6, %xmm2
andps %xmm5, %xmm2
jbe 0x6a696
divss %xmm6, %xmm0
movl $0x3, 0x8(%rbx)
movl (%rbx), %r12d
leal (%r12,%r12,2), %r15d
testb $0x3, %r15b
movaps %xmm2, 0x10(%rsp)
movss %xmm0, 0x8(%rsp)
je 0x6a6c9
leal 0x3(%r15), %eax
testl %r15d, %r15d
cmovnsl %r15d, %eax
andl $-0x4, %eax
addl $0x4, %eax
movl %eax, %r15d
movl %r15d, 0xc(%rbx)
movl 0x4(%rbx), %ebp
imull %ebp, %r15d
movslq %r15d, %rdi
callq 0xe030
movq %rax, 0x10(%rbx)
testl %ebp, %ebp
jle 0x6a350
xorl %eax, %eax
movss 0x3f46a(%rip), %xmm0    # 0xa9b5c
movss 0x3f456(%rip), %xmm1    # 0xa9b50
movaps 0x10(%rsp), %xmm3
movss 0x8(%rsp), %xmm4
testl %r12d, %r12d
jle 0x6a77a
movslq 0xc(%rbx), %rdx
movslq %eax, %rcx
imulq %rdx, %rcx
addq 0x10(%rbx), %rcx
xorl %edx, %edx
movss (%r14), %xmm2
xorl %edi, %edi
ucomiss %xmm0, %xmm2
jne 0x6a731
jp 0x6a731
movb $0x20, %r8b
movb $0x40, %sil
jmp 0x6a75a
subss %xmm3, %xmm2
mulss %xmm1, %xmm2
mulss %xmm4, %xmm2
cvttss2si %xmm2, %esi
testl %esi, %esi
jg 0x6a747
xorl %esi, %esi
movl $0xff, %edi
cmpl %edi, %esi
jl 0x6a755
movl $0xff, %esi
movl %esi, %r8d
movl %esi, %edi
movb %sil, (%rcx)
movb %r8b, 0x1(%rcx)
movb %dil, 0x2(%rcx)
addq $0x3, %rcx
addq $0x4, %r14
incl %edx
movl (%rbx), %r12d
cmpl %r12d, %edx
jl 0x6a71b
movl 0x4(%rbx), %ebp
incl %eax
cmpl %ebp, %eax
jl 0x6a705
jmp 0x6a350
leaq 0x3f613(%rip), %rdi     # 0xa9d9f
xorl %eax, %eax
callq 0x719d2
callq 0xe270