// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::WriteImageFile(char const*) const
int R2Grid::
WriteImageFile(const char *filename) const
{
  // Allocate image
  R2Image *image = new R2Image(grid_resolution[0], grid_resolution[1], 3);
  if (!image) {
    RNFail("Unable to allocate image for %s\n", filename);
    return 0;
  }

  // Fill image pixel values
  RNInterval range = Range();
  if (range.Diameter() > 0) {
    for (int j = 0; j < image->Height(); j++) {
      for (int i = 0; i < image->Width(); i++) {
        RNScalar value = (GridValue(i, j) - range.Min()) / (range.Max() - range.Min());
        image->SetPixelRGB(i, j, RNRgb(value, value, value));
      }
    }
  }

  // Mark unknown values
  RNRgb unknown_rgb(1, 0.5, 0);
  for (int j = 0; j < image->Height(); j++) {
    for (int i = 0; i < image->Width(); i++) {
      if (GridValue(i, j) != R2_GRID_UNKNOWN_VALUE) continue;
      image->SetPixelRGB(i, j, unknown_rgb);
    }
  }

  // Write image
  if (!image->Write(filename)) { 
    // RNFail("Unable to write image to %s\n", filename);
    delete image; 
    return 0; 
  }

  // Delete image
  delete image;

  // Return success
  return 1;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x50(%rsp)
movl $0x18, %edi
callq 0xf4c0
movq %rax, %rdi
movq 0x50(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x58(%rsp)
movl 0xc0(%rax), %esi
movl 0xc4(%rax), %edx
movl $0x3, %ecx
callq 0xc5000
jmp 0xb7da6
movq 0x58(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0xb7e0c
movq 0xd0(%rsp), %rsi
leaq 0x7e370(%rip), %rdi     # 0x13613d
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xe4(%rsp)
jmp 0xb8105
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
movl $0x18, %esi
callq 0xf4d0
jmp 0xb8114
movq 0x50(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xa76f0
leaq 0xa8(%rsp), %rdi
callq 0x6bf70
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xb7fad
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xb93c0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0xb7fab
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xb93b0
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0xb7f93
movq 0x50(%rsp), %rdi
movl 0xa0(%rsp), %esi
movl 0xa4(%rsp), %edx
callq 0xb92a0
movsd %xmm0, 0x20(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x13960
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x13970
movsd %xmm0, 0x28(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x13960
movsd 0x28(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x30(%rsp), %xmm0
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x44(%rsp)
movsd 0x98(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
leaq 0x80(%rsp), %rdi
callq 0xd14d0
movq 0x38(%rsp), %rdi
movl 0x40(%rsp), %esi
movl 0x44(%rsp), %edx
leaq 0x80(%rsp), %rcx
callq 0xc5850
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0xb7e74
jmp 0xb7f95
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0xb7e43
jmp 0xb7fad
leaq 0x68(%rsp), %rdi
movsd 0x7b04e(%rip), %xmm0    # 0x133008
movsd 0x7b04e(%rip), %xmm1    # 0x133010
xorps %xmm2, %xmm2
callq 0xd14d0
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xb93c0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0xb807b
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xb93b0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0xb8069
movq 0x50(%rsp), %rdi
movl 0x60(%rsp), %esi
movl 0x64(%rsp), %edx
callq 0xb92a0
movsd 0x7be7a(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xb8040
jp 0xb8040
jmp 0xb8042
jmp 0xb805c
movq 0xc8(%rsp), %rdi
movl 0x60(%rsp), %esi
movl 0x64(%rsp), %edx
leaq 0x68(%rsp), %rcx
callq 0xc5850
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0xb7ffd
jmp 0xb806b
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0xb7fd2
movq 0xc8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0xb98b0
cmpl $0x0, %eax
jne 0xb80ce
movq 0xc8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xb80c1
movq 0x10(%rsp), %rdi
callq 0xc5520
movq 0x10(%rsp), %rdi
movl $0x18, %esi
callq 0xf4d0
movl $0x0, 0xe4(%rsp)
jmp 0xb8105
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb80fa
movq 0x8(%rsp), %rdi
callq 0xc5520
movq 0x8(%rsp), %rdi
movl $0x18, %esi
callq 0xf4d0
movl $0x1, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
movq 0xc0(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)