// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/RGBD/RGBDUtil.cpp
// Function: gaps::RGBDResampleColorImage(gaps::R2Image&, gaps::R3Matrix&, int, int)
int RGBDResampleColorImage(R2Image& image, R3Matrix& intrinsics_matrix, int xres, int yres)
{
  // Just checking
  if ((xres == 0) || (yres == 0)) return 0;
  if ((image.Width() == 0) || (image.Height() == 0)) return 0;

  // Compute scale factors
  double xscale = (double) image.Width() / (double) xres;
  double yscale = (double) image.Height() / (double) yres;
  if (RNIsNotEqual(yscale/xscale, 1.0, 0.05)) {
    RNFail("Warning: anisotropic scaling of color image by factor %g\n", yscale/xscale);
  }
  
  // Copy original image and then initialize to zeroes
  R2Image copy_image(image);
  image = R2Image(xres, yres, 3);

  // 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.Height()) 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.Height()) max_y = copy_image.Height()-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.Width()) 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.Width()) max_x = copy_image.Width()-1;
      if (max_x < min_x) max_x = min_x;

      // Compute weighted sum of colors in neighborhood
      RNRgb sum(0,0,0);
      RNScalar weight = 0;
      for (int j = min_y; j <= max_y; j++) {
        for (int i = min_x; i <= max_x; i++) {
          RNRgb color = copy_image.PixelRGB(i, j);
          if (color == RNblack_rgb) continue; // ??? for borders
          RNScalar w = 1.0;
          sum += w * color;
          weight += w;
        }
      }

      // Compute average
      if (weight > 0) {
        RNRgb color = sum / weight;
        image.SetPixelRGB(ix, iy, color);
      }
    }
  }

  // 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 $0xd8, %rsp
movl %edx, %r14d
movq %rsi, %rbx
testl %edx, %edx
sete %sil
testl %ecx, %ecx
sete %dl
xorl %eax, %eax
orb %sil, %dl
jne 0x222ca
movq %rdi, %r15
movl %ecx, %ebp
movl (%rdi), %ecx
testl %ecx, %ecx
je 0x222ca
movl 0x4(%r15), %edx
testl %edx, %edx
je 0x222ca
movd %ecx, %xmm0
movd %edx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2pd %xmm1, %xmm2
movd %r14d, %xmm0
movd %ebp, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2pd %xmm1, %xmm0
divpd %xmm0, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
movapd %xmm2, 0x40(%rsp)
movapd %xmm2, %xmm0
movapd %xmm1, 0x50(%rsp)
divsd %xmm1, %xmm0
movsd 0x8604c(%rip), %xmm1    # 0xa8008
addsd %xmm0, %xmm1
andpd 0x86548(%rip), %xmm1    # 0xa8510
ucomisd 0x87008(%rip), %xmm1    # 0xa8fd8
jbe 0x21fe0
leaq 0x871c7(%rip), %rdi     # 0xa91a0
movb $0x1, %al
callq 0x6e732
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0x673c2
leaq 0x28(%rsp), %rdi
pushq $0x3
popq %rcx
movl %r14d, %esi
movl %ebp, %edx
callq 0x671d2
movq %rbx, 0x60(%rsp)
leaq 0x28(%rsp), %rsi
movq %r15, 0x78(%rsp)
movq %r15, %rdi
callq 0x674b2
leaq 0x28(%rsp), %rdi
callq 0x67414
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
testl %ebp, %ebp
cmovlel %eax, %ebp
movl %ebp, 0x10(%rsp)
movsd 0x86f93(%rip), %xmm1    # 0xa8fd0
movapd 0x40(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rsp)
movsd 0x85fc3(%rip), %xmm1    # 0xa8018
mulsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x86f69(%rip), %xmm2    # 0xa8fd0
movapd 0x50(%rsp), %xmm0
mulsd %xmm0, %xmm2
movsd %xmm2, 0x88(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0xc0(%rsp), %r13
leaq 0xd847a(%rip), %rbp     # 0xfa510
xorl %eax, %eax
movl %r14d, 0x14(%rsp)
cmpl 0x10(%rsp), %eax
je 0x2227d
movl %eax, 0x4(%rsp)
cvtsi2sd %eax, %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x85f51(%rip), %xmm1    # 0xa8018
cvttsd2si %xmm1, %ecx
movl 0x94(%rsp), %eax
cmpl %ecx, %eax
jle 0x22272
movl %ecx, %edx
addsd 0x68(%rsp), %xmm0
addsd 0x85f2e(%rip), %xmm0    # 0xa8018
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x22272
xorl %ebx, %ebx
testl %edx, %edx
cmovlel %ebx, %edx
leal -0x1(%rax), %esi
cmpl %ecx, %eax
cmovgl %ecx, %esi
cmpl %edx, %esi
movl %edx, 0x18(%rsp)
cmovlel %edx, %esi
movl %esi, 0x24(%rsp)
cmpl %r14d, %ebx
je 0x22272
cvtsi2sd %ebx, %xmm0
mulsd 0x50(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x85ede(%rip), %xmm1    # 0xa8018
cvttsd2si %xmm1, %ecx
movl 0x90(%rsp), %eax
cmpl %ecx, %eax
jle 0x2226b
movl %ecx, %edx
addsd 0x80(%rsp), %xmm0
addsd 0x85eb8(%rip), %xmm0    # 0xa8018
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x2226b
movl %ebx, 0x1c(%rsp)
movl %edx, %esi
testl %edx, %edx
movl $0x0, %edx
cmovlel %edx, %esi
leal -0x1(%rax), %r15d
cmpl %ecx, %eax
cmovgl %ecx, %r15d
cmpl %esi, %r15d
movl %esi, 0x20(%rsp)
cmovlel %esi, %r15d
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x28(%rsp), %rdi
callq 0x6bf32
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x18(%rsp), %r14d
cmpl 0x24(%rsp), %r14d
ja 0x22232
movl 0x20(%rsp), %r12d
cmpl %r15d, %r12d
ja 0x2222d
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x67422
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6bf60
testl %eax, %eax
jne 0x22228
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movsd 0x85e26(%rip), %xmm0    # 0xa8028
callq 0x1569f
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x6bfe2
movsd 0x8(%rsp), %xmm0
addsd 0x85e06(%rip), %xmm0    # 0xa8028
movsd %xmm0, 0x8(%rsp)
incl %r12d
jmp 0x221c2
incl %r14d
jmp 0x221b6
movsd 0x8(%rsp), %xmm0
ucomisd 0x85df8(%rip), %xmm0    # 0xa8038
movl 0x14(%rsp), %r14d
movl 0x1c(%rsp), %ebx
jbe 0x2226b
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x1564c
movq 0x78(%rsp), %rdi
movl %ebx, %esi
movl 0x4(%rsp), %edx
movq %r13, %rcx
callq 0x67560
incl %ebx
jmp 0x22112
movl 0x4(%rsp), %eax
incl %eax
jmp 0x2209d
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x10(%rax), %xmm1
movapd 0x50(%rsp), %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, (%rax)
divsd %xmm2, %xmm1
movsd %xmm1, 0x10(%rax)
movupd 0x20(%rax), %xmm0
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
divpd %xmm1, %xmm0
movupd %xmm0, 0x20(%rax)
leaq 0x90(%rsp), %rdi
callq 0x67414
pushq $0x1
popq %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x67414
jmp 0x222f6
jmp 0x222f3
jmp 0x222f3
jmp 0x222f3
jmp 0x222f3
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x67414
movq %rbx, %rdi
callq 0x11a80
nop