// File: /workspace/github_cpp_makefile_star2_O2/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
movsd 0xa4f1d(%rip), %xmm4    # 0xc8148
movapd %xmm4, %xmm2
divsd %xmm0, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm5, %xmm5
cmplesd %xmm0, %xmm5
andpd %xmm5, %xmm2
andnpd %xmm4, %xmm5
orpd %xmm2, %xmm5
movapd %xmm5, 0x20(%rsp)
movapd %xmm4, %xmm0
divsd %xmm1, %xmm0
cmpneqsd %xmm3, %xmm1
andpd %xmm1, %xmm0
andnpd %xmm4, %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
testl %edx, %edx
jle 0x23286
movl %edx, %ecx
movl 0x9c(%rdi), %eax
cltd
idivl %ecx
incl %eax
cltq
jmp 0x23289
pushq $0x1
popq %rax
movq %rax, (%rsp)
leaq 0x100(%rsp), %r12
leaq 0x30(%rsp), %r13
xorl %r15d, %r15d
movq %rdi, 0x8(%rsp)
movslq 0x9c(%rdi), %r14
cmpq %r14, %r15
jge 0x2354b
movq 0x90(%rdi), %rax
movq (%rax,%r15,8), %rbp
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa588e(%rip), %rsi     # 0xc8b5c
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x2334d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa587e(%rip), %rsi     # 0xc8b73
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x2334d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa586e(%rip), %rsi     # 0xc8b8a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x2334d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa5860(%rip), %rsi     # 0xc8ba3
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
je 0x23389
movq %r13, %rdi
callq 0x942b8
movq %r13, %rdi
movq %r12, %rsi
callq 0x942d4
testl %eax, %eax
je 0x23541
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x48(%rax)
movq %r13, %rdi
callq 0x94804
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa5822(%rip), %rsi     # 0xc8bbc
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x233cb
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa5812(%rip), %rsi     # 0xc8bd3
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
je 0x23427
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq %r13, %rdi
movq %r12, %rsi
callq 0x8b4ba
testl %eax, %eax
je 0x23535
movq %r13, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x87fce
movq %r13, %rdi
movapd 0x10(%rsp), %xmm0
callq 0x880f4
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x30(%rax)
movq %r13, %rdi
callq 0x846a8
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa57b3(%rip), %rsi     # 0xc8beb
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x23469
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa57a6(%rip), %rsi     # 0xc8c05
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
je 0x234a9
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq %r13, %rdi
movq %r12, %rsi
callq 0x8b4ba
testl %eax, %eax
je 0x23535
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x38(%rax)
movq %r13, %rdi
callq 0x846a8
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa5766(%rip), %rsi     # 0xc8c20
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
jne 0x234eb
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xa5759(%rip), %rsi     # 0xc8c3a
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0x9c3f8
testl %eax, %eax
je 0x23527
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq %r13, %rdi
movq %r12, %rsi
callq 0x8b4ba
testl %eax, %eax
je 0x23535
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x40(%rax)
movq %r13, %rdi
callq 0x846a8
addq (%rsp), %r15
movq 0x8(%rsp), %rdi
jmp 0x232a2
leaq 0x30(%rsp), %rdi
callq 0x846a8
jmp 0x2354b
leaq 0x30(%rsp), %rdi
callq 0x94804
xorl %eax, %eax
cmpq %r14, %r15
setge %al
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23578
jmp 0x23578
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x94804
jmp 0x23585
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x846a8
movq %rbx, %rdi
callq 0x138f0