// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDImage.cpp
// Function: gaps::RGBDImage::ReadDepthChannel()
int RGBDImage::
ReadDepthChannel(void)
{
  // Check if already resident/update read count
  if (depth_resident_count > 0) {
    depth_resident_count++;
    return 1;
  }

  // Initialize image
  R2Grid depth_image(width, height);

  // Check filename
  if (depth_filename) {
    // Get full filename
    char full_filename[4096];
    const char *dirname = (configuration) ? configuration->DepthDirectory() : NULL;
    if (dirname) sprintf(full_filename, "%s/%s", dirname, depth_filename);
    else sprintf(full_filename, "%s", depth_filename);

    // Read depth image
    if (!depth_image.ReadFile(full_filename)) return 0;

    // Shift 3 bits (to compensate for shift done by SUNRGBD capture)
    if (configuration && configuration->DatasetFormat()) {
      if (!strcmp(configuration->DatasetFormat(), "sun3d") || !strcmp(configuration->DatasetFormat(), "sunrgbd")) {
        for (int i = 0; i < depth_image.NEntries(); i++) {
          unsigned int d = (unsigned int) (depth_image.GridValue(i) + 0.5);
          d = ((d >> 3) & 0x1FFF) | ((d & 0x7) << 13);
          depth_image.SetGridValue(i, d);
        }
      }
    }

    // Substite unknown for zero
    depth_image.Substitute(0, R2_GRID_UNKNOWN_VALUE);
    // MaskBoundaries(depth_image);

    // Perform dataset-dependent processing
    if (strstr(depth_filename, ".png")) {
      depth_image.Multiply(0.001);
      if (configuration && configuration->DatasetFormat()) {
        if (!strcmp(configuration->DatasetFormat(), "matterport")) {
          depth_image.Multiply(0.25);
        }
        else if (!strcmp(configuration->DatasetFormat(), "tum")) {
          depth_image.Multiply(0.2);
        }
        else if (!strcmp(configuration->DatasetFormat(), "gsv")) {
          depth_image.Multiply(0.5);
          depth_image.Pow(2);
        }
        else if (!strcmp(configuration->DatasetFormat(), "icl")) {
          depth_image.Multiply(0.2);
        }
        else if (!strcmp(configuration->DatasetFormat(), "sumo")) {
          if (RNIsNotZero(intrinsics[0][0]) && RNIsNotZero(intrinsics[1][1])) {
            for (int ix = 0; ix < depth_image.XResolution(); ix++) {
              for (int iy = 0; iy < depth_image.YResolution(); iy++) {
                RNScalar value = depth_image.GridValue(ix, iy);
                if (value == 0) continue;
                RNScalar distance = 65.535 * 0.3 / value;
                RNScalar dx = (ix - intrinsics[0][2]) / intrinsics[0][0];
                RNScalar dy = (iy - intrinsics[1][2]) / intrinsics[1][1];
                RNScalar r = sqrt(dx*dx + dy*dy);
                RNScalar depth =  distance / sqrt(1 + r*r);
                depth_image.SetGridValue(ix, iy, depth);
              }
            }
          }
        }
      }
    }

#if 0
    // Smooth depth image
    if (!configuration || !configuration->DatasetFormat() ||
        (strcmp(configuration->DatasetFormat(), "processed") &&
         strcmp(configuration->DatasetFormat(), "matterport") &&         
         strcmp(configuration->DatasetFormat(), "scannet"))) {
      RNScalar d_sigma_fraction = 0.015;
      RNScalar xy_sigma = 3 * depth_image.XResolution() / 640.0;
      depth_image.BilateralFilter(xy_sigma, d_sigma_fraction, TRUE);
    }
#endif
    
    // Resize depth image if necessary
    if ((width > 0) && (height > 0) && ((width != depth_image.XResolution()) || (height != depth_image.YResolution()))) {
      R3Matrix tmp(R3identity_matrix);
      RGBDResampleDepthImage(depth_image, tmp, width, height);
    }
  }
  
  // Create depth channel
  return CreateDepthChannel(depth_image);
}

