// File: /workspace/github_cpp_makefile_star2_O1/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)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, -0xb0(%rbp)
movq %rax, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
movabsq $0x4020000000000000, %rcx # imm = 0x4020000000000000
movq %rcx, -0x90(%rbp)
movq %rax, -0x88(%rbp)
movq %rax, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq %rax, -0x70(%rbp)
cmpl $0x0, 0xc(%rdi)
jle 0xfeba
movq $0x0, -0x38(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movq %rdi, -0x50(%rbp)
cmpl $0x0, 0x8(%rdi)
jle 0xfea4
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
cmpl $0x0, 0x10(%rdi)
jle 0xfe91
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, %r10
xorl %esi, %esi
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 0xfe15
movq %r14, %r9
addq %rax, %r9
setns %dl
cmpq %r12, %r9
setl %r9b
andb %dl, %r9b
cmpb $0x1, %r9b
jne 0xfe15
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(%r10,%rax,4)
incq %rax
addl %r15d, %ecx
cmpq $0x2, %rax
jne 0xfdd7
incl %r8d
incq %rsi
movl -0x48(%rbp), %edx
addl -0x40(%rbp), %edx
addq $0xc, %r10
addq $0x18, %r11
cmpq $0x3, %rsi
jne 0xfdb8
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 0xfe4c
incq %rax
addq $0xc, %rbx
cmpq $0x3, %rax
jne 0xfe4a
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
cmpl 0x10(%rdi), %esi
jl 0xfd5a
incq %r14
movslq 0x8(%rdi), %rax
incl -0x30(%rbp)
cmpq %rax, %r14
jl 0xfd4e
movq -0x38(%rbp), %rax
incl %eax
incl -0x2c(%rbp)
movq %rax, -0x38(%rbp)
cmpl 0xc(%rdi), %eax
jl 0xfd3a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq