// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::ReadGRDFile(char const*)
int R2Image::
ReadGRDFile(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Read grid resolution from file
  int grid_resolution[2];
  if (fread(&grid_resolution, sizeof(int), 2, fp) != 2) {
    RNFail("Unable to read resolution from grid file");
    return 0;
  }

  // Read world_to_grid transformation from file
  RNScalar m[9];
  if (fread(m, sizeof(RNScalar), 9, fp) != 9) {
    RNFail("Unable to read transformation matrix from file");
    return 0;
  }

  // Allocate grid values
  int grid_size = grid_resolution[0] * grid_resolution[1];
  RNScalar *grid_values = new RNScalar [ grid_size ];
  assert(grid_values);

  // Read grid values
  if (fread(grid_values, sizeof(RNScalar), grid_size, fp) != (unsigned int) grid_size) {
    RNFail("Unable to read %d grid values from file", grid_size);
    return 0;
  }

  // Close file
  fclose(fp);

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

  // Allocate pixels
  ncomponents = 3;
  width = grid_resolution[0];
  height = grid_resolution[1];
  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
  RNScalar *grid_valuesp = grid_values;
  for (int j = 0; j < height; j++) {
    unsigned char *pixelsp = &pixels[j*rowsize];
    for (int i = 0; i < width; i++) {
      int value = (int) ((*(grid_valuesp++) - offset) * 255.0 * scale);
      if (value < 0) value = 0;
      if (value > 255) value = 255;
      unsigned char p = (unsigned char) value;
      *(pixelsp++) = p;
      *(pixelsp++) = p;
      *(pixelsp++) = p;
    }
  }

  // Delete grid values
  delete [] grid_values;

  // Return number of bytes
  return nbytes;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x120(%rsp), %rdi
leaq 0x6aff6(%rip), %rsi     # 0x1335d9
callq 0xf5e0
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
jne 0xc8621
movq 0x120(%rsp), %rsi
leaq 0x6add6(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x134(%rsp)
jmp 0xc8c03
movq 0x118(%rsp), %rcx
leaq 0x110(%rsp), %rdi
movl $0x4, %esi
movl $0x2, %edx
callq 0xf780
cmpq $0x2, %rax
je 0xc8664
leaq 0x6d9cb(%rip), %rdi     # 0x136018
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x134(%rsp)
jmp 0xc8c03
leaq 0xc0(%rsp), %rdi
movq 0x118(%rsp), %rcx
movl $0x8, %esi
movl $0x9, %edx
callq 0xf780
cmpq $0x9, %rax
je 0xc86a7
leaq 0x6d9b1(%rip), %rdi     # 0x136041
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x134(%rsp)
jmp 0xc8c03
movl 0x110(%rsp), %eax
imull 0x114(%rsp), %eax
movl %eax, 0xbc(%rsp)
movslq 0xbc(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
movslq 0xbc(%rsp), %rdx
movq 0x118(%rsp), %rcx
movl $0x8, %esi
callq 0xf780
movl 0xbc(%rsp), %ecx
cmpq %rcx, %rax
je 0xc8741
movl 0xbc(%rsp), %esi
leaq 0x6e0fa(%rip), %rdi     # 0x136824
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x134(%rsp)
jmp 0xc8c03
movq 0x118(%rsp), %rdi
callq 0xf4b0
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0x6a8a6(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xa0(%rsp)
cmpl $0x0, 0xbc(%rsp)
jle 0xc8a38
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x6a893(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x90(%rsp)
movsd 0x6b522(%rip), %xmm0    # 0x133cc0
movsd %xmm0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jge 0xc88ad
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6b6cd(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xc87f0
jp 0xc87f0
jmp 0xc8897
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x88(%rsp), %xmm0
jbe 0xc882e
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movsd 0x90(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc8870
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xb0(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xc87b2
movsd 0x98(%rsp), %xmm0
cvtsi2sdl 0xbc(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0xbc(%rsp), %eax
jge 0xc8956
movq 0xb0(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x6b5af(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xc890b
jp 0xc890b
jmp 0xc8949
movq 0xb0(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x60(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0xc88da
movsd 0x70(%rsp), %xmm0
callq 0xf390
movsd %xmm0, 0x58(%rsp)
movsd 0x78(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movsd 0x6d9e5(%rip), %xmm2    # 0x136360
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc89ad
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x78(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movsd 0x6a81f(%rip), %xmm2    # 0x1331e0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
ucomisd 0x88(%rsp), %xmm0
jbe 0xc89ef
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xc8a36
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0x6a5e1(%rip), %xmm0    # 0x133008
divsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
jmp 0xc8a38
movq 0x8(%rsp), %rax
movl $0x3, 0x8(%rax)
movl 0x110(%rsp), %ecx
movl %ecx, (%rax)
movl 0x114(%rsp), %ecx
movl %ecx, 0x4(%rax)
movl 0x8(%rax), %ecx
imull (%rax), %ecx
movl %ecx, 0xc(%rax)
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xc8a90
movq 0x8(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0x8(%rsp), %rax
addl $0x1, %ecx
shll $0x2, %ecx
movl %ecx, 0xc(%rax)
movq 0x8(%rsp), %rcx
movl 0xc(%rcx), %eax
imull 0x4(%rcx), %eax
movl %eax, 0x3c(%rsp)
movslq 0x3c(%rsp), %rdi
callq 0xf030
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x8(%rsp), %rcx
movl 0x2c(%rsp), %eax
cmpl 0x4(%rcx), %eax
jge 0xc8bdd
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rax
movl 0x2c(%rsp), %ecx
imull 0xc(%rdx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0x8(%rsp), %rcx
movl 0x1c(%rsp), %eax
cmpl (%rcx), %eax
jge 0xc8bcb
movq 0x30(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x30(%rsp)
movsd (%rax), %xmm0
subsd 0xa8(%rsp), %xmm0
movsd 0x6a6e8(%rip), %xmm1    # 0x133220
mulsd %xmm1, %xmm0
mulsd 0xa0(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x18(%rsp)
cmpl $0x0, 0x18(%rsp)
jge 0xc8b5c
movl $0x0, 0x18(%rsp)
cmpl $0xff, 0x18(%rsp)
jle 0xc8b6e
movl $0xff, 0x18(%rsp)
movl 0x18(%rsp), %eax
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %cl
movq 0x20(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x20(%rsp)
movb %cl, (%rax)
movb 0x17(%rsp), %cl
movq 0x20(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x20(%rsp)
movb %cl, (%rax)
movb 0x17(%rsp), %cl
movq 0x20(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x20(%rsp)
movb %cl, (%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xc8b01
jmp 0xc8bcd
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xc8acb
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xc8bf8
movq (%rsp), %rdi
callq 0xf670
movl 0x3c(%rsp), %eax
movl %eax, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
nopw %cs:(%rax,%rax)