// File: /workspace/github_cpp_makefile_star2_O1/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, -0x110(%rbp)
movq %rdi, -0xb0(%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 %rdx, -0xa8(%rbp)
leaq 0xf(,%rdx,8), %rsi
movabsq $0x7fffffff0, %rdx      # imm = 0x7FFFFFFF0
andq %rsi, %rdx
movq %rsp, %rsi
subq %rdx, %rsi
movq %rsi, -0xf0(%rbp)
movq %rsi, %rsp
movq %rsp, %rsi
subq %rdx, %rsi
movq %rsi, -0xe8(%rbp)
movq %rsi, %rsp
xorpd %xmm1, %xmm1
cltq
movq %rax, -0x60(%rbp)
movl %ecx, -0xc4(%rbp)
cmpl $-0x1, %ecx
jl 0xa508
cvtsi2sd %r8d, %xmm0
mulsd 0x7120(%rip), %xmm0     # 0x11528
movsd 0x6e78(%rip), %xmm2     # 0x11288
divsd %xmm0, %xmm2
movslq -0x3c(%rbp), %r13
imull %r8d, %r8d
addl %r8d, %r8d
cvtsi2sd %r8, %xmm3
movq -0x60(%rbp), %rsi
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
incl %eax
movl %eax, -0x70(%rbp)
leaq (%rsi,%r13), %rax
movq -0x90(%rbp), %rdx
imulq %rdx, %rax
leaq (,%rsi,8), %rcx
leaq (%rcx,%rax,8), %rax
leaq (%rax,%r13,8), %r12
addq -0xa0(%rbp), %r12
leaq (,%rdx,8), %rax
movq %rax, -0x68(%rbp)
cmpq %r13, %rsi
movq %r13, %r14
cmovgq %rsi, %r14
incq %r14
movapd 0x70e5(%rip), %xmm4     # 0x11560
movq %r13, -0xb8(%rbp)
movsd %xmm2, -0x98(%rbp)
movsd %xmm3, -0x38(%rbp)
movq %r13, %rbx
imulq %r13, %rbx
movq %r12, -0x78(%rbp)
movq -0xb8(%rbp), %r15
movsd %xmm1, -0x50(%rbp)
movl %r15d, %eax
imull %r15d, %eax
addl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm4, %xmm0
divsd %xmm3, %xmm0
callq 0x22c0
movapd 0x7095(%rip), %xmm4     # 0x11560
movsd -0x38(%rbp), %xmm3
movsd -0x98(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
movsd %xmm0, (%r12)
addsd %xmm0, %xmm1
incq %r15
addq $0x8, %r12
cmpl %r15d, %r14d
jne 0xa4a1
incq %r13
movq -0x78(%rbp), %r12
addq -0x68(%rbp), %r12
cmpl %r13d, -0x70(%rbp)
jne 0xa48f
cmpl $0x0, -0x48(%rbp)
movq -0xb0(%rbp), %rbx
jle 0xa553
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 0xa532
incq %rdx
addq %rcx, %rsi
cmpq %rax, %rdx
jne 0xa530
cmpl $0x0, 0xc(%rbx)
movq -0xa8(%rbp), %r14
movq -0xf0(%rbp), %r15
movq -0xe8(%rbp), %r12
jle 0xaa77
movl $0x1, %eax
movq -0x48(%rbp), %rdx
leal -0x1(%rdx), %ecx
subl %edx, %eax
shrl $0x1f, %ecx
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
addl %ecx, %edx
decl %edx
sarl %esi
sarl %edx
xorps %xmm0, %xmm0
cvtsi2sdl -0x40(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x130(%rbp)
cmpl $0x1, %r14d
adcl $0x0, %r14d
leaq (,%r14,8), %rax
movq %rax, -0x108(%rbp)
movq -0x60(%rbp), %r8
movl %r8d, %eax
movq %rax, -0x150(%rbp)
movl -0x3c(%rbp), %ecx
movslq %ecx, %rdi
movl %r8d, %eax
negl %eax
cmovsl %r8d, %eax
addl %r8d, %eax
incl %eax
movq %rax, -0x148(%rbp)
movslq %esi, %rax
cmpl %edx, %eax
movl %edx, -0xbc(%rbp)
movl %esi, -0x80(%rbp)
cmovgl %esi, %edx
movl %edx, -0xc0(%rbp)
subl %eax, %edx
incl %edx
movq %rdx, -0x118(%rbp)
movl %eax, %edx
subl %r8d, %edx
movl %edx, -0x7c(%rbp)
movq %rax, -0x120(%rbp)
subq %r8, %rax
movq %rax, -0xf8(%rbp)
shlq $0x3, -0x90(%rbp)
movq %rdi, -0x100(%rbp)
cmpl %edi, %r8d
movl %r8d, %eax
cmovlel %ecx, %eax
leal 0x1(%r8,%rax), %eax
movq %rax, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movl %ecx, -0x84(%rbp)
movq %r14, -0xa8(%rbp)
cmpl $0x0, 0x8(%rbx)
jle 0xaa5b
movq -0x100(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0x38(%rbp)
cmpl $0x0, 0x10(%rbx)
jle 0xaa38
movl $0x0, -0x50(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0xa6ba
movq %r15, %rdi
xorl %esi, %esi
movq -0x108(%rbp), %r13
movq %r13, %rdx
callq 0x20c0
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20c0
movl -0xbc(%rbp), %eax
cmpl %eax, -0x80(%rbp)
jg 0xa9b6
xorl %r13d, %r13d
movl -0x7c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x80(%rbp), %eax
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x128(%rbp)
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
movslq %r13d, %r13
movq -0x118(%rbp), %rax
leal (%rax,%r13), %ecx
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x120(%rbp), %rdx
movl %ecx, -0xc8(%rbp)
cmpl $0x0, -0x2c(%rbp)
js 0xa987
movl -0x2c(%rbp), %eax
cmpl 0xc(%rbx), %eax
jge 0xa987
movq %rdx, %rsi
addq -0x38(%rbp), %rsi
js 0xa987
movslq 0x8(%rbx), %rax
cmpq %rax, %rsi
jge 0xa987
movq %rsi, -0x98(%rbp)
movq %rdx, -0x138(%rbp)
movq %r13, -0x140(%rbp)
xorpd %xmm0, %xmm0
cmpl $-0x1, -0xc4(%rbp)
movq -0x60(%rbp), %r9
jl 0xa90d
movq -0xb0(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0xd0(%rbp)
movl 0x10(%rax), %edx
movslq 0x8(%rax), %rcx
movq 0x18(%rax), %rdi
movl -0x88(%rbp), %r8d
imull %ecx, %r8d
addl -0x68(%rbp), %r8d
imull %edx, %r8d
movl -0x84(%rbp), %ebx
imull %ecx, %ebx
movq %rcx, -0x78(%rbp)
movl %ecx, %eax
imull %edx, %eax
movl %eax, -0xcc(%rbp)
addl -0x70(%rbp), %ebx
imull %edx, %ebx
movq -0xa0(%rbp), %r11
xorl %ecx, %ecx
movl -0x3c(%rbp), %r12d
movq %rcx, -0x158(%rbp)
movl %r12d, %r10d
addl -0x58(%rbp), %r10d
sets %al
movl -0xd0(%rbp), %esi
cmpl %esi, %r10d
setge %r15b
movl %r12d, -0xd4(%rbp)
addl -0x2c(%rbp), %r12d
sets %cl
cmpl %esi, %r12d
setge %sil
orb %cl, %sil
movb %sil, -0xb8(%rbp)
movq -0x78(%rbp), %rcx
imull %ecx, %r10d
imull %ecx, %r12d
orb %al, %r15b
movl %ebx, -0xd8(%rbp)
movl %ebx, %eax
movl %r8d, -0x40(%rbp)
movl %r8d, %esi
xorl %ebx, %ebx
movq -0x150(%rbp), %r13
testb %r15b, %r15b
jne 0xa86e
movq -0x70(%rbp), %rcx
addq %rbx, %rcx
sets %r8b
cmpq -0x78(%rbp), %rcx
setge %cl
orb %r8b, %cl
orb -0xb8(%rbp), %cl
testb $0x1, %cl
jne 0xa86e
movq -0x68(%rbp), %rcx
addq %rbx, %rcx
setns %r9b
cmpq -0x78(%rbp), %rcx
setl %r14b
movl %esi, %r8d
movl %eax, %ecx
testb %r14b, %r9b
movq -0x60(%rbp), %r9
jne 0xa88c
movq -0x38(%rbp), %rcx
addl %r13d, %ecx
addl %r10d, %ecx
imull %edx, %ecx
movq -0x98(%rbp), %r8
addl %r13d, %r8d
addl %r12d, %r8d
imull %edx, %r8d
movl -0x50(%rbp), %r14d
addl %r14d, %ecx
movslq %ecx, %rcx
movzbl (%rdi,%rcx), %ecx
addl %r14d, %r8d
movslq %r8d, %r8
movzbl (%rdi,%r8), %r8d
subl %r8d, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd %xmm1, %xmm1
mulsd (%r11,%rbx,8), %xmm1
addsd %xmm1, %xmm0
incq %rbx
decq %r13
addl %edx, %esi
addl %edx, %eax
cmpq %rbx, %r9
jne 0xa828
movl -0xd4(%rbp), %r12d
incl %r12d
movq -0x158(%rbp), %rcx
incq %rcx
movl -0x40(%rbp), %r8d
movl -0xcc(%rbp), %eax
addl %eax, %r8d
movl -0xd8(%rbp), %ebx
addl %eax, %ebx
addq -0x90(%rbp), %r11
cmpq -0x148(%rbp), %rcx
jne 0xa7c4
xorpd 0x6c4b(%rip), %xmm0     # 0x11560
movq -0xb0(%rbp), %rbx
movl 0x8(%rbx), %eax
imull -0x2c(%rbp), %eax
movq -0x98(%rbp), %rcx
addl %eax, %ecx
imull 0x10(%rbx), %ecx
movq 0x18(%rbx), %rax
addl -0x50(%rbp), %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movq -0xe8(%rbp), %r12
movq -0x140(%rbp), %r13
movsd %xmm1, (%r12,%r13,8)
divsd -0x130(%rbp), %xmm0
callq 0x22c0
movq -0xf0(%rbp), %r15
movsd %xmm0, (%r15,%r13,8)
movq -0xa8(%rbp), %r14
movl -0xc8(%rbp), %ecx
movq -0x138(%rbp), %rdx
incq %r13
incq %rdx
incq -0x68(%rbp)
cmpl %r13d, %ecx
jne 0xa711
movq -0x128(%rbp), %rcx
leal 0x1(%rcx), %eax
incl -0x88(%rbp)
cmpl -0xc0(%rbp), %ecx
jne 0xa6d8
cmpl $0x0, -0x48(%rbp)
je 0xa9d2
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%r15,%rax,8), %xmm0
incq %rax
cmpq %rax, %r14
jne 0xa9c2
jmp 0xa9d6
xorpd %xmm0, %xmm0
cmpl $0x0, -0x48(%rbp)
je 0xaa04
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm2
divsd %xmm0, %xmm2
mulsd (%r12,%rax,8), %xmm2
addsd %xmm2, %xmm1
incq %rax
cmpq %rax, %r14
jne 0xa9e2
cvttsd2si %xmm1, %eax
jmp 0xaa06
xorl %eax, %eax
movq -0x110(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x58(%rbp), %edx
addl -0x38(%rbp), %edx
imull 0x10(%rsi), %edx
movl -0x50(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %esi
movl %esi, -0x50(%rbp)
cmpl 0x10(%rbx), %esi
jl 0xa693
movq -0x38(%rbp), %rdx
incq %rdx
movslq 0x8(%rbx), %rax
incq -0xe0(%rbp)
incq -0x70(%rbp)
movq %rdx, -0x38(%rbp)
cmpq %rax, %rdx
jl 0xa682
movq -0x58(%rbp), %rcx
incl %ecx
incl -0x7c(%rbp)
incl -0x84(%rbp)
movq %rcx, -0x58(%rbp)
cmpl 0xc(%rbx), %ecx
jl 0xa657
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq