// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x2870
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x2860
cvtsi2sdl -0x18(%rbp), %xmm0
movsd 0x3a7b(%rip), %xmm1     # 0x6008
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
cvtsi2sdl (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
callq 0x2090
cvttss2si %xmm0, %eax
movl %eax, -0x24(%rbp)
movsd -0x20(%rbp), %xmm0
cvtsi2sdl -0x24(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x78(%rbp)
testl %eax, %eax
je 0x25ed
jmp 0x25d6
movl -0x78(%rbp), %eax
subl $0x1, %eax
je 0x261d
jmp 0x25e0
movl -0x78(%rbp), %eax
subl $0x2, %eax
je 0x264a
jmp 0x2677
movss 0x3a4f(%rip), %xmm0     # 0x6044
movss %xmm0, -0x34(%rbp)
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd 0x3a0d(%rip), %xmm1     # 0x6018
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0x26a2
movsd -0x30(%rbp), %xmm1
addsd %xmm1, %xmm1
movsd 0x39e2(%rip), %xmm0     # 0x6010
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movss 0x3a01(%rip), %xmm0     # 0x6044
movss %xmm0, -0x38(%rbp)
jmp 0x26a2
movss 0x39ee(%rip), %xmm0     # 0x6040
movss %xmm0, -0x34(%rbp)
movsd -0x30(%rbp), %xmm1
addsd %xmm1, %xmm1
movsd 0x39a8(%rip), %xmm0     # 0x6010
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0x26a2
movsd -0x30(%rbp), %xmm0
addsd %xmm0, %xmm0
movsd 0x3990(%rip), %xmm1     # 0x6018
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movss 0x39a3(%rip), %xmm0     # 0x6040
movss %xmm0, -0x38(%rbp)
cvtsi2sdl -0x14(%rbp), %xmm1
movsd 0x3979(%rip), %xmm0     # 0x6028
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm1
divsd %xmm1, %xmm0
movsd 0x3948(%rip), %xmm1     # 0x6010
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
movss -0x34(%rbp), %xmm0
movss -0x34(%rbp), %xmm2
movss -0x38(%rbp), %xmm1
mulss -0x38(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
callq 0x2370
movaps %xmm0, %xmm1
movss 0x3943(%rip), %xmm0     # 0x6044
divss %xmm1, %xmm0
movss %xmm0, -0x40(%rbp)
movss -0x40(%rbp), %xmm0
mulss -0x34(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x40(%rbp), %xmm0
mulss -0x38(%rbp), %xmm0
movss %xmm0, -0x48(%rbp)
movss -0x40(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x48(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x44(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
callq 0x2040
movsd 0x38ca(%rip), %xmm1     # 0x6020
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x50(%rbp), %xmm0
jbe 0x278a
movss -0x50(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x38b3(%rip), %xmm1     # 0x6030
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x50(%rbp)
movq -0x8(%rbp), %rax
cvtsi2ssl 0x4(%rax), %xmm0
movss 0x38a9(%rip), %xmm1     # 0x6044
addss -0x4c(%rbp), %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x3878(%rip), %xmm1     # 0x6028
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x54(%rbp)
movq -0x8(%rbp), %rax
cvtsi2ssl (%rax), %xmm0
mulss -0x50(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x385a(%rip), %xmm1     # 0x6030
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movss -0x54(%rbp), %xmm0
movss -0x58(%rbp), %xmm1
leaq -0x64(%rbp), %rsi
callq 0x4690
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0x2b20
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x3, -0x74(%rbp)
jge 0x2850
movslq -0x74(%rbp), %rax
movss -0x64(%rbp,%rax,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x3808(%rip), %xmm1     # 0x6038
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, %dl
movq -0x70(%rbp), %rax
movslq -0x74(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x2814
jmp 0x2852
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x2571
jmp 0x2862
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x255a
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)