// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0xe440
movq %rax, %rbx
movl 0xc0(%r15), %esi
movl 0xc4(%r15), %edx
movq %rax, %rdi
movl $0x3, %ecx
callq 0x68c5a
movq %r14, 0x8(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x59b34
movsd 0x8(%r12), %xmm0
ucomisd (%r12), %xmm0
jbe 0x6260a
movl 0x4(%rbx), %ecx
testl %ecx, %ecx
jle 0x6260a
movl (%rbx), %eax
xorl %r14d, %r14d
leaq 0x20(%rsp), %r12
testl %eax, %eax
jle 0x62602
movslq %r14d, %r13
xorl %ebp, %ebp
movq 0xb8(%r15), %rax
movslq 0xc8(%r15), %rcx
imulq %r13, %rcx
addq %rbp, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
subsd %xmm1, %xmm0
subsd %xmm1, %xmm2
divsd %xmm2, %xmm0
movq %r12, %rdi
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
callq 0x6f680
movq %rbx, %rdi
movl %ebp, %esi
movl %r13d, %edx
movq %r12, %rcx
callq 0x6900a
movl (%rbx), %eax
incq %rbp
cmpl %eax, %ebp
jl 0x625a4
movl 0x4(%rbx), %ecx
incl %r14d
cmpl %ecx, %r14d
jl 0x6259b
leaq 0x20(%rsp), %rdi
movsd 0x3fa21(%rip), %xmm0    # 0xa2038
movsd 0x3fa11(%rip), %xmm1    # 0xa2030
xorpd %xmm2, %xmm2
callq 0x6f680
movl 0x4(%rbx), %ecx
testl %ecx, %ecx
jle 0x62698
movl (%rbx), %eax
xorl %r12d, %r12d
movsd 0x4097c(%rip), %xmm1    # 0xa2fb8
leaq 0x20(%rsp), %r13
testl %eax, %eax
jle 0x62690
movslq %r12d, %r14
xorl %ebp, %ebp
movq 0xb8(%r15), %rcx
movslq 0xc8(%r15), %rdx
imulq %r14, %rdx
addq %rbp, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x62686
jp 0x62686
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %edx
movq %r13, %rcx
callq 0x6900a
movsd 0x40934(%rip), %xmm1    # 0xa2fb8
movl (%rbx), %eax
incq %rbp
cmpl %eax, %ebp
jl 0x6264a
movl 0x4(%rbx), %ecx
incl %r12d
cmpl %ecx, %r12d
jl 0x62641
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x6aad8
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movq %rbx, %rdi
callq 0x68eb4
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe450
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe450
movq %r14, %rdi
callq 0xe7e0
nop