// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::ReadImageDirectory(gaps::R3SurfelScene*, char const*, double, double, int)
int
ReadImageDirectory(R3SurfelScene *scene, const char *image_directory,
  double depth_scale, double depth_exponent, int max_images)
{
  /// 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;
  
  // Read all channels for all images
  char filename[1024];
  int skip = (max_images > 0) ? scene->NImages() / max_images + 1 : 1;
  for (int i = 0; i < scene->NImages(); i += skip) {
    R3SurfelImage *image = scene->Image(i);

    // Get color image name
    sprintf(filename, "%s/color_images/%s.png", image_directory, image->Name());
    if (!RNFileExists(filename)) {
      sprintf(filename, "%s/color_images/%s.jpg", image_directory, image->Name());
      if (!RNFileExists(filename)) {
        sprintf(filename, "%s/color_channels/%s.png", image_directory, image->Name());
        if (!RNFileExists(filename)) {
          sprintf(filename, "%s/color_channels/%s.jpg", image_directory, image->Name());
        }
      }
    }

    // Read color image 
    if (RNFileExists(filename)) {
      R2Image color_image;
      if (!color_image.ReadFile(filename)) return 0;
      image->SetColorChannels(color_image);
    }

    // Get depth image name
    sprintf(filename, "%s/depth_images/%s.png", image_directory, image->Name());
    if (!RNFileExists(filename)) {
      sprintf(filename, "%s/depth_channel/%s.png", image_directory, image->Name());
    }

    // Read depth image
    if (RNFileExists(filename)) {
      R2Grid depth_image;
      if (!depth_image.ReadFile(filename)) return 0;
      depth_image.Multiply(ds);
      depth_image.Pow(de);
      image->SetDepthChannel(depth_image);
    }

    // Get category image name
    sprintf(filename, "%s/category_images/%s.png", image_directory, image->Name());
    if (!RNFileExists(filename)) {
      sprintf(filename, "%s/category_channel/%s.png", image_directory, image->Name());
    }

    // Read category image
    if (RNFileExists(filename)) {
      R2Grid category_image;
      if (!category_image.ReadFile(filename)) return 0;
      image->SetCategoryChannel(category_image);
    }

    // Get instance image name
    sprintf(filename, "%s/instance_images/%s.png", image_directory, image->Name());
    if (!RNFileExists(filename)) {
      sprintf(filename, "%s/instance_channel/%s.png", image_directory, image->Name());
    }

    // Read instance image
    if (RNFileExists(filename)) {
      R2Grid instance_image;
      if (!instance_image.ReadFile(filename)) return 0;
      image->SetInstanceChannel(instance_image);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x7e8, %rsp            # imm = 0x7E8
movq %rdi, 0x7d8(%rsp)
movq %rsi, 0x7d0(%rsp)
movsd %xmm0, 0x7c8(%rsp)
movsd %xmm1, 0x7c0(%rsp)
movl %edx, 0x7bc(%rsp)
movsd 0x7c8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x37bce
movsd 0x16c5ae(%rip), %xmm0   # 0x1a4168
divsd 0x7c8(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
jmp 0x37be1
movsd 0x16c592(%rip), %xmm0   # 0x1a4168
movsd %xmm0, 0xf0(%rsp)
jmp 0x37be1
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x7b0(%rsp)
movsd 0x7c0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x37c09
jp 0x37c09
jmp 0x37c25
movsd 0x16c557(%rip), %xmm0   # 0x1a4168
divsd 0x7c0(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
jmp 0x37c38
movsd 0x16c53b(%rip), %xmm0   # 0x1a4168
movsd %xmm0, 0xe8(%rsp)
jmp 0x37c38
movsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0x7a8(%rsp)
cmpl $0x0, 0x7bc(%rsp)
jle 0x37c75
movq 0x7d8(%rsp), %rdi
callq 0x16cc0
cltd
idivl 0x7bc(%rsp)
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x37c83
movl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x37c83
movl 0xe4(%rsp), %eax
movl %eax, 0x39c(%rsp)
movl $0x0, 0x398(%rsp)
movl 0x398(%rsp), %eax
movl %eax, 0xe0(%rsp)
movq 0x7d8(%rsp), %rdi
callq 0x16cc0
movl %eax, %ecx
movl 0xe0(%rsp), %eax
cmpl %ecx, %eax
jge 0x383c0
movq 0x7d8(%rsp), %rdi
movl 0x398(%rsp), %esi
callq 0x30800
movq %rax, 0x390(%rsp)
leaq 0x3a0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rdx
movq %rax, %rcx
leaq 0x16ce43(%rip), %rsi    # 0x1a4b6e
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x37e5e
leaq 0x3a0(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cdf6(%rip), %rsi    # 0x1a4b85
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x37e5c
leaq 0x3a0(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cda9(%rip), %rsi    # 0x1a4b9c
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x37e5a
leaq 0x3a0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cd62(%rip), %rsi    # 0x1a4bb5
movb $0x0, %al
callq 0x130f0
jmp 0x37e5c
jmp 0x37e5e
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
je 0x37f38
leaq 0x378(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x1352f0
movq 0x90(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x135370
movl %eax, 0x9c(%rsp)
jmp 0x37ea7
movl 0x9c(%rsp), %eax
cmpl $0x0, %eax
jne 0x37ef1
movl $0x0, 0x7e4(%rsp)
movl $0x1, 0x368(%rsp)
jmp 0x37f17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x370(%rsp)
movl %eax, 0x36c(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x135d40
jmp 0x383da
movq 0x390(%rsp), %rdi
movq (%rdi), %rax
movq 0x48(%rax), %rax
leaq 0x378(%rsp), %rsi
callq *%rax
jmp 0x37f0c
movl $0x0, 0x368(%rsp)
leaq 0x378(%rsp), %rdi
callq 0x135d40
movl 0x368(%rsp), %eax
testl %eax, %eax
je 0x37f36
jmp 0x37f31
jmp 0x383cb
jmp 0x37f38
leaq 0x3a0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cc4f(%rip), %rsi    # 0x1a4bce
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x37fda
leaq 0x3a0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cc12(%rip), %rsi    # 0x1a4be5
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
je 0x380dc
leaq 0x298(%rsp), %rdi
movq %rdi, 0x60(%rsp)
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
movq 0x60(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x120e90
movl %eax, 0x6c(%rsp)
jmp 0x3801e
movl 0x6c(%rsp), %eax
cmpl $0x0, %eax
jne 0x38065
movl $0x0, 0x7e4(%rsp)
movl $0x1, 0x368(%rsp)
jmp 0x380bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x370(%rsp)
movl %eax, 0x36c(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x1127b0
jmp 0x383da
movsd 0x7b0(%rsp), %xmm0
leaq 0x298(%rsp), %rdi
callq 0x1198a0
jmp 0x3807d
movsd 0x7a8(%rsp), %xmm0
leaq 0x298(%rsp), %rdi
callq 0x119be0
jmp 0x38095
movq 0x390(%rsp), %rdi
movq (%rdi), %rax
movq 0x30(%rax), %rax
leaq 0x298(%rsp), %rsi
callq *%rax
jmp 0x380b0
movl $0x0, 0x368(%rsp)
leaq 0x298(%rsp), %rdi
callq 0x1127b0
movl 0x368(%rsp), %eax
testl %eax, %eax
je 0x380da
jmp 0x380d5
jmp 0x383cb
jmp 0x380dc
leaq 0x3a0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rdx
movq %rax, %rcx
leaq 0x16cae6(%rip), %rsi    # 0x1a4bfd
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x38172
leaq 0x3a0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq %rax, %rcx
leaq 0x16caac(%rip), %rsi    # 0x1a4c17
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
je 0x38244
leaq 0x1c8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
movq 0x30(%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x120e90
movl %eax, 0x3c(%rsp)
jmp 0x381b6
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
jne 0x381fd
movl $0x0, 0x7e4(%rsp)
movl $0x1, 0x368(%rsp)
jmp 0x38223
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x370(%rsp)
movl %eax, 0x36c(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x1127b0
jmp 0x383da
movq 0x390(%rsp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
leaq 0x1c8(%rsp), %rsi
callq *%rax
jmp 0x38218
movl $0x0, 0x368(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x1127b0
movl 0x368(%rsp), %eax
testl %eax, %eax
je 0x38242
jmp 0x3823d
jmp 0x383cb
jmp 0x38244
leaq 0x3a0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq %rax, %rcx
leaq 0x16c9b3(%rip), %rsi    # 0x1a4c32
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
jne 0x382da
leaq 0x3a0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x7d0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x390(%rsp), %rdi
callq 0x30890
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rax, %rcx
leaq 0x16c979(%rip), %rsi    # 0x1a4c4c
movb $0x0, %al
callq 0x130f0
leaq 0x3a0(%rsp), %rdi
callq 0x145290
cmpl $0x0, %eax
je 0x383a4
leaq 0xf8(%rsp), %rdi
movq %rdi, (%rsp)
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
movq (%rsp), %rdi
leaq 0x3a0(%rsp), %rsi
callq 0x120e90
movl %eax, 0xc(%rsp)
jmp 0x3831c
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x38360
movl $0x0, 0x7e4(%rsp)
movl $0x1, 0x368(%rsp)
jmp 0x38386
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x370(%rsp)
movl %eax, 0x36c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x1127b0
jmp 0x383da
movq 0x390(%rsp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
leaq 0xf8(%rsp), %rsi
callq *%rax
jmp 0x3837b
movl $0x0, 0x368(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x1127b0
movl 0x368(%rsp), %eax
testl %eax, %eax
je 0x383a2
jmp 0x383a0
jmp 0x383cb
jmp 0x383a4
jmp 0x383a6
movl 0x39c(%rsp), %eax
addl 0x398(%rsp), %eax
movl %eax, 0x398(%rsp)
jmp 0x37c9c
movl $0x1, 0x7e4(%rsp)
movl 0x7e4(%rsp), %eax
addq $0x7e8, %rsp            # imm = 0x7E8
retq
movq 0x370(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)