// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movl %edx, %ebp
movq %rsi, %rbx
testl %edx, %edx
sete %sil
movl %ecx, (%rsp)
testl %ecx, %ecx
sete %dl
xorl %eax, %eax
orb %sil, %dl
jne 0x23d14
movq %rdi, %r14
movl (%rdi), %ecx
testl %ecx, %ecx
je 0x23d14
movl 0x4(%r14), %edx
testl %edx, %edx
je 0x23d14
movd %ecx, %xmm0
movd %edx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
cvtdq2pd %xmm1, %xmm2
movd %ebp, %xmm0
movd (%rsp), %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 0x976b4(%rip), %xmm1    # 0xbb038
addsd %xmm0, %xmm1
andpd 0x97c00(%rip), %xmm1    # 0xbb590
ucomisd 0x986c0(%rip), %xmm1    # 0xbc058
jbe 0x239a8
leaq 0x9887f(%rip), %rdi     # 0xbc220
movb $0x1, %al
callq 0x77f42
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x6fe96
leaq 0x28(%rsp), %rdi
movl %ebp, %esi
movl (%rsp), %edx
movl $0x3, %ecx
callq 0x6fc8a
movq %rbx, 0x68(%rsp)
leaq 0x28(%rsp), %rsi
movq %r14, 0x78(%rsp)
movq %r14, %rdi
callq 0x6ff82
leaq 0x28(%rsp), %rdi
callq 0x6fee4
cmpl $0x0, (%rsp)
jle 0x23cc5
movsd 0x98651(%rip), %xmm1    # 0xbc050
movapd 0x40(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movsd 0x97606(%rip), %xmm1    # 0xbb020
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x98624(%rip), %xmm2    # 0xbc050
movapd 0x50(%rsp), %xmm0
mulsd %xmm0, %xmm2
movsd %xmm2, 0x90(%rsp)
mulsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
leaq 0xb8(%rsp), %r13
leaq 0xa0(%rsp), %rbx
leaq 0xe3a8d(%rip), %r15     # 0x1074f0
xorl %eax, %eax
movl %ebp, 0x1c(%rsp)
movl %eax, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x40(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x97591(%rip), %xmm1    # 0xbb020
cvttsd2si %xmm1, %ecx
movl 0xa4(%rsp), %eax
cmpl %ecx, %eax
jle 0x23cb6
movl %ecx, %edx
addsd 0x70(%rsp), %xmm0
addsd 0x9756e(%rip), %xmm0    # 0xbb020
cvttsd2si %xmm0, %ecx
testl %ecx, %ecx
js 0x23cb6
movl %edx, %esi
testl %edx, %edx
movl $0x0, %edx
cmovlel %edx, %esi
movl %esi, 0x4(%rsp)
testl %ebp, %ebp
jle 0x23cb6
leal -0x1(%rax), %edx
cmpl %ecx, %eax
cmovgl %ecx, %edx
movl 0x4(%rsp), %eax
cmpl %eax, %edx
cmovlel %eax, %edx
cmpl %eax, %edx
cmovbel %eax, %edx
movl %edx, 0x20(%rsp)
xorl %eax, %eax
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x50(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x97508(%rip), %xmm1    # 0xbb020
cvttsd2si %xmm1, %ecx
movl 0xa0(%rsp), %r14d
cmpl %ecx, %r14d
jle 0x23ca8
addsd 0x88(%rsp), %xmm0
addsd 0x974e2(%rip), %xmm0    # 0xbb020
cvttsd2si %xmm0, %r12d
testl %r12d, %r12d
js 0x23ca8
movl %ecx, %eax
movl %ecx, %ebp
testl %eax, %eax
movl $0x0, %eax
cmovlel %eax, %ebp
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
leaq 0x28(%rsp), %rdi
callq 0x75244
leal -0x1(%r14), %ecx
cmpl %r12d, %r14d
cmovgl %r12d, %ecx
cmpl %ebp, %ecx
cmovlel %ebp, %ecx
cmpl %ebp, %ecx
cmovbel %ebp, %ecx
incl %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x4(%rsp), %eax
movl %eax, %r14d
movl %ebp, %r12d
movl %ecx, %ebp
movl %r12d, 0x24(%rsp)
movq %r14, 0x98(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movl %r14d, %ecx
callq 0x6fef2
movq %r13, %rdi
movq %r15, %rsi
callq 0x75272
testl %eax, %eax
jne 0x23c36
movl %ebp, %r14d
movq %r13, %rbp
movq %r15, %r13
movq %rbx, %r15
movsd 0xb8(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
movsd 0xc8(%rsp), %xmm2
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
callq 0x75244
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x752f6
movsd 0x10(%rsp), %xmm0
addsd 0x9740c(%rip), %xmm0    # 0xbb028
movsd %xmm0, 0x10(%rsp)
movq %r15, %rbx
movq %r13, %r15
movq %rbp, %r13
movl %r14d, %ebp
movq 0x98(%rsp), %r14
incl %r12d
cmpl %r12d, %ebp
jne 0x23baa
leal 0x1(%r14), %eax
cmpl 0x20(%rsp), %r14d
movl %eax, %r14d
movl 0x24(%rsp), %r12d
jne 0x23ba2
movsd 0x10(%rsp), %xmm3
ucomisd 0x973a9(%rip), %xmm3    # 0xbb010
movl 0x1c(%rsp), %ebp
jbe 0x23ca8
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
divsd %xmm3, %xmm0
divsd %xmm3, %xmm1
movsd 0x38(%rsp), %xmm2
divsd %xmm3, %xmm2
movq %r13, %rdi
callq 0x75244
movl 0xc(%rsp), %esi
movq 0x78(%rsp), %rdi
movl 0x8(%rsp), %edx
movq %r13, %rcx
callq 0x7003a
movl 0xc(%rsp), %eax
incl %eax
cmpl %ebp, %eax
jne 0x23af2
movl 0x8(%rsp), %eax
incl %eax
cmpl (%rsp), %eax
jne 0x23a69
movq 0x68(%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 0xa0(%rsp), %rdi
callq 0x6fee4
movl $0x1, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6fee4
jmp 0x23d40
jmp 0x23d3d
jmp 0x23d3d
jmp 0x23d3d
jmp 0x23d3d
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x6fee4
movq %rbx, %rdi
callq 0x11a60
nopl (%rax)