// File: /workspace/github_cpp_makefile_star2_O3/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 0x1fe55
incl %eax
movl %eax, 0x1fc(%rbx)
movl $0x1, %ebx
jmp 0x2020b
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
leaq 0x8(%rsp), %rdi
callq 0x5f630
movq 0x1e0(%rbx), %rcx
testq %rcx, %rcx
je 0x201ee
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1fe9f
movq 0x38(%rax), %rdx
testq %rdx, %rdx
je 0x1fe9f
leaq 0x9c12a(%rip), %rsi     # 0xbbfb8
leaq 0x120(%rsp), %rdi
xorl %eax, %eax
callq 0x11110
jmp 0x1feaf
leaq 0x120(%rsp), %rdi
movq %rcx, %rsi
callq 0x11530
leaq 0x8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x67a4e
testl %eax, %eax
je 0x20180
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1ff59
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x1ff59
leaq 0x9c304(%rip), %rsi     # 0xbc1ea
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
je 0x1ff05
leaq 0x9c2f7(%rip), %rsi     # 0xbc1f0
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
jne 0x1ff59
movslq 0xd4(%rsp), %rax
testq %rax, %rax
jle 0x1ff59
movq 0xc0(%rsp), %rcx
xorl %edx, %edx
movsd 0x9b0fc(%rip), %xmm0    # 0xbb020
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
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1ff24
leaq 0x8(%rsp), %rdi
movsd 0x9b0b2(%rip), %xmm1    # 0xbb018
xorpd %xmm0, %xmm0
callq 0x63f00
movq 0x1e0(%rbx), %rdi
leaq 0x9bcbf(%rip), %rsi     # 0xbbc3c
callq 0x11140
testq %rax, %rax
je 0x20196
leaq 0x8(%rsp), %rdi
movsd 0x9c0d0(%rip), %xmm0    # 0xbc068
callq 0x64064
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x20196
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x20196
leaq 0x9b798(%rip), %rsi     # 0xbb756
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
je 0x20184
leaq 0x9c223(%rip), %rsi     # 0xbc1f8
movq %r14, %rdi
callq 0x11840
movsd 0x9c08b(%rip), %xmm0    # 0xbc070
testl %eax, %eax
je 0x2018c
leaq 0x9c208(%rip), %rsi     # 0xbc1fc
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
je 0x20218
leaq 0x9c1f5(%rip), %rsi     # 0xbc200
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
movsd 0x9c053(%rip), %xmm0    # 0xbc070
je 0x2018c
leaq 0x9c1da(%rip), %rsi     # 0xbc204
movq %r14, %rdi
callq 0x11840
testl %eax, %eax
jne 0x20196
movsd 0x140(%rbx), %xmm2
leaq 0xe4c8f(%rip), %rax     # 0x104cd8
movsd (%rax), %xmm1
ucomisd %xmm1, %xmm2
movapd 0x9aff7(%rip), %xmm0    # 0xbb050
xorpd %xmm1, %xmm0
ja 0x20069
ucomisd %xmm2, %xmm0
jbe 0x20196
movsd 0x160(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x20081
ucomisd %xmm2, %xmm0
jbe 0x20196
movslq 0xc8(%rsp), %rax
testq %rax, %rax
jle 0x20196
movl 0xcc(%rsp), %ecx
movq 0xc0(%rsp), %rdx
movslq 0xd0(%rsp), %rsi
shlq $0x3, %rsi
xorl %edi, %edi
xorpd %xmm0, %xmm0
movsd 0x9bfbd(%rip), %xmm1    # 0xbc078
movsd 0x9af65(%rip), %xmm2    # 0xbb028
xorl %r8d, %r8d
testl %ecx, %ecx
jle 0x2016a
xorps %xmm3, %xmm3
cvtsi2sd %r8d, %xmm3
movslq 0xd0(%rsp), %r9
movq 0xc0(%rsp), %r10
addq %rdi, %r10
shlq $0x3, %r9
movq %rdx, %r11
xorl %r14d, %r14d
movsd (%r11), %xmm4
ucomisd %xmm0, %xmm4
jne 0x20100
jnp 0x2015c
movapd %xmm1, %xmm5
movapd %xmm3, %xmm6
subsd 0x150(%rbx), %xmm6
divsd 0x140(%rbx), %xmm6
xorps %xmm7, %xmm7
cvtsi2sd %r14d, %xmm7
subsd 0x168(%rbx), %xmm7
divsd %xmm4, %xmm5
divsd 0x160(%rbx), %xmm7
mulsd %xmm7, %xmm7
mulsd %xmm6, %xmm6
addsd %xmm7, %xmm6
xorps %xmm4, %xmm4
sqrtsd %xmm6, %xmm4
mulsd %xmm4, %xmm4
addsd %xmm2, %xmm4
sqrtsd %xmm4, %xmm4
divsd %xmm4, %xmm5
movsd %xmm5, (%r10)
incq %r14
addq %rsi, %r11
addq %r9, %r10
cmpq %r14, %rcx
jne 0x200f3
incq %r8
addq $0x8, %rdx
addq $0x8, %rdi
cmpq %rax, %r8
jne 0x200c6
jmp 0x20196
xorl %ebx, %ebx
jmp 0x20201
movsd 0x9bebc(%rip), %xmm0    # 0xbc048
leaq 0x8(%rsp), %rdi
callq 0x64064
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 0x201ee
cmpl 0xc8(%rsp), %ecx
jne 0x201c2
cmpl 0xcc(%rsp), %eax
je 0x201ee
leaq 0x9e0f7(%rip), %rsi     # 0xbe2c0
leaq 0xd8(%rsp), %rdi
callq 0x6d1a2
movl 0x28(%rbx), %edx
movl 0x2c(%rbx), %ecx
leaq 0x8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x23568
movq (%rbx), %rax
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq *0x180(%rax)
movl %eax, %ebx
leaq 0x8(%rsp), %rdi
callq 0x60264
movl %ebx, %eax
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r14
retq
leaq 0x8(%rsp), %rdi
movsd 0x9adfb(%rip), %xmm0    # 0xbb020
callq 0x64064
leaq 0x8(%rsp), %rdi
movsd 0x9be49(%rip), %xmm0    # 0xbc080
callq 0x64196
jmp 0x20196
jmp 0x20245
jmp 0x20245
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x60264
movq %rbx, %rdi
callq 0x11a60