// File: /workspace/github_cpp_makefile_star2_O3/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 0x9b174
leaq 0xc3c77(%rip), %rdx     # 0xe8893
movq %r15, %rdi
movq %r14, %rsi
callq 0x9bb74
testl %eax, %eax
je 0x24c40
movl 0x9c(%rbp), %ecx
testl %ebx, %ebx
jle 0x24c47
movl %ecx, %eax
cltd
idivl %ebx
incl %eax
cltq
jmp 0x24c4c
xorl %ebx, %ebx
jmp 0x24f40
movl $0x1, %eax
movq %rax, 0x8(%rsp)
testl %ecx, %ecx
jle 0x24f2f
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 0xa2dd4
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8ec7(%rip), %rsi     # 0xddb67
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %r13
movq %r14, %rdx
callq 0x9b306
testl %eax, %eax
jne 0x24d46
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8eab(%rip), %rsi     # 0xddb7e
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9b306
testl %eax, %eax
jne 0x24d46
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8e96(%rip), %rsi     # 0xddb95
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9b306
testl %eax, %eax
jne 0x24d46
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8e83(%rip), %rsi     # 0xddbae
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x9b306
testl %eax, %eax
je 0x24d53
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 0x90cb8
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8e4f(%rip), %rsi     # 0xddbc7
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
jne 0x24dce
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8e35(%rip), %rsi     # 0xddbde
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x160(%rsp), %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
je 0x24de0
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 0x90cb8
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8df6(%rip), %rsi     # 0xddbf6
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
jne 0x24e51
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8ddf(%rip), %rsi     # 0xddc10
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
je 0x24e5e
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 0x90cb8
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8da8(%rip), %rsi     # 0xddc2b
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
jne 0x24ed9
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xb8d91(%rip), %rsi     # 0xddc45
xorl %eax, %eax
callq 0x130e0
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x90(%rsp), %rdx
movl $0x1, %ecx
callq 0x9b3ea
testl %eax, %eax
je 0x24eeb
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq *0x40(%rax)
leaq 0x90(%rsp), %rdi
callq 0x918ec
movq %r14, %rdi
callq 0x918ec
leaq 0x160(%rsp), %rdi
callq 0x918ec
movq %r13, %rdi
callq 0xa3350
addq 0x8(%rsp), %r15
movq 0x10(%rsp), %rbp
movslq 0x9c(%rbp), %rax
cmpq %rax, %r15
jl 0x24c76
leaq 0x18(%rsp), %rdi
callq 0x9bc92
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
leaq 0x18(%rsp), %rdi
callq 0x9b26e
movl %ebx, %eax
addq $0x708, %rsp            # imm = 0x708
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24f66
jmp 0x24f66
jmp 0x24fa3
jmp 0x24f6d
movq %rax, %rbx
jmp 0x24fb0
jmp 0x24f91
movq %rax, %rbx
jmp 0x24f82
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x918ec
leaq 0x230(%rsp), %rdi
callq 0x918ec
jmp 0x24f94
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x918ec
jmp 0x24fa6
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0xa3350
leaq 0x18(%rsp), %rdi
callq 0x9b26e
movq %rbx, %rdi
callq 0x138b0