// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %r9d, -0x40(%rbp)
movq %rsi, -0x118(%rbp)
movq %rdi, -0xf0(%rbp)
movl %ecx, %eax
movq %rax, -0x90(%rbp)
imulq %rax, %rax
movq %rsp, %rsi
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, -0xa0(%rbp)
movq %rsi, %rsp
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, %esi
negl %esi
movl %esi, -0x3c(%rbp)
movq %rdx, -0x48(%rbp)
imull %edx, %edx
movq %rsp, %rsi
movq %rdx, -0xa8(%rbp)
leaq 0xf(,%rdx,8), %rdx
andq $-0x10, %rdx
subq %rdx, %rsi
movq %rsi, -0xe8(%rbp)
movq %rsi, %rsp
movq %rsp, %rsi
subq %rdx, %rsi
movq %rsi, -0xe0(%rbp)
movq %rsi, %rsp
xorpd %xmm1, %xmm1
cltq
movq %rax, -0x50(%rbp)
movl %ecx, -0xbc(%rbp)
cmpl $-0x1, %ecx
jl 0xa5cf
cvtsi2sd %r8d, %xmm0
mulsd 0x6ff9(%rip), %xmm0     # 0x114c8
movsd 0x6db1(%rip), %xmm2     # 0x11288
divsd %xmm0, %xmm2
movslq -0x3c(%rbp), %rbx
imull %r8d, %r8d
addl %r8d, %r8d
cvtsi2sd %r8, %xmm3
movq -0x50(%rbp), %rsi
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
incl %eax
movl %eax, -0x68(%rbp)
leaq (%rsi,%rbx), %rax
movq -0x90(%rbp), %rdx
imulq %rdx, %rax
leaq (,%rsi,8), %rcx
leaq (%rcx,%rax,8), %rax
leaq (%rax,%rbx,8), %r13
addq -0xa0(%rbp), %r13
leaq (,%rdx,8), %rax
movq %rax, -0x60(%rbp)
cmpq %rbx, %rsi
movq %rbx, %r14
cmovgq %rsi, %r14
incq %r14
movapd 0x709e(%rip), %xmm4     # 0x115e0
movq %rbx, -0x70(%rbp)
movsd %xmm2, -0x98(%rbp)
movsd %xmm3, -0x38(%rbp)
movq %rbx, %r15
imulq %rbx, %r15
movq %r13, -0xb0(%rbp)
movq -0x70(%rbp), %r12
movsd %xmm1, -0x78(%rbp)
movl %r12d, %eax
imull %r12d, %eax
addl %r15d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm4, %xmm0
divsd %xmm3, %xmm0
callq 0x22b0
movapd 0x7050(%rip), %xmm4     # 0x115e0
movsd -0x38(%rbp), %xmm3
movsd -0x98(%rbp), %xmm2
movsd -0x78(%rbp), %xmm1
mulsd %xmm2, %xmm0
movsd %xmm0, (%r13)
addsd %xmm0, %xmm1
incq %r12
addq $0x8, %r13
cmpl %r12d, %r14d
jne 0xa565
incq %rbx
movq -0xb0(%rbp), %r13
addq -0x60(%rbp), %r13
cmpl %ebx, -0x68(%rbp)
jne 0xa553
cmpl $0x0, -0x48(%rbp)
movq -0xf0(%rbp), %rbx
jle 0xa61a
movl -0x48(%rbp), %eax
movq -0x90(%rbp), %rcx
leaq (,%rcx,8), %rcx
xorl %edx, %edx
movq -0xa0(%rbp), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0xa5f9
incq %rdx
addq %rcx, %rsi
cmpq %rax, %rdx
jne 0xa5f7
movl 0xc(%rbx), %eax
testl %eax, %eax
movq -0xe8(%rbp), %r14
movq -0xe0(%rbp), %r15
jle 0xab54
movl $0x1, %ecx
movq -0x48(%rbp), %rsi
leal -0x1(%rsi), %edx
subl %esi, %ecx
shrl $0x1f, %edx
movl %ecx, %edi
shrl $0x1f, %edi
addl %ecx, %edi
addl %esi, %edx
decl %edx
sarl %edi
xorps %xmm0, %xmm0
cvtsi2sdl -0x40(%rbp), %xmm0
sarl %edx
mulsd %xmm0, %xmm0
movsd %xmm0, -0x138(%rbp)
movq -0xa8(%rbp), %r9
cmpq $0x1, %r9
movq %r9, %rcx
adcq $0x0, %rcx
shlq $0x3, %rcx
movq %rcx, -0x110(%rbp)
movq -0x50(%rbp), %r10
movl %r10d, %ecx
negl %ecx
cmovsl %r10d, %ecx
movslq %edi, %rsi
cmpl %edx, %esi
movl %edx, -0xb4(%rbp)
movl %edx, %r8d
movl %edi, -0x80(%rbp)
cmovgl %edi, %r8d
movl -0x3c(%rbp), %edx
movslq %edx, %rdi
movl %r8d, -0xb8(%rbp)
subl %esi, %r8d
incl %r8d
movq %r8, -0x120(%rbp)
cmpl $0x2, %r9d
movl $0x1, %r8d
cmovael %r9d, %r8d
addl %r10d, %ecx
incl %ecx
movq %rcx, -0x158(%rbp)
movl %esi, %ecx
subl %r10d, %ecx
movl %ecx, -0x7c(%rbp)
movq %rsi, -0x128(%rbp)
subq %r10, %rsi
movq %rsi, -0x100(%rbp)
shlq $0x3, -0x90(%rbp)
movq %rdi, -0x108(%rbp)
cmpl %edi, %r10d
movl %edx, %ecx
cmovgl %r10d, %ecx
leal 0x1(%r10,%rcx), %r13d
movl %r10d, %ecx
movq %rcx, -0x150(%rbp)
movl 0x8(%rbx), %ecx
movq $0x0, -0x58(%rbp)
movl %edx, -0x84(%rbp)
movq %r8, -0xf8(%rbp)
testl %ecx, %ecx
jle 0xab39
movl 0x10(%rbx), %eax
movq -0x108(%rbp), %rdx
movq %rdx, -0x70(%rbp)
movq -0x100(%rbp), %rdx
movq %rdx, -0xd8(%rbp)
xorl %r9d, %r9d
testl %eax, %eax
jle 0xab1c
xorl %r11d, %r11d
movq %r9, -0x50(%rbp)
cmpl $0x0, -0x48(%rbp)
movl %r11d, -0x60(%rbp)
je 0xa7a2
movq %r14, %rdi
xorl %esi, %esi
movq -0x110(%rbp), %r12
movq %r12, %rdx
callq 0x20c0
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20c0
movl -0x60(%rbp), %r11d
movq -0x50(%rbp), %r9
movq -0xf8(%rbp), %r8
movl -0xb4(%rbp), %eax
cmpl %eax, -0x80(%rbp)
jg 0xaa9e
xorl %ecx, %ecx
movl -0x7c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x80(%rbp), %eax
movq %rax, %rdx
movq -0x58(%rbp), %rax
movq %rdx, -0x130(%rbp)
addl %edx, %eax
movl %eax, -0x2c(%rbp)
movslq %ecx, %r12
movq -0x120(%rbp), %rax
leal (%rax,%r12), %ecx
movq -0xd8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x128(%rbp), %rax
movl %ecx, -0xc0(%rbp)
cmpl $0x0, -0x2c(%rbp)
js 0xaa6f
movl 0xc(%rbx), %edx
movl %edx, -0xc8(%rbp)
cmpl %edx, -0x2c(%rbp)
jge 0xaa6f
movq %rax, %rdx
addq %r9, %rdx
js 0xaa6f
movslq 0x8(%rbx), %rsi
movq %rsi, -0x38(%rbp)
cmpq %rsi, %rdx
jge 0xaa6f
movq %rdx, -0x98(%rbp)
movq %rax, -0x140(%rbp)
movq %r12, -0x148(%rbp)
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edi
xorpd %xmm0, %xmm0
cmpl $-0x1, -0xbc(%rbp)
jl 0xa9f2
movl -0x88(%rbp), %r8d
movq -0x38(%rbp), %rax
imull %eax, %r8d
addl -0x68(%rbp), %r8d
imull %edi, %r8d
movl -0x84(%rbp), %ebx
imull %eax, %ebx
imull %edi, %eax
movl %eax, -0xc4(%rbp)
addl -0x70(%rbp), %ebx
imull %edi, %ebx
movq -0xa0(%rbp), %rax
movq %rax, -0x78(%rbp)
xorl %ecx, %ecx
movl -0x3c(%rbp), %r12d
movq %rcx, -0x160(%rbp)
movl %r12d, %r10d
addl -0x58(%rbp), %r10d
sets %al
movl -0xc8(%rbp), %edx
cmpl %edx, %r10d
setge %r15b
movl %r12d, -0xcc(%rbp)
addl -0x2c(%rbp), %r12d
sets %cl
cmpl %edx, %r12d
setge %dl
orb %cl, %dl
movb %dl, -0xb0(%rbp)
movq -0x38(%rbp), %rcx
imull %ecx, %r10d
imull %ecx, %r12d
orb %al, %r15b
movl %ebx, -0x40(%rbp)
movl %ebx, %edx
movl %r8d, -0xa8(%rbp)
movl %r8d, %eax
xorl %ebx, %ebx
movq -0x150(%rbp), %r14
testb %r15b, %r15b
jne 0xa94f
movq -0x70(%rbp), %rcx
addq %rbx, %rcx
sets %r8b
cmpq -0x38(%rbp), %rcx
setge %cl
orb %r8b, %cl
orb -0xb0(%rbp), %cl
testb $0x1, %cl
jne 0xa94f
movq -0x68(%rbp), %rcx
addq %rbx, %rcx
setns %r9b
cmpq -0x38(%rbp), %rcx
movq %r13, %r11
setl %r13b
movl %eax, %r8d
movl %edx, %ecx
testb %r13b, %r9b
movq -0x50(%rbp), %r9
movq %r11, %r13
movl -0x60(%rbp), %r11d
jne 0xa96a
leal (%r14,%r9), %ecx
addl %r10d, %ecx
imull %edi, %ecx
movq -0x98(%rbp), %r8
addl %r14d, %r8d
addl %r12d, %r8d
imull %edi, %r8d
addl %r11d, %ecx
movslq %ecx, %rcx
movzbl (%rsi,%rcx), %ecx
addl %r11d, %r8d
movslq %r8d, %r8
movzbl (%rsi,%r8), %r8d
subl %r8d, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm1, %xmm1
movq -0x78(%rbp), %rcx
mulsd (%rcx,%rbx,8), %xmm1
addsd %xmm1, %xmm0
incq %rbx
decq %r14
addl %edi, %eax
addl %edi, %edx
cmpq %rbx, %r13
jne 0xa8ff
movl -0xcc(%rbp), %r12d
incl %r12d
movq -0x160(%rbp), %rcx
incq %rcx
movl -0xa8(%rbp), %r8d
movl -0xc4(%rbp), %eax
addl %eax, %r8d
movl -0x40(%rbp), %ebx
addl %eax, %ebx
movq -0x78(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq -0x158(%rbp), %rcx
jne 0xa89e
xorpd 0x6be6(%rip), %xmm0     # 0x115e0
movq -0x38(%rbp), %rcx
imull -0x2c(%rbp), %ecx
movq -0x98(%rbp), %rax
addl %ecx, %eax
imull %edi, %eax
addl %r11d, %eax
cltq
movzbl (%rsi,%rax), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movq -0xe0(%rbp), %r15
movq -0x148(%rbp), %r12
movsd %xmm1, (%r15,%r12,8)
divsd -0x138(%rbp), %xmm0
callq 0x22b0
movl -0x60(%rbp), %r11d
movq -0x50(%rbp), %r9
movq -0xe8(%rbp), %r14
movsd %xmm0, (%r14,%r12,8)
movq -0xf0(%rbp), %rbx
movq -0xf8(%rbp), %r8
movl -0xc0(%rbp), %ecx
movq -0x140(%rbp), %rax
incq %r12
incq %rax
incq -0x68(%rbp)
cmpl %r12d, %ecx
jne 0xa7f8
movq -0x130(%rbp), %rdx
leal 0x1(%rdx), %eax
incl -0x88(%rbp)
cmpl -0xb8(%rbp), %edx
jne 0xa7bf
cmpl $0x0, -0x48(%rbp)
je 0xaae6
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%r14,%rax,8), %xmm0
incq %rax
cmpq %rax, %r8
jne 0xaaaa
cmpl $0x0, -0x48(%rbp)
je 0xaae6
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm2
divsd %xmm0, %xmm2
mulsd (%r15,%rax,8), %xmm2
addsd %xmm2, %xmm1
incq %rax
cmpq %rax, %r8
jne 0xaac4
cvttsd2si %xmm1, %eax
jmp 0xaae8
xorl %eax, %eax
movq -0x118(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x58(%rbp), %edx
addl %r9d, %edx
imull 0x10(%rsi), %edx
addl %r11d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r11d
movl 0x10(%rbx), %eax
cmpl %eax, %r11d
jl 0xa768
movl 0x8(%rbx), %ecx
incq %r9
movslq %ecx, %rdx
incq -0xd8(%rbp)
incq -0x70(%rbp)
cmpq %rdx, %r9
jl 0xa759
movl 0xc(%rbx), %eax
movq -0x58(%rbp), %rsi
incl %esi
incl -0x7c(%rbp)
incl -0x84(%rbp)
movq %rsi, -0x58(%rbp)
cmpl %eax, %esi
jl 0xa732
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop