// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %ecx
leaq 0x6b22(%rip), %r15      # 0x121e7
leaq -0xf8(%rbp), %rdi
movq %r15, %rsi
callq 0x4c6e
movl 0x8(%r14), %edx
movl 0xc(%r14), %ecx
leaq -0xd8(%rbp), %rdi
movq %r15, %rsi
callq 0x4c6e
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 0xb6fb
movq %r12, -0x78(%rbp)
movl $0xfffffff8, %r13d      # imm = 0xFFFFFFF8
xorl %ecx, %ecx
movss 0x5f0e(%rip), %xmm1     # 0x11634
movss 0x5f02(%rip), %xmm2     # 0x11630
movss 0x5f02(%rip), %xmm3     # 0x11638
movl %r13d, -0x38(%rbp)
imull %r13d, %r13d
movq -0x78(%rbp), %rax
movq %rcx, -0x50(%rbp)
movq (%rax,%rcx,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, -0x40(%rbp)
callq 0x2270
movss 0x5eb8(%rip), %xmm3     # 0x11638
movss 0x5ea8(%rip), %xmm2     # 0x11630
movss 0x5ea4(%rip), %xmm1     # 0x11634
mulss -0x40(%rbp), %xmm0
movss %xmm0, (%r15,%r12,4)
incq %r12
cmpq $0x11, %r12
jne 0xb74d
movl -0x38(%rbp), %r13d
incl %r13d
movq -0x50(%rbp), %rcx
incq %rcx
cmpq $0x11, %rcx
jne 0xb736
movq %rsp, -0x80(%rbp)
movl 0x8(%r14), %edx
movl 0xc(%r14), %eax
movq %rdx, -0x68(%rbp)
imulq %rdx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movslq 0xc(%r14), %rcx
movq %rcx, -0x48(%rbp)
testq %rcx, %rcx
jle 0xb839
movl 0x8(%r14), %ecx
movq -0x68(%rbp), %rax
leaq (,%rax,4), %rax
movq %rax, -0x40(%rbp)
movq %rcx, %r15
shlq $0x2, %rcx
movq %rcx, -0x38(%rbp)
movq -0x48(%rbp), %r13
movq -0x60(%rbp), %r12
testl %r15d, %r15d
jle 0xb830
movq %r12, %rdi
xorl %esi, %esi
movq -0x38(%rbp), %rdx
callq 0x20c0
addq -0x40(%rbp), %r12
decq %r13
jne 0xb81d
cmpl $0x0, -0x48(%rbp)
jle 0xb98d
movl 0x8(%r14), %eax
movl 0x10(%r14), %edi
movq %rax, -0x70(%rbp)
cltq
decq %rax
movq %rax, -0x88(%rbp)
movss 0x5ddd(%rip), %xmm2     # 0x11640
movss 0x5dd1(%rip), %xmm0     # 0x1163c
movss %xmm0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
cmpl $0x0, -0x70(%rbp)
jle 0xb970
movq -0x58(%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, -0x90(%rbp)
imulq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0x50(%rbp)
testl %edi, %edi
jle 0xb953
movq -0x90(%rbp), %rax
cmpq -0x48(%rbp), %rax
setae %al
cmpq $0x0, -0x58(%rbp)
sete %cl
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
sete %dl
orb %cl, %dl
cmpq -0x88(%rbp), %rsi
setge %cl
orb %al, %cl
orb %dl, %cl
xorl %r15d, %r15d
movb %cl, -0x29(%rbp)
testb %cl, %cl
je 0xb8f4
movaps %xmm2, %xmm1
jmp 0xb946
movq %r14, %rsi
movq %rbx, %r13
movl %edi, %ebx
movq -0x78(%rbp), %rdi
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %r12
movl %r12d, %ecx
movl %r15d, %r8d
movl $0x11, %r9d
movss %xmm2, -0x40(%rbp)
callq 0xbd5c
movq -0x98(%rbp), %rax
movss %xmm0, (%rax,%r12,4)
movaps %xmm0, %xmm1
minss -0x40(%rbp), %xmm1
ucomiss -0x38(%rbp), %xmm0
jbe 0xb93e
movss %xmm0, -0x38(%rbp)
movl %ebx, %edi
movq %r13, %rbx
movb -0x29(%rbp), %cl
incl %r15d
movaps %xmm1, %xmm2
cmpl %r15d, %edi
jne 0xb8eb
jmp 0xb956
movaps %xmm2, %xmm1
movq -0x50(%rbp), %rcx
incq %rcx
movaps %xmm1, %xmm2
movq %rcx, -0x50(%rbp)
cmpq -0x70(%rbp), %rcx
jne 0xb8ad
jmp 0xb973
movaps %xmm2, %xmm1
movq -0x58(%rbp), %rcx
incq %rcx
movaps %xmm1, %xmm2
movq %rcx, -0x58(%rbp)
cmpq -0x48(%rbp), %rcx
jne 0xb878
jmp 0xb9a2
movss 0x5ca7(%rip), %xmm0     # 0x1163c
movss %xmm0, -0x38(%rbp)
movss 0x5c9e(%rip), %xmm1     # 0x11640
cmpl $0x0, -0x48(%rbp)
jle 0xba1c
andps 0x5bc1(%rip), %xmm1     # 0x11570
cvttps2dq %xmm1, %xmm0
cvtdq2ps %xmm0, %xmm0
movss -0x38(%rbp), %xmm1
addss %xmm0, %xmm1
movl 0x8(%r14), %eax
cvtss2sd %xmm1, %xmm1
movq -0x68(%rbp), %rcx
leaq (,%rcx,4), %rcx
xorl %edx, %edx
movsd 0x5b33(%rip), %xmm2     # 0x11510
movq -0x60(%rbp), %rsi
testl %eax, %eax
jle 0xba10
xorl %edi, %edi
movss (%rsi,%rdi,4), %xmm3
addss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2ps %xmm3, %xmm3
movss %xmm3, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0xb9e7
incq %rdx
addq %rcx, %rsi
cmpq -0x48(%rbp), %rdx
jne 0xb9e1
cmpl $0x0, 0xc(%r14)
jle 0xbafc
movq -0xe0(%rbp), %rax
movl -0xe8(%rbp), %ecx
movl -0xf0(%rbp), %edx
imull %ecx, %edx
movl %edx, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
xorl %edi, %edi
cmpl $0x0, 0x8(%r14)
jle 0xbae1
leaq 0x1(%rdi), %r8
movq %rdi, %r9
imulq -0x68(%rbp), %r9
movq -0x60(%rbp), %rdx
leaq (%rdx,%r9,4), %r9
movq -0x40(%rbp), %rdx
movl %edx, %r10d
xorl %r11d, %r11d
cmpl $0x0, 0x10(%r14)
jle 0xbad2
movslq %r10d, %r15
addq %rax, %r15
leaq 0x1(%r11), %r12
xorl %r13d, %r13d
testq %rdi, %rdi
je 0xbac6
testq %r11, %r11
je 0xbac6
movslq 0xc(%r14), %rdx
cmpq %rdx, %r8
jge 0xbac6
movslq 0x8(%r14), %rdx
cmpq %rdx, %r12
jge 0xbac6
cvttss2si (%r9,%r11,4), %edx
movl 0x8(%rbx), %esi
imull %edi, %esi
addl %r11d, %esi
imull 0x10(%rbx), %esi
movslq %esi, %rsi
addq 0x18(%rbx), %rsi
movb %dl, (%r13,%rsi)
movb %dl, (%r15,%r13)
incq %r13
movslq 0x10(%r14), %rdx
cmpq %rdx, %r13
jl 0xba87
incq %r11
movslq 0x8(%r14), %r15
addl %ecx, %r10d
cmpq %r15, %r11
jl 0xba73
incq %rdi
movslq 0xc(%r14), %r8
movq -0x40(%rbp), %rdx
addl -0x38(%rbp), %edx
movq %rdx, -0x40(%rbp)
cmpq %r8, %rdi
jl 0xba4a
leaq 0x5c87(%rip), %rsi      # 0x1178a
leaq -0xf8(%rbp), %rdi
callq 0x4e1a
cmpl $0x0, 0xc(%r14)
jle 0xbc00
movq -0xc0(%rbp), %rax
movl -0xd0(%rbp), %ecx
movl %ecx, -0x38(%rbp)
movl -0xc8(%rbp), %edx
xorl %esi, %esi
cmpl $0x0, 0x8(%r14)
jle 0xbbf4
leal 0x1(%rsi), %r8d
movl -0x38(%rbp), %ecx
imull %esi, %ecx
movq %rcx, -0x40(%rbp)
xorl %r10d, %r10d
cmpl $0x0, 0x10(%r14)
jle 0xbbe4
leaq 0x1(%r10), %r11
movq -0x40(%rbp), %rcx
leal (%rcx,%r10), %edi
xorl %r12d, %r12d
testl %esi, %esi
je 0xbbd8
testq %r10, %r10
je 0xbbd8
cmpl 0xc(%r14), %r8d
jge 0xbbd8
movslq 0x8(%r14), %r13
cmpq %r13, %r11
jge 0xbbd8
movl 0x8(%rbx), %r13d
imull %esi, %r13d
addl %r10d, %r13d
imull 0x10(%rbx), %r13d
movslq %r13d, %r13
addq 0x18(%rbx), %r13
movb (%r12,%r13), %cl
cmpb $-0x5a, %cl
setae %r9b
movl %edi, %r15d
cmovael %edi, %r15d
shlb $0x6, %r9b
orb $-0x80, %r9b
cmpb $-0x79, %cl
movzbl %r9b, %ecx
movl $0x40, %r9d
cmovbl %r9d, %ecx
cmovbl %edi, %r15d
imull %edx, %r15d
movslq %r15d, %r9
movb %cl, (%r12,%r13)
addq %rax, %r9
movb %cl, (%r12,%r9)
incq %r12
movslq 0x10(%r14), %rcx
cmpq %rcx, %r12
jl 0xbb68
incq %r10
movslq 0x8(%r14), %r11
cmpq %r11, %r10
jl 0xbb4e
incl %esi
cmpl 0xc(%r14), %esi
jl 0xbb32
leaq 0x5ba5(%rip), %rsi      # 0x117ac
leaq -0xd8(%rbp), %rdi
callq 0x4e1a
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %ecx
leaq 0x65c7(%rip), %rsi      # 0x121e7
leaq -0xb8(%rbp), %rdi
callq 0x4c6e
cmpl $0x0, 0xc(%r14)
jle 0xbcae
movq -0xa0(%rbp), %rax
movl -0xa8(%rbp), %ecx
movl -0xb0(%rbp), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %edi, %edi
cmpl $0x0, 0x8(%r14)
jle 0xbca4
movl %esi, %r8d
xorl %r9d, %r9d
cmpl $0x0, 0x10(%r14)
jle 0xbc95
movslq %r8d, %r10
addq %rax, %r10
xorl %r11d, %r11d
movl 0x8(%rbx), %r15d
imull %edi, %r15d
addl %r9d, %r15d
imull 0x10(%rbx), %r15d
movslq %r15d, %r15
addq 0x18(%rbx), %r15
movb (%r11,%r15), %r15b
movb %r15b, (%r10,%r11)
incq %r11
movslq 0x10(%r14), %r15
cmpq %r15, %r11
jl 0xbc6a
incq %r9
movslq 0x8(%r14), %r10
addl %ecx, %r8d
cmpq %r10, %r9
jl 0xbc5a
incl %edi
addl %edx, %esi
cmpl 0xc(%r14), %edi
jl 0xbc4d
cmpl $0x0, 0xc(%r14)
jle 0xbd49
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r14)
jle 0xbd3c
leal 0x1(%r15), %eax
movl %eax, -0x40(%rbp)
xorl %r13d, %r13d
cmpl $0x0, 0x10(%r14)
jle 0xbd33
leal 0x1(%r13), %eax
movl %eax, -0x38(%rbp)
xorl %r12d, %r12d
testl %r15d, %r15d
je 0xbd2a
testl %r13d, %r13d
je 0xbd2a
movl -0x40(%rbp), %eax
cmpl 0xc(%r14), %eax
jge 0xbd2a
movl -0x38(%rbp), %eax
cmpl 0x8(%r14), %eax
jge 0xbd2a
leaq -0xb8(%rbp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %r12d, %ecx
callq 0xbdec
decb %al
movl 0x8(%rbx), %ecx
imull %r15d, %ecx
addl %r13d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
addq 0x18(%rbx), %rcx
movb %al, (%r12,%rcx)
incq %r12
cmpl 0x10(%r14), %r12d
jl 0xbcde
incl %r13d
cmpl 0x8(%r14), %r13d
jl 0xbccd
incl %r15d
cmpl 0xc(%r14), %r15d
jl 0xbcbc
movq -0x80(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq