// File: tomfunkhouser[P]gaps/apps/scn2img/scn2img.cpp
// Function: ComputeBoundaryImage(gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid const&, gaps::R2Grid&)
static int
ComputeBoundaryImage(const R2Grid& depth_image, const R2Grid& node_image,
  const R2Grid& xnormal_image, const R2Grid& ynormal_image, const R2Grid& znormal_image,
  R2Grid& result)
{                  
  // Find node boundaries
  R2Grid node_boundaries(node_image);
  node_boundaries.GradientMagnitude();
  node_boundaries.Threshold(RN_EPSILON, 0, 1);

  // Find silhouette boundaries (where depth difference is > 10% of depth)
  R2Grid silhouette_boundaries(depth_image);
  silhouette_boundaries.Substitute(0, R2_GRID_UNKNOWN_VALUE);
  silhouette_boundaries.GradientMagnitude();
  silhouette_boundaries.Divide(depth_image);
  silhouette_boundaries.Threshold(0.1, 0, 1);
  silhouette_boundaries.Substitute(R2_GRID_UNKNOWN_VALUE, 0);

  // Find crease boundaries
  R2Grid crease_boundaries(width, height);
  R2Grid xcrease_boundaries(xnormal_image);
  xcrease_boundaries.GradientMagnitude();
  R2Grid ycrease_boundaries(ynormal_image);
  ycrease_boundaries.GradientMagnitude();
  R2Grid zcrease_boundaries(znormal_image);
  zcrease_boundaries.GradientMagnitude();
  crease_boundaries.Add(xcrease_boundaries);
  crease_boundaries.Add(ycrease_boundaries);
  crease_boundaries.Add(zcrease_boundaries);
  crease_boundaries.Threshold(1, 0, 1);
  R2Grid silhouette_mask(silhouette_boundaries);
  silhouette_mask.Threshold(0.5, 1, 0);
  crease_boundaries.Mask(silhouette_mask);
    
  // Combine boundaries into one image
  result = R2Grid(width, height);
  node_boundaries.Multiply(2);
  result.Add(node_boundaries);
  silhouette_boundaries.Multiply(4);
  result.Add(silhouette_boundaries);
  crease_boundaries.Multiply(8);
  result.Add(crease_boundaries);

  // Return success
  return 1;
}

