// File: /workspace/github_cpp_makefile_star2_O2/manikandan5[P]Image-Matching-Stitching-Warping/./Homography.h
// Function: projectiveTransform(Image const&, cimg_library::CImg<double>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void projectiveTransform(const Image& I,CImg<double>& projection,const string& name)
{
	CImg<double> imageData = I.getImageData();
	
	CImg<double> transformedImage(imageData.width(),imageData.height(),imageData.depth(),3,255);
	
	projection.invert();
	
	
	double ai = projection(0,0) ,bi = projection(1,0), ci = projection(2,0), di = projection(0,1), ei = projection(1,1), 
	        fi = projection(2,1), gi = projection(0,2), hi = projection(1,2), ii = projection(2,2);
	
	// Inverse warping
	for(int i = 0;i<transformedImage.width();++i)
	{
		for(int j = 0;j<transformedImage.height();++j)
		{
			double w = (ai*i + bi*j + ci)/(gi*i + hi*j + ii);
			double h = (di*i + ei*j + fi)/(gi*i + hi*j + ii);
			
			//Bilinear Interpolation
			// Credit to wikipedia  = https://en.wikipedia.org/wiki/Bilinear_interpolation	
			int w0 = (int)floor(w);
			int h0 = (int)floor(h);
			int w1 = (int)ceil(w);
			int h1 = (int)ceil(h);
			
			
			if(w0 <0 || h0<0 || w1<0 || h1<0 || w0>=imageData.width() || w1>=imageData.width() || h0>=imageData.height() || h1>=imageData.height())
				continue;
			
			for(int k = 0;k<3;++k)
			{
				double v1 = imageData(w0,h0,0,k);
				double v2 = imageData(w1,h0,0,k);
				double v3 = imageData(w0,h1,0,k);
				double v4 = imageData(w1,h1,0,k);
				
				double Tw = (((w1 - w)/(w1-w0)) * v1) + (((w-w0)/(w1-w0)) * v2);
				double Th = (((w1 - w)/(w1-w0)) * v3) + (((w-w0)/(w1-w0)) * v4);

				double value = (((h1-h)/(h1-h0)) * Tw) + (((h-h0)/(h1-h0)) * Th);
				
				transformedImage(i,j,k)	= value;		
			}
		}
	}
	
	transformedImage.get_normalize(0,255).save(name.c_str());	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x38(%rdi), %rsi
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
callq 0x7064
movl (%r15), %esi
movl 0x4(%r15), %edx
movl 0x8(%r15), %ecx
leaq 0x80(%rsp), %rdi
pushq $0x3
popq %r8
movsd 0x4f362(%rip), %xmm0    # 0x54008
callq 0x5dbe
movq %rbx, 0x20(%rsp)
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0x5f66
movq 0x18(%r14), %rax
movl (%r14), %ecx
leal 0x1(%rcx), %edx
leal 0x2(%rcx), %esi
leal (%rcx,%rcx), %edi
movsd (%rax,%rdi,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x8(%rax,%rdi,8), %xmm0
movsd %xmm0, 0x70(%rsp)
leal 0x2(%rcx,%rcx), %edi
movsd (%rax,%rdi,8), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x80(%rsp), %ebx
movl 0x84(%rsp), %r15d
xorl %edi, %edi
testl %r15d, %r15d
movl $0x0, %r13d
cmovgl %r15d, %r13d
testl %ebx, %ebx
movl $0x0, %r8d
cmovgl %ebx, %r8d
movq %r8, 0x30(%rsp)
movsd (%rax), %xmm0
movsd 0x10(%rax), %xmm1
movhpd (%rax,%rsi,8), %xmm1    # xmm1 = xmm1[0],mem[0]
movapd %xmm1, 0xf0(%rsp)
movhps (%rax,%rcx,8), %xmm0    # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, 0xd0(%rsp)
movsd 0x8(%rax), %xmm0
movhpd (%rax,%rdx,8), %xmm0    # xmm0 = xmm0[0],mem[0]
movapd %xmm0, 0xe0(%rsp)
imulq %rbx, %r15
shlq $0x3, %rbx
shlq $0x3, %r15
xorl %eax, %eax
cmpq 0x30(%rsp), %rax
je 0x4ff6
movq %rax, 0x38(%rsp)
cvtsi2sd %eax, %xmm0
movl 0xa0(%rsp), %ecx
movl 0xa4(%rsp), %r12d
movq 0xb8(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0xa8(%rsp), %eax
movsd 0x28(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x78(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0xd0(%rsp), %xmm0
movapd %xmm0, 0x100(%rsp)
movq 0x98(%rsp), %rbp
movq %rdi, 0x40(%rsp)
addq %rdi, %rbp
movq %r12, 0x58(%rsp)
imulq %rcx, %r12
movq %rcx, 0x60(%rsp)
shlq $0x3, %rcx
movq %rcx, 0x48(%rsp)
imulq %rax, %r12
shlq $0x3, %r12
xorl %r14d, %r14d
cmpq %r13, %r14
je 0x4fe0
cvtsi2sd %r14d, %xmm0
movsd 0x70(%rsp), %xmm1
mulsd %xmm0, %xmm1
addsd 0x78(%rsp), %xmm1
addsd 0x68(%rsp), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0xe0(%rsp), %xmm0
addpd 0x100(%rsp), %xmm0
addpd 0xf0(%rsp), %xmm0
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
divpd %xmm1, %xmm0
movapd %xmm0, (%rsp)
callq 0x4900
movapd %xmm0, 0x10(%rsp)
movaps (%rsp), %xmm0
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movaps %xmm0, 0xc0(%rsp)
callq 0x4900
movaps 0x10(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x10(%rsp)
movaps (%rsp), %xmm0
callq 0x4070
movaps %xmm0, 0x110(%rsp)
movapd 0xc0(%rsp), %xmm0
callq 0x4070
movapd 0xc0(%rsp), %xmm6
movapd (%rsp), %xmm5
movapd 0x110(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvttpd2dq %xmm1, %xmm1
cvttpd2dq 0x10(%rsp), %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movmskps %xmm0, %eax
testl %eax, %eax
jne 0x4fd5
movd %xmm0, %eax
movq 0x60(%rsp), %rdx
cmpl %eax, %edx
setle %cl
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
movd %xmm1, %r9d
cmpl %r9d, %edx
setbe %dl
orb %cl, %dl
jne 0x4fd5
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ecx
movq 0x58(%rsp), %rsi
cmpl %ecx, %esi
setle %dl
pshufd $0xff, %xmm0, %xmm0     # xmm0 = xmm0[3,3,3,3]
movd %xmm0, %r10d
cmpl %r10d, %esi
setbe %sil
orb %dl, %sil
jne 0x4fd5
movl %eax, %edx
movl %ecx, %edi
movl %r9d, %esi
movl %r10d, %r8d
cvtsi2sd %r9d, %xmm0
subsd %xmm5, %xmm0
subl %eax, %r9d
cvtsi2sd %r9d, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %eax, %xmm2
subsd %xmm2, %xmm5
divsd %xmm1, %xmm5
cvtsi2sd %r10d, %xmm1
subsd %xmm6, %xmm1
subl %ecx, %r10d
cvtsi2sd %r10d, %xmm2
cvtsi2sd %ecx, %xmm3
divsd %xmm2, %xmm1
subsd %xmm3, %xmm6
divsd %xmm2, %xmm6
movq 0x48(%rsp), %r9
imulq %r9, %rdi
leaq (%rdi,%rdx,8), %rax
leaq (%rdi,%rsi,8), %rcx
imulq %r9, %r8
leaq (%r8,%rdx,8), %rdx
leaq (%r8,%rsi,8), %rsi
movq 0x50(%rsp), %rdi
movq %rbp, %r8
pushq $0x3
popq %r9
subq $0x1, %r9
jb 0x4fd5
movsd (%rdi,%rcx), %xmm2
mulsd %xmm5, %xmm2
movsd (%rdi,%rax), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
movsd (%rdi,%rsi), %xmm2
mulsd %xmm5, %xmm2
movsd (%rdi,%rdx), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm2, %xmm4
mulsd %xmm6, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r8)
addq %r15, %r8
addq %r12, %rdi
jmp 0x4f8a
incq %r14
addq %rbx, %rbp
jmp 0x4df3
movq 0x38(%rsp), %rax
incq %rax
movq 0x40(%rsp), %rdi
addq $0x8, %rdi
jmp 0x4d64
leaq 0x128(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movsd 0x4effa(%rip), %xmm1    # 0x54008
xorpd %xmm0, %xmm0
callq 0x644e
movq 0x20(%rsp), %rax
movq (%rax), %rsi
leaq 0x128(%rsp), %rdi
pushq $-0x1
popq %rdx
pushq $0x6
popq %rcx
callq 0x64bc
leaq 0x128(%rsp), %rdi
callq 0x6c96
leaq 0x80(%rsp), %rdi
callq 0x6c96
leaq 0xa0(%rsp), %rdi
callq 0x6c96
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x6c96
jmp 0x5082
jmp 0x507f
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6c96
jmp 0x5094
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x6c96
movq %rbx, %rdi
callq 0x4950