// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rsi, -0x38(%rbp)
movq %rdi, %r14
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %ecx
leaq 0x7820(%rip), %r15      # 0x116b4
leaq -0xe8(%rbp), %rdi
movq %r15, %rsi
callq 0x3fd6
movl 0x8(%r14), %edx
movl 0xc(%r14), %ecx
leaq -0xc8(%rbp), %rdi
movq %r15, %rsi
callq 0x3fd6
movl $0x88, %edi
callq 0x2030
movq %rax, -0x60(%rbp)
xorl %ebx, %ebx
pushq $0x44
popq %r15
cmpq $0x11, %rbx
je 0x9ee9
movq %r15, %rdi
callq 0x2030
movq -0x60(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0x9ece
pushq $-0x8
popq %r12
xorl %r15d, %r15d
cmpq $0x11, %r15
je 0x9f5e
movl %r12d, -0x30(%rbp)
imull %r12d, %r12d
movq -0x60(%rbp), %rax
movq (%rax,%r15,8), %r13
xorl %ebx, %ebx
cmpq $0x11, %rbx
je 0x9f52
leaq -0x8(%rbx), %rax
imull %eax, %eax
addl %r12d, %eax
cvtsi2ss %eax, %xmm0
mulss 0x677c(%rip), %xmm0     # 0x106a0
movaps %xmm0, %xmm1
addss 0x676d(%rip), %xmm1     # 0x1069c
mulss 0x676d(%rip), %xmm1     # 0x106a4
movss %xmm1, -0x50(%rbp)
callq 0x2250
mulss -0x50(%rbp), %xmm0
movss %xmm0, (%r13,%rbx,4)
incq %rbx
jmp 0x9f08
movl -0x30(%rbp), %r12d
incl %r12d
incq %r15
jmp 0x9ef0
movq %rsp, -0x80(%rbp)
movl 0x8(%r14), %ecx
movl 0xc(%r14), %eax
imulq %rcx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movl 0x8(%r14), %r12d
movl 0xc(%r14), %r9d
xorl %eax, %eax
testl %r12d, %r12d
movl $0x0, %edi
cmovgl %r12d, %edi
testl %r9d, %r9d
movl $0x0, %r8d
cmovgl %r9d, %r8d
movq %rcx, -0x70(%rbp)
leaq (,%rcx,4), %r10
movq %rdx, -0x58(%rbp)
movq %rdx, %rcx
cmpq %r8, %rax
je 0x9fd6
xorl %edx, %edx
cmpq %rdx, %rdi
je 0x9fce
andl $0x0, (%rcx,%rdx,4)
incq %rdx
jmp 0x9fc0
incq %rax
addq %r10, %rcx
jmp 0x9fb9
movl 0x10(%r14), %esi
xorl %edx, %edx
testl %esi, %esi
movl $0x0, %r11d
cmovgl %esi, %r11d
movss 0x66b8(%rip), %xmm0     # 0x106a8
movss %xmm0, -0x30(%rbp)
movss 0x66af(%rip), %xmm1     # 0x106ac
movl %r12d, -0x68(%rbp)
movl %r9d, -0x64(%rbp)
movq %rdi, -0x98(%rbp)
movq %r8, -0x90(%rbp)
movq %r10, -0x88(%rbp)
movl %r11d, -0x3c(%rbp)
cmpq %r8, %rdx
je 0xa11b
leaq 0x1(%rdx), %rax
movq %rax, -0x78(%rbp)
movq %rdx, %rax
imulq -0x70(%rbp), %rax
movq -0x58(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xa8(%rbp)
xorl %ebx, %ebx
cmpq %rdi, %rbx
je 0xa104
cmpl -0x78(%rbp), %r9d
setle %al
testq %rdx, %rdx
sete %dil
testq %rbx, %rbx
sete %r8b
orb %dil, %r8b
leaq 0x1(%rbx), %rdi
movq %rdi, -0xa0(%rbp)
cmpl %edi, %r12d
setle %dil
orb %al, %r8b
orb %dil, %r8b
xorl %r13d, %r13d
movb %r8b, -0x29(%rbp)
cmpl %r13d, %r11d
je 0xa0e9
testb %r8b, %r8b
jne 0xa0e4
movl %esi, %r12d
movq -0x60(%rbp), %rdi
movq %r14, %r15
movq %r14, %rsi
movq %rdx, %r14
movl %ebx, %ecx
movl %r13d, %r8d
pushq $0x11
popq %r9
movaps %xmm1, -0x50(%rbp)
callq 0xa472
movq -0xa8(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
movaps %xmm0, %xmm1
minss -0x50(%rbp), %xmm1
ucomiss -0x30(%rbp), %xmm0
jbe 0xa0d3
movss %xmm0, -0x30(%rbp)
movl %r12d, %esi
movq %r14, %rdx
movq %r15, %r14
movl -0x3c(%rbp), %r11d
movb -0x29(%rbp), %r8b
incl %r13d
jmp 0xa088
movq -0xa0(%rbp), %rbx
movl -0x68(%rbp), %r12d
movl -0x64(%rbp), %r9d
movq -0x98(%rbp), %rdi
jmp 0xa048
movq -0x78(%rbp), %rdx
movq -0x90(%rbp), %r8
movq -0x88(%rbp), %r10
jmp 0xa01e
andps 0x649e(%rip), %xmm1     # 0x105c0
cvttps2dq %xmm1, %xmm0
cvtdq2ps %xmm0, %xmm0
movss -0x30(%rbp), %xmm1
addss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm1
xorl %eax, %eax
movsd 0x6370(%rip), %xmm2     # 0x104b0
movq -0x58(%rbp), %rcx
cmpq %r8, %rax
je 0xa17e
xorl %edx, %edx
cmpq %rdx, %rdi
je 0xa176
movss (%rcx,%rdx,4), %xmm3
addss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm1, %xmm3
cvttpd2dq %xmm3, %xmm3
cvtdq2ps %xmm3, %xmm3
movss %xmm3, (%rcx,%rdx,4)
incq %rdx
jmp 0xa14b
incq %rax
addq %r10, %rcx
jmp 0xa144
movq -0xd0(%rbp), %rax
movl -0xd8(%rbp), %ecx
movl -0xe0(%rbp), %edx
imull %ecx, %edx
movl %edx, -0x30(%rbp)
xorl %r10d, %r10d
xorl %edi, %edi
movslq %r9d, %r8
cmpq %r8, %rdi
jge 0xa247
leaq 0x1(%rdi), %r8
movq %rdi, %r9
imulq -0x70(%rbp), %r9
movq -0x58(%rbp), %rdx
leaq (%rdx,%r9,4), %r9
movq %r10, -0x50(%rbp)
xorl %r11d, %r11d
movslq %r12d, %rbx
cmpq %rbx, %r11
jge 0xa233
movslq %r10d, %r12
addq %rax, %r12
leaq 0x1(%r11), %r15
xorl %ebx, %ebx
movslq %esi, %r13
cmpq %r13, %rbx
jge 0xa227
testq %rdi, %rdi
je 0xa222
testq %r11, %r11
je 0xa222
cmpl %r8d, 0xc(%r14)
jle 0xa222
movslq 0x8(%r14), %r13
cmpq %r13, %r15
jge 0xa222
cvttss2si (%r9,%r11,4), %r13d
movq -0x38(%rbp), %rsi
movl 0x8(%rsi), %edx
imull %edi, %edx
addl %r11d, %edx
imull 0x10(%rsi), %edx
movslq %edx, %rdx
addq 0x18(%rsi), %rdx
movb %r13b, (%rbx,%rdx)
movb %r13b, (%r12,%rbx)
movl 0x10(%r14), %esi
incq %rbx
jmp 0xa1d7
movl 0x8(%r14), %r12d
addl %ecx, %r10d
movq %r15, %r11
jmp 0xa1c3
movl 0xc(%r14), %r9d
movq -0x50(%rbp), %r10
addl -0x30(%rbp), %r10d
movq %r8, %rdi
jmp 0xa19c
leaq 0x6a0d(%rip), %rsi      # 0x10c5b
leaq -0xe8(%rbp), %rdi
callq 0x411c
movq -0xb0(%rbp), %rax
movl -0xb8(%rbp), %ecx
movl -0xc0(%rbp), %edx
imull %ecx, %edx
movl %edx, -0x30(%rbp)
xorl %edx, %edx
pushq $0x40
popq %rdi
xorl %r8d, %r8d
cmpl 0xc(%r14), %r8d
jge 0xa32a
leal 0x1(%r8), %r9d
movq %rdx, -0x50(%rbp)
movl %edx, %r10d
xorl %r11d, %r11d
movslq 0x8(%r14), %rbx
cmpq %rbx, %r11
jge 0xa31b
movslq %r10d, %r15
addq %rax, %r15
leaq 0x1(%r11), %rbx
xorl %r12d, %r12d
movslq 0x10(%r14), %r13
cmpq %r13, %r12
jge 0xa310
testl %r8d, %r8d
je 0xa30b
testq %r11, %r11
je 0xa30b
cmpl 0xc(%r14), %r9d
jge 0xa30b
movslq 0x8(%r14), %r13
cmpq %r13, %rbx
jge 0xa30b
movq -0x38(%rbp), %rdx
movl 0x8(%rdx), %r13d
imull %r8d, %r13d
addl %r11d, %r13d
imull 0x10(%rdx), %r13d
movslq %r13d, %r13
addq 0x18(%rdx), %r13
movb (%r12,%r13), %dl
cmpb $-0x5a, %dl
setae %sil
shlb $0x6, %sil
orb $-0x80, %sil
cmpb $-0x79, %dl
movzbl %sil, %edx
cmovbl %edi, %edx
movb %dl, (%r12,%r13)
movb %dl, (%r15,%r12)
incq %r12
jmp 0xa2a9
addl %ecx, %r10d
movq %rbx, %r11
jmp 0xa293
movq -0x50(%rbp), %rdx
addl -0x30(%rbp), %edx
movl %r9d, %r8d
jmp 0xa27b
leaq 0x694c(%rip), %rsi      # 0x10c7d
leaq -0xc8(%rbp), %rdi
callq 0x411c
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %ecx
leaq 0x7366(%rip), %rsi      # 0x116b4
leaq -0x108(%rbp), %r15
movq %r15, %rdi
callq 0x3fd6
movq 0x18(%r15), %rax
movl 0x10(%r15), %ecx
movl 0x8(%r15), %edx
imull %ecx, %edx
xorl %esi, %esi
xorl %edi, %edi
movq -0x38(%rbp), %r15
movl 0xc(%r14), %r8d
cmpl %r8d, %edi
jge 0xa3cf
movl %esi, %r8d
xorl %r9d, %r9d
movslq 0x8(%r14), %r10
cmpq %r10, %r9
jge 0xa3c9
movslq %r8d, %r10
addq %rax, %r10
xorl %r11d, %r11d
movslq 0x10(%r14), %rbx
cmpq %rbx, %r11
jge 0xa3c1
movl 0x8(%r15), %ebx
imull %edi, %ebx
addl %r9d, %ebx
imull 0x10(%r15), %ebx
movslq %ebx, %rbx
addq 0x18(%r15), %rbx
movb (%r11,%rbx), %bl
movb %bl, (%r10,%r11)
incq %r11
jmp 0xa395
incq %r9
addl %ecx, %r8d
jmp 0xa383
incl %edi
addl %edx, %esi
jmp 0xa374
xorl %r12d, %r12d
cmpl %r8d, %r12d
jge 0xa45f
leal 0x1(%r12), %eax
movl %eax, -0x50(%rbp)
xorl %r13d, %r13d
cmpl 0x8(%r14), %r13d
jge 0xa450
leal 0x1(%r13), %ebx
xorl %r15d, %r15d
cmpl 0x10(%r14), %r15d
jge 0xa44b
testl %r12d, %r12d
je 0xa446
testl %r13d, %r13d
je 0xa446
movl -0x50(%rbp), %eax
cmpl 0xc(%r14), %eax
jge 0xa446
cmpl 0x8(%r14), %ebx
jge 0xa446
leaq -0x108(%rbp), %rdi
movl %r12d, %esi
movl %r13d, %edx
movl %r15d, %ecx
callq 0xa51e
movq -0x38(%rbp), %rdx
movl 0x8(%rdx), %ecx
imull %r12d, %ecx
addl %r13d, %ecx
imull 0x10(%rdx), %ecx
movslq %ecx, %rcx
addq 0x18(%rdx), %rcx
decb %al
movb %al, (%r15,%rcx)
incq %r15
jmp 0xa3f3
movl %ebx, %r13d
jmp 0xa3e6
movl 0xc(%r14), %r8d
movl -0x50(%rbp), %eax
movl %eax, %r12d
jmp 0xa3d2
movq -0x80(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq