// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::Resize(int, int, int)
void R2Image::
Resize(int w, int h, int c)
{
  // Check if empty size
  if ((w == 0) || (h == 0) || (c == 0)) {
    if (pixels) delete [] pixels;
    pixels = NULL;
    width = 0;
    height = 0;
    ncomponents = 0;
    rowsize = 0;
  }
  else {
    // Create output image
    R2Image output(w, h, c);
    if ((width > 0) && (height > 0)) {
      // Compute scale factors
      RNScalar xfactor = (RNScalar) width / (RNScalar) w;
      RNScalar yfactor = (RNScalar) height / (RNScalar) h;

      // Resample original image
      for (int ix = 0; ix < w; ix++) {
        for (int iy = 0; iy < h; iy++) {
          double x = (ix+0.5) * xfactor;
          double y = (iy+0.5) * yfactor;
          if ((x < 0) || (x >= width)) continue;
          if ((y < 0) || (y >= height)) continue;

          // Bilinear interpolation
          int ix1 = (int) x;
          int iy1 = (int) y;
          int ix2 = ix1 + 1;
          int iy2 = iy1 + 1;
          if (ix2 >= width) ix2 = ix1;
          if (iy2 >= height) iy2 = iy1;
          RNScalar dx = x - ix1;
          RNScalar dy = y - iy1;
          RNRgb color11 = PixelRGB(ix1, iy1);
          RNRgb color12 = PixelRGB(ix1, iy2);
          RNRgb color21 = PixelRGB(ix2, iy1);
          RNRgb color22 = PixelRGB(ix2, iy2);
          RNScalar weight11 = (1.0-dx) * (1.0-dy);
          RNScalar weight12 = (1.0-dx) * dy;
          RNScalar weight21 = dx * (1.0-dy);
          RNScalar weight22 = dx * dy;
          RNRgb color(0,0,0);
          RNScalar weight = 0;
          color += weight11 * color11; weight += weight11; 
          color += weight12 * color12; weight += weight12;
          color += weight21 * color21; weight += weight21;
          color += weight22 * color22; weight += weight22;
          if (weight == 0) output.SetPixelRGB(ix, iy, RNblack_rgb);
          else output.SetPixelRGB(ix, iy, color / weight);
        }
      }
    }
    
    // Copy output to this image
    *this = output;
  }
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1b0(%rsp)
movl %esi, 0x1ac(%rsp)
movl %edx, 0x1a8(%rsp)
movl %ecx, 0x1a4(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpl $0x0, 0x1ac(%rsp)
je 0xc5d7f
cmpl $0x0, 0x1a8(%rsp)
je 0xc5d7f
cmpl $0x0, 0x1a4(%rsp)
jne 0xc5dd8
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc5dab
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xc5da9
movq 0x8(%rsp), %rdi
callq 0xf670
jmp 0xc5dab
movq 0x10(%rsp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, (%rax)
movl $0x0, 0x4(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, 0xc(%rax)
jmp 0xc6378
movl 0x1ac(%rsp), %esi
movl 0x1a8(%rsp), %edx
movl 0x1a4(%rsp), %ecx
leaq 0x188(%rsp), %rdi
callq 0xc5000
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
jle 0xc6357
movq 0x10(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jle 0xc6357
movq 0x10(%rsp), %rax
cvtsi2sdl (%rax), %xmm0
cvtsi2sdl 0x1ac(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
cvtsi2sdl 0x4(%rax), %xmm0
cvtsi2sdl 0x1a8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movl 0x174(%rsp), %eax
cmpl 0x1ac(%rsp), %eax
jge 0xc6355
movl $0x0, 0x170(%rsp)
movl 0x170(%rsp), %eax
cmpl 0x1a8(%rsp), %eax
jge 0xc633d
cvtsi2sdl 0x174(%rsp), %xmm0
movsd 0x6d170(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
mulsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
cvtsi2sdl 0x170(%rsp), %xmm0
movsd 0x6d149(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
mulsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x168(%rsp), %xmm0
ja 0xc5f03
movq 0x10(%rsp), %rax
movsd 0x168(%rsp), %xmm0
cvtsi2sdl (%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0xc5f08
jmp 0xc6327
xorps %xmm0, %xmm0
ucomisd 0x160(%rsp), %xmm0
ja 0xc5f2f
movq 0x10(%rsp), %rax
movsd 0x160(%rsp), %xmm0
cvtsi2sdl 0x4(%rax), %xmm1
ucomisd %xmm1, %xmm0
jb 0xc5f34
jmp 0xc6327
movq 0x10(%rsp), %rcx
cvttsd2si 0x168(%rsp), %eax
movl %eax, 0x15c(%rsp)
cvttsd2si 0x160(%rsp), %eax
movl %eax, 0x158(%rsp)
movl 0x15c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
movl 0x158(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x150(%rsp)
movl 0x154(%rsp), %eax
cmpl (%rcx), %eax
jl 0xc5f94
movl 0x15c(%rsp), %eax
movl %eax, 0x154(%rsp)
movq 0x10(%rsp), %rcx
movl 0x150(%rsp), %eax
cmpl 0x4(%rcx), %eax
jl 0xc5fb3
movl 0x158(%rsp), %eax
movl %eax, 0x150(%rsp)
movq 0x10(%rsp), %rsi
movsd 0x168(%rsp), %xmm0
cvtsi2sdl 0x15c(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x148(%rsp)
movsd 0x160(%rsp), %xmm0
cvtsi2sdl 0x158(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x140(%rsp)
movl 0x15c(%rsp), %edx
movl 0x158(%rsp), %ecx
leaq 0x128(%rsp), %rdi
callq 0xc5560
jmp 0xc6013
movq 0x10(%rsp), %rsi
movl 0x15c(%rsp), %edx
movl 0x150(%rsp), %ecx
leaq 0x100(%rsp), %rdi
callq 0xc5560
jmp 0xc6035
movq 0x10(%rsp), %rsi
movl 0x154(%rsp), %edx
movl 0x158(%rsp), %ecx
leaq 0xe8(%rsp), %rdi
callq 0xc5560
jmp 0xc6057
movq 0x10(%rsp), %rsi
movl 0x154(%rsp), %edx
movl 0x150(%rsp), %ecx
leaq 0xd0(%rsp), %rdi
callq 0xc5560
jmp 0xc6079
movsd 0x148(%rsp), %xmm2
movsd 0x6cf7e(%rip), %xmm1    # 0x133008
movaps %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x140(%rsp), %xmm3
movaps %xmm1, %xmm2
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0x148(%rsp), %xmm2
movaps %xmm1, %xmm0
subsd %xmm2, %xmm0
movsd 0x140(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x148(%rsp), %xmm0
movsd 0x140(%rsp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0x148(%rsp), %xmm0
movsd 0x140(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
leaq 0x98(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xd14d0
jmp 0xc612e
movq $0x0, 0x90(%rsp)
movsd 0xc8(%rsp), %xmm0
leaq 0x78(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x37950
jmp 0xc6157
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xd1690
jmp 0xc616b
movsd 0xc8(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xc0(%rsp), %xmm0
leaq 0x60(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x37950
jmp 0xc61a7
leaq 0x98(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xd1690
jmp 0xc61bb
movsd 0xc0(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x48(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x37950
jmp 0xc61f7
leaq 0x98(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xd1690
jmp 0xc620b
movsd 0xb8(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xb0(%rsp), %xmm0
leaq 0x30(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x37950
jmp 0xc6247
leaq 0x98(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xd1690
jmp 0xc625b
movsd 0xb0(%rsp), %xmm0
addsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xc62d6
jp 0xc62d6
movl 0x174(%rsp), %esi
movl 0x170(%rsp), %edx
leaq 0x4b50b9(%rip), %rcx    # 0x57b358
leaq 0x188(%rsp), %rdi
callq 0xc5850
jmp 0xc62ae
jmp 0xc6325
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0x188(%rsp), %rdi
callq 0xc5520
jmp 0xc6380
movl 0x174(%rsp), %eax
movl %eax, (%rsp)
movl 0x170(%rsp), %eax
movl %eax, 0x4(%rsp)
movsd 0x90(%rsp), %xmm0
leaq 0x18(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x371a0
jmp 0xc6308
movl 0x4(%rsp), %edx
movl (%rsp), %esi
leaq 0x188(%rsp), %rdi
leaq 0x18(%rsp), %rcx
callq 0xc5850
jmp 0xc6323
jmp 0xc6325
jmp 0xc6327
movl 0x170(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x170(%rsp)
jmp 0xc5e7b
jmp 0xc633f
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0xc5e5c
jmp 0xc6357
movq 0x10(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0xc56d0
jmp 0xc636b
leaq 0x188(%rsp), %rdi
callq 0xc5520
addq $0x1b8, %rsp            # imm = 0x1B8
retq
movq 0x120(%rsp), %rdi
callq 0xf890
nopl (%rax)