// Assembly:
subq $0x6c8, %rsp            # imm = 0x6C8
movq %rdi, 0x6c0(%rsp)
movq %rsi, 0x6b8(%rsp)
movq %rdx, 0x6b0(%rsp)
movq %rcx, 0x6a8(%rsp)
movq %r8, 0x6a0(%rsp)
movq %r9, 0x698(%rsp)
movq 0x6b8(%rsp), %rsi
leaq 0x5c8(%rsp), %rdi
movq %rdi, (%rsp)
callq 0xe48a0
movq (%rsp), %rdi
callq 0xeadb0
jmp 0x2ad0b
leaq 0x1ee586(%rip), %rax    # 0x219298
movsd (%rax), %xmm0
leaq 0x5c8(%rsp), %rdi
xorps %xmm1, %xmm1
movsd 0x18231f(%rip), %xmm2   # 0x1ad048
callq 0xe7a80
jmp 0x2ad30
movq 0x6c0(%rsp), %rsi
leaq 0x4e8(%rsp), %rdi
callq 0xe48a0
jmp 0x2ad47
leaq 0x4e8(%rsp), %rdi
xorps %xmm0, %xmm0
movsd 0x1822fe(%rip), %xmm1   # 0x1ad058
callq 0xeb950
jmp 0x2ad61
leaq 0x4e8(%rsp), %rdi
callq 0xeadb0
jmp 0x2ad70
movq 0x6c0(%rsp), %rsi
leaq 0x4e8(%rsp), %rdi
callq 0xebe00
jmp 0x2ad87
leaq 0x4e8(%rsp), %rdi
movsd 0x1822c9(%rip), %xmm0   # 0x1ad060
xorps %xmm1, %xmm1
movsd 0x1822a6(%rip), %xmm2   # 0x1ad048
callq 0xe7a80
jmp 0x2ada9
leaq 0x4e8(%rsp), %rdi
movsd 0x18229f(%rip), %xmm0   # 0x1ad058
xorps %xmm1, %xmm1
callq 0xeb950
jmp 0x2adc3
movl 0x1edce7(%rip), %esi    # 0x218ab0
movl 0x1edce5(%rip), %edx    # 0x218ab4
leaq 0x418(%rsp), %rdi
callq 0xe36e0
jmp 0x2adde
movq 0x6b0(%rsp), %rsi
leaq 0x348(%rsp), %rdi
callq 0xe48a0
jmp 0x2adf5
leaq 0x348(%rsp), %rdi
callq 0xeadb0
jmp 0x2ae04
movq 0x6a8(%rsp), %rsi
leaq 0x278(%rsp), %rdi
callq 0xe48a0
jmp 0x2ae1b
leaq 0x278(%rsp), %rdi
callq 0xeadb0
jmp 0x2ae2a
movq 0x6a0(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0xe48a0
jmp 0x2ae41
leaq 0x1a8(%rsp), %rdi
callq 0xeadb0
jmp 0x2ae50
leaq 0x418(%rsp), %rdi
leaq 0x348(%rsp), %rsi
callq 0xeba60
jmp 0x2ae67
leaq 0x418(%rsp), %rdi
leaq 0x278(%rsp), %rsi
callq 0xeba60
jmp 0x2ae7e
leaq 0x418(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xeba60
jmp 0x2ae95
leaq 0x418(%rsp), %rdi
xorps %xmm1, %xmm1
movsd 0x1821a0(%rip), %xmm2   # 0x1ad048
movaps %xmm2, %xmm0
callq 0xe7a80
jmp 0x2aeb2
leaq 0xd8(%rsp), %rdi
leaq 0x4e8(%rsp), %rsi
callq 0xe48a0
jmp 0x2aec9
leaq 0xd8(%rsp), %rdi
movsd 0x182157(%rip), %xmm0   # 0x1ad030
movsd 0x182167(%rip), %xmm1   # 0x1ad048
xorps %xmm2, %xmm2
callq 0xe7a80
jmp 0x2aeeb
leaq 0x418(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0xec060
jmp 0x2af02
movl 0x1edba8(%rip), %esi    # 0x218ab0
movl 0x1edba6(%rip), %edx    # 0x218ab4
leaq 0x8(%rsp), %rdi
callq 0xe36e0
jmp 0x2af1a
movq 0x698(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe4720
jmp 0x2af2e
leaq 0x8(%rsp), %rdi
callq 0xe4b90
leaq 0x5c8(%rsp), %rdi
movsd 0x1820f0(%rip), %xmm0   # 0x1ad038
callq 0xebc80
jmp 0x2af4f
movq 0x698(%rsp), %rdi
leaq 0x5c8(%rsp), %rsi
callq 0xeba60
jmp 0x2af66
leaq 0x4e8(%rsp), %rdi
movsd 0x1820f2(%rip), %xmm0   # 0x1ad068
callq 0xebc80
jmp 0x2af7d
movq 0x698(%rsp), %rdi
leaq 0x4e8(%rsp), %rsi
callq 0xeba60
jmp 0x2af94
leaq 0x418(%rsp), %rdi
movsd 0x1820cc(%rip), %xmm0   # 0x1ad070
callq 0xebc80
jmp 0x2afab
movq 0x698(%rsp), %rdi
leaq 0x418(%rsp), %rsi
callq 0xeba60
jmp 0x2afc2
leaq 0xd8(%rsp), %rdi
callq 0xe4b90
leaq 0x1a8(%rsp), %rdi
callq 0xe4b90
leaq 0x278(%rsp), %rdi
callq 0xe4b90
leaq 0x348(%rsp), %rdi
callq 0xe4b90
leaq 0x418(%rsp), %rdi
callq 0xe4b90
leaq 0x4e8(%rsp), %rdi
callq 0xe4b90
leaq 0x5c8(%rsp), %rdi
callq 0xe4b90
movl $0x1, %eax
addq $0x6c8, %rsp            # imm = 0x6C8
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b13c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b12f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b115
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b108
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b0fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
jmp 0x2b0ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x5c0(%rsp)
movl %eax, 0x5bc(%rsp)
leaq 0x8(%rsp), %rdi
callq 0xe4b90
leaq 0xd8(%rsp), %rdi
callq 0xe4b90
leaq 0x1a8(%rsp), %rdi
callq 0xe4b90
leaq 0x278(%rsp), %rdi
callq 0xe4b90
leaq 0x348(%rsp), %rdi
callq 0xe4b90
leaq 0x418(%rsp), %rdi
callq 0xe4b90
leaq 0x4e8(%rsp), %rdi
callq 0xe4b90
leaq 0x5c8(%rsp), %rdi
callq 0xe4b90
movq 0x5c0(%rsp), %rdi
callq 0x24270
nopw %cs:(%rax,%rax)