// File: 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:
subq $0x188, %rsp            # imm = 0x188
movq %rdi, 0x178(%rsp)
movq %rsi, 0x170(%rsp)
movl %edx, 0x16c(%rsp)
movl %ecx, 0x168(%rsp)
cmpl $0x0, 0x16c(%rsp)
je 0x35189
cmpl $0x0, 0x168(%rsp)
jne 0x35199
movl $0x0, 0x184(%rsp)
jmp 0x358c5
movq 0x178(%rsp), %rdi
callq 0x22940
cmpl $0x0, %eax
je 0x351bd
movq 0x178(%rsp), %rdi
callq 0x22950
cmpl $0x0, %eax
jne 0x351cd
movl $0x0, 0x184(%rsp)
jmp 0x358c5
movq 0x178(%rsp), %rdi
callq 0x22940
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x16c(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x160(%rsp)
movq 0x178(%rsp), %rdi
callq 0x22950
cvtsi2sd %eax, %xmm0
cvtsi2sdl 0x168(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movsd 0x158(%rsp), %xmm0
divsd 0x160(%rsp), %xmm0
movsd 0x130dfb(%rip), %xmm1   # 0x166030
movsd 0x131dcb(%rip), %xmm2   # 0x167008
callq 0x35a60
cmpl $0x0, %eax
je 0x35267
movsd 0x158(%rsp), %xmm0
divsd 0x160(%rsp), %xmm0
leaq 0x131f60(%rip), %rdi    # 0x1671c0
movb $0x1, %al
callq 0xdf990
movq 0x178(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0xd2090
movl 0x16c(%rsp), %esi
movl 0x168(%rsp), %edx
leaq 0x128(%rsp), %rdi
movl $0x3, %ecx
callq 0xd1c40
jmp 0x3529e
movq 0x178(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xd2310
jmp 0x352b5
leaq 0x128(%rsp), %rdi
callq 0xd2160
movl $0x0, 0x118(%rsp)
movl 0x118(%rsp), %eax
cmpl 0x168(%rsp), %eax
jge 0x357a0
movsd 0x158(%rsp), %xmm0
cvtsi2sdl 0x118(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm1
movsd 0x158(%rsp), %xmm0
movsd 0x131246(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x130cfe(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x22950
movl %eax, 0x74(%rsp)
jmp 0x35357
movl 0x70(%rsp), %eax
movl 0x74(%rsp), %ecx
cmpl %ecx, %eax
jl 0x353a7
jmp 0x3578a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0x358b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0xd2160
jmp 0x358b6
movsd 0x110(%rsp), %xmm1
movsd 0x158(%rsp), %xmm0
movsd 0x130c67(%rip), %xmm2   # 0x166028
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x130c57(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x108(%rsp)
cmpl $0x0, 0x108(%rsp)
jge 0x353ef
jmp 0x3578a
cmpl $0x0, 0x10c(%rsp)
jge 0x35404
movl $0x0, 0x10c(%rsp)
movl 0x108(%rsp), %eax
movl %eax, 0x68(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x22950
movl %eax, 0x6c(%rsp)
jmp 0x35422
movl 0x68(%rsp), %eax
movl 0x6c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x3544f
leaq 0x140(%rsp), %rdi
callq 0x22950
movl %eax, 0x64(%rsp)
jmp 0x35441
movl 0x64(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x108(%rsp)
movl 0x108(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jge 0x3546d
movl 0x10c(%rsp), %eax
movl %eax, 0x108(%rsp)
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x35788
movsd 0x160(%rsp), %xmm0
cvtsi2sdl 0x104(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0xf8(%rsp), %xmm1
movsd 0x160(%rsp), %xmm0
movsd 0x13109b(%rip), %xmm2   # 0x166560
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x130b53(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, 0x5c(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x22940
movl %eax, 0x60(%rsp)
jmp 0x35502
movl 0x5c(%rsp), %eax
movl 0x60(%rsp), %ecx
cmpl %ecx, %eax
jl 0x35513
jmp 0x35772
movsd 0xf8(%rsp), %xmm1
movsd 0x160(%rsp), %xmm0
movsd 0x130afb(%rip), %xmm2   # 0x166028
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x130aeb(%rip), %xmm1   # 0x166028
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xf0(%rsp)
cmpl $0x0, 0xf0(%rsp)
jge 0x3555b
jmp 0x35772
cmpl $0x0, 0xf4(%rsp)
jge 0x35570
movl $0x0, 0xf4(%rsp)
movl 0xf0(%rsp), %eax
movl %eax, 0x54(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x22940
movl %eax, 0x58(%rsp)
jmp 0x3558e
movl 0x54(%rsp), %eax
movl 0x58(%rsp), %ecx
cmpl %ecx, %eax
jl 0x355bb
leaq 0x140(%rsp), %rdi
callq 0x22940
movl %eax, 0x50(%rsp)
jmp 0x355ad
movl 0x50(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xf0(%rsp)
movl 0xf0(%rsp), %eax
cmpl 0xf4(%rsp), %eax
jge 0x355d9
movl 0xf4(%rsp), %eax
movl %eax, 0xf0(%rsp)
leaq 0xd8(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xdb1d0
jmp 0x355f1
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0x10c(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0x108(%rsp), %eax
jg 0x3571d
movl 0xf4(%rsp), %eax
movl %eax, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jg 0x35705
movl 0xc8(%rsp), %edx
movl 0xcc(%rsp), %ecx
leaq 0xb0(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0xd21a0
jmp 0x35666
leaq 0x17f363(%rip), %rsi    # 0x1b49d0
leaq 0xb0(%rsp), %rdi
callq 0xdb250
movl %eax, 0x4c(%rsp)
jmp 0x35680
movl 0x4c(%rsp), %eax
cmpl $0x0, %eax
je 0x3568b
jmp 0x356ef
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
leaq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1a0e0
jmp 0x356bd
leaq 0xd8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xdb390
jmp 0x356d4
movsd 0xa8(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x3562d
jmp 0x35707
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x3560b
movsd 0xd0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x35770
movsd 0xd0(%rsp), %xmm0
leaq 0x78(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x1a120
jmp 0x3574c
movq 0x178(%rsp), %rdi
movl 0x104(%rsp), %esi
movl 0x118(%rsp), %edx
leaq 0x78(%rsp), %rcx
callq 0xd2490
jmp 0x3576e
jmp 0x35770
jmp 0x35772
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x35478
jmp 0x3578a
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x352cd
movsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x40(%rsp)
jmp 0x357c5
movq 0x40(%rsp), %rax
movsd 0x38(%rsp), %xmm1
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x170(%rsp), %rdi
xorl %esi, %esi
callq 0x226f0
movq %rax, 0x30(%rsp)
jmp 0x35801
movq 0x30(%rsp), %rax
movsd 0x28(%rsp), %xmm1
movsd 0x10(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x170(%rsp), %rdi
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x20(%rsp)
jmp 0x35842
movq 0x20(%rsp), %rax
movsd 0x18(%rsp), %xmm1
movsd 0x8(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x170(%rsp), %rdi
movl $0x1, %esi
callq 0x226f0
movq %rax, 0x10(%rsp)
jmp 0x35883
movq 0x10(%rsp), %rax
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movl $0x1, 0x184(%rsp)
leaq 0x140(%rsp), %rdi
callq 0xd2160
jmp 0x358c5
leaq 0x140(%rsp), %rdi
callq 0xd2160
jmp 0x358d4
movl 0x184(%rsp), %eax
addq $0x188, %rsp            # imm = 0x188
retq
movq 0x120(%rsp), %rdi
callq 0x12c20
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)