// File: /workspace/github_cpp_makefile_star2_O3/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
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd 0x709e(%rip), %xmm1     # 0xd428
mulsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd %xmm1, %xmm0
movq %rbx, 0x40(%rsp)
fildll 0x40(%rsp)
fldt 0x7131(%rip)            # 0xd4e0
fmulp %st, %st(1)
fstpl 0x30(%rsp)
movq %rbp, 0x48(%rsp)
fildll 0x48(%rsp)
fldt 0x712c(%rip)            # 0xd4f0
fmulp %st, %st(1)
fstpl 0x38(%rsp)
mulsd 0x38(%rsp), %xmm4
addsd %xmm4, %xmm0
callq 0x4310
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 0x4310
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
movzwl (%rcx,%rax), %edx
movd %edx, %xmm0
movb 0x2(%rcx,%rax), %al
cmpb $0x1, 0x78(%r12)
jne 0x64f8
movsd 0x8(%rsp), %xmm2
mulsd 0xb0(%r15), %xmm2
movsd 0x10(%rsp), %xmm1
mulsd 0xa8(%r15), %xmm1
addsd %xmm2, %xmm1
movapd 0x50(%rsp), %xmm2
mulsd 0xb8(%r15), %xmm2
addsd %xmm1, %xmm2
movsd 0x6fc0(%rip), %xmm1     # 0xd430
ucomisd %xmm2, %xmm1
jbe 0x64ae
xorpd %xmm1, %xmm1
punpcklbw %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
punpcklwd %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3]
cvtdq2pd %xmm0, %xmm0
mulpd 0x7072(%rip), %xmm0     # 0xd500
cvttpd2dq %xmm0, %xmm0
packuswb %xmm0, %xmm0
packuswb %xmm0, %xmm0
movzbl %al, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x6f7c(%rip), %xmm1     # 0xd428
jmp 0x64f4
mulsd 0x6f82(%rip), %xmm2     # 0xd438
addsd 0x6f82(%rip), %xmm2     # 0xd440
xorpd %xmm1, %xmm1
punpcklbw %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3],xmm0[4],xmm1[4],xmm0[5],xmm1[5],xmm0[6],xmm1[6],xmm0[7],xmm1[7]
punpcklwd %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1],xmm0[2],xmm1[2],xmm0[3],xmm1[3]
cvtdq2pd %xmm0, %xmm0
movapd %xmm2, %xmm1
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
mulpd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm0
packuswb %xmm0, %xmm0
movzbl %al, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
packuswb %xmm0, %xmm0
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %eax
movl 0x4(%rsp), %ecx
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rdx
addq %rcx, %rdx
addq %rcx, %rdx
movd %xmm0, %ebx
movb %bl, (%rcx,%rdx)
movb %bh, 0x1(%rcx,%rdx)
movb %al, 0x2(%rcx,%rdx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop