// File: 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:
subq $0x298, %rsp            # imm = 0x298
movq %rdi, 0x288(%rsp)
movq %rsi, 0x280(%rsp)
movl %edx, 0x27c(%rsp)
movl %ecx, 0x278(%rsp)
cmpl $0x0, 0x27c(%rsp)
je 0x34a49
cmpl $0x0, 0x278(%rsp)
jne 0x34a59
movl $0x0, 0x294(%rsp)
jmp 0x35126
movq 0x288(%rsp), %rdi
callq 0x27940
cmpl $0x0, %eax
je 0x34a7d
movq 0x288(%rsp), %rdi
callq 0x27960
cmpl $0x0, %eax
jne 0x34a8d
movl $0x0, 0x294(%rsp)
jmp 0x35126
movq 0x288(%rsp), %rdi
callq 0x27940
cmpl 0x27c(%rsp), %eax
jne 0x34ac9
movq 0x288(%rsp), %rdi
callq 0x27960
cmpl 0x278(%rsp), %eax
jne 0x34ac9
movl $0x1, 0x294(%rsp)
jmp 0x35126
movq 0x288(%rsp), %rdi
callq 0x27940
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x27c(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x270(%rsp)
movq 0x288(%rsp), %rdi
callq 0x27960
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x278(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x268(%rsp)
movq 0x288(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0xb30a0
movl 0x27c(%rsp), %esi
movl 0x278(%rsp), %edx
leaq 0xc8(%rsp), %rdi
callq 0xb1ee0
jmp 0x34b49
movq 0x288(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xb2f20
jmp 0x34b60
leaq 0xc8(%rsp), %rdi
callq 0xb3390
movq 0x288(%rsp), %rdi
movsd 0x1314a3(%rip), %xmm0   # 0x166020
callq 0xb5a20
jmp 0x34b84
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
cmpl 0x278(%rsp), %eax
jge 0x35001
movsd 0x268(%rsp), %xmm0
cvtsi2sdl 0xb8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0xb0(%rsp), %xmm1
movsd 0x268(%rsp), %xmm0
movsd 0x131984(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x13143c(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x27960
movl %eax, 0x74(%rsp)
jmp 0x34c19
movl 0x70(%rsp), %eax
movl 0x74(%rsp), %ecx
cmpl %ecx, %eax
jl 0x34c69
jmp 0x34feb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x35117
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0xb3390
jmp 0x35117
movsd 0xb0(%rsp), %xmm1
movsd 0x268(%rsp), %xmm0
movsd 0x1313a5(%rip), %xmm2   # 0x166028
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x131395(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xa8(%rsp)
cmpl $0x0, 0xa8(%rsp)
jge 0x34cb1
jmp 0x34feb
cmpl $0x0, 0xac(%rsp)
jge 0x34cc6
movl $0x0, 0xac(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x27960
movl %eax, 0x6c(%rsp)
jmp 0x34ce4
movl 0x68(%rsp), %eax
movl 0x6c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x34d11
leaq 0x198(%rsp), %rdi
callq 0x27960
movl %eax, 0x64(%rsp)
jmp 0x34d03
movl 0x64(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
cmpl 0xac(%rsp), %eax
jge 0x34d2f
movl 0xac(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x27c(%rsp), %eax
jge 0x34fe9
movsd 0x270(%rsp), %xmm0
cvtsi2sdl 0xa4(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm1
movsd 0x270(%rsp), %xmm0
movsd 0x1317d9(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x131291(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x5c(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x27940
movl %eax, 0x60(%rsp)
jmp 0x34dc4
movl 0x5c(%rsp), %eax
movl 0x60(%rsp), %ecx
cmpl %ecx, %eax
jl 0x34dd5
jmp 0x34fd3
movsd 0x98(%rsp), %xmm1
movsd 0x270(%rsp), %xmm0
movsd 0x131239(%rip), %xmm2   # 0x166028
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x131229(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x90(%rsp)
jge 0x34e1d
jmp 0x34fd3
cmpl $0x0, 0x94(%rsp)
jge 0x34e32
movl $0x0, 0x94(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x54(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x27940
movl %eax, 0x58(%rsp)
jmp 0x34e50
movl 0x54(%rsp), %eax
movl 0x58(%rsp), %ecx
cmpl %ecx, %eax
jl 0x34e7d
leaq 0x198(%rsp), %rdi
callq 0x27940
movl %eax, 0x50(%rsp)
jmp 0x34e6f
movl 0x50(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
cmpl 0x94(%rsp), %eax
jge 0x34e9b
movl 0x94(%rsp), %eax
movl %eax, 0x90(%rsp)
movsd 0x131165(%rip), %xmm0   # 0x166008
movsd %xmm0, 0x88(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xa8(%rsp), %eax
jg 0x34f96
movl 0x94(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x90(%rsp), %eax
jg 0x34f7e
movl 0x80(%rsp), %esi
movl 0x84(%rsp), %edx
leaq 0x198(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x48(%rsp)
jmp 0x34f13
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x78(%rsp), %xmm0
jae 0x34f40
movsd 0x78(%rsp), %xmm0
movsd 0x1310e8(%rip), %xmm1   # 0x166020
ucomisd %xmm1, %xmm0
jne 0x34f42
jp 0x34f42
jmp 0x34f68
movsd 0x78(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x34f66
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x34f68
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x34edc
jmp 0x34f80
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x34eba
movsd 0x13106a(%rip), %xmm0   # 0x166008
ucomisd 0x88(%rsp), %xmm0
jbe 0x34fd1
movq 0x288(%rsp), %rdi
movl 0xa4(%rsp), %esi
movl 0xb8(%rsp), %edx
movsd 0x88(%rsp), %xmm0
callq 0x1a1b0
jmp 0x34fcf
jmp 0x34fd1
jmp 0x34fd3
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x34d3a
jmp 0x34feb
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x34b8f
movsd 0x270(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x280(%rsp), %rdi
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x40(%rsp)
jmp 0x35026
movq 0x40(%rsp), %rax
movsd 0x38(%rsp), %xmm1
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movsd 0x270(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x280(%rsp), %rdi
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x30(%rsp)
jmp 0x35062
movq 0x30(%rsp), %rax
movsd 0x28(%rsp), %xmm1
movsd 0x10(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x268(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x280(%rsp), %rdi
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x20(%rsp)
jmp 0x350a3
movq 0x20(%rsp), %rax
movsd 0x18(%rsp), %xmm1
movsd 0x8(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x268(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x280(%rsp), %rdi
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x10(%rsp)
jmp 0x350e4
movq 0x10(%rsp), %rax
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movl $0x1, 0x294(%rsp)
leaq 0x198(%rsp), %rdi
callq 0xb3390
jmp 0x35126
leaq 0x198(%rsp), %rdi
callq 0xb3390
jmp 0x35135
movl 0x294(%rsp), %eax
addq $0x298, %rsp            # imm = 0x298
retq
movq 0xc0(%rsp), %rdi
callq 0x12c20
nopw %cs:(%rax,%rax)