// File: 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:
subq $0x868, %rsp            # imm = 0x868
movq %rdi, 0x858(%rsp)
movq %rsi, 0x850(%rsp)
movl %edx, 0x84c(%rsp)
leaq 0x7e8(%rsp), %rdi
movq %rdi, 0x110(%rsp)
callq 0x124840
movq 0x110(%rsp), %rdi
movq 0x850(%rsp), %rsi
leaq 0x172f19(%rip), %rdx    # 0x1ab353
callq 0x1259b0
movl %eax, 0x118(%rsp)
jmp 0x38448
movl 0x118(%rsp), %eax
cmpl $0x0, %eax
jne 0x38488
movl $0x0, 0x864(%rsp)
movl $0x1, 0x7d8(%rsp)
jmp 0x38c53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7e0(%rsp)
movl %eax, 0x7dc(%rsp)
jmp 0x38c6f
cmpl $0x0, 0x84c(%rsp)
jle 0x384c3
movq 0x858(%rsp), %rdi
callq 0x16cc0
movl %eax, 0x10c(%rsp)
jmp 0x384a8
movl 0x10c(%rsp), %eax
cltd
idivl 0x84c(%rsp)
addl $0x1, %eax
movl %eax, 0x108(%rsp)
jmp 0x384d1
movl $0x1, %eax
movl %eax, 0x108(%rsp)
jmp 0x384d1
movl 0x108(%rsp), %eax
movl %eax, 0x7d4(%rsp)
movl $0x0, 0x7d0(%rsp)
movl 0x7d0(%rsp), %eax
movl %eax, 0x100(%rsp)
movq 0x858(%rsp), %rdi
callq 0x16cc0
movl %eax, 0x104(%rsp)
jmp 0x3850e
movl 0x100(%rsp), %eax
movl 0x104(%rsp), %ecx
cmpl %ecx, %eax
jge 0x38c09
movq 0x858(%rsp), %rdi
movl 0x7d0(%rsp), %esi
callq 0x30800
movq %rax, 0xf8(%rsp)
jmp 0x38542
movq 0xf8(%rsp), %rax
movq %rax, 0x7c8(%rsp)
leaq 0x3a8(%rsp), %rdi
callq 0x1352f0
jmp 0x38561
movl $0x1, 0x3a4(%rsp)
leaq 0x3c0(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0xf0(%rsp)
jmp 0x38593
movq 0xf0(%rsp), %rdx
movq 0xe8(%rsp), %rdi
leaq 0x16c5c7(%rip), %rsi    # 0x1a4b71
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x3a8(%rsp), %rdx
callq 0x124ae0
movl %eax, 0xe4(%rsp)
jmp 0x385d7
movl 0xe4(%rsp), %eax
cmpl $0x0, %eax
jne 0x38780
leaq 0x3c0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0xd8(%rsp)
jmp 0x3860e
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rdi
leaq 0x16c563(%rip), %rsi    # 0x1a4b88
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x3a8(%rsp), %rdx
callq 0x124ae0
movl %eax, 0xcc(%rsp)
jmp 0x38652
movl 0xcc(%rsp), %eax
cmpl $0x0, %eax
jne 0x3877e
leaq 0x3c0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0xc0(%rsp)
jmp 0x38689
movq 0xc0(%rsp), %rdx
movq 0xb8(%rsp), %rdi
leaq 0x16c4ff(%rip), %rsi    # 0x1a4b9f
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x3a8(%rsp), %rdx
callq 0x124ae0
movl %eax, 0xb4(%rsp)
jmp 0x386cd
movl 0xb4(%rsp), %eax
cmpl $0x0, %eax
jne 0x3877c
leaq 0x3c0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0xa8(%rsp)
jmp 0x38704
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rdi
leaq 0x16c49d(%rip), %rsi    # 0x1a4bb8
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x3a8(%rsp), %rdx
callq 0x124ae0
movl %eax, 0x9c(%rsp)
jmp 0x38748
movl 0x9c(%rsp), %eax
cmpl $0x0, %eax
jne 0x3877a
movl $0x0, 0x3a4(%rsp)
jmp 0x3877a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7e0(%rsp)
movl %eax, 0x7dc(%rsp)
jmp 0x38bfa
jmp 0x3877c
jmp 0x3877e
jmp 0x38780
cmpl $0x0, 0x3a4(%rsp)
je 0x387a7
movq 0x7c8(%rsp), %rdi
movq (%rdi), %rax
movq 0x48(%rax), %rax
leaq 0x3a8(%rsp), %rsi
callq *%rax
jmp 0x387a5
jmp 0x387a7
leaq 0x2d0(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
jmp 0x387ba
movl $0x1, 0x2cc(%rsp)
leaq 0x3c0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x90(%rsp)
jmp 0x387ec
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %rdi
leaq 0x16c3ce(%rip), %rsi    # 0x1a4bd1
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0x84(%rsp)
jmp 0x38835
movl 0x84(%rsp), %eax
cmpl $0x0, %eax
jne 0x388d7
leaq 0x3c0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x78(%rsp)
jmp 0x38866
movq 0x78(%rsp), %rdx
movq 0x70(%rsp), %rdi
leaq 0x16c371(%rip), %rsi    # 0x1a4be8
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0x6c(%rsp)
jmp 0x388a6
movl 0x6c(%rsp), %eax
cmpl $0x0, %eax
jne 0x388d5
movl $0x0, 0x2cc(%rsp)
jmp 0x388d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7e0(%rsp)
movl %eax, 0x7dc(%rsp)
jmp 0x38bed
jmp 0x388d7
cmpl $0x0, 0x2cc(%rsp)
je 0x388fe
movq 0x7c8(%rsp), %rdi
movq (%rdi), %rax
movq 0x30(%rax), %rax
leaq 0x2d0(%rsp), %rsi
callq *%rax
jmp 0x388fc
jmp 0x388fe
leaq 0x1f8(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
jmp 0x38911
movl $0x1, 0x1f4(%rsp)
leaq 0x3c0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x60(%rsp)
jmp 0x3893d
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rdi
leaq 0x16c2b2(%rip), %rsi    # 0x1a4c00
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x1f8(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0x54(%rsp)
jmp 0x3897d
movl 0x54(%rsp), %eax
cmpl $0x0, %eax
jne 0x38a1c
leaq 0x3c0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x48(%rsp)
jmp 0x389ab
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rdi
leaq 0x16c25e(%rip), %rsi    # 0x1a4c1a
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x1f8(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0x3c(%rsp)
jmp 0x389eb
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
jne 0x38a1a
movl $0x0, 0x1f4(%rsp)
jmp 0x38a1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7e0(%rsp)
movl %eax, 0x7dc(%rsp)
jmp 0x38be0
jmp 0x38a1c
cmpl $0x0, 0x1f4(%rsp)
je 0x38a43
movq 0x7c8(%rsp), %rdi
movq (%rdi), %rax
movq 0x38(%rax), %rax
leaq 0x1f8(%rsp), %rsi
callq *%rax
jmp 0x38a41
jmp 0x38a43
leaq 0x120(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x111300
jmp 0x38a56
movl $0x1, 0x11c(%rsp)
leaq 0x3c0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x30(%rsp)
jmp 0x38a82
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rdi
leaq 0x16c1a2(%rip), %rsi    # 0x1a4c35
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x120(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0x24(%rsp)
jmp 0x38ac2
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x38b6b
leaq 0x3c0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x7c8(%rsp), %rdi
callq 0x30890
movq %rax, 0x18(%rsp)
jmp 0x38af0
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rdi
leaq 0x16c14e(%rip), %rsi    # 0x1a4c4f
xorl %eax, %eax
callq 0x130f0
leaq 0x7e8(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x120(%rsp), %rdx
movl $0x1, %ecx
callq 0x124c90
movl %eax, 0xc(%rsp)
jmp 0x38b30
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x38b69
movl $0x0, 0x11c(%rsp)
jmp 0x38b69
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x7e0(%rsp)
movl %eax, 0x7dc(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x1127b0
jmp 0x38be0
jmp 0x38b6b
cmpl $0x0, 0x11c(%rsp)
je 0x38b92
movq 0x7c8(%rsp), %rdi
movq (%rdi), %rax
movq 0x40(%rax), %rax
leaq 0x120(%rsp), %rsi
callq *%rax
jmp 0x38b90
jmp 0x38b92
leaq 0x120(%rsp), %rdi
callq 0x1127b0
leaq 0x1f8(%rsp), %rdi
callq 0x1127b0
leaq 0x2d0(%rsp), %rdi
callq 0x1127b0
leaq 0x3a8(%rsp), %rdi
callq 0x135d40
movl 0x7d4(%rsp), %eax
addl 0x7d0(%rsp), %eax
movl %eax, 0x7d0(%rsp)
jmp 0x384ea
leaq 0x1f8(%rsp), %rdi
callq 0x1127b0
leaq 0x2d0(%rsp), %rdi
callq 0x1127b0
leaq 0x3a8(%rsp), %rdi
callq 0x135d40
jmp 0x38c6f
leaq 0x7e8(%rsp), %rdi
callq 0x125b90
movl %eax, 0x8(%rsp)
jmp 0x38c1c
movl 0x8(%rsp), %eax
cmpl $0x0, %eax
jne 0x38c3d
movl $0x0, 0x864(%rsp)
movl $0x1, 0x7d8(%rsp)
jmp 0x38c53
movl $0x1, 0x864(%rsp)
movl $0x1, 0x7d8(%rsp)
leaq 0x7e8(%rsp), %rdi
callq 0x1249c0
movl 0x864(%rsp), %eax
addq $0x868, %rsp            # imm = 0x868
retq
leaq 0x7e8(%rsp), %rdi
callq 0x1249c0
movq 0x7e0(%rsp), %rdi
callq 0x13970
nopl (%rax)