// File: tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDCreateBoundaryChannel(gaps::R2Grid const&, gaps::R2Grid&, double)
int RGBDCreateBoundaryChannel(
  const R2Grid& input_depth_image, R2Grid& output_boundary_image,
  RNScalar depth_threshold)
{
  // Initialize position images
  output_boundary_image = input_depth_image; 
  output_boundary_image.Clear(R2_GRID_UNKNOWN_VALUE);

  // Mark bottom and top border boundaries
  for (int i = 0; i < output_boundary_image.XResolution(); i++) {
    for (int j = 0; j < output_boundary_image.YResolution(); j++) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
    for (int j = output_boundary_image.YResolution()-1; j >= 0; j--) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
  }

  // Mark left and right border boundaries
  for (int j = 0; j < output_boundary_image.YResolution(); j++) {
    for (int i = 0; i < output_boundary_image.XResolution(); i++) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
    for (int i = output_boundary_image.XResolution()-1; i >= 0; i--) {
      output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
      if (RNIsPositive(input_depth_image.GridValue(i, j))) break;
    }
  }

  // Create copy of depth image with holes filled
  R2Grid filled_input_depth_image(input_depth_image);
  filled_input_depth_image.Substitute(0, R2_GRID_UNKNOWN_VALUE);
  filled_input_depth_image.FillHoles();

  // Mark interior boundaries, silhouettes, and shadows
  for (int i = 1; i < output_boundary_image.XResolution()-1; i++) {
    for (int j = 1; j < output_boundary_image.YResolution()-1; j++) {
      // Get original depth
      RNScalar depth = input_depth_image.GridValue(i, j);

      // Check if in hole
      if (RNIsNegativeOrZero(depth)) {
        output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
        continue;
      }

      // Get filled depth
      depth = filled_input_depth_image.GridValue(i, j);
      if (RNIsNegativeOrZero(depth)) continue;

      // Check depth relative to neighbors
      for (int k = 0; k < 4; k++) {
        int s = (k < 3) ? -1 : 0;
        int t = (k < 3) ? k-1 : -1;

        // Get depth on one side
        RNScalar depthA = filled_input_depth_image.GridValue(i-s, j-t);
        if (RNIsNegativeOrZero(depthA)) {
          output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
          break;
        }

        // Get depth on other side
        RNScalar depthB = filled_input_depth_image.GridValue(i+s, j+t);
        if (RNIsNegativeOrZero(depthB)) {
          output_boundary_image.SetGridValue(i, j, RGBD_BORDER_BOUNDARY);
          break;
        }

        // Check differences of depth for shadow/silhouette
        RNScalar deltaA = depth - depthA;
        RNScalar deltaB = depthB - depth;
        RNScalar threshold = depth * depth_threshold;
        if (threshold < 0.1) threshold = 0.1;
        if (deltaA < -threshold) {
          if (deltaA < 4*deltaB) {
            output_boundary_image.SetGridValue(i-s, j-t, RGBD_SHADOW_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SILHOUETTE_BOUNDARY);
          }
        }
        else if (deltaA > threshold) {
          if (deltaA > 4*deltaB) {
            output_boundary_image.SetGridValue(i-s, j-t, RGBD_SILHOUETTE_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SHADOW_BOUNDARY);
          }
        }
        if (deltaB < -threshold) {
          if (deltaB < 4*deltaA) {
            output_boundary_image.SetGridValue(i+s, j+t, RGBD_SILHOUETTE_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SHADOW_BOUNDARY);
          }
        }
        else if (deltaB > threshold) {
          if (deltaB > 4*deltaA) {
            output_boundary_image.SetGridValue(i+s, j+t, RGBD_SHADOW_BOUNDARY);
            output_boundary_image.SetGridValue(i, j, RGBD_SILHOUETTE_BOUNDARY);
          }
        }
      }
    }
  }

  // Return success 
  return 1;
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
movsd %xmm0, 0x1b0(%rsp)
movq 0x1c0(%rsp), %rsi
movq 0x1b8(%rsp), %rdi
callq 0xb2f20
movq 0x1b8(%rsp), %rdi
movsd 0x134bdb(%rip), %xmm0   # 0x166020
callq 0xb5a20
movl $0x0, 0x1ac(%rsp)
movl 0x1ac(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27940
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x315b8
movl $0x0, 0x1a8(%rsp)
movl 0x1a8(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27960
movl %eax, %ecx
movl 0x68(%rsp), %eax
cmpl %ecx, %eax
jge 0x31515
movq 0x1b8(%rsp), %rdi
movl 0x1ac(%rsp), %esi
movl 0x1a8(%rsp), %edx
movsd 0x134b6a(%rip), %xmm0   # 0x166030
callq 0x1a1b0
movq 0x1c0(%rsp), %rdi
movl 0x1ac(%rsp), %esi
movl 0x1a8(%rsp), %edx
callq 0x1a060
leaq 0x1809d3(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x308c0
cmpl $0x0, %eax
je 0x314fd
jmp 0x31515
jmp 0x314ff
movl 0x1a8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
jmp 0x31486
movq 0x1b8(%rsp), %rdi
callq 0x27960
subl $0x1, %eax
movl %eax, 0x1a4(%rsp)
cmpl $0x0, 0x1a4(%rsp)
jl 0x315a0
movq 0x1b8(%rsp), %rdi
movl 0x1ac(%rsp), %esi
movl 0x1a4(%rsp), %edx
movsd 0x134adc(%rip), %xmm0   # 0x166030
callq 0x1a1b0
movq 0x1c0(%rsp), %rdi
movl 0x1ac(%rsp), %esi
movl 0x1a4(%rsp), %edx
callq 0x1a060
leaq 0x180945(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x308c0
cmpl $0x0, %eax
je 0x3158b
jmp 0x315a0
jmp 0x3158d
movl 0x1a4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1a4(%rsp)
jmp 0x3152c
jmp 0x315a2
movl 0x1ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ac(%rsp)
jmp 0x31455
movl $0x0, 0x1a0(%rsp)
movl 0x1a0(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27960
movl %eax, %ecx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x31726
movl $0x0, 0x19c(%rsp)
movl 0x19c(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27940
movl %eax, %ecx
movl 0x60(%rsp), %eax
cmpl %ecx, %eax
jge 0x31683
movq 0x1b8(%rsp), %rdi
movl 0x19c(%rsp), %esi
movl 0x1a0(%rsp), %edx
movsd 0x1349fc(%rip), %xmm0   # 0x166030
callq 0x1a1b0
movq 0x1c0(%rsp), %rdi
movl 0x19c(%rsp), %esi
movl 0x1a0(%rsp), %edx
callq 0x1a060
leaq 0x180865(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x308c0
cmpl $0x0, %eax
je 0x3166b
jmp 0x31683
jmp 0x3166d
movl 0x19c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x19c(%rsp)
jmp 0x315f4
movq 0x1b8(%rsp), %rdi
callq 0x27940
subl $0x1, %eax
movl %eax, 0x198(%rsp)
cmpl $0x0, 0x198(%rsp)
jl 0x3170e
movq 0x1b8(%rsp), %rdi
movl 0x198(%rsp), %esi
movl 0x1a0(%rsp), %edx
movsd 0x13496e(%rip), %xmm0   # 0x166030
callq 0x1a1b0
movq 0x1c0(%rsp), %rdi
movl 0x198(%rsp), %esi
movl 0x1a0(%rsp), %edx
callq 0x1a060
leaq 0x1807d7(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x308c0
cmpl $0x0, %eax
je 0x316f9
jmp 0x3170e
jmp 0x316fb
movl 0x198(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x198(%rsp)
jmp 0x3169a
jmp 0x31710
movl 0x1a0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a0(%rsp)
jmp 0x315c3
movq 0x1c0(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0xb30a0
movq 0x58(%rsp), %rdi
xorps %xmm0, %xmm0
movsd 0x1348d0(%rip), %xmm1   # 0x166020
callq 0xba150
jmp 0x31757
leaq 0xc8(%rsp), %rdi
callq 0xb4960
jmp 0x31766
movl $0x1, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27940
movl %eax, 0x54(%rsp)
jmp 0x3178f
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x31da2
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0x27960
movl %eax, 0x4c(%rsp)
jmp 0x317cb
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x31d8a
movq 0x1c0(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
callq 0x1a060
movsd %xmm0, 0x40(%rsp)
jmp 0x31801
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
leaq 0x1806a0(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0x3c(%rsp)
jmp 0x3182f
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
je 0x31888
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x1347da(%rip), %xmm0   # 0x166030
callq 0x1a1b0
jmp 0x3185d
jmp 0x31d74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0xb3390
jmp 0x31dbc
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
leaq 0xc8(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x30(%rsp)
jmp 0x318ab
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
leaq 0x1805f6(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0x2c(%rsp)
jmp 0x318d9
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
je 0x318e7
jmp 0x31d74
movl $0x0, 0xa4(%rsp)
cmpl $0x4, 0xa4(%rsp)
jge 0x31d72
movl 0xa4(%rsp), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $0x3, %edx
cmovll %ecx, %eax
movl %eax, 0xa0(%rsp)
cmpl $0x3, 0xa4(%rsp)
jge 0x31935
movl 0xa4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x31940
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x28(%rsp)
jmp 0x31940
movl 0x28(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
subl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
subl %eax, %edx
leaq 0xc8(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x20(%rsp)
jmp 0x31980
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
leaq 0x180521(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0x1c(%rsp)
jmp 0x319ae
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
je 0x319e1
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x13465b(%rip), %xmm0   # 0x166030
callq 0x1a1b0
jmp 0x319dc
jmp 0x31d72
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
addl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
addl %eax, %edx
leaq 0xc8(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x10(%rsp)
jmp 0x31a16
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
leaq 0x18048b(%rip), %rax    # 0x1b1ec0
movsd (%rax), %xmm1
callq 0x1a3d0
movl %eax, 0xc(%rsp)
jmp 0x31a44
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x31a77
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x1345c5(%rip), %xmm0   # 0x166030
callq 0x1a1b0
jmp 0x31a72
jmp 0x31d72
movsd 0xa8(%rsp), %xmm0
subsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rsp), %xmm0
subsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0xa8(%rsp), %xmm0
mulsd 0x1b0(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x1356de(%rip), %xmm0   # 0x1671a8
ucomisd 0x70(%rsp), %xmm0
jbe 0x31ae0
movsd 0x1356ce(%rip), %xmm0   # 0x1671a8
movsd %xmm0, 0x70(%rsp)
movsd 0x80(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x31b90
movsd 0x80(%rsp), %xmm1
movsd 0x135517(%rip), %xmm0   # 0x167038
mulsd 0x78(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x31b8b
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
subl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
subl %eax, %edx
movsd 0x1354db(%rip), %xmm0   # 0x167038
callq 0x1a1b0
jmp 0x31b64
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x1349d6(%rip), %xmm0   # 0x166558
callq 0x1a1b0
jmp 0x31b89
jmp 0x31b8b
jmp 0x31c20
movsd 0x80(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x31c1e
movsd 0x80(%rsp), %xmm0
movsd 0x135486(%rip), %xmm1   # 0x167038
mulsd 0x78(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x31c1c
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
subl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
subl %eax, %edx
movsd 0x13496a(%rip), %xmm0   # 0x166558
callq 0x1a1b0
jmp 0x31bf5
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x135425(%rip), %xmm0   # 0x167038
callq 0x1a1b0
jmp 0x31c1a
jmp 0x31c1c
jmp 0x31c1e
jmp 0x31c20
movsd 0x78(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x31ccd
movsd 0x78(%rsp), %xmm1
movsd 0x1353dd(%rip), %xmm0   # 0x167038
mulsd 0x80(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x31cc8
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
addl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
addl %eax, %edx
movsd 0x1348be(%rip), %xmm0   # 0x166558
callq 0x1a1b0
jmp 0x31ca1
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x135379(%rip), %xmm0   # 0x167038
callq 0x1a1b0
jmp 0x31cc6
jmp 0x31cc8
jmp 0x31d5a
movsd 0x78(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x31d58
movsd 0x78(%rsp), %xmm0
movsd 0x13534f(%rip), %xmm1   # 0x167038
mulsd 0x80(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x31d56
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xa0(%rsp), %eax
addl %eax, %esi
movl 0xb4(%rsp), %edx
movl 0x9c(%rsp), %eax
addl %eax, %edx
movsd 0x135310(%rip), %xmm0   # 0x167038
callq 0x1a1b0
jmp 0x31d2f
movq 0x1b8(%rsp), %rdi
movl 0xb8(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x13480b(%rip), %xmm0   # 0x166558
callq 0x1a1b0
jmp 0x31d54
jmp 0x31d56
jmp 0x31d58
jmp 0x31d5a
jmp 0x31d5c
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x318f2
jmp 0x31d74
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x317ad
jmp 0x31d8c
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x31771
leaq 0xc8(%rsp), %rdi
callq 0xb3390
movl $0x1, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
movq 0xc0(%rsp), %rdi
callq 0x12c20
nopl (%rax)