// File: /workspace/github_cpp_makefile_star2_O1/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 0x69a46
movq %r14, 0x8(%rsp)
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5a83c
movsd 0x8(%r12), %xmm0
ucomisd (%r12), %xmm0
jbe 0x63440
cmpl $0x0, 0x4(%rbx)
jle 0x63440
xorl %r14d, %r14d
leaq 0x20(%rsp), %r12
cmpl $0x0, (%rbx)
jle 0x63437
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 0x70460
movq %rbx, %rdi
movl %ebp, %esi
movl %r13d, %edx
movq %r12, %rcx
callq 0x69df6
incq %rbp
cmpl (%rbx), %ebp
jl 0x633de
incl %r14d
cmpl 0x4(%rbx), %r14d
jl 0x633d4
leaq 0x20(%rsp), %rdi
movsd 0x3ebeb(%rip), %xmm0    # 0xa2038
movsd 0x3ebdb(%rip), %xmm1    # 0xa2030
xorpd %xmm2, %xmm2
callq 0x70460
cmpl $0x0, 0x4(%rbx)
jle 0x634c8
xorl %r12d, %r12d
movsd 0x3fb49(%rip), %xmm1    # 0xa2fb8
leaq 0x20(%rsp), %r13
cmpl $0x0, (%rbx)
jle 0x634bf
movslq %r12d, %r14
xorl %ebp, %ebp
movq 0xb8(%r15), %rax
movslq 0xc8(%r15), %rcx
imulq %r14, %rcx
addq %rbp, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x634b8
jp 0x634b8
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %edx
movq %r13, %rcx
callq 0x69df6
movsd 0x3fb00(%rip), %xmm1    # 0xa2fb8
incq %rbp
cmpl (%rbx), %ebp
jl 0x6347e
incl %r12d
cmpl 0x4(%rbx), %r12d
jl 0x63474
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x6b880
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
movq %rbx, %rdi
callq 0x69cae
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