// File: /workspace/github_cpp_makefile_star2_O2/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 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x8d250
leaq 0xafff9(%rip), %rdx     # 0xd35b3
movq %r15, %rdi
movq %r14, %rsi
callq 0x8da9a
testl %eax, %eax
je 0x235dc
testl %ebx, %ebx
jle 0x235e3
movl 0x9c(%rbp), %eax
cltd
idivl %ebx
incl %eax
cltq
jmp 0x235e6
xorl %ebx, %ebx
jmp 0x238de
pushq $0x1
popq %rax
movq %rax, 0x8(%rsp)
leaq 0x300(%rsp), %r12
pushq $0x1
popq %r14
leaq 0x230(%rsp), %r13
xorl %ecx, %ecx
movq %rbp, 0x28(%rsp)
movslq 0x9c(%rbp), %rax
cmpq %rax, %rcx
jge 0x238cd
movq %r13, %rbx
movl %r14d, %r13d
movq 0x90(%rbp), %rax
movq %rcx, %r15
movq (%rax,%rcx,8), %rbp
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x942b8
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa5517(%rip), %rsi     # 0xc8b5f
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x8d394
testl %eax, %eax
jne 0x236f1
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa54fe(%rip), %rsi     # 0xc8b76
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
leaq 0x10(%rsp), %rdx
callq 0x8d394
testl %eax, %eax
jne 0x236f1
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa54e7(%rip), %rsi     # 0xc8b8d
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
leaq 0x10(%rsp), %rdx
callq 0x8d394
testl %eax, %eax
jne 0x236f1
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa54d2(%rip), %rsi     # 0xc8ba6
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
leaq 0x10(%rsp), %rdx
callq 0x8d394
testl %eax, %eax
je 0x23700
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq *0x48(%rax)
leaq 0x160(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa549a(%rip), %rsi     # 0xc8bbf
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl %r13d, %r14d
movl %r13d, %ecx
callq 0x8d47e
testl %eax, %eax
jne 0x2377a
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa547f(%rip), %rsi     # 0xc8bd6
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
leaq 0x160(%rsp), %rdx
movl %r14d, %ecx
callq 0x8d47e
testl %eax, %eax
je 0x2378c
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x160(%rsp), %rsi
callq *0x30(%rax)
movq %rbx, %r13
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa5442(%rip), %rsi     # 0xc8bee
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x8d47e
testl %eax, %eax
jne 0x237f9
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa542d(%rip), %rsi     # 0xc8c08
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x8d47e
testl %eax, %eax
je 0x23806
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x38(%rax)
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x83b64
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa53f8(%rip), %rsi     # 0xc8c23
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %r14d, %ecx
callq 0x8d47e
testl %eax, %eax
jne 0x2387d
movq 0x1c0(%rbp), %rdx
movq %r12, %rdi
leaq 0xa53e3(%rip), %rsi     # 0xc8c3d
xorl %eax, %eax
callq 0x130e0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
leaq 0x90(%rsp), %rdx
movl %r14d, %ecx
callq 0x8d47e
testl %eax, %eax
je 0x2388f
movq (%rbp), %rax
movq %rbp, %rdi
leaq 0x90(%rsp), %rsi
callq *0x40(%rax)
leaq 0x90(%rsp), %rdi
callq 0x846a8
movq %r13, %rdi
callq 0x846a8
leaq 0x160(%rsp), %rdi
callq 0x846a8
leaq 0x10(%rsp), %rdi
callq 0x94804
movq %r15, %rcx
addq 0x8(%rsp), %rcx
movq 0x28(%rsp), %rbp
jmp 0x23606
leaq 0x30(%rsp), %rdi
callq 0x8dba8
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
leaq 0x30(%rsp), %rdi
callq 0x8d30e
movl %ebx, %eax
addq $0x708, %rsp            # imm = 0x708
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23906
jmp 0x23906
jmp 0x2391d
jmp 0x2392f
jmp 0x23941
movq %rax, %rbx
jmp 0x2394e
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x846a8
jmp 0x23920
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x846a8
jmp 0x23932
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x846a8
jmp 0x23944
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x94804
leaq 0x30(%rsp), %rdi
callq 0x8d30e
movq %rbx, %rdi
callq 0x138f0