// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::sobel_edge_detector(Image&, Image&, int)
void ImageProc::sobel_edge_detector(Image &img_orig, Image &img_edge_map, int percentage)
{
	Image img_Gx("bw", img_orig.cols, img_orig.rows);
	Image img_Gy("bw", img_orig.cols, img_orig.rows);
	Image img_modified("bw", img_orig.cols, img_orig.rows);
	int **sobel_opX, **sobel_opY;
	sobel_opX = new int *[3];
	sobel_opY = new int *[3];
	for(int i = 0; i < 3; i++)
	{
		sobel_opX[i] = new int[3];
		sobel_opY[i] = new int[3];
	}

	sobel_opX[0][0] = -1;	sobel_opX[0][1] = 0;	sobel_opX[0][2] = 1;
	sobel_opX[1][0] = -2;	sobel_opX[1][1] = 0;	sobel_opX[1][2] = 2;
	sobel_opX[2][0] = -1;	sobel_opX[2][1] = 0;	sobel_opX[2][2] = 1;

	sobel_opY[0][0] = -1;	sobel_opY[0][1] = -2;	sobel_opY[0][2] = -1;
	sobel_opY[1][0] = 0;	sobel_opY[1][1] = 0;	sobel_opY[1][2] = 0;
	sobel_opY[2][0] = 1;	sobel_opY[2][1] = 2;	sobel_opY[2][2] = 1;
	//int sum = 0;
	int Gx[img_orig.rows][img_orig.cols];
	int Gy[img_orig.rows][img_orig.cols];
	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
		{
			Gx[i][j] = 0;
			Gy[i][j] = 0;
		}
	int minGx = std::numeric_limits<int>::max();
	int minGy = std::numeric_limits<int>::max();
	int maxGx = std::numeric_limits<int>::min();
	int maxGy = std::numeric_limits<int>::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)
				{
					Gx[i][j] = func_gradient(sobel_opX, img_orig, i, j, k, 3);
					if(Gx[i][j] < minGx) minGx = Gx[i][j];
					if(Gx[i][j] > maxGx) maxGx = Gx[i][j];

					Gy[i][j] = func_gradient(sobel_opY, img_orig, i, j, k, 3);
					if(Gy[i][j] < minGy) minGy = Gy[i][j];
					if(Gy[i][j] > maxGy) maxGy = Gy[i][j];

				}

			}


	//Normalize Gradient values
	int abs_minGx = abs(minGx);
	int abs_minGy = abs(minGy);
	maxGx = maxGx + abs_minGx;
	maxGy = maxGy + abs_minGy;


	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
		{
			Gx[i][j] = Gx[i][j] + abs_minGx;
			Gx[i][j] = (int)(1.0*Gx[i][j]*255/maxGx);
			Gy[i][j] = Gy[i][j] + abs_minGy;
			Gy[i][j] = (int)(1.0*Gy[i][j]*255/maxGy);
		}

	int nGradient[img_orig.rows][img_orig.cols];
	int min_nGradient = std::numeric_limits<int>::max();
	int max_nGradient = std::numeric_limits<int>::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)
				{
					nGradient[i][j] = (int)sqrt(Gx[i][j]*Gx[i][j] + Gy[i][j]*Gy[i][j]);
					if(nGradient[i][j] < min_nGradient) min_nGradient = nGradient[i][j];
					if(nGradient[i][j] > max_nGradient) max_nGradient = nGradient[i][j];

					img_Gx.setvalue(i,j,k,Gx[i][j]);
					img_Gy.setvalue(i,j,k,Gy[i][j]);
				}

			}

	int abs_min_nGradient = abs(min_nGradient);
	max_nGradient = max_nGradient + abs_min_nGradient;

	for(int i = 0; i < img_orig.rows; i++)
		for(int j = 0; j < img_orig.cols; j++)
		{
			nGradient[i][j] = nGradient[i][j] + abs_min_nGradient;
			nGradient[i][j] = (int)(1.0*nGradient[i][j]*255/max_nGradient);
			img_modified.setvalue(i,j,0,nGradient[i][j]);
		}

	img_Gx.write_image("../output_images/Sobel_gradientInX.raw", img_Gx.cols, img_Gx.rows);
	img_Gy.write_image("../output_images/Sobel_gradientInY.raw", img_Gy.cols, img_Gy.rows);


	int histogram_bins[256], hist_cumulative[256];
	get_histogram(img_modified, histogram_bins);
	get_cumulative_histogram(histogram_bins, hist_cumulative);

	int threshold = (int)(((100-percentage)/100.0)*hist_cumulative[255]);

	for(int i = 0; i < 256; i++)
	{
		if(hist_cumulative[i] > threshold)
		{
			threshold = i-1;
			break;
		}
	}

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

	img_modified.write_image("../output_images/Sobel_gradient_map.raw", img_modified.cols, img_modified.rows);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x928, %rsp            # imm = 0x928
