// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
movq %rsi, %rbx
movq %rdi, %r14
movl 0x9c(%rdi), %ecx
testl %edx, %edx
jle 0x2489b
movl %edx, %r8d
movl %ecx, %eax
cltd
idivl %r8d
incl %eax
cltq
jmp 0x248a0
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %eax
testl %ecx, %ecx
jle 0x24bb5
movsd 0xb888e(%rip), %xmm4    # 0xdd148
xorpd %xmm5, %xmm5
movapd %xmm4, %xmm2
divsd %xmm0, %xmm2
movapd %xmm4, %xmm3
divsd %xmm1, %xmm3
cmpneqsd %xmm5, %xmm1
cmplesd %xmm0, %xmm5
andpd %xmm5, %xmm2
andnpd %xmm4, %xmm5
orpd %xmm2, %xmm5
movapd %xmm5, 0x20(%rsp)
andpd %xmm1, %xmm3
andnpd %xmm4, %xmm1
orpd %xmm3, %xmm1
movapd %xmm1, 0x10(%rsp)
leaq 0x100(%rsp), %r12
leaq 0x30(%rsp), %rbp
xorl %r15d, %r15d
movq 0x90(%r14), %rax
movq (%rax,%r15,8), %r13
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb923c(%rip), %rsi     # 0xddb64
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x249a7
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb922c(%rip), %rsi     # 0xddb7b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x249a7
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb921c(%rip), %rsi     # 0xddb92
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x249a7
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb920e(%rip), %rsi     # 0xddbab
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
je 0x249e3
movq %rbp, %rdi
callq 0xa2dd4
movq %rbp, %rdi
movq %r12, %rsi
callq 0xa2df8
testl %eax, %eax
je 0x24ba9
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x48(%rax)
movq %rbp, %rdi
callq 0xa3350
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb91d0(%rip), %rsi     # 0xddbc4
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x24a25
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb91c0(%rip), %rsi     # 0xddbdb
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
je 0x24a81
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x90cb8
movq %rbp, %rdi
movq %r12, %rsi
callq 0x990d6
testl %eax, %eax
je 0x24b9d
movq %rbp, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x956ec
movq %rbp, %rdi
movapd 0x10(%rsp), %xmm0
callq 0x9581e
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x30(%rax)
movq %rbp, %rdi
callq 0x918ec
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb9161(%rip), %rsi     # 0xddbf3
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x24ac3
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb9154(%rip), %rsi     # 0xddc0d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
je 0x24b03
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x90cb8
movq %rbp, %rdi
movq %r12, %rsi
callq 0x990d6
testl %eax, %eax
je 0x24b9d
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x38(%rax)
movq %rbp, %rdi
callq 0x918ec
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb9114(%rip), %rsi     # 0xddc28
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
jne 0x24b45
movq 0x1c0(%r13), %rcx
movq %r12, %rdi
leaq 0xb9107(%rip), %rsi     # 0xddc42
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xabbec
testl %eax, %eax
je 0x24b81
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x90cb8
movq %rbp, %rdi
movq %r12, %rsi
callq 0x990d6
testl %eax, %eax
je 0x24b9d
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x40(%rax)
movq %rbp, %rdi
callq 0x918ec
addq 0x8(%rsp), %r15
movslq 0x9c(%r14), %rax
cmpq %rax, %r15
jl 0x2490c
movl $0x1, %eax
jmp 0x24bb5
leaq 0x30(%rsp), %rdi
callq 0x918ec
jmp 0x24bb3
leaq 0x30(%rsp), %rdi
callq 0xa3350
xorl %eax, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24bda
jmp 0x24bda
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa3350
jmp 0x24be7
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x918ec
movq %rbx, %rdi
callq 0x138b0