// File: matthewarcus[P]mmps/transform.cpp
// Function: Transform::SetDataInv(Image const&, Image&, TransformParams const&, double, double, double, double, unsigned int)
bool Transform::SetDataInv(const Image& inImage, Image& outImage,
			   const TransformParams& params,
			   double ox, double oy, // Coordinates in output image
			   double x, double y,   // scaled coordinates in input image
			   unsigned int outIndex)	     
{
  (void(ox));
  (void(oy));
  int iw = inImage.Width();
  int ih = inImage.Height();
  double orgx = 0.5 * iw;
  double orgy = 0.5 * ih;

  // Scale so that half width is 1
  double scaleFactor = BasicScale(iw,ih) / params.scale;

  // This is what we are inverting
  //double x = scaleFactor * (ix - orgx) + params.xoff;
  //double y = scaleFactor * (orgy - iy) + params.yoff;
  //applyRotation(-params.rotate,x,y)
  
  if (params.rotate != 0) {
    applyRotation(params.rotate,x,y);
  }
  int ix = int(floor(orgx + (x - params.xoff) / scaleFactor));
  int iy = int(floor(orgy - (y - params.yoff) / scaleFactor));

  if (ix < 0 || ix >= iw || iy < 0 || iy >= ih) {
    return false;
  } else {
    Rgb rgb;
    unsigned int inIndex = iy * iw + ix;
    inImage.GetRgb(inIndex, rgb);
    outImage.SetRgb(outIndex, rgb);
    return true;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x38(%rbp)
movsd %xmm2, -0x40(%rbp)
movsd %xmm3, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
callq 0x74e0
movl %eax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
callq 0x7500
movq -0x88(%rbp), %rdi
movl %eax, -0x54(%rbp)
cvtsi2sdl -0x50(%rbp), %xmm1
movsd 0xc07e(%rip), %xmm0     # 0x15408
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
cvtsi2sdl -0x54(%rbp), %xmm1
movsd 0xc068(%rip), %xmm0     # 0x15408
mulsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x28(%rbp), %rax
divsd 0x28(%rax), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x28(%rbp), %rax
movsd 0x10(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x93d9
jp 0x93d9
jmp 0x93ef
movq -0x28(%rbp), %rax
movsd 0x10(%rax), %xmm0
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x8d30
movsd -0x60(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movq -0x28(%rbp), %rax
subsd 0xb0(%rax), %xmm1
divsd -0x70(%rbp), %xmm1
addsd %xmm1, %xmm0
callq 0x5400
cvttsd2si %xmm0, %eax
movl %eax, -0x74(%rbp)
movsd -0x68(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movq -0x28(%rbp), %rax
subsd 0xb8(%rax), %xmm1
divsd -0x70(%rbp), %xmm1
subsd %xmm1, %xmm0
callq 0x5400
cvttsd2si %xmm0, %eax
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x74(%rbp)
jl 0x9461
movl -0x74(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x9461
cmpl $0x0, -0x78(%rbp)
jl 0x9461
movl -0x78(%rbp), %eax
cmpl -0x54(%rbp), %eax
jl 0x9467
movb $0x0, -0x1(%rbp)
jmp 0x94ab
leaq -0x7b(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x74b0
movl -0x78(%rbp), %eax
imull -0x50(%rbp), %eax
addl -0x74(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x7b(%rbp), %rdx
callq 0x7520
movq -0x20(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, %esi
leaq -0x7b(%rbp), %rdx
callq 0x7590
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)