// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDResampleDepthImage(gaps::R2Grid&, gaps::R3Matrix&, int, int)
int RGBDResampleDepthImage(R2Grid& image, R3Matrix& intrinsics_matrix, int xres, int yres)
{
  // Just checking
  if ((xres == 0) || (yres == 0)) return 0;
  if ((image.XResolution() == 0) || (image.YResolution() == 0)) return 0;
  if ((image.XResolution() == xres) && (image.YResolution() == yres)) return 1;

  // Compute scale factors
  double xscale = (double) image.XResolution() / (double) xres;
  double yscale = (double) image.YResolution() / (double) yres;

  // Copy original image and then initialize to zeroes
  R2Grid copy_image(image);
  image = R2Grid(xres, yres);
  image.Clear(R2_GRID_UNKNOWN_VALUE);
  
  // Resample images
  for (int iy = 0; iy < yres; iy++) {
    double cy = yscale * iy;
    int min_y = cy - 0.5*yscale + 0.5;
    if (min_y >= copy_image.YResolution()) continue;
    int max_y = cy + 0.5*yscale + 0.5;
    if (max_y < 0) continue;
    if (min_y < 0) min_y = 0;
    if (max_y >= copy_image.YResolution()) max_y = copy_image.YResolution()-1;
    if (max_y < min_y) max_y = min_y;
    
    for (int ix = 0; ix < xres; ix++) {
      double cx = xscale * ix;
      int min_x = cx - 0.5*xscale + 0.5;
      if (min_x >= copy_image.XResolution()) continue;
      int max_x = cx + 0.5*xscale + 0.5;
      if (max_x < 0) continue;
      if (min_x < 0) min_x = 0;
      if (max_x >= copy_image.XResolution()) max_x = copy_image.XResolution()-1;
      if (max_x < min_x) max_x = min_x;

      // Find minimum depth in neighborhood
      RNScalar min_depth = FLT_MAX;
      for (int j = min_y; j <= max_y; j++) {
        for (int i = min_x; i <= max_x; i++) {
          RNScalar depth = copy_image.GridValue(i, j);
          if ((depth <= 0) || (depth == R2_GRID_UNKNOWN_VALUE)) continue;
          if (depth < min_depth) min_depth = depth;
        }
      }

      // Set depth
      if (min_depth < FLT_MAX) {
        image.SetGridValue(ix, iy, min_depth);
      }
    }
  }

  // Update depth intrinsics matrix
  intrinsics_matrix[0][0] /= xscale;
  intrinsics_matrix[0][2] /= xscale;
  intrinsics_matrix[1][1] /= yscale;
  intrinsics_matrix[1][2] /= yscale;

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movl %ecx, %ebp
movl %edx, %r12d
testl %edx, %edx
sete %dl
testl %ecx, %ecx
sete %cl
xorl %eax, %eax
orb %dl, %cl
jne 0x238b8
movq %rdi, %r13
movl 0xc0(%rdi), %ebx
testl %ebx, %ebx
je 0x238b8
movl 0xc4(%r13), %r14d
testl %r14d, %r14d
je 0x238b8
movl %ebx, %edx
xorl %r12d, %edx
movl %r14d, %ecx
xorl %ebp, %ecx
movl $0x1, %eax
orl %edx, %ecx
je 0x238b8
movq %rsi, %r15
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x6008c
leaq 0xf8(%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x5f630
leaq 0xf8(%rsp), %rsi
movq %r13, %rdi
callq 0x5ffa2
movq %r15, 0x18(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x60264
movsd 0x97a02(%rip), %xmm0    # 0xbb018
movq %r13, 0x20(%rsp)
movq %r13, %rdi
callq 0x617b0
cvtsi2sd %ebx, %xmm1
movl %r12d, 0x4(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
cvtsi2sd %ebp, %xmm2
divsd %xmm2, %xmm0
testl %ebp, %ebp
jle 0x23878
movl 0xe8(%rsp), %eax
movl 0xec(%rsp), %ecx
movq %rcx, 0x10(%rsp)
decl %ecx
movl %ecx, 0x8(%rsp)
leal -0x1(%rax), %esi
movl 0x4(%rsp), %edi
xorpd %xmm2, %xmm2
movsd 0x989d4(%rip), %xmm3    # 0xbc050
mulsd %xmm0, %xmm3
movsd 0x97998(%rip), %xmm4    # 0xbb020
movapd %xmm0, %xmm5
mulsd %xmm4, %xmm5
movsd 0x989b8(%rip), %xmm6    # 0xbc050
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm7
mulsd %xmm4, %xmm7
movsd 0x97983(%rip), %xmm8    # 0xbb030
xorpd %xmm10, %xmm10
xorl %r9d, %r9d
movl %ebp, 0xc(%rsp)
movsd 0x97956(%rip), %xmm9    # 0xbb018
movapd %xmm0, %xmm11
mulsd %xmm10, %xmm11
movapd %xmm3, %xmm12
addsd %xmm11, %xmm12
addsd %xmm4, %xmm12
cvttsd2si %xmm12, %r8d
cmpl %r8d, 0x10(%rsp)
jle 0x2385f
cmpl $0x0, 0x4(%rsp)
addsd %xmm5, %xmm11
addsd %xmm4, %xmm11
cvttsd2si %xmm11, %r11d
setle %cl
testl %r11d, %r11d
sets %dl
orb %cl, %dl
jne 0x2385f
cmpl %r11d, 0x10(%rsp)
cmovlel 0x8(%rsp), %r11d
testl %r8d, %r8d
movl $0x0, %ecx
cmovlel %ecx, %r8d
cmpl %r8d, %r11d
cmovlel %r8d, %r11d
movq 0x20(%rsp), %rdx
movslq 0xc8(%rdx), %rcx
movslq %r9d, %r13
imulq %rcx, %r13
cmpl %r8d, %r11d
cmovbel %r8d, %r11d
incl %r11d
shlq $0x3, %r13
addq 0xb8(%rdx), %r13
leaq (,%r8,8), %rcx
xorl %r14d, %r14d
xorps %xmm11, %xmm11
cvtsi2sd %r14d, %xmm11
mulsd %xmm1, %xmm11
movapd %xmm6, %xmm12
addsd %xmm11, %xmm12
addsd %xmm4, %xmm12
cvttsd2si %xmm12, %ebp
cmpl %ebp, %eax
jle 0x23853
addsd %xmm7, %xmm11
addsd %xmm4, %xmm11
cvttsd2si %xmm11, %r12d
testl %r12d, %r12d
js 0x23853
cmpl %r12d, %eax
cmovlel %esi, %r12d
testl %ebp, %ebp
movl $0x0, %edx
cmovlel %edx, %ebp
cmpl %ebp, %r12d
cmovlel %ebp, %r12d
movslq 0xf0(%rsp), %r15
movq %rcx, %rdx
imulq %r15, %rdx
addq 0xe0(%rsp), %rdx
incl %r12d
shlq $0x3, %r15
movq %r8, %r10
movapd %xmm8, %xmm11
movq %rbp, %rbx
movsd (%rdx,%rbx,8), %xmm12
movapd %xmm12, %xmm13
minsd %xmm11, %xmm13
movapd %xmm12, %xmm14
cmpeqsd %xmm9, %xmm14
movapd %xmm14, %xmm15
andnpd %xmm13, %xmm15
andpd %xmm11, %xmm14
orpd %xmm15, %xmm14
cmplesd %xmm2, %xmm12
movapd %xmm12, %xmm13
andnpd %xmm14, %xmm13
andpd %xmm11, %xmm12
orpd %xmm13, %xmm12
movapd %xmm12, %xmm11
incq %rbx
cmpq %rbx, %r12
jne 0x237e4
incq %r10
addq %r15, %rdx
cmpq %r11, %r10
jne 0x237e1
ucomisd %xmm11, %xmm8
jbe 0x23853
movsd %xmm11, (%r13,%r14,8)
incq %r14
cmpq %rdi, %r14
jne 0x23761
addsd 0x977c0(%rip), %xmm10   # 0xbb028
incl %r9d
movl 0xc(%rsp), %ebp
cmpl %ebp, %r9d
jne 0x236c2
movq 0x18(%rsp), %rax
movsd (%rax), %xmm2
movsd 0x10(%rax), %xmm3
divsd %xmm1, %xmm2
movsd %xmm2, (%rax)
divsd %xmm1, %xmm3
movsd %xmm3, 0x10(%rax)
movupd 0x20(%rax), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm1
movupd %xmm1, 0x20(%rax)
leaq 0x28(%rsp), %rdi
callq 0x60264
movl $0x1, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x238de
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x60264
jmp 0x238e1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x60264
movq %rbx, %rdi
callq 0x11a60