// File: /workspace/github_cpp_makefile_star2_O3/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::LoG_edge_detector(Image&, Image&)
void ImageProc::LoG_edge_detector(Image &img_orig, Image &img_modified)
{
	Image img_GradiantLoG("bw", img_orig.cols, img_orig.rows);
	Image img_ternary_map_LoG("bw", img_orig.cols, img_orig.rows);
	int size = 17;
	float **LoG_kernel;
	LoG_kernel = new float *[size];
	for(int i = 0; i < size; i++)
		LoG_kernel[i] = new float[size];

	float sigma = 2;
	int kernel_radius = size/2;
	float coeff = (-1.0)/(M_PI * pow(sigma,4));
	for(int p = -1*kernel_radius, row = 0 ; p <= kernel_radius ; p++, row++)
		for(int q = -1*kernel_radius, col = 0 ; q <= kernel_radius ; q++, col++)
		{
			float value = (-1.0) * (((p*p) + (q*q))/(2*sigma*sigma));
			LoG_kernel[row][col] = coeff * (1+value) * (exp(value));
		}

	float Gradient_LoG[img_orig.rows][img_orig.cols];
	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
		{
			Gradient_LoG[i][j] = 0.0;
		}
	float minGradient = std::numeric_limits<float>::max();
	float maxGradient = std::numeric_limits<float>::min();
	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
			{
				if((i-1)>=0 && (i+1)<img_orig.rows && (j-1)>=0 && (j+1)<img_orig.cols)
				{
					Gradient_LoG[i][j] = func_gradient_float(LoG_kernel, img_orig, i, j, k, size);
					if(Gradient_LoG[i][j] < minGradient) minGradient = Gradient_LoG[i][j];
					if(Gradient_LoG[i][j] > maxGradient) maxGradient = Gradient_LoG[i][j];
				}

			}


	//Normalize Gradient values
	int abs_minGradient = abs(minGradient);
	maxGradient = maxGradient + abs_minGradient;

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
		{
			Gradient_LoG[i][j] = Gradient_LoG[i][j] + abs_minGradient;
			Gradient_LoG[i][j] = (int)(1.0*Gradient_LoG[i][j]*255/maxGradient);
		}


	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
				if((i-1)>=0 && (i+1)<img_orig.rows && (j-1)>=0 && (j+1)<img_orig.cols)
				{
					img_modified.setvalue(i,j,k,Gradient_LoG[i][j]);
					img_GradiantLoG.setvalue(i,j,k,Gradient_LoG[i][j]);
				}

	img_GradiantLoG.write_image("../output_images/Gradient_LoG.raw", img_GradiantLoG.cols, img_GradiantLoG.rows);

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
			{
				if((i-1)>=0 && (i+1)<img_orig.rows && (j-1)>=0 && (j+1)<img_orig.cols)
				{
					if(img_modified.getvalue(i,j,k) < 135)
					{
						img_modified.setvalue(i,j,k,64);
						img_ternary_map_LoG.setvalue(i,j,k,64);
					}
					else if(img_modified.getvalue(i,j,k) > 165)
					{
						img_modified.setvalue(i,j,k,192);
						img_ternary_map_LoG.setvalue(i,j,k,192);
					}
					else
					{
						img_modified.setvalue(i,j,k,128);
						img_ternary_map_LoG.setvalue(i,j,k,128);
					}
				}

			}

	img_ternary_map_LoG.write_image("../output_images/ternary_map_LoG.raw", img_ternary_map_LoG.cols, img_ternary_map_LoG.rows);

	Image img_modified_copy("bw", img_modified.cols, img_modified.rows);

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
				img_modified_copy.setvalue(i,j,k,img_modified.getvalue(i,j,k));

	int zero_crossing;
	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
			for(int k = 0; k < img_orig.channels; k++)
			{
				if((i-1)>=0 && (i+1)<img_orig.rows && (j-1)>=0 && (j+1)<img_orig.cols)
				{
					zero_crossing = check_zero_crossing(img_modified_copy, i, j, k);
					if(zero_crossing == 0) 	img_modified.setvalue(i,j,k,255);
					else					img_modified.setvalue(i,j,k,0);
				}

			}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, -0x50(%rbp)
movq %rdi, %r14
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %ecx
leaq 0x6b19(%rip), %r15      # 0x12253
leaq -0x120(%rbp), %rdi
movq %r15, %rsi
callq 0x4c56
movl 0x8(%r14), %edx
movq %r14, -0x30(%rbp)
movl 0xc(%r14), %ecx
leaq -0x100(%rbp), %rdi
movq %r15, %rsi
callq 0x4c56
movl $0x88, %edi
callq 0x2030
movq %rax, %r12
xorl %r15d, %r15d
movl $0x44, %edi
callq 0x2030
movq %rax, (%r12,%r15,8)
incq %r15
cmpq $0x11, %r15
jne 0xb774
movq %r12, -0x98(%rbp)
movl $0xfffffff8, %ebx       # imm = 0xFFFFFFF8
xorl %r14d, %r14d
movss 0x5efe(%rip), %xmm1     # 0x116a0
movss 0x5ef2(%rip), %xmm2     # 0x1169c
movss 0x5ef2(%rip), %xmm3     # 0x116a4
movl %ebx, %r13d
imull %ebx, %r13d
movq -0x98(%rbp), %rax
movq (%rax,%r14,8), %r15
xorl %r12d, %r12d
leal -0x8(%r12), %eax
imull %eax, %eax
addl %r13d, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm0
movaps %xmm0, %xmm1
addss %xmm2, %xmm1
mulss %xmm3, %xmm1
movss %xmm1, -0x70(%rbp)
callq 0x2260
movss 0x5eaa(%rip), %xmm3     # 0x116a4
movss 0x5e9a(%rip), %xmm2     # 0x1169c
movss 0x5e96(%rip), %xmm1     # 0x116a0
mulss -0x70(%rbp), %xmm0
movss %xmm0, (%r15,%r12,4)
incq %r12
cmpq $0x11, %r12
jne 0xb7c7
incl %ebx
incq %r14
cmpq $0x11, %r14
jne 0xb7b2
movq %rsp, -0xa0(%rbp)
movq -0x30(%rbp), %rsi
movl 0x8(%rsi), %edx
movl 0xc(%rsi), %eax
movq %rdx, -0x78(%rbp)
imulq %rdx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movslq 0xc(%rsi), %r15
testq %r15, %r15
jle 0xbb9b
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r13d
movq -0x78(%rbp), %rax
leaq (,%rax,4), %rax
movq %rax, -0x80(%rbp)
movq %r13, %rbx
shlq $0x2, %rbx
movq %r15, -0x88(%rbp)
movq -0x58(%rbp), %r12
testl %r13d, %r13d
jle 0xb8a4
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x20c0
addq -0x80(%rbp), %r12
decq %r15
jne 0xb892
movq -0x88(%rbp), %r9
testl %r9d, %r9d
jle 0xbb9b
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %eax
movl 0x10(%rcx), %ebx
movq %rax, -0x90(%rbp)
cltq
movq %rax, -0xa8(%rbp)
movss 0x5dc9(%rip), %xmm1     # 0x116a8
movss 0x5dc5(%rip), %xmm0     # 0x116ac
movss %xmm0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, -0x90(%rbp)
jle 0xb9fc
movq -0x40(%rbp), %rax
leaq 0x1(%rax), %rsi
imulq -0x78(%rbp), %rax
movq -0x58(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x48(%rbp)
movq %rsi, -0xb0(%rbp)
testl %ebx, %ebx
jle 0xb9de
cmpq %r9, %rsi
setge %al
cmpq $0x0, -0x40(%rbp)
sete %cl
movq -0x48(%rbp), %rsi
testq %rsi, %rsi
sete %r12b
orb %cl, %r12b
leaq 0x1(%rsi), %rcx
movq %rcx, -0xb8(%rbp)
cmpq -0xa8(%rbp), %rcx
setge %cl
orb %al, %r12b
orb %cl, %r12b
xorl %r15d, %r15d
testb %r12b, %r12b
jne 0xb9bb
movq -0x30(%rbp), %rsi
movq -0x98(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %r13
movl %r13d, %ecx
movl %r15d, %r8d
movl $0x11, %r9d
movaps %xmm1, -0x70(%rbp)
callq 0xbe8e
movq -0xc0(%rbp), %rax
movss %xmm0, (%rax,%r13,4)
movaps %xmm0, %xmm1
minss -0x70(%rbp), %xmm1
ucomiss -0x34(%rbp), %xmm0
jbe 0xb9bb
movss %xmm0, -0x34(%rbp)
incl %r15d
cmpl %r15d, %ebx
jne 0xb96e
movq -0xb8(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq -0x88(%rbp), %r9
movq -0xb0(%rbp), %rsi
jmp 0xb9e9
movq -0x48(%rbp), %rcx
incq %rcx
movq %rcx, -0x48(%rbp)
cmpq -0x90(%rbp), %rcx
jne 0xb92c
movq %rsi, -0x40(%rbp)
jmp 0xba0a
movq -0x40(%rbp), %rcx
incq %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsi
cmpq %r9, %rsi
jne 0xb8f4
testl %r9d, %r9d
jle 0xbb9b
andps 0x5bdd(%rip), %xmm1     # 0x11600
cvttps2dq %xmm1, %xmm0
cvtdq2ps %xmm0, %xmm0
movss -0x34(%rbp), %xmm1
addss %xmm0, %xmm1
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
cvtss2sd %xmm1, %xmm1
xorl %ecx, %ecx
movsd 0x5a68(%rip), %xmm2     # 0x114b0
movq -0x58(%rbp), %rdx
testl %eax, %eax
jle 0xba7b
xorl %esi, %esi
movss (%rdx,%rsi,4), %xmm3
addss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2ps %xmm3, %xmm3
movss %xmm3, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0xba52
incq %rcx
addq -0x80(%rbp), %rdx
cmpq %r9, %rcx
jne 0xba4c
testl %r9d, %r9d
jle 0xbb9b
movq -0x108(%rbp), %rax
movl -0x118(%rbp), %ecx
movl %ecx, -0x70(%rbp)
movl -0x110(%rbp), %edx
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %r15d
xorl %esi, %esi
movq -0x50(%rbp), %r14
testl %r15d, %r15d
jle 0xbb89
leaq 0x1(%rsi), %rdi
movq %rsi, %r8
imulq -0x78(%rbp), %r8
movq -0x58(%rbp), %rcx
leaq (%rcx,%r8,4), %r8
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %r11d
movl -0x70(%rbp), %ecx
movl %ecx, %r9d
imull %esi, %r9d
xorl %r10d, %r10d
testl %r11d, %r11d
jle 0xbb6a
leaq 0x1(%r10), %r12
leal (%r9,%r10), %r15d
imull %edx, %r15d
movslq %r15d, %r15
addq %rax, %r15
xorl %r13d, %r13d
testq %rsi, %rsi
je 0xbb52
testq %r10, %r10
je 0xbb52
movq -0x30(%rbp), %rcx
movslq 0xc(%rcx), %rcx
cmpq %rcx, %rdi
jge 0xbb52
movq -0x30(%rbp), %rcx
movslq 0x8(%rcx), %rcx
cmpq %rcx, %r12
jge 0xbb52
cvttss2si (%r8,%r10,4), %ecx
movl 0x8(%r14), %r11d
imull %esi, %r11d
addl %r10d, %r11d
imull 0x10(%r14), %r11d
movslq %r11d, %r11
addq 0x18(%r14), %r11
movb %cl, (%r13,%r11)
movb %cl, (%r15,%r13)
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %r11d
incq %r13
movslq %r11d, %rcx
cmpq %rcx, %r13
jl 0xbb00
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %r15d
movq %r12, %r10
jmp 0xbb70
incq %r10
movq %r10, %r12
movslq %r15d, %r13
cmpq %r13, %r12
jl 0xbae6
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %r9d
movq %rdi, %rsi
jmp 0xbb8f
incq %rsi
movq %rsi, %rdi
movslq %r9d, %r8
cmpq %r8, %rdi
jl 0xbab4
leaq 0x5c54(%rip), %rsi      # 0x117f6
leaq -0x120(%rbp), %rdi
callq 0x4dee
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r9d
testl %r9d, %r9d
jle 0xbcce
movq -0xe8(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0xf0(%rbp), %edx
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r11d
xorl %edi, %edi
movl $0x40, %esi
testl %r11d, %r11d
jle 0xbcbd
leal 0x1(%rdi), %r8d
movl -0x34(%rbp), %eax
movl %eax, %r9d
imull %edi, %r9d
movq -0x30(%rbp), %rax
movl 0x10(%rax), %r13d
xorl %r10d, %r10d
testl %r13d, %r13d
jle 0xbca1
leaq 0x1(%r10), %r15
leal (%r9,%r10), %r11d
imull %edx, %r11d
movslq %r11d, %r11
addq -0x70(%rbp), %r11
xorl %r12d, %r12d
testl %edi, %edi
je 0xbc84
testq %r10, %r10
je 0xbc84
movq -0x30(%rbp), %rax
cmpl 0xc(%rax), %r8d
jge 0xbc84
movq -0x30(%rbp), %rax
movslq 0x8(%rax), %r13
cmpq %r13, %r15
jge 0xbc84
movq -0x50(%rbp), %rax
movl 0x8(%rax), %r13d
imull %edi, %r13d
addl %r10d, %r13d
imull 0x10(%rax), %r13d
movslq %r13d, %r13
addq 0x18(%rax), %r13
movb (%r12,%r13), %cl
cmpb $-0x5a, %cl
setae %al
shlb $0x6, %al
orb $-0x80, %al
cmpb $-0x79, %cl
movzbl %al, %eax
cmovbl %esi, %eax
movb %al, (%r12,%r13)
movb %al, (%r11,%r12)
incq %r12
movq -0x30(%rbp), %rax
movslq 0x10(%rax), %r13
cmpq %r13, %r12
jl 0xbc29
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r11d
movq %r15, %r10
jmp 0xbca7
incq %r10
movq %r10, %r15
movslq %r11d, %r12
cmpq %r12, %r15
jl 0xbc0a
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r9d
jmp 0xbcc2
incl %edi
movl %edi, %r8d
movl %r8d, %edi
cmpl %r9d, %r8d
jl 0xbbe8
leaq 0x5b43(%rip), %rsi      # 0x11818
leaq -0x100(%rbp), %rdi
callq 0x4dee
movq -0x50(%rbp), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %ecx
leaq 0x6561(%rip), %rsi      # 0x12253
leaq -0xe0(%rbp), %rdi
callq 0x4c56
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r8d
testl %r8d, %r8d
jle 0xbe77
movq -0xc8(%rbp), %rax
movl -0xd0(%rbp), %ecx
movq -0x30(%rbp), %rdx
movl 0x8(%rdx), %r9d
movl -0xd8(%rbp), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %edi, %edi
movq -0x50(%rbp), %r14
testl %r9d, %r9d
jle 0xbda3
movq -0x30(%rbp), %r8
movl 0x10(%r8), %r11d
movl %esi, %r8d
xorl %r10d, %r10d
testl %r11d, %r11d
jle 0xbd8d
movslq %r8d, %r9
addq %rax, %r9
xorl %r15d, %r15d
movl 0x8(%r14), %r11d
imull %edi, %r11d
addl %r10d, %r11d
imull 0x10(%r14), %r11d
movslq %r11d, %r11
addq 0x18(%r14), %r11
movb (%r15,%r11), %r11b
movb %r11b, (%r9,%r15)
incq %r15
movq -0x30(%rbp), %r11
movslq 0x10(%r11), %r11
cmpq %r11, %r15
jl 0xbd56
movq -0x30(%rbp), %r9
movl 0x8(%r9), %r9d
incq %r10
movslq %r9d, %r15
addl %ecx, %r8d
cmpq %r15, %r10
jl 0xbd48
movq -0x30(%rbp), %r8
movl 0xc(%r8), %r8d
incl %edi
addl %edx, %esi
cmpl %r8d, %edi
jl 0xbd35
testl %r8d, %r8d
jle 0xbe77
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
xorl %r12d, %r12d
testl %eax, %eax
jle 0xbe65
leal 0x1(%r12), %r14d
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
xorl %r13d, %r13d
testl %ecx, %ecx
jle 0xbe4a
leal 0x1(%r13), %ebx
xorl %r15d, %r15d
testl %r12d, %r12d
je 0xbe32
testl %r13d, %r13d
je 0xbe32
movq -0x30(%rbp), %rax
cmpl 0xc(%rax), %r14d
jge 0xbe32
movq -0x30(%rbp), %rax
cmpl 0x8(%rax), %ebx
jge 0xbe32
leaq -0xe0(%rbp), %rdi
movl %r12d, %esi
movl %r13d, %edx
movl %r15d, %ecx
callq 0xbf1e
movq -0x50(%rbp), %rsi
movl 0x8(%rsi), %ecx
imull %r12d, %ecx
addl %r13d, %ecx
imull 0x10(%rsi), %ecx
movslq %ecx, %rcx
addq 0x18(%rsi), %rcx
decb %al
movb %al, (%r15,%rcx)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
incq %r15
cmpl %ecx, %r15d
jl 0xbde1
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
jmp 0xbe50
incl %r13d
movl %r13d, %ebx
movl %ebx, %r13d
cmpl %eax, %ebx
jl 0xbdd6
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %r8d
jmp 0xbe6b
incl %r12d
movl %r12d, %r14d
movl %r14d, %r12d
cmpl %r8d, %r14d
jl 0xbdbf
movq -0xa0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop