// File: /workspace/github_cpp_makefile_star2_O1/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 0x37530(%rip), %rsi     # 0xa25ae
movq %r14, %rdi
callq 0xe530
testq %rax, %rax
je 0x6b112
movq %rax, %r15
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x6b109
movq %r15, %rdi
callq 0xe720
cmpl $0x66, %eax
jne 0x6b109
movq %r15, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x6b109
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 0x6b100
cmpl $0x20, %eax
je 0x6b139
cmpl $0xa, %eax
je 0x6b139
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x6b1b2
movslq %r12d, %rcx
incl %r12d
movb %al, 0x220(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6b0c1
jmp 0x6b147
leaq 0x3e415(%rip), %rdi     # 0xa9525
jmp 0x6b119
leaq 0x3e3f2(%rip), %rdi     # 0xa950b
xorl %r15d, %r15d
movq %r14, %rsi
xorl %eax, %eax
callq 0x72a1e
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 0x6b162
leaq 0x3e3fc(%rip), %rdi     # 0xa955c
jmp 0x6b119
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 0x6b1a9
cmpl $0x20, %eax
je 0x6b1be
cmpl $0xa, %eax
je 0x6b1be
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x6b245
movslq %r12d, %rcx
incl %r12d
movb %al, 0x120(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6b16a
jmp 0x6b1cc
leaq 0x3e385(%rip), %rdi     # 0xa953e
jmp 0x6b24c
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 0x6b1ea
leaq 0x3e3b1(%rip), %rdi     # 0xa9596
jmp 0x6b119
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 0x6b23c
cmpl $0x20, %eax
je 0x6b260
cmpl $0xa, %eax
je 0x6b260
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x6b3d0
movslq %r12d, %rcx
incl %r12d
movb %al, 0x20(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6b1f2
jmp 0x6b26b
leaq 0x3e32b(%rip), %rdi     # 0xa9577
xorl %r15d, %r15d
movl %eax, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x6b126
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 0x6b289
leaq 0x3e39b(%rip), %rdi     # 0xa961f
jmp 0x6b119
leaq 0x220(%rsp), %rdi
callq 0xe700
movl %eax, (%rbx)
leaq 0x120(%rsp), %rdi
callq 0xe700
movl %eax, 0x4(%rbx)
leaq 0x20(%rsp), %rdi
callq 0xe580
cvtsd2ss %xmm0, %xmm0
ucomiss 0x3e0e3(%rip), %xmm0    # 0xa93a0
jne 0x6b2cf
jp 0x6b2cf
leaq 0x3e373(%rip), %rdi     # 0xa963b
xorl %eax, %eax
callq 0x72a1e
movslq (%rbx), %rax
movslq 0x4(%rbx), %r12
imulq %rax, %r12
movq %r12, %rax
shlq $0x2, %rax
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r12d, %r12d
jle 0x6b325
movl %r12d, %ebp
movl %r12d, %eax
subl %ebp, %eax
cltq
leaq (%r14,%rax,4), %rdi
movl %ebp, %edx
movl $0x4, %esi
movq %r15, %rcx
callq 0xe6c0
testl %eax, %eax
jle 0x6b541
subl %eax, %ebp
jg 0x6b2ff
movq %r15, %rdi
callq 0xe430
testl %r12d, %r12d
jle 0x6b3e1
movl %r12d, %eax
xorps %xmm7, %xmm7
movss 0x3e7f0(%rip), %xmm5    # 0xa9b34
movss 0x3e7ec(%rip), %xmm6    # 0xa9b38
xorl %ecx, %ecx
movss 0x3e7e6(%rip), %xmm1    # 0xa9b3c
movss (%r14,%rcx,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x6b363
jnp 0x6b378
movaps %xmm0, %xmm2
maxss %xmm6, %xmm2
addss %xmm0, %xmm7
minss %xmm5, %xmm0
movaps %xmm0, %xmm5
movaps %xmm2, %xmm6
incq %rcx
cmpq %rcx, %rax
jne 0x6b356
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %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 0x6b391
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jb 0x6b3ee
sqrtss %xmm0, %xmm0
jmp 0x6b418
leaq 0x3e229(%rip), %rdi     # 0xa9600
xorl %r15d, %r15d
movl %eax, %esi
jmp 0x6b251
movss 0x36ef7(%rip), %xmm0    # 0xa22e0
xorps %xmm2, %xmm2
jmp 0x6b459
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 0x3e720(%rip), %xmm1    # 0xa9b40
mulss %xmm0, %xmm1
addss %xmm7, %xmm1
maxss %xmm1, %xmm5
mulss 0x3e710(%rip), %xmm0    # 0xa9b44
addss %xmm7, %xmm0
minss %xmm0, %xmm6
subss %xmm5, %xmm6
ucomiss %xmm2, %xmm6
movss 0x36e95(%rip), %xmm0    # 0xa22e0
cmpltss %xmm6, %xmm2
andps %xmm5, %xmm2
jbe 0x6b459
divss %xmm6, %xmm0
movl $0x3, 0x8(%rbx)
movl (%rbx), %eax
leal (%rax,%rax,2), %r15d
testb $0x3, %r15b
movaps %xmm2, 0x10(%rsp)
movss %xmm0, 0x8(%rsp)
je 0x6b48b
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 0x6b126
xorl %eax, %eax
movss 0x3e688(%rip), %xmm0    # 0xa9b3c
movss 0x3e674(%rip), %xmm1    # 0xa9b30
movaps 0x10(%rsp), %xmm3
movss 0x8(%rsp), %xmm4
cmpl $0x0, (%rbx)
jle 0x6b535
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 0x6b4f3
jp 0x6b4f3
movb $0x20, %r8b
movb $0x40, %sil
jmp 0x6b51c
subss %xmm3, %xmm2
mulss %xmm1, %xmm2
mulss %xmm4, %xmm2
cvttss2si %xmm2, %esi
testl %esi, %esi
jg 0x6b509
xorl %esi, %esi
movl $0xff, %edi
cmpl %edi, %esi
jl 0x6b517
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
cmpl (%rbx), %edx
jl 0x6b4dd
incl %eax
cmpl 0x4(%rbx), %eax
jl 0x6b4c7
jmp 0x6b126
leaq 0x3e837(%rip), %rdi     # 0xa9d7f
xorl %eax, %eax
callq 0x72a1e
callq 0xe270