// File: 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:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x38(%rsp)
movl $0x18, %edi
callq 0xf4c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0xc4ad0
jmp 0xb73e0
movq 0x40(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0xb743a
movq 0x80(%rsp), %rsi
leaq 0x7ed3c(%rip), %rdi     # 0x13613d
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0xb767c
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x18, %esi
callq 0xf4d0
jmp 0xb768b
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xb9880
cmpl $0x0, %eax
jne 0xb748a
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0xb747a
movq 0x30(%rsp), %rdi
callq 0xc5520
movq 0x30(%rsp), %rdi
movl $0x18, %esi
callq 0xf4d0
movl $0x0, 0x94(%rsp)
jmp 0xb767c
movq 0x78(%rsp), %rdi
callq 0xb93b0
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0xc0(%rax)
movq 0x78(%rsp), %rdi
callq 0xb93c0
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0xc4(%rax)
movq 0x78(%rsp), %rdi
callq 0xb93b0
movl %eax, %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0xc8(%rax)
movq 0x78(%rsp), %rdi
callq 0xb93b0
movl %eax, 0x2c(%rsp)
movq 0x78(%rsp), %rdi
callq 0xb93c0
movq 0x38(%rsp), %rdi
movl %eax, %ecx
movl 0x2c(%rsp), %eax
imull %ecx, %eax
movl %eax, 0xcc(%rdi)
movsd 0x7bb05(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rdi)
addq $0x58, %rdi
leaq 0x4c3552(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x38(%rsp), %rdi
leaq 0x4c3541(%rip), %rsi    # 0x57aa68
callq 0xb9200
movq 0x38(%rsp), %rax
cmpq $0x0, 0xb8(%rax)
je 0xb755e
movq 0x38(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0xb755c
movq 0x20(%rsp), %rdi
callq 0xf670
jmp 0xb755e
movq 0x38(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0xb8(%rax)
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x78(%rsp), %rdi
callq 0xb93c0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0xb7648
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
callq 0xb93b0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0xb7636
movl 0x64(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x78(%rsp), %rsi
movl 0x64(%rsp), %edx
movl 0x68(%rsp), %ecx
leaq 0x48(%rsp), %rdi
callq 0xc5560
leaq 0x48(%rsp), %rdi
callq 0xb93d0
movq 0x38(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
callq 0x6bc00
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0xb75ca
jmp 0xb7638
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0xb75a2
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xb7671
movq 0x8(%rsp), %rdi
callq 0xc5520
movq 0x8(%rsp), %rdi
movl $0x18, %esi
callq 0xf4d0
movl $0x1, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)