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

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

  // Fill in grid info
  grid_resolution[0] = image->Width();
  grid_resolution[1] = image->Height();
  grid_row_size = image->Width();
  grid_size = image->Width() * image->Height();
  world_to_grid_scale_factor = 1;
  world_to_grid_transform = R2identity_affine;
  grid_to_world_transform = R2identity_affine;
  if (grid_values) delete [] grid_values;
  grid_values = new RNScalar [ grid_size ];
  for (int j = 0; j < image->Height(); j++) {
    for (int i = 0; i < image->Width(); i++) {
      SetGridValue(i, j, image->PixelRGB(i, j).Luminance());
    }
  }

  // Delete image
  delete image;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl $0x18, %edi
callq 0xe440
movq %rax, %rbx
movq %rax, %rdi
callq 0x68938
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6895c
testl %eax, %eax
je 0x6209b
movl (%rbx), %eax
movl %eax, 0xc0(%r14)
movl 0x4(%rbx), %ecx
movl %ecx, 0xc4(%r14)
movl %eax, 0xc8(%r14)
imull %eax, %ecx
movl %ecx, 0xcc(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%r14)
leaq 0x60(%r14), %rdi
leaq 0x485fdc(%rip), %r12    # 0x4e7f58
leaq 0x8(%r12), %r15
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0xa8(%r14)
leaq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r12), %eax
movl %eax, 0x50(%r14)
movq 0xb8(%r14), %rdi
testq %rdi, %rdi
je 0x61fbb
callq 0xe5b0
movslq 0xcc(%r14), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0xb8(%r14)
movl 0x4(%rbx), %ecx
testl %ecx, %ecx
jle 0x6207f
movl (%rbx), %eax
xorl %ebp, %ebp
movq %rsp, %r15
testl %eax, %eax
jle 0x62075
movslq %ebp, %r12
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %r12d, %ecx
callq 0x68ec2
movsd 0x472df(%rip), %xmm2    # 0xa92f8
movsd 0x472cf(%rip), %xmm1    # 0xa92f0
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd (%rsp), %xmm1
mulsd %xmm2, %xmm1
movsd 0x472c4(%rip), %xmm2    # 0xa9300
addsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0xb8(%r14), %rax
movslq 0xc8(%r14), %rcx
imulq %r12, %rcx
addq %r13, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl (%rbx), %eax
incq %r13
cmpl %eax, %r13d
jl 0x62000
movl 0x4(%rbx), %ecx
incl %ebp
cmpl %ecx, %ebp
jl 0x61ff6
movq %rbx, %rdi
callq 0x68eb4
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe450
movl $0x1, %eax
jmp 0x620b2
movq %rbx, %rdi
callq 0x68eb4
movl $0x18, %esi
movq %rbx, %rdi
callq 0xe450
xorl %eax, %eax
addq $0x18, %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