// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r14
pushq %rbx
subq $0x1130, %rsp           # imm = 0x1130
movq %rdi, %rbx
movl 0x1fc(%rdi), %eax
testl %eax, %eax
jle 0x200d6
incl %eax
movl %eax, 0x1fc(%rbx)
movl $0x1, %ebx
jmp 0x204b7
movl 0x28(%rbx), %esi
movl 0x2c(%rbx), %edx
leaq 0x18(%rsp), %rdi
callq 0x603d0
movq 0x1e0(%rbx), %rcx
testq %rcx, %rcx
je 0x2049a
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x20105
movq 0x38(%rax), %rdx
jmp 0x20107
xorl %edx, %edx
testq %rdx, %rdx
je 0x20124
leaq 0x9dea5(%rip), %rsi     # 0xbdfb8
leaq 0x130(%rsp), %rdi
xorl %eax, %eax
callq 0x11110
jmp 0x20134
leaq 0x130(%rsp), %rdi
movq %rcx, %rsi
callq 0x11530
leaq 0x18(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x688d2
testl %eax, %eax
je 0x2042c
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x201de
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x201de
leaq 0x9e07f(%rip), %rsi     # 0xbe1ea
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
je 0x2018a
leaq 0x9e072(%rip), %rsi     # 0xbe1f0
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
jne 0x201de
movslq 0xe4(%rsp), %rax
testq %rax, %rax
jle 0x201de
movq 0xd0(%rsp), %rcx
xorl %edx, %edx
movsd 0x9ce77(%rip), %xmm0    # 0xbd020
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 0x201a9
leaq 0x18(%rsp), %rdi
movsd 0x9ce2d(%rip), %xmm1    # 0xbd018
xorpd %xmm0, %xmm0
callq 0x64d52
movq 0x1e0(%rbx), %rdi
leaq 0x9da3a(%rip), %rsi     # 0xbdc3c
callq 0x11140
testq %rax, %rax
je 0x20442
leaq 0x18(%rsp), %rdi
movsd 0x9de4b(%rip), %xmm0    # 0xbe068
callq 0x64eb6
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x20442
movq 0x58(%rax), %r14
testq %r14, %r14
je 0x20442
leaq 0x9d513(%rip), %rsi     # 0xbd756
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
je 0x20430
leaq 0x9df9e(%rip), %rsi     # 0xbe1f8
movq %r14, %rdi
callq 0x11850
movsd 0x9de06(%rip), %xmm0    # 0xbe070
testl %eax, %eax
je 0x20438
leaq 0x9df83(%rip), %rsi     # 0xbe1fc
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
je 0x204c5
leaq 0x9df70(%rip), %rsi     # 0xbe200
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
movsd 0x9ddce(%rip), %xmm0    # 0xbe070
je 0x20438
leaq 0x9df55(%rip), %rsi     # 0xbe204
movq %r14, %rdi
callq 0x11850
testl %eax, %eax
jne 0x20442
movsd 0x140(%rbx), %xmm2
leaq 0xe6a1a(%rip), %rax     # 0x106ce8
movsd (%rax), %xmm1
ucomisd %xmm1, %xmm2
movapd 0x9cd72(%rip), %xmm0    # 0xbd050
xorpd %xmm1, %xmm0
ja 0x202ee
ucomisd %xmm2, %xmm0
jbe 0x20442
movsd 0x160(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x20306
ucomisd %xmm2, %xmm0
jbe 0x20442
cmpl $0x0, 0xd8(%rsp)
jle 0x20442
xorl %ebp, %ebp
xorpd %xmm2, %xmm2
cmpl $0x0, 0xdc(%rsp)
jle 0x2041b
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
movq 0xd0(%rsp), %rax
movl 0xe0(%rsp), %ecx
imull %r14d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm3
ucomisd %xmm2, %xmm3
jne 0x20361
jnp 0x2040a
movsd 0x10(%rsp), %xmm0
subsd 0x150(%rbx), %xmm0
divsd 0x140(%rbx), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
subsd 0x168(%rbx), %xmm1
divsd 0x160(%rbx), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
movsd %xmm3, 0x8(%rsp)
jb 0x203ad
sqrtsd %xmm0, %xmm0
jmp 0x203b6
callq 0x11480
xorpd %xmm2, %xmm2
mulsd %xmm0, %xmm0
addsd 0x9cc66(%rip), %xmm0    # 0xbd028
ucomisd %xmm2, %xmm0
jb 0x203ce
sqrtsd %xmm0, %xmm0
jmp 0x203d7
callq 0x11480
xorpd %xmm2, %xmm2
movsd 0x8(%rsp), %xmm3
movsd 0x9dc93(%rip), %xmm1    # 0xbe078
divsd %xmm3, %xmm1
divsd %xmm0, %xmm1
movq 0xd0(%rsp), %rax
movl 0xe0(%rsp), %ecx
imull %r14d, %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movsd %xmm1, (%rax,%rcx,8)
incl %r14d
cmpl 0xdc(%rsp), %r14d
jl 0x20338
incl %ebp
cmpl 0xd8(%rsp), %ebp
jl 0x2031a
jmp 0x20442
xorl %ebx, %ebx
jmp 0x204ad
movsd 0x9dc10(%rip), %xmm0    # 0xbe048
leaq 0x18(%rsp), %rdi
callq 0x64eb6
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 0x2049a
cmpl 0xd8(%rsp), %ecx
jne 0x2046e
cmpl 0xdc(%rsp), %eax
je 0x2049a
leaq 0x9fe33(%rip), %rsi     # 0xc02a8
leaq 0xe8(%rsp), %rdi
callq 0x6dfb2
movl 0x28(%rbx), %edx
movl 0x2c(%rbx), %ecx
leaq 0x18(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x238b1
movq (%rbx), %rax
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq *0x180(%rax)
movl %eax, %ebx
leaq 0x18(%rsp), %rdi
callq 0x60ff0
movl %ebx, %eax
addq $0x1130, %rsp           # imm = 0x1130
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x18(%rsp), %rdi
movsd 0x9cb4e(%rip), %xmm0    # 0xbd020
callq 0x64eb6
leaq 0x18(%rsp), %rdi
movsd 0x9db9c(%rip), %xmm0    # 0xbe080
callq 0x64fe8
jmp 0x20442
jmp 0x204f2
jmp 0x204f2
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x60ff0
movq %rbx, %rdi
callq 0x11a70
nop