// File: /workspace/github_cpp_makefile_star2_O2/wcochran[P]pano_to_cube/pano2cube.cpp
// Function: cylindricalPanoramaToCube(PPM const&, PPM&)
void cylindricalPanoramaToCube(const PPM& inImage, PPM& outImage) {
  for (int row = 0; row < outImage.H; row++) {
    for (int col = 0; col < outImage.W; col++) {
      const double s = 4.0*col/outImage.W;
      const int face = static_cast<int>(floorf(s));
      const double f = s - face;
      float u,v,w;
      switch(face) {
      case 0:
	u = 1;
	v = 2*f - 1;
	break;
      case 1:
	u = 1 - 2*f;
	v = 1;
	break;
      case 2:
	u = -1;
	v = 1 - 2*f;
	break;
      default:
	u = 2*f - 1;
	v = -1;
      }
      w = 2.0*row/outImage.H - 1;
      const float a = 1/sqrtf(u*u + v*v);
      const float x = a*u;
      const float y = a*v;
      const float z = a*w;
      float theta = atan2(y,x) + M_PI/4;
      if (theta < 0) theta += 2*M_PI;
      const float r = inImage.H*(z + 1)/2.0;
      const float c = inImage.W*theta/(2*M_PI);
      float inpix[3];
      inImage.bilerp(r,c,inpix);
      unsigned char *outpix = outImage(row,col);
      for (int i = 0; i < 3; i++)
	outpix[i] = static_cast<unsigned char>(inpix[i] + 0.5);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorl %r12d, %r12d
leaq 0x14(%rsp), %r15
cmpl 0x4(%rbx), %r12d
jge 0x2612
leal (%r12,%r12), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
movl (%rbx), %eax
cmpl %eax, %ebp
jge 0x260a
cvtsi2sd %ebp, %xmm1
cvtsi2sd %eax, %xmm0
mulsd 0x1bad(%rip), %xmm1     # 0x4008
divsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
cvtsd2ss %xmm1, %xmm0
callq 0x2080
movsd (%rsp), %xmm1
cvttss2si %xmm0, %eax
cvttps2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm1
addsd %xmm1, %xmm1
cmpl $0x2, %eax
je 0x24c6
cmpl $0x1, %eax
movsd 0x1b82(%rip), %xmm4     # 0x4018
movss 0x1b9e(%rip), %xmm5     # 0x403c
je 0x24b1
testl %eax, %eax
jne 0x24f0
addsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm0
movaps %xmm5, %xmm1
jmp 0x2500
movsd 0x1b57(%rip), %xmm0     # 0x4010
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm1
movaps %xmm5, %xmm0
jmp 0x2500
movsd 0x1b42(%rip), %xmm0     # 0x4010
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss 0x1b5a(%rip), %xmm1     # 0x4038
movsd 0x1b32(%rip), %xmm4     # 0x4018
movss 0x1b4e(%rip), %xmm5     # 0x403c
jmp 0x2500
addsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm1
movss 0x1b38(%rip), %xmm0     # 0x4038
cvtsi2sdl 0x4(%rbx), %xmm2
movsd 0x8(%rsp), %xmm3
divsd %xmm2, %xmm3
addsd %xmm4, %xmm3
cvtsd2ss %xmm3, %xmm2
movaps %xmm0, %xmm3
mulss %xmm0, %xmm3
movaps %xmm1, %xmm4
mulss %xmm1, %xmm4
addss %xmm3, %xmm4
sqrtss %xmm4, %xmm3
movaps %xmm5, %xmm4
divss %xmm3, %xmm4
mulss %xmm4, %xmm1
mulss %xmm4, %xmm0
mulss %xmm2, %xmm4
movss %xmm4, (%rsp)
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
callq 0x2040
addsd 0x1ac6(%rip), %xmm0     # 0x4020
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x1abe(%rip), %xmm4     # 0x4028
addsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm0, %xmm2
cmpltss 0x1ac6(%rip), %xmm2     # 0x4044
movaps %xmm2, %xmm3
andnps %xmm0, %xmm3
andps %xmm1, %xmm2
cvtsi2ssl 0x4(%r14), %xmm0
orps %xmm3, %xmm2
movss (%rsp), %xmm1
addss 0x1a9f(%rip), %xmm1     # 0x403c
mulss %xmm0, %xmm1
mulss 0x1a97(%rip), %xmm1     # 0x4040
movaps %xmm1, %xmm0
cvtsi2ssl (%r14), %xmm1
mulss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
divsd %xmm4, %xmm1
cvtsd2ss %xmm1, %xmm1
movq %r14, %rdi
movq %r15, %rsi
callq 0x307c
movl (%rbx), %eax
imull %r12d, %eax
addl %ebp, %eax
leal (%rax,%rax,2), %eax
cltq
addq 0x8(%rbx), %rax
xorl %ecx, %ecx
movsd 0x1a49(%rip), %xmm1     # 0x4030
cmpq $0x3, %rcx
je 0x2603
cvtss2sd 0x14(%rsp,%rcx,4), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
movb %dl, (%rax,%rcx)
incq %rcx
jmp 0x25e7
incl %ebp
jmp 0x2441
incl %r12d
jmp 0x2426
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq