// File: /workspace/github_cpp_makefile_star2_O3/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, -0xb4(%rbp)
movq %rsi, -0x110(%rbp)
movq %rdi, %r15
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %ecx
leaq 0x7516(%rip), %r14      # 0x12253
leaq -0x150(%rbp), %rdi
movq %r14, %rsi
callq 0x4c56
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
leaq -0x130(%rbp), %rdi
movq %r14, %rsi
callq 0x4c56
movl 0x8(%r15), %edx
movl 0xc(%r15), %ecx
leaq -0xe0(%rbp), %rdi
movq %r14, %rsi
callq 0x4c56
movl $0x18, %edi
callq 0x2030
movq %rax, %r14
movl $0x18, %edi
callq 0x2030
movq %rax, %r12
xorl %ebx, %ebx
movl $0xc, %edi
callq 0x2030
movq %rax, (%r14,%rbx,8)
movl $0xc, %edi
callq 0x2030
movq %rax, (%r12,%rbx,8)
incq %rbx
cmpq $0x3, %rbx
jne 0xad96
movq %rsp, -0xf0(%rbp)
movq (%r14), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, (%rax)
movl $0x1, %edx
movl %edx, 0x8(%rax)
movq 0x8(%r14), %rax
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %rsi, (%rax)
movl $0x2, 0x8(%rax)
movq %r14, -0x98(%rbp)
movq 0x10(%r14), %rax
movq %rcx, (%rax)
movl %edx, 0x8(%rax)
movq (%r12), %rax
movabsq $-0x100000001, %rcx     # imm = 0xFFFFFFFEFFFFFFFF
movq %rcx, (%rax)
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq 0x8(%r12), %rax
movq $0x0, (%rax)
movl $0x0, 0x8(%rax)
movq %r12, -0x78(%rbp)
movq 0x10(%r12), %rax
movabsq $0x200000001, %rcx      # imm = 0x200000001
movq %rcx, (%rax)
movl %edx, 0x8(%rax)
movl 0x8(%r15), %edx
movl 0xc(%r15), %eax
movq %rdx, -0x88(%rbp)
imulq %rdx, %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movl 0x8(%r15), %ecx
movl 0xc(%r15), %ebx
movq %rcx, %rax
imulq %rbx, %rax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rax
movq %rdx, -0xe8(%rbp)
subq %rdx, %rax
movq %rax, -0x60(%rbp)
movq %rax, %rsp
testl %ebx, %ebx
jle 0xb3dc
movq %r15, -0x80(%rbp)
movslq %ecx, %rax
movq %rax, -0x100(%rbp)
movq -0x88(%rbp), %rax
leaq (,%rax,4), %r13
movq %rcx, -0x70(%rbp)
leaq (,%rcx,4), %r15
movq %rbx, -0x50(%rbp)
movq -0x68(%rbp), %r14
movq -0x60(%rbp), %r12
cmpl $0x0, -0x70(%rbp)
jle 0xaef5
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
addq %r15, %r12
addq %r13, %r14
decq %rbx
jne 0xaed5
movq %r15, -0xc0(%rbp)
movq -0x50(%rbp), %r14
testl %r14d, %r14d
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rbx
jle 0xb3dc
movl 0x10(%rax), %edi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl $0x80000000, %r15d      # imm = 0x80000000
xorl %edx, %edx
movl $0x80000000, -0x40(%rbp) # imm = 0x80000000
movl $0x7fffffff, -0x30(%rbp) # imm = 0x7FFFFFFF
movq %r13, -0xf8(%rbp)
movl %edi, -0x44(%rbp)
testl %ebx, %ebx
jle 0xb0bb
leaq 0x1(%rdx), %r8
movq %rdx, %rax
imulq -0x88(%rbp), %rax
movq -0x68(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xa8(%rbp)
movq %rdx, %rax
imulq %rbx, %rax
movq -0x60(%rbp), %rcx
leaq (%rcx,%rax,4), %rax
movq %rax, -0xa0(%rbp)
movq %r8, %rax
movq $0x0, -0x38(%rbp)
movq %r8, -0x108(%rbp)
testl %edi, %edi
jle 0xb09b
cmpq %r14, %rax
setl %al
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
setne %r8b
incq %rcx
movq %rcx, -0x90(%rbp)
cmpq -0x100(%rbp), %rcx
setl %cl
andb %al, %r8b
andb %cl, %r8b
xorl %r14d, %r14d
testq %rdx, %rdx
je 0xb073
testb %r8b, %r8b
je 0xb073
movq -0x98(%rbp), %rdi
movq -0x80(%rbp), %rbx
movl %esi, -0x58(%rbp)
movq %rbx, %rsi
movl %r15d, -0xb0(%rbp)
movq -0x38(%rbp), %r12
movl %r12d, %ecx
movq %rdx, %r13
movl %r8d, %r15d
movl %r14d, %r8d
movl $0x3, %r9d
callq 0xb5a6
movl -0x30(%rbp), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, -0x30(%rbp)
movq -0xa8(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
movl -0x40(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x40(%rbp)
movq -0x78(%rbp), %rdi
movq %rbx, %rsi
movl %r13d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
movl $0x3, %r9d
callq 0xb5a6
movl %r15d, %r8d
movq %r13, %rdx
movl -0x44(%rbp), %edi
movl -0x58(%rbp), %esi
movl -0xb0(%rbp), %r15d
cmpl %esi, %eax
cmovll %eax, %esi
movq -0xa0(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
cmpl %r15d, %eax
cmovgl %eax, %r15d
incl %r14d
cmpl %r14d, %edi
jne 0xafc8
movq -0x90(%rbp), %r8
movq %r8, -0x38(%rbp)
movq -0x70(%rbp), %rbx
movq -0x50(%rbp), %r14
movq -0x108(%rbp), %rax
jmp 0xb0a6
movq -0x38(%rbp), %r8
incq %r8
movq %r8, -0x38(%rbp)
cmpq %rbx, %r8
jne 0xaf92
movq %rax, %rdx
movq -0xf8(%rbp), %r13
jmp 0xb0c1
incq %rdx
movq %rdx, %rax
cmpq %r14, %rax
jne 0xaf44
movl -0x30(%rbp), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
movl %esi, %ecx
negl %ecx
cmovsl %esi, %ecx
testl %r14d, %r14d
movq -0x80(%rbp), %r12
jle 0xb3dc
addl %ecx, %r15d
movl -0x40(%rbp), %esi
addl %eax, %esi
cvtsi2sd %esi, %xmm1
cvtsi2sd %r15d, %xmm2
xorl %edx, %edx
movsd 0x63ad(%rip), %xmm0     # 0x114b0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0xc0(%rbp), %r11
testl %ebx, %ebx
jle 0xb161
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
addl %eax, %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r9d, %xmm3
mulsd %xmm0, %xmm3
divsd %xmm1, %xmm3
cvttsd2si %xmm3, %r9d
movl (%rdi,%r8,4), %r10d
addl %ecx, %r10d
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %xmm3
mulsd %xmm0, %xmm3
divsd %xmm2, %xmm3
cvttsd2si %xmm3, %r10d
movl %r9d, (%rsi,%r8,4)
movl %r10d, (%rdi,%r8,4)
incq %r8
cmpq %r8, %rbx
jne 0xb119
incq %rdx
addq %r11, %rdi
addq %r13, %rsi
cmpq %r14, %rdx
jne 0xb112
movq %rsp, %rax
subq -0xe8(%rbp), %rax
movq %rax, %rsp
testl %r14d, %r14d
jle 0xb3dc
movq %rax, -0x78(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x148(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x140(%rbp), %eax
movl %eax, -0xa8(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0x128(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x120(%rbp), %eax
movl %eax, -0x98(%rbp)
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x80000000, %ecx       # imm = 0x80000000
movq %rbx, %rax
xorl %r14d, %r14d
movl %eax, %r15d
movl %eax, %edx
testl %edx, %edx
jle 0xb330
leaq 0x1(%r14), %rbx
movq %r14, %rdx
imulq -0x88(%rbp), %rdx
movq -0x68(%rbp), %rax
leaq (%rax,%rdx,4), %rax
movq %rax, -0x30(%rbp)
movq %r14, %rdx
imulq -0x70(%rbp), %rdx
movq -0x60(%rbp), %rax
leaq (%rax,%rdx,4), %rax
movq %rax, -0x40(%rbp)
movq -0x78(%rbp), %rax
leaq (%rax,%rdx,4), %rsi
movl 0x10(%r12), %r9d
movl -0x44(%rbp), %eax
imull %r14d, %eax
movq %rax, -0x58(%rbp)
movl -0x90(%rbp), %eax
imull %r14d, %eax
movq %rax, -0xb0(%rbp)
xorl %r8d, %r8d
testl %r9d, %r9d
jle 0xb311
leaq 0x1(%r8), %r11
movq -0x58(%rbp), %rax
leal (%rax,%r8), %edx
imull -0xa8(%rbp), %edx
movq -0xb0(%rbp), %rax
leal (%rax,%r8), %r10d
imull -0x98(%rbp), %r10d
movslq %r10d, %r15
addq -0xa0(%rbp), %r15
movslq %edx, %r10
addq -0x38(%rbp), %r10
xorl %edx, %edx
testq %r14, %r14
je 0xb2fc
testq %r8, %r8
je 0xb2fc
movslq 0xc(%r12), %rax
cmpq %rax, %rbx
jge 0xb2fc
movslq 0x8(%r12), %rax
cmpq %rax, %r11
jge 0xb2fc
movq -0x30(%rbp), %rax
movl (%rax,%r8,4), %eax
movl %eax, %r9d
imull %eax, %r9d
movq -0x40(%rbp), %r12
movl (%r12,%r8,4), %r13d
movl %r13d, %r12d
imull %r13d, %r12d
addl %r9d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movq -0x80(%rbp), %r12
sqrtsd %xmm1, %xmm1
cvttsd2si %xmm1, %r9d
cmpl %r9d, %edi
cmovgel %r9d, %edi
cmpl %r9d, %ecx
cmovlel %r9d, %ecx
movl %r9d, (%rsi,%r8,4)
movb %al, (%r10,%rdx)
movb %r13b, (%r15,%rdx)
movl 0x10(%r12), %r9d
incq %rdx
movslq %r9d, %rax
cmpq %rax, %rdx
jl 0xb289
movl 0x8(%r12), %r15d
movq %r11, %r8
jmp 0xb317
incq %r8
movq %r8, %r11
movslq %r15d, %rdx
cmpq %rdx, %r11
jl 0xb247
movl 0xc(%r12), %eax
movl %r15d, %edx
movq %rbx, %r14
jmp 0xb33a
incq %r14
movq %r14, %rbx
movq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movslq %eax, %rsi
cmpq %rsi, %rbx
jl 0xb1e4
movl %edi, %edx
negl %edx
cmovsl %edi, %edx
movq -0x50(%rbp), %r10
testl %r10d, %r10d
movq -0x78(%rbp), %rbx
jle 0xb3dc
addl %edx, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movq -0xc8(%rbp), %rcx
movl -0xd0(%rbp), %esi
movl -0xd8(%rbp), %edi
imull %esi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
testl %r15d, %r15d
jle 0xb3c7
movl %r8d, %r10d
xorl %r11d, %r11d
movl (%rbx,%r11,4), %eax
addl %edx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
divsd %xmm1, %xmm2
cvttsd2si %xmm2, %eax
movl %eax, (%rbx,%r11,4)
movslq %r10d, %r10
movb %al, (%rcx,%r10)
incq %r11
movslq 0x8(%r12), %r15
addl %esi, %r10d
cmpq %r15, %r11
jl 0xb38e
movl 0xc(%r12), %r10d
incq %r9
movslq %r10d, %rax
addq -0xc0(%rbp), %rbx
addl %edi, %r8d
cmpq %rax, %r9
jl 0xb383
leaq 0x639d(%rip), %rsi      # 0x11780
leaq -0x150(%rbp), %rdi
callq 0x4dee
leaq 0x63b1(%rip), %rsi      # 0x117a7
leaq -0x130(%rbp), %rdi
callq 0x4dee
leaq -0xe0(%rbp), %rdi
leaq -0x950(%rbp), %r14
movq %r14, %rsi
callq 0xb620
movl (%r14), %eax
movl %eax, -0x550(%rbp)
movl $0x1, %ecx
addl -0x950(%rbp,%rcx,4), %eax
movl %eax, -0x550(%rbp,%rcx,4)
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0xb426
movl $0x64, %eax
subl -0xb4(%rbp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x6076(%rip), %xmm0     # 0x114d0
xorps %xmm1, %xmm1
cvtsi2sdl -0x154(%rbp), %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
xorl %ecx, %ecx
cmpl %eax, -0x550(%rbp,%rcx,4)
jg 0xb486
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0xb46f
jmp 0xb48a
decl %ecx
movl %ecx, %eax
movslq -0xd4(%rbp), %rcx
movq %rcx, -0x30(%rbp)
testq %rcx, %rcx
jle 0xb57c
movl -0xd8(%rbp), %edx
movl -0xd0(%rbp), %esi
movq -0xc8(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movslq %edx, %rcx
movq %rcx, -0x58(%rbp)
xorl %r9d, %r9d
movq -0x110(%rbp), %rcx
testl %edx, %edx
jle 0xb56c
leaq 0x1(%r9), %r10
movq %r9, %r11
imulq %rdx, %r11
xorl %ebx, %ebx
testl %esi, %esi
jle 0xb558
cmpq -0x30(%rbp), %r10
setge %r13b
testq %r9, %r9
sete %r14b
testq %rbx, %rbx
sete %r15b
orb %r14b, %r15b
leaq 0x1(%rbx), %r14
cmpq -0x58(%rbp), %r14
setge %dil
leal (%r11,%rbx), %r12d
imull %esi, %r12d
movslq %r12d, %r12
addq -0x40(%rbp), %r12
orb %r13b, %r15b
orb %dil, %r15b
xorl %r13d, %r13d
testb %r15b, %r15b
jne 0xb54b
movzbl (%r12,%r13), %edi
cmpl %edi, %eax
setg %dil
negb %dil
movl 0x8(%rcx), %r8d
imull %r9d, %r8d
addl %ebx, %r8d
imull 0x10(%rcx), %r8d
movslq %r8d, %r8
addq 0x18(%rcx), %r8
movb %dil, (%r13,%r8)
incq %r13
cmpq %r13, %rsi
jne 0xb51c
movq %r14, %rbx
jmp 0xb55e
incq %rbx
movq %rbx, %r14
cmpq %rdx, %r14
jne 0xb4db
movq %r10, %r9
jmp 0xb572
incq %r9
movq %r9, %r10
cmpq -0x30(%rbp), %r10
jne 0xb4c6
leaq 0x624b(%rip), %rsi      # 0x117ce
leaq -0xe0(%rbp), %rdi
callq 0x4dee
movq -0xf0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop