// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::ReadPixelDatabase(gaps::R3SurfelScene*, char const*, int)
int
ReadPixelDatabase(R3SurfelScene *scene, const char *filename, int max_images)
{
  // Open pixel database
  R2PixelDatabase database;
  if (!database.OpenFile(filename, "r")) return 0;
  
  // Read all channels for all images
  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);
    char imagekey[1024];

    // Get color image
    R2Image color_image;
    RNBoolean color_image_found = TRUE;
    sprintf(imagekey, "color_images/%s.png", image->Name());
    if (!database.FindImage(imagekey, &color_image)) {
      sprintf(imagekey, "color_images/%s.jpg", image->Name());
      if (!database.FindImage(imagekey, &color_image)) {
        sprintf(imagekey, "color_channels/%s.png", image->Name());
        if (!database.FindImage(imagekey, &color_image)) {
          sprintf(imagekey, "color_channels/%s.jpg", image->Name());
          if (!database.FindImage(imagekey, &color_image)) {
            color_image_found = FALSE;
          }
        }
      }
    }

    // Set color channel
    if (color_image_found) image->SetColorChannels(color_image);

    // Get depth image
    R2Grid depth_image;
    RNBoolean depth_image_found = TRUE;
    sprintf(imagekey, "depth_images/%s.png", image->Name());
    if (!database.FindGrid(imagekey, &depth_image)) {
      sprintf(imagekey, "depth_channel/%s.png", image->Name());
      if (!database.FindGrid(imagekey, &depth_image)) {
        depth_image_found = FALSE;
      }
    }

    // Set depth channel
    if (depth_image_found) image->SetDepthChannel(depth_image);

    // Get category image
    R2Grid category_image;
    RNBoolean category_image_found = TRUE;
    sprintf(imagekey, "category_images/%s.png", image->Name());
    if (!database.FindGrid(imagekey, &category_image)) {
      sprintf(imagekey, "category_channel/%s.png", image->Name());
      if (!database.FindGrid(imagekey, &category_image)) {
        category_image_found = FALSE;
      }
    }

    // Set category channel
    if (category_image_found) image->SetCategoryChannel(category_image);

    // Get instance image
    R2Grid instance_image;
    RNBoolean instance_image_found = TRUE;
    sprintf(imagekey, "instance_images/%s.png", image->Name());
    if (!database.FindGrid(imagekey, &instance_image)) {
      sprintf(imagekey, "instance_channel/%s.png", image->Name());
      if (!database.FindGrid(imagekey, &instance_image)) {
        instance_image_found = FALSE;
      }
    }

    // Set instance channel
    if (instance_image_found) image->SetInstanceChannel(instance_image);
  }

  // Close pixel database
  if (!database.CloseFile()) return 0;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x708, %rsp            # imm = 0x708
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %rbp
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x9c404
leaq 0xc49d9(%rip), %rdx     # 0xe9883
movq %r15, %rdi
movq %r14, %rsi
callq 0x9ce0c
testl %eax, %eax
je 0x24ecc
testl %ebx, %ebx
jle 0x24ed3
movl 0x9c(%rbp), %eax
cltd
idivl %ebx
incl %eax
cltq
jmp 0x24ed8
xorl %ebx, %ebx
jmp 0x251d1
movl $0x1, %eax
movq %rax, 0x8(%rsp)
cmpl $0x0, 0x9c(%rbp)
jle 0x251c0
leaq 0x78(%rsp), %r13
leaq 0x300(%rsp), %r12
leaq 0x230(%rsp), %r14
xorl %r15d, %r15d
movq %rbp, 0x10(%rsp)
movq %r14, %rbx
movq 0x90(%rbp), %rax
movq (%rax,%r15,8), %rbp
movq %r13, %r14
movq %r13, %rdi
callq 0xa406c
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9c46(%rip), %rsi     # 0xdeb77
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %r13
movq %r14, %rdx
callq 0x9c59e
testl %eax, %eax
jne 0x24fd7
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9c2a(%rip), %rsi     # 0xdeb8e
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9c59e
testl %eax, %eax
jne 0x24fd7
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9c15(%rip), %rsi     # 0xdeba5
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9c59e
testl %eax, %eax
jne 0x24fd7
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9c02(%rip), %rsi     # 0xdebbe
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9c59e
testl %eax, %eax
je 0x24fe4
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x48(%rax)
leaq 0x160(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9bce(%rip), %rsi     # 0xdebd7
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
jne 0x2505f
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9bb4(%rip), %rsi     # 0xdebee
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x160(%rsp), %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
je 0x25071
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x160(%rsp), %rsi
callq *0x30(%rax)
movq %rbx, %r14
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9b75(%rip), %rsi     # 0xdec06
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
jne 0x250e2
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9b5e(%rip), %rsi     # 0xdec20
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
je 0x250ef
movq (%rbp), %rax
movq %rbp, %rdi
movq %r14, %rsi
callq *0x38(%rax)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x91e28
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9b27(%rip), %rsi     # 0xdec3b
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
jne 0x2516a
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb9b10(%rip), %rsi     # 0xdec55
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x90(%rsp), %rdx
movl $0x1, %ecx
callq 0x9c682
testl %eax, %eax
je 0x2517c
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq *0x40(%rax)
leaq 0x90(%rsp), %rdi
callq 0x92a48
movq %r14, %rdi
callq 0x92a48
leaq 0x160(%rsp), %rdi
callq 0x92a48
movq %r13, %rdi
callq 0xa45f6
addq 0x8(%rsp), %r15
movq 0x10(%rsp), %rbp
movslq 0x9c(%rbp), %rax
cmpq %rax, %r15
jl 0x24f07
leaq 0x18(%rsp), %rdi
callq 0x9cf2a
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
leaq 0x18(%rsp), %rdi
callq 0x9c506
movl %ebx, %eax
addq $0x708, %rsp            # imm = 0x708
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x251f7
jmp 0x251f7
jmp 0x25234
jmp 0x251fe
movq %rax, %rbx
jmp 0x25241
jmp 0x25222
movq %rax, %rbx
jmp 0x25213
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x92a48
leaq 0x230(%rsp), %rdi
callq 0x92a48
jmp 0x25225
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x92a48
jmp 0x25237
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0xa45f6
leaq 0x18(%rsp), %rdi
callq 0x9c506
movq %rbx, %rdi
callq 0x138b0