movl %edx, -0xc0(%rbp)
movq %rsi, -0xf0(%rbp)
movq %rdi, %r13
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %ecx
leaq 0x81c5(%rip), %rbx      # 0x116b4
leaq -0x150(%rbp), %rdi
movq %rbx, %rsi
callq 0x3fd6
movl 0x8(%r13), %edx
movl 0xc(%r13), %ecx
leaq -0x130(%rbp), %rdi
movq %rbx, %rsi
callq 0x3fd6
movl 0x8(%r13), %edx
movl 0xc(%r13), %ecx
leaq -0x110(%rbp), %rdi
movq %rbx, %rsi
callq 0x3fd6
pushq $0x18
popq %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
callq 0x2030
movq %rax, -0x40(%rbp)
xorl %r14d, %r14d
pushq $0xc
popq %rbx
cmpq $0x3, %r14
je 0x9578
movq %rbx, %rdi
callq 0x2030
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
movq %rbx, %rdi
callq 0x2030
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
jmp 0x954d
movq %rsp, -0xe0(%rbp)
movq -0x48(%rbp), %rdi
movq (%rdi), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, (%rax)
pushq $0x1
popq %rdx
movl %edx, 0x8(%rax)
movq 0x8(%rdi), %rax
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %rsi, (%rax)
movl $0x2, 0x8(%rax)
movq 0x10(%rdi), %rax
movq %rcx, (%rax)
movl %edx, 0x8(%rax)
movq -0x40(%rbp), %rsi
movq (%rsi), %rax
movabsq $-0x100000001, %rcx     # imm = 0xFFFFFFFEFFFFFFFF
movq %rcx, (%rax)
orl $-0x1, 0x8(%rax)
movq 0x8(%rsi), %rax
andq $0x0, (%rax)
andl $0x0, 0x8(%rax)
movq 0x10(%rsi), %rax
movabsq $0x200000001, %rcx      # imm = 0x200000001
movq %rcx, (%rax)
movl %edx, 0x8(%rax)
movl 0x8(%r13), %edx
movl 0xc(%r13), %eax
imulq %rdx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, %rsp
movl 0x8(%r13), %ecx
movl 0xc(%r13), %r11d
movq %r11, %rax
imulq %rcx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rdi
movq %rax, -0xd8(%rbp)
subq %rax, %rdi
movq %rdi, %rsp
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %r10d
cmovgl %ecx, %r10d
testl %r11d, %r11d
movl $0x0, %r8d
cmovgl %r11d, %r8d
movq %rcx, -0x88(%rbp)
leaq (,%rcx,4), %rcx
movq %rcx, -0xa0(%rbp)
movq %rdx, -0xc8(%rbp)
leaq (,%rdx,4), %r9
movq %rsi, -0x98(%rbp)
movq %rsi, %rcx
movq %rdi, -0x90(%rbp)
movq %rdi, %rdx
cmpq %r8, %rax
je 0x96b1
xorl %esi, %esi
cmpq %rsi, %r10
je 0x96a2
andl $0x0, (%rcx,%rsi,4)
andl $0x0, (%rdx,%rsi,4)
incq %rsi
jmp 0x9690
incq %rax
addq -0xa0(%rbp), %rdx
addq %r9, %rcx
jmp 0x9689
movq %r9, -0xd0(%rbp)
movq %r8, -0x78(%rbp)
movl 0x10(%r13), %eax
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
movl $0x80000000, -0x50(%rbp) # imm = 0x80000000
movl $0x7fffffff, -0x38(%rbp) # imm = 0x7FFFFFFF
movl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl $0x80000000, %esi       # imm = 0x80000000
movq %r13, -0x58(%rbp)
movl %eax, -0xbc(%rbp)
movq %r11, -0xe8(%rbp)
movq %r10, -0xa8(%rbp)
movl %edi, -0x68(%rbp)
movq -0x58(%rbp), %r15
cmpq -0x78(%rbp), %rdx
je 0x984f
leaq 0x1(%rdx), %rax
movq %rax, -0x80(%rbp)
movq %rdx, %rax
imulq -0xc8(%rbp), %rax
movq -0x98(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xb8(%rbp)
movq %rdx, %rax
imulq -0x88(%rbp), %rax
movq -0x90(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xb0(%rbp)
xorl %r12d, %r12d
cmpq %r10, %r12
je 0x9842
cmpl -0x80(%rbp), %r11d
setg %al
testq %r12, %r12
setne %r9b
leaq 0x1(%r12), %rcx
movq %rcx, -0x60(%rbp)
cmpl %ecx, -0x88(%rbp)
setg %cl
andb %al, %r9b
andb %cl, %r9b
xorl %ebx, %ebx
cmpl %ebx, %edi
je 0x982b
testq %rdx, %rdx
je 0x9824
testb %r9b, %r9b
je 0x9824
movq -0x48(%rbp), %rdi
movl %esi, -0x2c(%rbp)
movq %r15, %rsi
movl %r14d, -0x70(%rbp)
movl %r12d, %ecx
movq %rdx, %r13
movl %ebx, %r8d
pushq $0x3
popq %rax
movl %r9d, %r14d
movl %eax, %r9d
callq 0x9cec
movl -0x38(%rbp), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, -0x38(%rbp)
movq -0xb8(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
movl -0x50(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x50(%rbp)
movq -0x40(%rbp), %rdi
movq %r15, %rsi
movl %r13d, %edx
movl %r12d, %ecx
movl %ebx, %r8d
pushq $0x3
popq %r9
callq 0x9cec
movl %r14d, %r9d
movl -0x68(%rbp), %edi
movq %r13, %rdx
movl -0x70(%rbp), %r14d
movl -0x2c(%rbp), %esi
cmpl %r14d, %eax
cmovll %eax, %r14d
movq -0xb0(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
cmpl %esi, %eax
cmovgl %eax, %esi
incl %ebx
jmp 0x9784
movq -0x60(%rbp), %r12
movq -0xe8(%rbp), %r11
movq -0xa8(%rbp), %r10
jmp 0x9753
movq -0x80(%rbp), %rdx
movq -0x58(%rbp), %r13
jmp 0x9704
movl -0x38(%rbp), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
movl -0x50(%rbp), %edx
addl %eax, %edx
addl %ecx, %esi
cvtsi2sd %edx, %xmm1
cvtsi2sd %esi, %xmm2
xorl %edx, %edx
movsd 0x6c35(%rip), %xmm0     # 0x104b0
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rbx
movq -0xd0(%rbp), %r14
cmpq %rbx, %rdx
je 0x98ef
xorl %r8d, %r8d
cmpq %r8, %r10
je 0x98e0
movl (%rsi,%r8,4), %r9d
addl %eax, %r9d
cvtsi2sd %r9d, %xmm3
mulsd %xmm0, %xmm3
divsd %xmm1, %xmm3
cvttsd2si %xmm3, %r9d
movl %r9d, (%rsi,%r8,4)
movl (%rdi,%r8,4), %r9d
addl %ecx, %r9d
cvtsi2sd %r9d, %xmm3
mulsd %xmm0, %xmm3
divsd %xmm2, %xmm3
cvttsd2si %xmm3, %r9d
movl %r9d, (%rdi,%r8,4)
incq %r8
jmp 0x989c
incq %rdx
addq -0xa0(%rbp), %rdi
addq %r14, %rsi
jmp 0x9894
movq %rsp, %rbx
subq -0xd8(%rbp), %rbx
movq %rbx, %rsp
movq -0x138(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x140(%rbp), %eax
movq -0x118(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movl -0x120(%rbp), %ecx
movl -0x128(%rbp), %edx
movl %ecx, -0xb0(%rbp)
imull %ecx, %edx
movl %edx, -0xa8(%rbp)
movl -0x148(%rbp), %ecx
movl %eax, -0x40(%rbp)
imull %eax, %ecx
movl %ecx, -0x78(%rbp)
movl $0x80000000, %edi       # imm = 0x80000000
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq $0x0, -0x68(%rbp)
movl $0x0, -0x60(%rbp)
movq -0x88(%rbp), %rax
movl %eax, %r9d
xorl %r15d, %r15d
movl -0xbc(%rbp), %ecx
movq %rbx, -0x80(%rbp)
movslq %r11d, %rax
cmpq %rax, %r15
jge 0x9ab6
leaq 0x1(%r15), %r12
movq %r15, %rax
imulq -0xc8(%rbp), %rax
movl %ecx, %edx
movq -0x98(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0x50(%rbp)
movq %r15, %rax
imulq -0x88(%rbp), %rax
movq -0x90(%rbp), %rcx
leaq (%rcx,%rax,4), %rcx
movq %rcx, -0x38(%rbp)
movl %edx, %ecx
leaq (%rbx,%rax,4), %rbx
movq -0x68(%rbp), %rax
movl %eax, %r8d
movl -0x60(%rbp), %r11d
xorl %edx, %edx
movslq %r9d, %rax
cmpq %rax, %rdx
jge 0x9a8f
movl %r11d, -0x70(%rbp)
movslq %r11d, %r9
addq -0xb8(%rbp), %r9
movl %r8d, -0x2c(%rbp)
movslq %r8d, %r8
addq -0x48(%rbp), %r8
leaq 0x1(%rdx), %rsi
xorl %eax, %eax
movslq %ecx, %r14
cmpq %r14, %rax
jge 0x9a70
testq %r15, %r15
je 0x9a6b
testq %rdx, %rdx
je 0x9a6b
cmpl %r12d, 0xc(%r13)
jle 0x9a6b
movslq 0x8(%r13), %r14
cmpq %r14, %rsi
jge 0x9a6b
movq -0x50(%rbp), %rcx
movl (%rcx,%rdx,4), %ecx
movl %ecx, %r14d
imull %ecx, %r14d
movq -0x38(%rbp), %r11
movl (%r11,%rdx,4), %r11d
movl %r11d, %r13d
imull %r11d, %r13d
addl %r14d, %r13d
cvtsi2sd %r13d, %xmm1
movq -0x58(%rbp), %r13
sqrtsd %xmm1, %xmm1
cvttsd2si %xmm1, %r14d
cmpl %r14d, %r10d
cmovgel %r14d, %r10d
cmpl %r14d, %edi
cmovlel %r14d, %edi
movl %r14d, (%rbx,%rdx,4)
movb %cl, (%r8,%rax)
movb %r11b, (%r9,%rax)
movl 0x10(%r13), %ecx
incq %rax
jmp 0x99fa
movl 0x8(%r13), %r9d
movl -0x70(%rbp), %r11d
addl -0xb0(%rbp), %r11d
movl -0x2c(%rbp), %r8d
addl -0x40(%rbp), %r8d
movq %rsi, %rdx
jmp 0x99cf
movl 0xc(%r13), %r11d
movl -0x60(%rbp), %eax
addl -0xa8(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x68(%rbp), %rax
addl -0x78(%rbp), %eax
movq %rax, -0x68(%rbp)
movq %r12, %r15
movq -0x80(%rbp), %rbx
jmp 0x9976
movl %r10d, %eax
negl %eax
cmovsl %r10d, %eax
addl %eax, %edi
cvtsi2sd %edi, %xmm1
movq -0xf8(%rbp), %r14
movslq -0x108(%rbp), %rcx
movl -0x100(%rbp), %edx
movq %rcx, -0x38(%rbp)
movl %edx, -0x58(%rbp)
imull %edx, %ecx
xorl %edx, %edx
xorl %esi, %esi
movslq %r11d, %rdi
cmpq %rdi, %rsi
jge 0x9b3e
movl %edx, %edi
xorl %r8d, %r8d
movslq %r9d, %r10
cmpq %r10, %r8
jge 0x9b2c
movl (%rbx,%r8,4), %r9d
addl %eax, %r9d
cvtsi2sd %r9d, %xmm2
mulsd %xmm0, %xmm2
divsd %xmm1, %xmm2
cvttsd2si %xmm2, %r9d
movl %r9d, (%rbx,%r8,4)
movslq %edi, %rdi
movb %r9b, (%r14,%rdi)
incq %r8
movl 0x8(%r13), %r9d
addl -0x58(%rbp), %edi
jmp 0x9af4
incq %rsi
movl 0xc(%r13), %r11d
addl %ecx, %edx
addq -0xa0(%rbp), %rbx
jmp 0x9ae7
leaq 0x70a0(%rip), %rsi      # 0x10be5
leaq -0x150(%rbp), %rdi
callq 0x411c
leaq 0x70b4(%rip), %rsi      # 0x10c0c
leaq -0x130(%rbp), %rdi
callq 0x411c
leaq -0x110(%rbp), %rdi
leaq -0x950(%rbp), %rbx
movq %rbx, %rsi
callq 0x9d8a
leaq -0x550(%rbp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9dd4
pushq $0x64
popq %rax
subl -0xc0(%rbp), %eax
cvtsi2sd %eax, %xmm0
divsd 0x692f(%rip), %xmm0     # 0x104d0
cvtsi2sdl 0x3fc(%r15), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
xorl %ecx, %ecx
cmpq $0x100, %rcx            # imm = 0x100
je 0x9bcf
cmpl %eax, -0x550(%rbp,%rcx,4)
jg 0x9bcb
incq %rcx
jmp 0x9bb4
decl %ecx
movl %ecx, %eax
movl -0x104(%rbp), %edi
xorl %r8d, %r8d
movl -0x58(%rbp), %edx
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
movq -0x38(%rbp), %rcx
testl %ecx, %ecx
movl $0x0, %r9d
cmovgl %ecx, %r9d
movq %r9, -0x50(%rbp)
testl %edi, %edi
movl $0x0, %r9d
movl %edi, -0x2c(%rbp)
cmovgl %edi, %r9d
movl %r9d, -0x48(%rbp)
imull %edx, %ecx
movl %ecx, -0x40(%rbp)
xorl %r10d, %r10d
movq -0xf0(%rbp), %rcx
cmpl -0x48(%rbp), %r10d
je 0x9cc3
leal 0x1(%r10), %r11d
movq %r8, -0x70(%rbp)
movl %r8d, %ebx
xorl %r15d, %r15d
cmpq -0x50(%rbp), %r15
je 0x9cb3
movl %r15d, %r8d
movslq %ebx, %r9
movq %r14, %rdi
addq %r14, %r9
cmpl -0x2c(%rbp), %r11d
setge %r13b
testl %r10d, %r10d
sete %r12b
testq %r15, %r15
sete %dl
orb %r12b, %dl
incq %r15
cmpq -0x38(%rbp), %r15
setge %r12b
orb %r13b, %dl
orb %r12b, %dl
xorl %r13d, %r13d
cmpq %r13, %rsi
je 0x9cab
testb %dl, %dl
jne 0x9ca6
movzbl (%r9,%r13), %r12d
cmpl %r12d, %eax
setg %r12b
movl 0x8(%rcx), %r14d
imull %r10d, %r14d
addl %r8d, %r14d
imull 0x10(%rcx), %r14d
movslq %r14d, %r14
addq 0x18(%rcx), %r14
negb %r12b
movb %r12b, (%r13,%r14)
incq %r13
jmp 0x9c72
addl -0x58(%rbp), %ebx
movq %rdi, %r14
jmp 0x9c34
movq -0x70(%rbp), %r8
addl -0x40(%rbp), %r8d
movl %r11d, %r10d
jmp 0x9c1c
leaq 0x6f69(%rip), %rsi      # 0x10c33
leaq -0x110(%rbp), %rdi
callq 0x411c
movq -0xe0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq