// File: /workspace/github_cpp_makefile_star2_O3/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, -0x58(%rbp)
movaps 0x1889(%rip), %xmm0     # 0x11640
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
movapd 0x1883(%rip), %xmm1     # 0x11650
movapd %xmm1, -0x90(%rbp)
movaps %xmm0, -0x80(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0x70(%rbp)
movl 0xc(%rdi), %edx
testl %edx, %edx
jle 0xff90
movl 0x8(%rdi), %ecx
movq $0x0, -0x38(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movq %rdi, -0x50(%rbp)
testl %ecx, %ecx
jle 0xff7b
movl 0x10(%rdi), %edx
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
testl %edx, %edx
jle 0xff66
xorl %esi, %esi
movq %rsp, -0x60(%rbp)
movq %rsp, %rax
leaq -0x30(%rax), %rbx
movq %rbx, %rsp
movaps %xmm0, -0x20(%rax)
movaps %xmm0, -0x30(%rax)
movl $0x0, -0x10(%rax)
movl 0xc(%rdi), %eax
movl %eax, -0x44(%rbp)
movl 0x10(%rdi), %r15d
movslq 0x8(%rdi), %r12
movq 0x18(%rdi), %r13
movl -0x2c(%rbp), %edx
imull %r12d, %edx
addl -0x30(%rbp), %edx
imull %r15d, %edx
movl %esi, -0x3c(%rbp)
addl %esi, %edx
movl %r12d, %eax
imull %r15d, %eax
movl %eax, -0x40(%rbp)
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
leaq -0xb0(%rbp), %r11
movq %rbx, %rsi
xorl %r10d, %r10d
movl %r8d, %eax
addl -0x38(%rbp), %eax
setns %cl
cmpl -0x44(%rbp), %eax
setl %dil
andb %cl, %dil
movq $-0x1, %rax
movl %edx, -0x48(%rbp)
movl %edx, %ecx
testb %dil, %dil
je 0xfee5
movq %r14, %r9
addq %rax, %r9
setns %dl
cmpq %r12, %r9
setl %r9b
andb %dl, %r9b
cmpb $0x1, %r9b
jne 0xfee5
movslq %ecx, %rdx
movzbl (%r13,%rdx), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd 0x8(%r11,%rax,8), %xmm1
cvttsd2si %xmm1, %edx
movl %edx, 0x4(%rsi,%rax,4)
incq %rax
addl %r15d, %ecx
cmpq $0x2, %rax
jne 0xfea8
incl %r8d
incq %r10
movl -0x48(%rbp), %edx
addl -0x40(%rbp), %edx
addq $0xc, %rsi
addq $0x18, %r11
cmpq $0x3, %r10
jne 0xfe89
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x50(%rbp), %rdi
movl -0x3c(%rbp), %esi
xorl %edx, %edx
addl (%rbx,%rdx,4), %ecx
incq %rdx
cmpq $0x3, %rdx
jne 0xff1c
incq %rax
addq $0xc, %rbx
cmpq $0x3, %rax
jne 0xff1a
movq -0x58(%rbp), %rdx
movl 0x8(%rdx), %eax
imull -0x38(%rbp), %eax
addl %r14d, %eax
imull 0x10(%rdx), %eax
movq 0x18(%rdx), %rdx
addl %esi, %eax
cltq
movb %cl, (%rdx,%rax)
movq -0x60(%rbp), %rsp
incl %esi
movl 0x10(%rdi), %edx
cmpl %edx, %esi
jl 0xfe2a
movl 0x8(%rdi), %ecx
incq %r14
movslq %ecx, %rax
incl -0x30(%rbp)
cmpq %rax, %r14
jl 0xfe20
movl 0xc(%rdi), %edx
movq -0x38(%rbp), %rax
incl %eax
incl -0x2c(%rbp)
movq %rax, -0x38(%rbp)
cmpl %edx, %eax
jl 0xfe0b
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq