// File: matthewarcus[P]mmps/transform.cpp
// Function: Transform::SetData(Image const&, Image&, TransformParams const&, double, double, double, double, double, unsigned int)
void Transform::SetData(const Image& inImage, Image& outImage,
			const TransformParams& params,
			double x, double y, double z,
			double phi, double lambda,
			unsigned int outIndex)	     
{
  unsigned int iw = inImage.Width();
  unsigned int ih = inImage.Height();

  double hh = ih * 0.5;
  double hw = iw * 0.5;
  double sh = ih * oneoverpi;
  double sw = iw * oneovertwopi;

  // Use unsigned so we don't have to test for negative indices
  unsigned int sx = unsigned(floor(hw + lambda * sw));
  // Clamp in case of rounding errors
  if (sx >= iw) sx = 0;

  // Use unsigned so we don't have to test for negative indices

  unsigned int sy = unsigned(floor(hh - phi * sh));
  if (sy >= ih) sy = ih-1;

  Rgb rgb;
  unsigned int inIndex = sy * iw + sx;
  inImage.GetRgb(inIndex, rgb);
  if (params.sun) {
    // Is where we are sunny?
    // Compute pi/2 - angle of the x-axis with the normal at the relevant point.
    // This should be asin(...) but we are only interested in small angles
    // so assume sin x = x
    double sunaltitude = sunx * x + suny * y + sunz * z;
#if 1
    if (sunaltitude < sunriseangle) {
      rgb.Dim(nightdim);
    } else {
      rgb.Dim(0.8 + 0.2 * sunaltitude);
    }
#else
    rgb.Dim(0.6 + 0.5 * sunaltitude * sunaltitude);
#endif
  }
  outImage.SetRgb(outIndex, rgb);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movsd %xmm1, -0x30(%rbp)
movsd %xmm2, -0x38(%rbp)
movsd %xmm3, -0x40(%rbp)
movsd %xmm4, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x74e0
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7500
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cvtsi2sd %rax, %xmm0
movsd 0xc91d(%rip), %xmm1     # 0x15408
mulsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x54(%rbp), %eax
movq %rax, -0x98(%rbp)
fildll -0x98(%rbp)
fldt 0xc965(%rip)            # 0x15480
fmulp %st, %st(1)
fstpl -0x70(%rbp)
movl -0x50(%rbp), %eax
movq %rax, -0xa0(%rbp)
fildll -0xa0(%rbp)
fldt 0xc95a(%rip)            # 0x15490
fmulp %st, %st(1)
fstpl -0x78(%rbp)
movsd -0x68(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
movsd -0x78(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x5400
cvttsd2si %xmm0, %rax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jb 0x8b6e
movl $0x0, -0x7c(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
callq 0x5400
cvttsd2si %xmm0, %rax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0x8ba3
movl -0x54(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x80(%rbp)
leaq -0x83(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x74b0
movl -0x80(%rbp), %eax
imull -0x50(%rbp), %eax
addl -0x7c(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %eax
movl %eax, %esi
leaq -0x83(%rbp), %rdx
callq 0x7520
movq -0x20(%rbp), %rax
testb $0x1, 0x78(%rax)
je 0x8c8b
movq -0xa8(%rbp), %rax
movsd 0xa8(%rax), %xmm1
movsd -0x28(%rbp), %xmm2
movsd 0xb0(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xb8(%rax), %xmm0
movsd -0x38(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x90(%rbp), %xmm1
movsd 0x146a7(%rip), %xmm0    # 0x1d2e8
ucomisd %xmm1, %xmm0
jbe 0x8c5d
leaq -0x83(%rbp), %rdi
movsd 0xc7b2(%rip), %xmm0     # 0x15408
callq 0xddf0
jmp 0x8c89
movsd -0x90(%rbp), %xmm0
movsd 0xc7a3(%rip), %xmm1     # 0x15410
mulsd %xmm1, %xmm0
movsd 0xc79f(%rip), %xmm1     # 0x15418
addsd %xmm1, %xmm0
leaq -0x83(%rbp), %rdi
callq 0xddf0
jmp 0x8c8b
movq -0x18(%rbp), %rdi
movl -0x4c(%rbp), %eax
movl %eax, %esi
leaq -0x83(%rbp), %rdx
callq 0x7590
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)