// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x338, %rsp            # imm = 0x338
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x31fe1(%rip), %rsi     # 0x9355b
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x61605
movq %rax, %r14
movq %rax, %rdi
callq 0xe750
cmpl $0x50, %eax
jne 0x615fc
movq %r14, %rdi
callq 0xe750
cmpl $0x66, %eax
jne 0x615fc
movq %r14, %rdi
callq 0xe750
cmpl $0xa, %eax
jne 0x615fc
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x61634
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x615f7
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x6162c
cmpl $0xa, %eax
je 0x6162c
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x6169c
leal 0x1(%rcx), %edx
movb %al, 0x230(%rsp,%rcx)
movl %edx, %r12d
jmp 0x615b9
leaq 0x38eb2(%rip), %rdi     # 0x9a4b5
jmp 0x6160c
leaq 0x38e8f(%rip), %rdi     # 0x9a49b
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, 0x230(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x61651
leaq 0x38e9d(%rip), %rdi     # 0x9a4ec
jmp 0x6160c
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x616b0
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x61697
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x616a8
cmpl $0xa, %eax
je 0x616a8
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x61728
leal 0x1(%rcx), %edx
movb %al, 0x130(%rsp,%rcx)
movl %edx, %r12d
jmp 0x61659
leaq 0x38e2b(%rip), %rdi     # 0x9a4ce
jmp 0x6172f
movb $0x0, 0x130(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x616d0
leaq 0x38e5b(%rip), %rdi     # 0x9a526
jmp 0x6160c
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x61747
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x61723
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x61742
cmpl $0xa, %eax
je 0x61742
leal -0x3a(%rax), %edx
cmpl $-0xa, %edx
setae %dl
movl %eax, %esi
addl $-0x2f, %esi
cmpl $-0x2, %esi
setae %sil
orb %dl, %sil
je 0x618c0
leal 0x1(%rcx), %edx
movb %al, 0x30(%rsp,%rcx)
movl %edx, %r12d
jmp 0x616d8
leaq 0x38dd8(%rip), %rdi     # 0x9a507
xorl %ebp, %ebp
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x61618
movb $0x0, 0x30(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x61767
leaq 0x38e4d(%rip), %rdi     # 0x9a5af
jmp 0x6160c
leaq 0x230(%rsp), %rdi
callq 0xe730
movl %eax, %r15d
movl %eax, (%rbx)
leaq 0x130(%rsp), %rdi
callq 0xe730
movl %eax, %ebp
movl %eax, 0x4(%rbx)
leaq 0x30(%rsp), %rdi
callq 0xe5a0
cvtsd2ss %xmm0, %xmm0
ucomiss 0x38b90(%rip), %xmm0    # 0x9a330
jne 0x617b8
jp 0x617b8
leaq 0x38e20(%rip), %rdi     # 0x9a5cb
xorl %eax, %eax
callq 0x681ca
movl (%rbx), %r15d
movl 0x4(%rbx), %ebp
imull %r15d, %ebp
movslq %ebp, %rax
leaq (,%rax,4), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r15
pushq $0x4
popq %r12
movl %ebp, %r13d
testl %r13d, %r13d
jle 0x61817
movl %ebp, %eax
subl %r13d, %eax
cltq
leaq (%r15,%rax,4), %rdi
movl %r13d, %edx
movq %r12, %rsi
movq %r14, %rcx
callq 0xe6f0
subl %eax, %r13d
testl %eax, %eax
jg 0x617df
leaq 0x39514(%rip), %rdi     # 0x9ad1f
xorl %eax, %eax
callq 0x681ca
callq 0xe280
movq %r14, %rdi
callq 0xe440
testl %ebp, %ebp
jle 0x618d0
movl %ebp, %eax
movss 0x392a3(%rip), %xmm5    # 0x9aad4
movss 0x3929f(%rip), %xmm6    # 0x9aad8
xorps %xmm7, %xmm7
xorl %ecx, %ecx
movss 0x39296(%rip), %xmm1    # 0x9aadc
cmpq %rcx, %rax
je 0x61872
movss (%r15,%rcx,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x61858
jnp 0x6186d
movaps %xmm0, %xmm2
maxss %xmm5, %xmm2
addss %xmm0, %xmm7
minss %xmm6, %xmm0
movaps %xmm0, %xmm6
movaps %xmm2, %xmm5
incq %rcx
jmp 0x61846
cvtsi2ss %ebp, %xmm0
divss %xmm0, %xmm7
xorps %xmm0, %xmm0
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x618b2
movss (%r15,%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
incq %rcx
movaps %xmm2, %xmm0
jmp 0x6187f
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
jb 0x618dd
sqrtss %xmm0, %xmm0
jmp 0x61907
leaq 0x38cc9(%rip), %rdi     # 0x9a590
xorl %ebp, %ebp
movl %eax, %esi
jmp 0x61733
movss 0x319b8(%rip), %xmm0    # 0x93290
xorps %xmm2, %xmm2
jmp 0x61948
movss %xmm5, 0x20(%rsp)
movaps %xmm6, 0x10(%rsp)
movss %xmm7, 0xc(%rsp)
callq 0xe850
movss 0xc(%rsp), %xmm7
movaps 0x10(%rsp), %xmm6
movss 0x20(%rsp), %xmm5
xorps %xmm2, %xmm2
movss 0x391d1(%rip), %xmm1    # 0x9aae0
mulss %xmm0, %xmm1
addss %xmm7, %xmm1
maxss %xmm1, %xmm6
mulss 0x391c1(%rip), %xmm0    # 0x9aae4
addss %xmm7, %xmm0
minss %xmm0, %xmm5
subss %xmm6, %xmm5
ucomiss %xmm2, %xmm5
movss 0x31956(%rip), %xmm0    # 0x93290
cmpltss %xmm5, %xmm2
andps %xmm6, %xmm2
jbe 0x61948
divss %xmm5, %xmm0
movl $0x3, 0x8(%rbx)
movl (%rbx), %r14d
leal (%r14,%r14,2), %ebp
testb $0x3, %bpl
movaps %xmm2, 0x20(%rsp)
movss %xmm0, 0x10(%rsp)
je 0x61976
pushq $0x4
popq %rcx
movl %ebp, %eax
cltd
idivl %ecx
leal 0x4(,%rax,4), %ebp
movl %ebp, 0xc(%rbx)
movl 0x4(%rbx), %r12d
imull %r12d, %ebp
movslq %ebp, %rdi
callq 0xe030
movq %rax, 0x10(%rbx)
xorl %eax, %eax
movss 0x39145(%rip), %xmm0    # 0x9aadc
movss 0x39131(%rip), %xmm1    # 0x9aad0
movaps 0x20(%rsp), %xmm3
movss 0x10(%rsp), %xmm4
cmpl %r12d, %eax
jge 0x61618
movslq 0xc(%rbx), %rdx
movslq %eax, %rcx
imulq %rdx, %rcx
addq 0x10(%rbx), %rcx
xorl %edx, %edx
cmpl %r14d, %edx
jge 0x61a22
movss (%r15), %xmm2
xorl %edi, %edi
ucomiss %xmm0, %xmm2
jne 0x619df
jp 0x619df
movb $0x20, %r8b
movb $0x40, %sil
jmp 0x61a08
subss %xmm3, %xmm2
mulss %xmm1, %xmm2
mulss %xmm4, %xmm2
cvttss2si %xmm2, %esi
testl %esi, %esi
jg 0x619f5
xorl %esi, %esi
movl $0xff, %edi
cmpl %edi, %esi
jl 0x61a03
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, %r15
incl %edx
movl (%rbx), %r14d
jmp 0x619c4
incl %eax
movl 0x4(%rbx), %r12d
jmp 0x619aa