// File: /workspace/github_cpp_makefile_star2_O2/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::unsharp_mask(Image&, Image&)
void ImageProc::unsharp_mask(Image &img_orig, Image &img_modified)
{
	int ws = 3;
	double kernel[ws][ws]; //discrete convolution operator

	// //gaussian_kernel(kernel, ws, sigma);
	// double kernel_sum = 0.0;
    int kernel_radius = ws / 2;
 //    //double coeff = 1.0 / (2.0 * M_PI * (sigma * sigma));
 //    double frac = 0.0;

 //    for (int row = -kernel_radius; row <= kernel_radius; row++)
 //        for (int col = -kernel_radius; col <= kernel_radius; col++)
 //        {
 //            frac = exp(-1.0 * ((row*row) + (col*col)) / (2.0 * (sigma*sigma)));

 //            kernel[row + kernel_radius][col + kernel_radius] = frac;

 //            kernel_sum += kernel[row + kernel_radius][col + kernel_radius];
 //        }

 //    for (int i = 0; i < ws; i++)
 //        for (int j = 0; j < ws; j++)
 //            kernel[i][j] = kernel[i][j] / kernel_sum; //Normalized

	kernel[0][0] = -1;	kernel[0][1] = -1;	kernel[0][2] = -1;
	kernel[1][0] = -1;	kernel[1][1] =  8;	kernel[1][2] = -1;
	kernel[2][0] = -1;	kernel[2][1] = -1;	kernel[2][2] = -1;


	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++)
		 	{
				int values[ws][ws];
				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values[row][col] = 0;
				int values_avg = 0;

				for(int m = -kernel_radius, row = 0; m <= kernel_radius; m++, row++)
					for(int n = -kernel_radius, col = 0; n <= kernel_radius; n++, col++)
					{
						if((i+m >= 0) && (i+m < img_orig.rows) && (j+n >= 0) && (j+n < img_orig.cols))
						{
							values[row][col] = img_orig.getvalue((i+m),(j+n),k) * kernel[row][col];
						}
						// else
						// {
						// 	values[row][col] = img_orig.getvalue((i+m),(j-n),k) * kernel[row][col];
						// }
					}

				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values_avg += values[row][col];

				img_modified.setvalue(i,j,k,values_avg);
			}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, -0x50(%rbp)
movapd 0x23b4(%rip), %xmm0     # 0x10600
leaq -0xb0(%rbp), %rcx
movapd %xmm0, (%rcx)
movapd %xmm0, 0x10(%rcx)
movaps 0x23ad(%rip), %xmm1     # 0x10610
movaps %xmm1, 0x20(%rcx)
movapd %xmm0, 0x30(%rcx)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, 0x40(%rcx)
xorl %r9d, %r9d
pushq $-0x1
popq %rax
movl %eax, -0x2c(%rbp)
movq %rdi, -0x48(%rbp)
cmpl 0xc(%rdi), %r9d
jge 0xe41b
pushq $-0x1
popq %rax
movl %eax, -0x30(%rbp)
xorl %r10d, %r10d
movq %r9, -0x60(%rbp)
movslq 0x8(%rdi), %rax
cmpq %rax, %r10
jge 0xe410
xorl %esi, %esi
cmpl 0x10(%rdi), %esi
jge 0xe405
movq %rsp, -0x58(%rbp)
movq %rsp, %r15
addq $-0x30, %r15
movq %r15, %rsp
movq %r15, %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0xe2e9
xorl %edx, %edx
cmpq $0x3, %rdx
je 0xe2e0
andl $0x0, (%rax,%rdx,4)
incq %rdx
jmp 0xe2d1
incq %rcx
addq $0xc, %rax
jmp 0xe2c9
movl 0xc(%rdi), %eax
movl %eax, -0x3c(%rbp)
movl 0x10(%rdi), %r13d
movslq 0x8(%rdi), %rax
movq 0x18(%rdi), %rdx
movl -0x2c(%rbp), %ecx
imull %eax, %ecx
addl -0x30(%rbp), %ecx
imull %r13d, %ecx
movl %esi, -0x34(%rbp)
addl %esi, %ecx
movl %eax, %esi
imull %r13d, %esi
movl %esi, -0x38(%rbp)
leaq -0xb0(%rbp), %r14
movq %r15, %rbx
xorl %esi, %esi
pushq $-0x1
popq %rdi
cmpq $0x3, %rsi
je 0xe3b1
movl %edi, %r8d
addl %r9d, %r8d
setns %r9b
cmpl -0x3c(%rbp), %r8d
setl %r8b
andb %r9b, %r8b
movl %ecx, -0x40(%rbp)
movl %ecx, %r9d
pushq $-0x1
popq %r12
cmpq $0x2, %r12
je 0xe395
testb %r8b, %r8b
je 0xe38d
movq %r10, %r11
addq %r12, %r11
setns %cl
cmpq %rax, %r11
setl %r11b
andb %cl, %r11b
cmpb $0x1, %r11b
jne 0xe38d
movslq %r9d, %rcx
movzbl (%rdx,%rcx), %ecx
cvtsi2sd %ecx, %xmm0
mulsd 0x8(%r14,%r12,8), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x4(%rbx,%r12,4)
incq %r12
addl %r13d, %r9d
jmp 0xe34e
incl %edi
incq %rsi
addq $0xc, %rbx
addq $0x18, %r14
movl -0x40(%rbp), %ecx
addl -0x38(%rbp), %ecx
movq -0x60(%rbp), %r9
jmp 0xe325
xorl %edx, %edx
xorl %eax, %eax
movq -0x48(%rbp), %rdi
movl -0x34(%rbp), %esi
cmpq $0x3, %rdx
je 0xe3dc
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0xe3d3
addl (%r15,%rcx,4), %eax
incq %rcx
jmp 0xe3c4
incq %rdx
addq $0xc, %r15
jmp 0xe3bc
movq -0x50(%rbp), %rdx
movl 0x8(%rdx), %ecx
imull %r9d, %ecx
addl %r10d, %ecx
imull 0x10(%rdx), %ecx
movq 0x18(%rdx), %rdx
addl %esi, %ecx
movslq %ecx, %rcx
movb %al, (%rdx,%rcx)
movq -0x58(%rbp), %rsp
incl %esi
jmp 0xe2ad
incq %r10
incl -0x30(%rbp)
jmp 0xe29e
incl %r9d
incl -0x2c(%rbp)
jmp 0xe287
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq