// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r8d, 0x4(%rsp)
movsd %xmm3, 0x20(%rsp)
movapd %xmm2, 0x50(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rcx, %r12
movq %rdx, 0x28(%rsp)
movq %rsi, %r13
movq %rdi, %r15
movl (%rsi), %ebp
movl 0x8(%rsi), %ebx
cvtsi2sd %rbx, %xmm0
movsd 0x66cf(%rip), %xmm1     # 0xc3f0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
cvtsi2sd %rbp, %xmm0
mulsd %xmm1, %xmm0
movq %rbx, 0x40(%rsp)
fildll 0x40(%rsp)
fldt 0x675d(%rip)            # 0xc4a0
fmulp %st, %st(1)
fstpl 0x30(%rsp)
movq %rbp, 0x48(%rsp)
fildll 0x48(%rsp)
fldt 0x6758(%rip)            # 0xc4b0
fmulp %st, %st(1)
fstpl 0x38(%rsp)
mulsd 0x38(%rsp), %xmm4
addsd %xmm4, %xmm0
callq 0x4340
cvttsd2si %xmm0, %rax
xorl %r14d, %r14d
cmpl %ebp, %eax
cmovbl %eax, %r14d
movsd 0x20(%rsp), %xmm0
mulsd 0x30(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
callq 0x4340
cvttsd2si %xmm0, %rax
leal -0x1(%rbx), %ecx
cmpl %ebx, %eax
cmovbl %eax, %ecx
imull %ebp, %ecx
addl %r14d, %ecx
movq 0x10(%r13), %rax
addq %rcx, %rax
addq %rcx, %rax
movb (%rcx,%rax), %dl
movb %dl, 0x1(%rsp)
movb 0x1(%rcx,%rax), %dl
movb %dl, 0x2(%rsp)
movb 0x2(%rcx,%rax), %al
movb %al, 0x3(%rsp)
cmpb $0x1, 0x78(%r12)
jne 0x5e42
movsd 0x8(%rsp), %xmm1
mulsd 0xb0(%r15), %xmm1
movsd 0x10(%rsp), %xmm0
mulsd 0xa8(%r15), %xmm0
addsd %xmm1, %xmm0
movapd 0x50(%rsp), %xmm2
mulsd 0xb8(%r15), %xmm2
addsd %xmm0, %xmm2
movsd 0x65ed(%rip), %xmm0     # 0xc3f8
mulsd %xmm2, %xmm0
addsd 0x65e9(%rip), %xmm0     # 0xc400
cmpltsd 0x65e8(%rip), %xmm2     # 0xc408
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
movsd 0x65c0(%rip), %xmm0     # 0xc3f0
andpd %xmm2, %xmm0
orpd %xmm1, %xmm0
leaq 0x1(%rsp), %rdi
callq 0x91d2
movl 0x4(%rsp), %eax
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %rcx
addq %rax, %rcx
addq %rax, %rcx
movb 0x1(%rsp), %dl
movb %dl, (%rax,%rcx)
movb 0x2(%rsp), %dl
movb %dl, 0x1(%rax,%rcx)
movb 0x3(%rsp), %dl
movb %dl, 0x2(%rax,%rcx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop