// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x38(%rdi), %rsi
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x8a94
movl (%r15), %esi
movl 0x4(%r15), %edx
movl 0x8(%r15), %ecx
leaq 0x20(%rsp), %rdi
movsd 0x6b54c(%rip), %xmm0    # 0x71008
movl $0x3, %r8d
callq 0x71ca
movq %rbx, 0x88(%rsp)
movq %r14, %rdi
movl $0x1, %esi
callq 0x736a
movslq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jle 0x5e5d
movq 0x18(%r14), %rax
movsd (%rax), %xmm1
movsd 0x8(%rax), %xmm2
movsd 0x10(%rax), %xmm3
movl (%r14), %ecx
movsd (%rax,%rcx,8), %xmm0
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd %xmm1, 0x100(%rsp)
leal 0x1(%rcx), %edx
movsd (%rax,%rdx,8), %xmm0
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd %xmm2, 0x120(%rsp)
leal 0x2(%rcx), %edx
movsd (%rax,%rdx,8), %xmm0
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movapd %xmm3, 0x110(%rsp)
leal (%rcx,%rcx), %edx
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x8(%rax,%rdx,8), %xmm0
movsd %xmm0, 0xc0(%rsp)
leal 0x2(%rcx,%rcx), %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0xb8(%rsp)
movl 0x24(%rsp), %r14d
movq 0x10(%rsp), %rax
leaq (,%rax,8), %r12
movq %r14, %r13
imulq %rax, %r13
shlq $0x3, %r13
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
testl %r14d, %r14d
jle 0x5e3f
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%rsp), %xmm0
movl 0x40(%rsp), %ecx
movl 0x44(%rsp), %ebp
movq 0x58(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl 0x48(%rsp), %eax
movsd 0x90(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0xc8(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x100(%rsp), %xmm0
movapd %xmm0, 0x130(%rsp)
movq 0x38(%rsp), %rbx
addq 0x18(%rsp), %rbx
movq %rbp, 0xa8(%rsp)
imulq %rcx, %rbp
movq %rcx, 0xb0(%rsp)
shlq $0x3, %rcx
movq %rcx, 0x98(%rsp)
imulq %rax, %rbp
shlq $0x3, %rbp
xorl %r15d, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movsd 0xc0(%rsp), %xmm1
mulsd %xmm0, %xmm1
addsd 0xc8(%rsp), %xmm1
addsd 0xb8(%rsp), %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x120(%rsp), %xmm0
addpd 0x130(%rsp), %xmm0
addpd 0x110(%rsp), %xmm0
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
divpd %xmm1, %xmm0
movapd %xmm0, 0x60(%rsp)
callq 0x48f0
movapd %xmm0, 0x70(%rsp)
movaps 0x60(%rsp), %xmm0
movhlps %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movaps %xmm0, 0xd0(%rsp)
callq 0x48f0
movaps 0x70(%rsp), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movaps %xmm1, 0x70(%rsp)
movaps 0x60(%rsp), %xmm0
callq 0x4080
movaps %xmm0, 0x140(%rsp)
movapd 0xd0(%rsp), %xmm0
callq 0x4080
movapd 0xd0(%rsp), %xmm6
movapd 0x60(%rsp), %xmm5
movapd 0x140(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvttpd2dq %xmm1, %xmm1
cvttpd2dq 0x70(%rsp), %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movmskps %xmm0, %eax
testl %eax, %eax
jne 0x5e30
movd %xmm0, %eax
movq 0xb0(%rsp), %rdx
cmpl %eax, %edx
setle %cl
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
movd %xmm1, %r9d
cmpl %r9d, %edx
setle %dl
orb %cl, %dl
jne 0x5e30
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ecx
movq 0xa8(%rsp), %rsi
cmpl %ecx, %esi
setle %dl
pshufd $0xff, %xmm0, %xmm0     # xmm0 = xmm0[3,3,3,3]
movd %xmm0, %r10d
cmpl %r10d, %esi
setle %sil
orb %dl, %sil
jne 0x5e30
movl %eax, %edx
movl %ecx, %edi
movl %r9d, %esi
movl %r10d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
subsd %xmm5, %xmm0
subl %eax, %r9d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %eax, %xmm2
subsd %xmm2, %xmm5
divsd %xmm1, %xmm5
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
subsd %xmm6, %xmm1
subl %ecx, %r10d
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
cvtsi2sd %ecx, %xmm3
divsd %xmm2, %xmm1
subsd %xmm3, %xmm6
divsd %xmm2, %xmm6
movq 0x98(%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
movl $0x3, %edi
movq 0xa0(%rsp), %r8
movq %rbx, %r9
movsd (%r8,%rcx), %xmm2
mulsd %xmm5, %xmm2
movsd (%r8,%rax), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
movsd (%r8,%rsi), %xmm2
mulsd %xmm5, %xmm2
movsd (%r8,%rdx), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm2, %xmm4
mulsd %xmm6, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, (%r9)
addq %r13, %r9
addq %rbp, %r8
decq %rdi
jne 0x5de4
incq %r15
addq %r12, %rbx
cmpq %r14, %r15
jne 0x5c2d
movq 0x8(%rsp), %rcx
incq %rcx
addq $0x8, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
cmpq 0x10(%rsp), %rcx
jne 0x5ba1
leaq 0xe0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movsd 0x6b196(%rip), %xmm1    # 0x71008
xorps %xmm0, %xmm0
callq 0x79c0
movq 0x88(%rsp), %rax
movq (%rax), %rsi
leaq 0xe0(%rsp), %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0x6, %ecx
callq 0x7a46
cmpb $0x0, 0xf0(%rsp)
jne 0x5eb8
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x5eb8
callq 0x4680
cmpb $0x0, 0x30(%rsp)
jne 0x5ece
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x5ece
callq 0x4680
cmpb $0x0, 0x50(%rsp)
jne 0x5ee4
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x5ee4
callq 0x4680
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
cmpb $0x0, 0xf0(%rsp)
jne 0x5f1c
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x5f1c
callq 0x4680
jmp 0x5f1c
jmp 0x5f19
movq %rax, %rbx
cmpb $0x0, 0x30(%rsp)
jne 0x5f37
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x5f37
callq 0x4680
jmp 0x5f37
movq %rax, %rbx
cmpb $0x0, 0x50(%rsp)
jne 0x5f4d
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x5f4d
callq 0x4680
movq %rbx, %rdi
callq 0x4950