// File: 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:
subq $0x8b8, %rsp            # imm = 0x8B8
movq %rdi, 0x8a8(%rsp)
movsd %xmm0, 0x8a0(%rsp)
movsd %xmm1, 0x898(%rsp)
cmpq $0x0, 0x2c6fff(%rip)    # 0x2fef28
jne 0x37f3c
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
cmpq $0x0, 0x8a8(%rsp)
jne 0x37f58
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
movsd 0x8a0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x37f83
movsd 0x22e09e(%rip), %xmm0   # 0x266010
divsd 0x8a0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x37f93
movsd 0x22e085(%rip), %xmm0   # 0x266010
movsd %xmm0, 0x48(%rsp)
jmp 0x37f93
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x890(%rsp)
movsd 0x898(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x37fb8
jp 0x37fb8
jmp 0x37fd1
movsd 0x22e050(%rip), %xmm0   # 0x266010
divsd 0x898(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x37fe1
movsd 0x22e037(%rip), %xmm0   # 0x266010
movsd %xmm0, 0x40(%rsp)
jmp 0x37fe1
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x888(%rsp)
movl $0x778, %edi            # imm = 0x778
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
callq 0x3aa50
jmp 0x3800c
movq 0x38(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x38069
movq 0x2b6fb3(%rip), %rax    # 0x2eefd8
movq (%rax), %rdi
leaq 0x22e905(%rip), %rsi    # 0x266934
movb $0x0, %al
callq 0x293c0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
jmp 0x385d5
leaq 0x80(%rsp), %rdi
movq 0x2c6eb0(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
leaq 0x22e8d0(%rip), %rsi    # 0x266957
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x380f6
movq 0x78(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1bb440
cmpl $0x0, %eax
jne 0x380f1
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x380e0
movq 0x30(%rsp), %rdi
callq 0x3ad30
movq 0x30(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x38182
leaq 0x80(%rsp), %rdi
movq 0x2c6e23(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
leaq 0x22e85a(%rip), %rsi    # 0x26696e
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x38180
movq 0x78(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1bb440
cmpl $0x0, %eax
jne 0x3817e
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x3816d
movq 0x28(%rsp), %rdi
callq 0x3ad30
movq 0x28(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x38180
jmp 0x38182
leaq 0x80(%rsp), %rdi
movq 0x2c6d97(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
leaq 0x22e7e5(%rip), %rsi    # 0x266985
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x38240
movq 0x78(%rsp), %rdi
addq $0x18, %rdi
leaq 0x80(%rsp), %rsi
callq 0x1a6ae0
cmpl $0x0, %eax
jne 0x38212
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x38201
movq 0x20(%rsp), %rdi
callq 0x3ad30
movq 0x20(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
movq 0x78(%rsp), %rdi
addq $0x18, %rdi
movsd 0x890(%rsp), %xmm0
callq 0x19f4f0
movq 0x78(%rsp), %rdi
addq $0x18, %rdi
movsd 0x888(%rsp), %xmm0
callq 0x19f830
leaq 0x80(%rsp), %rdi
movq 0x2c6cd9(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
leaq 0x22e73e(%rip), %rsi    # 0x26699c
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x382d1
movq 0x78(%rsp), %rdi
addq $0xe8, %rdi
leaq 0x80(%rsp), %rsi
callq 0x1a6ae0
cmpl $0x0, %eax
jne 0x382cf
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x382be
movq 0x18(%rsp), %rdi
callq 0x3ad30
movq 0x18(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x382d1
leaq 0x80(%rsp), %rdi
movq 0x2c6c48(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
leaq 0x22e6c7(%rip), %rsi    # 0x2669b6
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x38362
movq 0x78(%rsp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
leaq 0x80(%rsp), %rsi
callq 0x1a6ae0
cmpl $0x0, %eax
jne 0x38360
movq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3834f
movq 0x10(%rsp), %rdi
callq 0x3ad30
movq 0x10(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x38362
movl $0x0, 0x68(%rsp)
cmpl $0x3, 0x68(%rsp)
jge 0x38479
leaq 0x22ec00(%rip), %rax    # 0x266f7c
movq %rax, 0x60(%rsp)
cmpl $0x0, 0x68(%rsp)
jne 0x38396
leaq 0x2331fa(%rip), %rax    # 0x26b589
movq %rax, 0x60(%rsp)
jmp 0x383c2
cmpl $0x1, 0x68(%rsp)
jne 0x383ab
leaq 0x22e62c(%rip), %rax    # 0x2669d0
movq %rax, 0x60(%rsp)
jmp 0x383c0
cmpl $0x2, 0x68(%rsp)
jne 0x383be
leaq 0x22e61a(%rip), %rax    # 0x2669d3
movq %rax, 0x60(%rsp)
jmp 0x383c0
jmp 0x383c2
leaq 0x80(%rsp), %rdi
movq 0x2c6b57(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
movq 0x60(%rsp), %r8
leaq 0x22e5f1(%rip), %rsi    # 0x2669d6
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x38467
movq 0x78(%rsp), %rdi
addq $0x288, %rdi            # imm = 0x288
movslq 0x68(%rsp), %rax
imulq $0xd0, %rax, %rax
addq %rax, %rdi
leaq 0x80(%rsp), %rsi
callq 0x1a6ae0
cmpl $0x0, %eax
jne 0x38465
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x38454
movq 0x8(%rsp), %rdi
callq 0x3ad30
movq 0x8(%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x38467
jmp 0x38469
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x3836a
movl $0x0, 0x5c(%rsp)
cmpl $0x3, 0x5c(%rsp)
jge 0x3858a
leaq 0x22eae9(%rip), %rax    # 0x266f7c
movq %rax, 0x50(%rsp)
cmpl $0x0, 0x5c(%rsp)
jne 0x384ad
leaq 0x23379d(%rip), %rax    # 0x26bc43
movq %rax, 0x50(%rsp)
jmp 0x384d9
cmpl $0x1, 0x5c(%rsp)
jne 0x384c2
leaq 0x22e533(%rip), %rax    # 0x2669ee
movq %rax, 0x50(%rsp)
jmp 0x384d7
cmpl $0x2, 0x5c(%rsp)
jne 0x384d5
leaq 0x22e521(%rip), %rax    # 0x2669f1
movq %rax, 0x50(%rsp)
jmp 0x384d7
jmp 0x384d9
leaq 0x80(%rsp), %rdi
movq 0x2c6a40(%rip), %rdx    # 0x2fef28
movq 0x8a8(%rsp), %rcx
movq 0x50(%rsp), %r8
leaq 0x22e4da(%rip), %rsi    # 0x2669d6
movb $0x0, %al
callq 0x2a280
leaq 0x80(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x38578
movq 0x78(%rsp), %rdi
addq $0x4f8, %rdi            # imm = 0x4F8
movslq 0x5c(%rsp), %rax
imulq $0xd0, %rax, %rax
addq %rax, %rdi
leaq 0x80(%rsp), %rsi
callq 0x1a6ae0
cmpl $0x0, %eax
jne 0x38576
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x38568
movq (%rsp), %rdi
callq 0x3ad30
movq (%rsp), %rdi
movl $0x778, %esi            # imm = 0x778
callq 0x291f0
movq $0x0, 0x8b0(%rsp)
jmp 0x385c5
jmp 0x38578
jmp 0x3857a
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x38481
movq 0x78(%rsp), %rdi
callq 0x39d10
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x768(%rax)
movq 0x78(%rsp), %rdi
callq 0x39d20
movl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x76c(%rax)
movq 0x78(%rsp), %rax
movq %rax, 0x8b0(%rsp)
movq 0x8b0(%rsp), %rax
addq $0x8b8, %rsp            # imm = 0x8B8
retq
movq 0x70(%rsp), %rdi
callq 0x29c10
nop