// Assembly:
subq $0x1298, %rsp           # imm = 0x1298
movq %rdi, 0x1288(%rsp)
movq 0x1288(%rsp), %rax
movq %rax, 0x108(%rsp)
cmpl $0x0, 0x1fc(%rax)
jle 0x2d72f
movq 0x108(%rsp), %rax
movl 0x1fc(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1fc(%rax)
movl $0x1, 0x1294(%rsp)
jmp 0x2e097
movq 0x108(%rsp), %rax
movl 0x28(%rax), %esi
movl 0x2c(%rax), %edx
leaq 0x11b8(%rsp), %rdi
callq 0xb1ee0
movq 0x108(%rsp), %rax
cmpq $0x0, 0x1e0(%rax)
je 0x2e052
movq 0x108(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2d79c
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x30730
movq %rax, 0x100(%rsp)
jmp 0x2d78a
movq 0x100(%rsp), %rax
movq %rax, 0xf8(%rsp)
jmp 0x2d7a8
xorl %eax, %eax
movq %rax, 0xf8(%rsp)
jmp 0x2d7a8
movq 0xf8(%rsp), %rax
movq %rax, 0x1a8(%rsp)
cmpq $0x0, 0x1a8(%rsp)
je 0x2d818
movq 0x108(%rsp), %rax
leaq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rdx
movq 0x1e0(%rax), %rcx
leaq 0x13977a(%rip), %rsi    # 0x166f63
movb $0x0, %al
callq 0x12130
jmp 0x2d83d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1a0(%rsp)
movl %eax, 0x19c(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0xb3390
jmp 0x2e0a6
movq 0x108(%rsp), %rax
leaq 0x1b0(%rsp), %rdi
movq 0x1e0(%rax), %rdx
leaq 0x13b8d5(%rip), %rsi    # 0x16910b
movb $0x0, %al
callq 0x12130
leaq 0x11b8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xc1a70
movl %eax, 0xf4(%rsp)
jmp 0x2d85b
movl 0xf4(%rsp), %eax
cmpl $0x0, %eax
jne 0x2d882
movl $0x0, 0x1294(%rsp)
movl $0x1, 0x198(%rsp)
jmp 0x2e08a
movq 0x108(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2da11
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xe8(%rsp)
jmp 0x2d8b0
movq 0xe8(%rsp), %rax
cmpq $0x0, %rax
je 0x2da11
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xe0(%rsp)
jmp 0x2d8dd
movq 0xe0(%rsp), %rdi
leaq 0x139884(%rip), %rsi    # 0x167170
callq 0x12a00
cmpl $0x0, %eax
je 0x2d92e
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xd8(%rsp)
jmp 0x2d911
movq 0xd8(%rsp), %rdi
leaq 0x139856(%rip), %rsi    # 0x167176
callq 0x12a00
cmpl $0x0, %eax
jne 0x2da0f
movl $0x0, 0x194(%rsp)
movl 0x194(%rsp), %eax
movl %eax, 0xd0(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0x30740
movl %eax, 0xd4(%rsp)
jmp 0x2d95d
movl 0xd0(%rsp), %eax
movl 0xd4(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2da0d
movl 0x194(%rsp), %esi
leaq 0x11b8(%rsp), %rdi
callq 0x30760
movsd %xmm0, 0xc8(%rsp)
jmp 0x2d992
movsd 0xc8(%rsp), %xmm0
movsd 0x138685(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0x190(%rsp)
movl 0x190(%rsp), %ecx
movl %ecx, %eax
shrl $0x3, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
andl $0x7, %ecx
shll $0xd, %ecx
orl %ecx, %eax
movl %eax, 0x190(%rsp)
movl 0x194(%rsp), %esi
movl 0x190(%rsp), %eax
cvtsi2sd %rax, %xmm0
leaq 0x11b8(%rsp), %rdi
callq 0x30780
jmp 0x2d9f5
jmp 0x2d9f7
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
jmp 0x2d939
jmp 0x2da0f
jmp 0x2da11
leaq 0x11b8(%rsp), %rdi
xorps %xmm0, %xmm0
movsd 0x1385fc(%rip), %xmm1   # 0x166020
callq 0xba150
jmp 0x2da2b
movq 0x108(%rsp), %rax
movq 0x1e0(%rax), %rdi
leaq 0x1391e3(%rip), %rsi    # 0x166c24
callq 0x12160
cmpq $0x0, %rax
je 0x2df93
leaq 0x11b8(%rsp), %rdi
movsd 0x1395b8(%rip), %xmm0   # 0x167018
callq 0xba480
jmp 0x2da67
movq 0x108(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2df91
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xc0(%rsp)
jmp 0x2da95
movq 0xc0(%rsp), %rax
cmpq $0x0, %rax
je 0x2df91
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xb8(%rsp)
jmp 0x2dac2
movq 0xb8(%rsp), %rdi
leaq 0x138c6d(%rip), %rsi    # 0x16673e
callq 0x12a00
cmpl $0x0, %eax
jne 0x2daf7
leaq 0x11b8(%rsp), %rdi
movsd 0x139515(%rip), %xmm0   # 0x167000
callq 0xba480
jmp 0x2daf2
jmp 0x2df8f
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xb0(%rsp)
jmp 0x2db12
movq 0xb0(%rsp), %rdi
leaq 0x13965d(%rip), %rsi    # 0x16717e
callq 0x12a00
cmpl $0x0, %eax
jne 0x2db47
leaq 0x11b8(%rsp), %rdi
movsd 0x1394e5(%rip), %xmm0   # 0x167020
callq 0xba480
jmp 0x2db42
jmp 0x2df8d
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xa8(%rsp)
jmp 0x2db62
movq 0xa8(%rsp), %rdi
leaq 0x139611(%rip), %rsi    # 0x167182
callq 0x12a00
cmpl $0x0, %eax
jne 0x2dbae
leaq 0x11b8(%rsp), %rdi
movsd 0x13849d(%rip), %xmm0   # 0x166028
callq 0xba480
jmp 0x2db92
leaq 0x11b8(%rsp), %rdi
movsd 0x1389b6(%rip), %xmm0   # 0x166558
callq 0xba7c0
jmp 0x2dba9
jmp 0x2df8b
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0xa0(%rsp)
jmp 0x2dbc9
movq 0xa0(%rsp), %rdi
leaq 0x1395ae(%rip), %rsi    # 0x167186
callq 0x12a00
cmpl $0x0, %eax
jne 0x2dbfe
leaq 0x11b8(%rsp), %rdi
movsd 0x13942e(%rip), %xmm0   # 0x167020
callq 0xba480
jmp 0x2dbf9
jmp 0x2df89
movq 0x108(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x225c0
movq %rax, 0x98(%rsp)
jmp 0x2dc19
movq 0x98(%rsp), %rdi
leaq 0x139562(%rip), %rsi    # 0x16718a
callq 0x12a00
cmpl $0x0, %eax
jne 0x2df87
movq 0x108(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x90(%rsp)
jmp 0x2dc56
movq 0x90(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x184257(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x307b0
movl %eax, 0x8c(%rsp)
jmp 0x2dc7b
movl 0x8c(%rsp), %eax
cmpl $0x0, %eax
je 0x2df85
movq 0x108(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x80(%rsp)
jmp 0x2dcae
movq 0x80(%rsp), %rax
movsd 0x8(%rax), %xmm0
leaq 0x1841fe(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x307b0
movl %eax, 0x7c(%rsp)
jmp 0x2dcd1
movl 0x7c(%rsp), %eax
cmpl $0x0, %eax
je 0x2df85
movl $0x0, 0x18c(%rsp)
movl 0x18c(%rsp), %eax
movl %eax, 0x74(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0x27940
movl %eax, 0x78(%rsp)
jmp 0x2dd07
movl 0x74(%rsp), %eax
movl 0x78(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2df83
movl $0x0, 0x188(%rsp)
movl 0x188(%rsp), %eax
movl %eax, 0x6c(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0x27960
movl %eax, 0x70(%rsp)
jmp 0x2dd40
movl 0x6c(%rsp), %eax
movl 0x70(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2df6b
movl 0x18c(%rsp), %esi
movl 0x188(%rsp), %edx
leaq 0x11b8(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x60(%rsp)
jmp 0x2dd73
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x180(%rsp)
movsd 0x180(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2dd9b
jp 0x2dd9b
jmp 0x2df55
movq 0x108(%rsp), %rdi
movsd 0x180(%rsp), %xmm1
movsd 0x139274(%rip), %xmm0   # 0x167028
divsd %xmm1, %xmm0
movsd %xmm0, 0x178(%rsp)
cvtsi2sdl 0x18c(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
addq $0x140, %rdi            # imm = 0x140
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x58(%rsp)
jmp 0x2dde5
movq 0x108(%rsp), %rdi
movsd 0x50(%rsp), %xmm0
movq 0x58(%rsp), %rax
movsd 0x10(%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
addq $0x140, %rdi            # imm = 0x140
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x48(%rsp)
jmp 0x2de1c
movq 0x108(%rsp), %rdi
movsd 0x40(%rsp), %xmm0
movq 0x48(%rsp), %rax
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x170(%rsp)
cvtsi2sdl 0x188(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
addq $0x140, %rdi            # imm = 0x140
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x38(%rsp)
jmp 0x2de67
movq 0x108(%rsp), %rdi
movsd 0x30(%rsp), %xmm0
movq 0x38(%rsp), %rax
movsd 0x10(%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
addq $0x140, %rdi            # imm = 0x140
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x28(%rsp)
jmp 0x2dea1
movsd 0x20(%rsp), %xmm0
movq 0x28(%rsp), %rax
movsd 0x8(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x168(%rsp)
movsd 0x170(%rsp), %xmm0
movsd 0x168(%rsp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
callq 0x12540
movsd %xmm0, 0x160(%rsp)
movsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x160(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd 0x138122(%rip), %xmm1   # 0x166030
addsd %xmm1, %xmm0
callq 0x12540
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movl 0x18c(%rsp), %esi
movl 0x188(%rsp), %edx
movsd 0x158(%rsp), %xmm0
leaq 0x11b8(%rsp), %rdi
callq 0x1a1b0
jmp 0x2df53
jmp 0x2df55
movl 0x188(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x188(%rsp)
jmp 0x2dd22
jmp 0x2df6d
movl 0x18c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18c(%rsp)
jmp 0x2dce9
jmp 0x2df85
jmp 0x2df87
jmp 0x2df89
jmp 0x2df8b
jmp 0x2df8d
jmp 0x2df8f
jmp 0x2df91
jmp 0x2df93
movq 0x108(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jle 0x2e050
movq 0x108(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jle 0x2e050
movq 0x108(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x10(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0x27940
movl %eax, 0x14(%rsp)
jmp 0x2dfd9
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jne 0x2e013
movq 0x108(%rsp), %rax
movl 0x2c(%rax), %eax
movl %eax, 0x8(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0x27960
movl %eax, 0xc(%rsp)
jmp 0x2e007
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
je 0x2e050
leaq 0x18657e(%rip), %rsi    # 0x1b4598
leaq 0x110(%rsp), %rdi
callq 0xcc3e0
jmp 0x2e029
movq 0x108(%rsp), %rax
movl 0x28(%rax), %edx
movl 0x2c(%rax), %ecx
leaq 0x11b8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x34a10
jmp 0x2e04e
jmp 0x2e050
jmp 0x2e052
movq 0x108(%rsp), %rdi
movq (%rdi), %rax
movq 0x180(%rax), %rax
leaq 0x11b8(%rsp), %rsi
callq *%rax
movl %eax, 0x4(%rsp)
jmp 0x2e074
movl 0x4(%rsp), %eax
movl %eax, 0x1294(%rsp)
movl $0x1, 0x198(%rsp)
leaq 0x11b8(%rsp), %rdi
callq 0xb3390
movl 0x1294(%rsp), %eax
addq $0x1298, %rsp           # imm = 0x1298
retq
movq 0x1a0(%rsp), %rdi
callq 0x12c20
nopw %cs:(%rax,%rax)