// File: /workspace/github_cpp_makefile_star2_O1/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
testl %edx, %edx
jle 0x24b19
movl %edx, %ecx
movl 0x9c(%rdi), %eax
cltd
idivl %ecx
incl %eax
cltq
jmp 0x24b1e
movl $0x1, %eax
movq %rax, (%rsp)
cmpl $0x0, 0x9c(%rdi)
setle %r15b
jle 0x24e3c
movsd 0xb9615(%rip), %xmm4    # 0xde150
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), %r13
xorl %r14d, %r14d
movq %rdi, 0x8(%rsp)
movq 0x90(%rdi), %rax
movq (%rax,%r14,8), %rbp
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9fc6(%rip), %rsi     # 0xdeb74
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24c2d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9fb6(%rip), %rsi     # 0xdeb8b
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24c2d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9fa6(%rip), %rsi     # 0xdeba2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24c2d
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9f98(%rip), %rsi     # 0xdebbb
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
je 0x24c69
movq %r13, %rdi
callq 0xa406c
movq %r13, %rdi
movq %r12, %rsi
callq 0xa4090
testl %eax, %eax
je 0x24e32
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x48(%rax)
movq %r13, %rdi
callq 0xa45f6
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9f5a(%rip), %rsi     # 0xdebd4
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24cab
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9f4a(%rip), %rsi     # 0xdebeb
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
je 0x24d07
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq %r13, %rdi
movq %r12, %rsi
callq 0x9a32a
testl %eax, %eax
je 0x24e26
movq %r13, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x9690e
movq %r13, %rdi
movapd 0x10(%rsp), %xmm0
callq 0x96a40
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x30(%rax)
movq %r13, %rdi
callq 0x92a48
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9eeb(%rip), %rsi     # 0xdec03
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24d49
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9ede(%rip), %rsi     # 0xdec1d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
je 0x24d89
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq %r13, %rdi
movq %r12, %rsi
callq 0x9a32a
testl %eax, %eax
je 0x24e26
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x38(%rax)
movq %r13, %rdi
callq 0x92a48
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9e9e(%rip), %rsi     # 0xdec38
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
jne 0x24dcb
movq 0x1c0(%rbp), %rcx
movq %r12, %rdi
leaq 0xb9e91(%rip), %rsi     # 0xdec52
movq %rbx, %rdx
xorl %eax, %eax
callq 0x130e0
movq %r12, %rdi
callq 0xad0e0
testl %eax, %eax
je 0x24e07
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq %r13, %rdi
movq %r12, %rsi
callq 0x9a32a
testl %eax, %eax
je 0x24e26
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x40(%rax)
movq %r13, %rdi
callq 0x92a48
addq (%rsp), %r14
movq 0x8(%rsp), %rdi
movslq 0x9c(%rdi), %rax
cmpq %rax, %r14
setge %r15b
jl 0x24b92
jmp 0x24e3c
leaq 0x30(%rsp), %rdi
callq 0x92a48
jmp 0x24e3c
leaq 0x30(%rsp), %rdi
callq 0xa45f6
movzbl %r15b, %eax
andl $0x1, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24e68
jmp 0x24e68
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa45f6
jmp 0x24e75
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x92a48
movq %rbx, %rdi
callq 0x138b0