// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %r14
pushq %rbx
subq $0x1128, %rsp           # imm = 0x1128
movq %rdi, %rbx
movl 0x1fc(%rdi), %eax
testl %eax, %eax
jle 0x1e641
incl %eax
movl %eax, 0x1fc(%rbx)
pushq $0x1
popq %rbx
jmp 0x1e9d4
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
leaq 0x8(%rsp), %rdi
callq 0x57e7c
movq 0x1e0(%rbx), %rcx
testq %rcx, %rcx
je 0x1e9b7
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1e68b
movq 0x38(%rax), %rdx
testq %rdx, %rdx
je 0x1e68b
leaq 0x8a8be(%rip), %rsi     # 0xa8f38
leaq 0x120(%rsp), %rdi
xorl %eax, %eax
callq 0x11110
jmp 0x1e69b
leaq 0x120(%rsp), %rdi
movq %rcx, %rsi
callq 0x11540
leaq 0x8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x5f7d2
testl %eax, %eax
je 0x1e8f0
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1e6f5
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x1e6f5
leaq 0x8aa9c(%rip), %rsi     # 0xa916a
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
je 0x1e8f7
leaq 0x8aa8b(%rip), %rsi     # 0xa9170
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
je 0x1e8f7
leaq 0x8(%rsp), %rdi
movsd 0x8990e(%rip), %xmm1    # 0xa8010
xorpd %xmm0, %xmm0
callq 0x5c1ba
movq 0x1e0(%rbx), %rdi
leaq 0x8a4a3(%rip), %rsi     # 0xa8bbc
callq 0x11140
testq %rax, %rax
je 0x1e95f
leaq 0x8(%rsp), %rdi
movsd 0x8a8b4(%rip), %xmm0    # 0xa8fe8
callq 0x5c2e6
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1e95f
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x1e95f
leaq 0x89f7c(%rip), %rsi     # 0xa86d6
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
je 0x1e94d
leaq 0x8aa07(%rip), %rsi     # 0xa9178
movq %r14, %rdi
callq 0x11870
movsd 0x8a86f(%rip), %xmm0    # 0xa8ff0
testl %eax, %eax
je 0x1e955
leaq 0x8a9ec(%rip), %rsi     # 0xa917c
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
je 0x1e9e1
leaq 0x8a9d9(%rip), %rsi     # 0xa9180
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
movsd 0x8a837(%rip), %xmm0    # 0xa8ff0
je 0x1e955
leaq 0x8a9be(%rip), %rsi     # 0xa9184
movq %r14, %rdi
callq 0x11870
testl %eax, %eax
jne 0x1e95f
movsd 0x140(%rbx), %xmm2
leaq 0xd9513(%rip), %rax     # 0xf7cf8
movsd (%rax), %xmm1
ucomisd %xmm1, %xmm2
movapd 0x8985b(%rip), %xmm0    # 0xa8050
xorpd %xmm1, %xmm0
ja 0x1e805
ucomisd %xmm2, %xmm0
jbe 0x1e95f
movsd 0x160(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x1e81d
ucomisd %xmm2, %xmm0
jbe 0x1e95f
movl 0xcc(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl 0xc8(%rsp), %edx
testl %edx, %edx
cmovlel %ecx, %edx
xorpd %xmm0, %xmm0
movsd 0x8a7b5(%rip), %xmm1    # 0xa8ff8
movsd 0x897dd(%rip), %xmm2    # 0xa8028
xorl %esi, %esi
cmpq %rdx, %rsi
je 0x1e95f
movslq 0xd0(%rsp), %rdi
cvtsi2sd %esi, %xmm3
movq 0xc0(%rsp), %r8
addq %rcx, %r8
shlq $0x3, %rdi
xorl %r9d, %r9d
cmpq %r9, %rax
je 0x1e8e4
movsd (%r8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x1e886
jnp 0x1e8dc
movapd %xmm1, %xmm5
movapd %xmm3, %xmm6
subsd 0x150(%rbx), %xmm6
divsd 0x140(%rbx), %xmm6
cvtsi2sd %r9d, %xmm7
subsd 0x168(%rbx), %xmm7
divsd %xmm4, %xmm5
divsd 0x160(%rbx), %xmm7
mulsd %xmm7, %xmm7
mulsd %xmm6, %xmm6
addsd %xmm7, %xmm6
sqrtsd %xmm6, %xmm4
mulsd %xmm4, %xmm4
addsd %xmm2, %xmm4
sqrtsd %xmm4, %xmm4
divsd %xmm4, %xmm5
movsd %xmm5, (%r8)
incq %r9
addq %rdi, %r8
jmp 0x1e874
incq %rsi
addq $0x8, %rcx
jmp 0x1e84d
xorl %ebx, %ebx
jmp 0x1e9ca
movl 0xd4(%rsp), %eax
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movsd 0x89703(%rip), %xmm0    # 0xa8018
cmpq %rdx, %rax
je 0x1e6f5
movsd (%rcx,%rdx,8), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
movl %esi, %edi
shrl $0x3, %edi
andl $0x1fff, %edi           # imm = 0x1FFF
shll $0xd, %esi
movzwl %si, %esi
orl %edi, %esi
cvtsi2sd %esi, %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
jmp 0x1e915
movsd 0x8a673(%rip), %xmm0    # 0xa8fc8
leaq 0x8(%rsp), %rdi
callq 0x5c2e6
movl 0x28(%rbx), %ecx
movl 0x2c(%rbx), %eax
testl %ecx, %ecx
setg %dl
testl %eax, %eax
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x1e9b7
cmpl 0xc8(%rsp), %ecx
jne 0x1e98b
cmpl 0xcc(%rsp), %eax
je 0x1e9b7
leaq 0x8c83e(%rip), %rsi     # 0xab1d0
leaq 0xd8(%rsp), %rdi
callq 0x648fc
movl 0x28(%rbx), %edx
movl 0x2c(%rbx), %ecx
leaq 0x8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x21bc6
movq (%rbx), %rax
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq *0x180(%rax)
movl %eax, %ebx
leaq 0x8(%rsp), %rdi
callq 0x589c0
movl %ebx, %eax
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r14
retq
leaq 0x8(%rsp), %rdi
movsd 0x8962a(%rip), %xmm0    # 0xa8018
callq 0x5c2e6
leaq 0x8(%rsp), %rdi
movsd 0x8a600(%rip), %xmm0    # 0xa9000
callq 0x5c40c
jmp 0x1e95f
jmp 0x1ea0e
jmp 0x1ea0e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x589c0
movq %rbx, %rdi
callq 0x11a80
nop