// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/gapsview/gapsview.cpp
// Function: ReadImageFiles(char const*, double, double)
static ImageData *
ReadImageFiles(const char *image_name,
  double depth_scale, double depth_exponent)
{
  // Check image directory
  if (!image_directory) return NULL;
  if (!image_name) return NULL;
  
  /// Get convenient variables
  double ds = (depth_scale >= 0) ? 1.0 / depth_scale : 1.0;
  double de = (depth_exponent != 0) ? 1.0 / depth_exponent : 1.0;
  char filename[2048];
  
  // Allocate GSV image data structure
  ImageData *data = new ImageData();
  if (!data) {
    fprintf(stderr, "Unable to allocate GSV image data\n");
    return NULL;
  }

  // Read color image 
  sprintf(filename, "%s/color_images/%s.png", image_directory, image_name);
  if (RNFileExists(filename)) {
    if (!data->color_image.ReadFile(filename)) { delete data; return NULL; }
  }
  else {
    sprintf(filename, "%s/color_images/%s.jpg", image_directory, image_name);
    if (RNFileExists(filename)) {
      if (!data->color_image.ReadFile(filename)) { delete data; return NULL; }
    }
  }

  // Read depth image
  sprintf(filename, "%s/depth_images/%s.png", image_directory, image_name);
  if (RNFileExists(filename)) {
    if (!data->depth_image.ReadFile(filename)) { delete data; return NULL; }
    data->depth_image.Multiply(ds);
    data->depth_image.Pow(de);
  }

  // Read category image
  sprintf(filename, "%s/category_images/%s.png", image_directory, image_name);
  if (RNFileExists(filename)) {
    if (!data->category_image.ReadFile(filename)) { delete data; return NULL; }
  }

  // Read instance image
  sprintf(filename, "%s/instance_images/%s.png", image_directory, image_name);
  if (RNFileExists(filename)) {
    if (!data->instance_image.ReadFile(filename)) { delete data; return NULL; }
  }

  // Read ray_origin images
  for (int i = 0; i < 3; i++) {
    const char *channel_name = "";
    if (i == 0) channel_name = "ox";
    else if (i == 1) channel_name = "oy";
    else if (i == 2) channel_name = "oz";
    sprintf(filename, "%s/ray_images/%s_%s.pfm", image_directory, image_name, channel_name);
    if (RNFileExists(filename)) {
      if (!data->ray_origin_images[i].ReadFile(filename)) { delete data; return NULL; }
    }
  }

  // Read ray_direction images
  for (int i = 0; i < 3; i++) {
    const char *channel_name = "";
    if (i == 0) channel_name = "dx";
    else if (i == 1) channel_name = "dy";
    else if (i == 2) channel_name = "dz";
    sprintf(filename, "%s/ray_images/%s_%s.pfm", image_directory, image_name, channel_name);
    if (RNFileExists(filename)) {
      if (!data->ray_direction_images[i].ReadFile(filename)) { delete data; return NULL; }
    }
  }

  // Set image dimensions
  data->image_width = data->color_image.Width();
  data->image_height = data->color_image.Height();
  
  // Return data
  return data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
xorl %eax, %eax
testq %rdi, %rdi
je 0x30d0a
cmpq $0x0, 0x1939b9(%rip)    # 0x1c4400
je 0x30d0a
movq %rdi, %r14
movsd %xmm0, 0x18(%rsp)
movapd %xmm1, (%rsp)
movl $0x778, %edi            # imm = 0x778
callq 0x27940
movq %rax, %rbx
movq %rax, %rdi
callq 0x311d2
movq 0x193989(%rip), %rdx    # 0x1c4400
leaq 0xfaddf(%rip), %rsi     # 0x12b85d
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xe391c
testl %eax, %eax
jne 0x30ac8
movq 0x19395d(%rip), %rdx    # 0x1c4400
leaq 0xfadca(%rip), %rsi     # 0x12b874
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30add
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0xdbafc
testl %eax, %eax
je 0x30cf3
movq 0x19391c(%rip), %rdx    # 0x1c4400
leaq 0xfada0(%rip), %rsi     # 0x12b88b
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30b8d
leaq 0x18(%rbx), %r15
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0xd2a06
testl %eax, %eax
je 0x30cf3
movsd 0xfa542(%rip), %xmm1    # 0x12b070
movapd %xmm1, %xmm2
movsd 0x18(%rsp), %xmm4
divsd %xmm4, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
cmplesd %xmm4, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
movapd %xmm1, %xmm2
movapd (%rsp), %xmm4
divsd %xmm4, %xmm2
cmpneqsd %xmm3, %xmm4
andpd %xmm4, %xmm2
andnpd %xmm1, %xmm4
orpd %xmm2, %xmm4
movapd %xmm4, (%rsp)
movq %r15, %rdi
callq 0xcf51a
movq %r15, %rdi
movapd (%rsp), %xmm0
callq 0xcf640
movq 0x19386c(%rip), %rdx    # 0x1c4400
leaq 0xfad07(%rip), %rsi     # 0x12b8a2
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30bd2
leaq 0xe8(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xd2a06
testl %eax, %eax
je 0x30cf3
movq 0x193827(%rip), %rdx    # 0x1c4400
leaq 0xfacdc(%rip), %rsi     # 0x12b8bc
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30c17
leaq 0x1b8(%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xd2a06
testl %eax, %eax
je 0x30cf3
movq %rbx, (%rsp)
leaq 0x288(%rbx), %r15
leaq 0xfa727(%rip), %rbp     # 0x12b350
leaq 0xfacac(%rip), %r12     # 0x12b8dc
leaq 0x20(%rsp), %r13
xorl %ebx, %ebx
cmpq $0xc, %rbx
je 0x30c83
movslq (%rbx,%rbp), %r8
addq %rbp, %r8
movq 0x1937b5(%rip), %rdx    # 0x1c4400
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r13, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30c76
movq %r15, %rdi
movq %r13, %rsi
callq 0xd2a06
testl %eax, %eax
je 0x30cef
addq $0x4, %rbx
addq $0xd0, %r15
jmp 0x30c37
movq (%rsp), %r15
addq $0x4f8, %r15            # imm = 0x4F8
leaq 0xfa6c7(%rip), %rbp     # 0x12b35c
leaq 0xfac40(%rip), %r12     # 0x12b8dc
leaq 0x20(%rsp), %r13
xorl %ebx, %ebx
cmpq $0xc, %rbx
je 0x30d1c
movslq (%rbx,%rbp), %r8
addq %rbp, %r8
movq 0x193749(%rip), %rdx    # 0x1c4400
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rcx
xorl %eax, %eax
callq 0x27140
movq %r13, %rdi
callq 0xe391c
testl %eax, %eax
je 0x30ce2
movq %r15, %rdi
movq %r13, %rsi
callq 0xd2a06
testl %eax, %eax
je 0x30cef
addq $0x4, %rbx
addq $0xd0, %r15
jmp 0x30ca3
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x31324
movl $0x778, %esi            # imm = 0x778
movq %rbx, %rdi
callq 0x27960
xorl %eax, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x768(%rax)
jmp 0x30d0a
movq %rax, %r14
movl $0x778, %esi            # imm = 0x778
movq %rbx, %rdi
callq 0x27960
movq %r14, %rdi
callq 0x28170