// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::non_local_mean(Image&, Image&, int, int, int, int)
void ImageProc::non_local_mean(Image &img_orig, Image &img_modified, int window_size, int region_size, int sigma, int h)
{
	int ws = window_size;
	int wleft = ((-1*ws) + 1) / 2;
	int wright = (( 1*ws) - 1) / 2;
	int rs = region_size;
	double kernel[rs][rs]; //discrete convolution operator
	double kernel_sum = 0.0;
    int kernel_radius = rs / 2;
    //double distance_value[rs*rs];
    double distortion_value[ws*ws], intensity_values[ws*ws], distortion_value_sum;
    double coeff = 1.0 / (sqrt(2.0 * M_PI) * (sigma));
    double frac = 0.0;
    int index = 0;
    //float distance = 0.0;
    double value_sum;

    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] = coeff * 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



    int M, N, K;
    double distance_sum = 0.0;
    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++)
	 		{
	 			value_sum = 0.0;
	 			distortion_value_sum = 0.0;
	 			for(int iter = 0; iter < ws*ws; iter++)
	 			{
	 				distortion_value[iter] = 0.0;
	 				intensity_values[iter] = 0.0;
	 			}
	 			index = 0;

	 			//std::cout << "\nstart" << std::endl;
	 			for(int m = wleft; m <= wright; m++)
					for(int n = wleft; n <= wright; n++)
					{
	 					distance_sum = 0.0;

						M = i+m; N = j+n; K = k;
						if((M >= 0) && (M < img_orig.rows) && (N >= 0) && (N < img_orig.cols))
						{
							for(int p = -kernel_radius, row = 0; p <= kernel_radius; p++, row++)
								for(int q = -kernel_radius, col = 0; q <= kernel_radius; q++, col++)
								{
									if((i+p >= 0) && (i+p < img_orig.rows) && (j+q >= 0) && (j+q < img_orig.cols) &&
										(M+p >= 0) && (M+p < img_orig.rows) && (N+q >= 0) && (N+q < img_orig.cols))
									{
										//distance_value[index] = kernel[row][col] * ((img_orig.getvalue((i+p),(j+q),k)) - img_orig.getvalue((M+p),(N+q),k))
										distance_sum += kernel[row][col] * (pow((img_orig.getvalue((i+p),(j+q),k))-img_orig.getvalue((M+p),(N+q),K),2));
									}
									else
									{
										//distance_value[index] = kernel[row][col] * ((img_orig.getvalue((i+p),(j-q),k)) - img_orig.getvalue((M+p),(N-q),k))
										distance_sum += kernel[row][col] * (pow((img_orig.getvalue((i+p),(j-q),k))-img_orig.getvalue((M+p),(N-q),K),2));
									}

								}
							intensity_values[index] = img_orig.getvalue(M,N,K);
							distortion_value[index] = exp(-1.0*(distance_sum/pow(h,2.0)));
							//std::cout << distortion_value[index] << std::endl;

							index++;
						}
						else
						{
							index++;
						}

					}

				for(int index = 0; index < ws*ws; index++)
				{
					distortion_value_sum += distortion_value[index];
					//std::cout << distortion_value_sum << " " << distortion_value[i] << " " << intensity_values[i] << std::endl;
				}

				//for(int i = 0; i < ws*ws; i++)

				for(int t = 0; t < ws*ws; t++)
				{
					value_sum += (intensity_values[t]*(distortion_value[t]/distortion_value_sum));
					//std::cout << intensity_values[i] << " " << (distortion_value[i]/distortion_value_sum) << " " << value_sum << std::endl;

				}
					//std::cout << "     " << value_sum << std::endl;


				img_modified.setvalue(i,j,k,value_sum);

			}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movl $0x1, %eax
subl %ecx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %ecx
movl %ecx, %edx
decl %edx
movl %ecx, %eax
shrl $0x1f, %edx
movl %edx, %ecx
leal -0x1(%rax,%rcx), %eax
sarl %eax
movl %eax, -0x2c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq %rax, -0x108(%rbp)
movq %rsp, %rcx
movq %rcx, -0x38(%rbp)
movq %rax, %rcx
imulq %rcx, %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x100(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
imull %eax, %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf8(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl -0x24(%rbp), %eax
imull %eax, %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movsd 0xc37f(%rip), %xmm0     # 0x1c4e0
callq 0x2150
movaps %xmm0, %xmm1
cvtsi2sdl -0x1c(%rbp), %xmm0
mulsd %xmm0, %xmm1
movsd 0xc0ee(%rip), %xmm0     # 0x1c268
divsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0x102b9
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0x102a3
movl -0x94(%rbp), %eax
imull -0x94(%rbp), %eax
movl -0x98(%rbp), %ecx
imull -0x98(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0xc2df(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
cvtsi2sd %eax, %xmm2
movsd 0xc038(%rip), %xmm1     # 0x1c240
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x2380
movq -0x108(%rbp), %rdx
movq -0x100(%rbp), %rax
movsd %xmm0, -0x80(%rbp)
movsd -0x78(%rbp), %xmm0
mulsd -0x80(%rbp), %xmm0
movl -0x94(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rsi
imulq %rdx, %rsi
shlq $0x3, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movl -0x98(%rbp), %esi
addl -0x54(%rbp), %esi
movslq %esi, %rsi
movsd %xmm0, (%rcx,%rsi,8)
movl -0x94(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movl -0x98(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x101ba
jmp 0x102a5
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x101a0
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x10360
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1034a
movq -0x100(%rbp), %rax
movq -0x108(%rbp), %rdx
movslq -0x9c(%rbp), %rsi
imulq %rdx, %rsi
shlq $0x3, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movslq -0xa0(%rbp), %rsi
movsd (%rcx,%rsi,8), %xmm0
divsd -0x50(%rbp), %xmm0
movslq -0x9c(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xa0(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x102dc
jmp 0x1034c
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x102c3
xorps %xmm0, %xmm0
movsd %xmm0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x10997
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x10981
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x1096b
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
movl -0x24(%rbp), %ecx
imull -0x24(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1042d
movq -0xf0(%rbp), %rax
movq -0xf8(%rbp), %rcx
movslq -0xc8(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0xc8(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x103df
movl $0x0, -0x84(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x1087b
movl -0x28(%rbp), %eax
movl %eax, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jg 0x10865
xorps %xmm0, %xmm0
movsd %xmm0, -0xb8(%rbp)
movl -0xbc(%rbp), %eax
addl -0xcc(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xc0(%rbp), %eax
addl -0xd0(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0xac(%rbp)
cmpl $0x0, -0xa4(%rbp)
jl 0x10840
movl -0xa4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x10840
cmpl $0x0, -0xa8(%rbp)
jl 0x10840
movl -0xa8(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x10840
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0x107ab
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl $0x0, -0xe0(%rbp)
movl -0xdc(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0x10786
movl -0xbc(%rbp), %eax
addl -0xd4(%rbp), %eax
cmpl $0x0, %eax
jl 0x106a4
movl -0xbc(%rbp), %eax
addl -0xd4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x106a4
movl -0xc0(%rbp), %eax
addl -0xdc(%rbp), %eax
cmpl $0x0, %eax
jl 0x106a4
movl -0xc0(%rbp), %eax
addl -0xdc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x106a4
movl -0xa4(%rbp), %eax
addl -0xd4(%rbp), %eax
cmpl $0x0, %eax
jl 0x106a4
movl -0xa4(%rbp), %eax
addl -0xd4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x106a4
movl -0xa8(%rbp), %eax
addl -0xdc(%rbp), %eax
cmpl $0x0, %eax
jl 0x106a4
movl -0xa8(%rbp), %eax
addl -0xdc(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x106a4
movq -0x100(%rbp), %rax
movq -0x108(%rbp), %rdx
movslq -0xd8(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xe0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x110(%rbp)
movq -0x8(%rbp), %rdi
movl -0xbc(%rbp), %esi
addl -0xd4(%rbp), %esi
movl -0xc0(%rbp), %edx
addl -0xdc(%rbp), %edx
movl -0xc4(%rbp), %ecx
callq 0x83b0
movl %eax, -0x114(%rbp)
movq -0x8(%rbp), %rdi
movl -0xa4(%rbp), %esi
addl -0xd4(%rbp), %esi
movl -0xa8(%rbp), %edx
addl -0xdc(%rbp), %edx
movl -0xac(%rbp), %ecx
callq 0x83b0
movl -0x114(%rbp), %edi
subl %eax, %edi
movl $0x2, %esi
callq 0x1b340
movaps %xmm0, %xmm2
movsd -0x110(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xb8(%rbp)
jmp 0x10761
movq -0x100(%rbp), %rax
movq -0x108(%rbp), %rdx
movslq -0xd8(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xe0(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x120(%rbp)
movq -0x8(%rbp), %rdi
movl -0xbc(%rbp), %esi
addl -0xd4(%rbp), %esi
movl -0xc0(%rbp), %edx
subl -0xdc(%rbp), %edx
movl -0xc4(%rbp), %ecx
callq 0x83b0
movl %eax, -0x124(%rbp)
movq -0x8(%rbp), %rdi
movl -0xa4(%rbp), %esi
addl -0xd4(%rbp), %esi
movl -0xa8(%rbp), %edx
subl -0xdc(%rbp), %edx
movl -0xac(%rbp), %ecx
callq 0x83b0
movl -0x124(%rbp), %edi
subl %eax, %edi
movl $0x2, %esi
callq 0x1b340
movaps %xmm0, %xmm2
movsd -0x120(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xb8(%rbp)
jmp 0x10763
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0x1051b
jmp 0x10788
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0x104f7
movq -0x8(%rbp), %rdi
movl -0xa4(%rbp), %esi
movl -0xa8(%rbp), %edx
movl -0xac(%rbp), %ecx
callq 0x83b0
movl %eax, %ecx
movq -0xf0(%rbp), %rax
cvtsi2sd %ecx, %xmm0
movslq -0x84(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
movl -0x20(%rbp), %edi
movsd 0xba46(%rip), %xmm0     # 0x1c240
callq 0x1b370
movsd -0x130(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd 0xbcbd(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
callq 0x2380
movq -0xf8(%rbp), %rax
movslq -0x84(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x1084f
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x10851
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x10458
jmp 0x10867
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x10440
movl $0x0, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
movl -0x24(%rbp), %ecx
imull -0x24(%rbp), %ecx
cmpl %ecx, %eax
jge 0x108c4
movq -0xf8(%rbp), %rax
movslq -0xe4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x10885
movl $0x0, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movl -0x24(%rbp), %ecx
imull -0x24(%rbp), %ecx
cmpl %ecx, %eax
jge 0x10933
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movslq -0xe8(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0xe8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
divsd -0x70(%rbp), %xmm2
movsd -0x90(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0x108ce
movq -0x10(%rbp), %rdi
movl -0xbc(%rbp), %esi
movl -0xc0(%rbp), %edx
movl -0xc4(%rbp), %ecx
cvttsd2si -0x90(%rbp), %r8d
callq 0x83f0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x103af
jmp 0x1096d
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x10392
jmp 0x10983
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x10375
movq -0x38(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)