// File: /workspace/github_cpp_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
movl %r9d, -0x34(%rbp)
movl %ecx, %eax
movl %edx, %r9d
movq %rsi, -0x108(%rbp)
movq %rdi, -0xf8(%rbp)
pushq $0x1
popq %rcx
subl %edx, %ecx
movl %ecx, -0x30(%rbp)
leal -0x1(%r9), %ecx
movl %ecx, -0x70(%rbp)
movl %eax, %esi
movq %rsi, %rcx
imulq %rsi, %rcx
movq %rsp, %r13
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r13
movq %r13, %rsp
pushq $0x2
popq %rcx
cltd
idivl %ecx
movq %r9, -0xb0(%rbp)
movl %r9d, %ecx
imull %ecx, %ecx
movq %rsp, %rdx
movq %rcx, -0x88(%rbp)
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0x90(%rbp)
movq %rdx, %rsp
cvtsi2sd %r8d, %xmm0
mulsd 0x776c(%rip), %xmm0     # 0x104c8
movsd 0x7524(%rip), %xmm1     # 0x10288
movslq %eax, %rcx
movl %eax, -0xc0(%rbp)
negl %eax
imull %r8d, %r8d
addl %r8d, %r8d
cvtsi2sd %r8, %xmm2
movsd %xmm2, -0x78(%rbp)
movl %eax, -0x4c(%rbp)
movslq %eax, %r14
leal 0x1(%rcx), %eax
cmpl %eax, %r14d
cmovgl %r14d, %eax
divsd %xmm0, %xmm1
movsd %xmm1, -0x80(%rbp)
movq %rax, -0xa8(%rbp)
cltq
movq %rax, -0xb8(%rbp)
shlq $0x3, %rsi
movq %rsi, -0x98(%rbp)
movq %rcx, -0x68(%rbp)
leaq 0x1(,%rcx,2), %r12
xorpd %xmm1, %xmm1
movq %r13, -0xf0(%rbp)
movq %r14, -0xa0(%rbp)
cmpq -0xb8(%rbp), %r14
je 0x8e44
movsd %xmm1, -0x40(%rbp)
movq %r14, %rbx
imulq %r14, %rbx
xorl %r15d, %r15d
cmpq %r15, %r12
je 0x8e33
movq -0xa0(%rbp), %rax
addq %r15, %rax
imull %eax, %eax
addl %ebx, %eax
cvtsi2sd %eax, %xmm0
xorpd 0x7796(%rip), %xmm0     # 0x105a0
divsd -0x78(%rbp), %xmm0
callq 0x22a0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, (%r13,%r15,8)
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
incq %r15
jmp 0x8dea
incq %r14
addq -0x98(%rbp), %r13
movsd -0x40(%rbp), %xmm1
jmp 0x8dd2
movl -0x30(%rbp), %eax
cltd
pushq $0x2
popq %r9
idivl %r9d
movq %rax, -0xe0(%rbp)
xorl %eax, %eax
movq -0xb0(%rbp), %rdx
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %edx, %edx
movq -0xf0(%rbp), %rsi
movq -0xf8(%rbp), %r8
cmpq %rcx, %rax
je 0x8ea4
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x8e98
movsd (%rsi,%rdi,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
jmp 0x8e80
incq %rax
addq -0x98(%rbp), %rsi
jmp 0x8e79
movl -0x70(%rbp), %eax
cltd
idivl %r9d
movl %eax, %ecx
cvtsi2sdl -0x34(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, -0x120(%rbp)
movq -0x68(%rbp), %r9
leal (%r9,%r9), %edx
movq -0xa8(%rbp), %rax
addl %r9d, %eax
movq %rax, -0xa8(%rbp)
movq -0xe0(%rbp), %rax
movslq %eax, %rsi
movl %ecx, -0xc4(%rbp)
movslq %ecx, %rdi
incq %rdx
movq %rdx, -0x40(%rbp)
leal (%rax,%r9), %ecx
movl %ecx, -0xbc(%rbp)
subl %r9d, %eax
movl %eax, -0x44(%rbp)
movq %rsi, -0x110(%rbp)
subq %r9, %rsi
movq %rsi, -0x100(%rbp)
xorl %ebx, %ebx
movl -0x4c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq %rdi, -0x118(%rbp)
cmpl 0xc(%r8), %ebx
jge 0x930e
movq -0x100(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0x48(%rbp)
xorl %r9d, %r9d
movq %rbx, -0x140(%rbp)
movslq 0x8(%r8), %rax
cmpq %rax, %r9
jge 0x9301
xorl %r13d, %r13d
movq %r9, -0x68(%rbp)
cmpl 0x10(%r8), %r13d
jge 0x92e8
xorl %eax, %eax
cmpq %rax, -0x88(%rbp)
je 0x8fa3
movq -0x60(%rbp), %rcx
andq $0x0, (%rcx,%rax,8)
movq -0x90(%rbp), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x8f80
movl -0x44(%rbp), %eax
movl %eax, -0x58(%rbp)
xorl %ecx, %ecx
movq -0xe0(%rbp), %rax
cmpl -0xc4(%rbp), %eax
jg 0x926d
leal (%rax,%rbx), %edx
movl %edx, -0x2c(%rbp)
movslq %ecx, %rcx
movq -0xe8(%rbp), %rdx
movq %rdx, -0x78(%rbp)
movl -0x48(%rbp), %r10d
movq -0x110(%rbp), %rsi
movq %rax, -0x128(%rbp)
cmpq %rdi, %rsi
jg 0x9263
cmpl $0x0, -0x2c(%rbp)
js 0x9251
movl 0xc(%r8), %edx
movl %edx, -0xd0(%rbp)
cmpl %edx, -0x2c(%rbp)
jge 0x9251
movq %rsi, %r11
addq %r9, %r11
js 0x9251
movslq 0x8(%r8), %rdx
cmpq %rdx, %r11
jge 0x9251
movq %r11, -0x70(%rbp)
movq %rsi, -0x130(%rbp)
movq %rcx, -0x138(%rbp)
movq 0x18(%r8), %rsi
movl 0x10(%r8), %edi
movl -0x58(%rbp), %r14d
imull %edx, %r14d
movl %r10d, -0xc8(%rbp)
addl %r10d, %r14d
imull %edi, %r14d
movl -0x50(%rbp), %r10d
imull %edx, %r10d
movl %edx, %eax
imull %edi, %eax
movl %eax, -0xcc(%rbp)
addl -0x54(%rbp), %r10d
imull %edi, %r10d
xorpd %xmm0, %xmm0
movq -0xf0(%rbp), %r11
xorl %ecx, %ecx
movl -0x4c(%rbp), %r15d
cmpq -0xa8(%rbp), %rcx
je 0x91da
movq %rcx, -0x148(%rbp)
movl %r15d, %r12d
addl %ebx, %r12d
sets %al
movl -0xd0(%rbp), %r9d
cmpl %r9d, %r12d
setge %r8b
movl %r15d, -0xd4(%rbp)
movl %r15d, %ebx
addl -0x2c(%rbp), %ebx
sets %cl
cmpl %r9d, %ebx
setge %r15b
orb %cl, %r15b
imull %edx, %r12d
movl %r12d, -0xb8(%rbp)
imull %edx, %ebx
movl %ebx, -0x34(%rbp)
orb %al, %r8b
movl %r10d, -0x30(%rbp)
movl %r10d, %eax
movl %r14d, -0xb0(%rbp)
movl %r14d, %r9d
xorl %r14d, %r14d
movq -0xa0(%rbp), %r12
cmpq %r14, -0x40(%rbp)
je 0x919c
movl %r9d, %ebx
movl %eax, %ecx
testb %r8b, %r8b
jne 0x9162
movq -0x80(%rbp), %rcx
addq %r14, %rcx
sets %bl
cmpq %rdx, %rcx
setge %r10b
orb %bl, %r10b
orb %r15b, %r10b
movl %r9d, %ebx
movl %eax, %ecx
testb $0x1, %r10b
jne 0x9162
movq -0x78(%rbp), %rcx
addq %r14, %rcx
sets %r10b
cmpq %rdx, %rcx
setge %cl
orb %r10b, %cl
movl %r9d, %ebx
movl %eax, %ecx
jne 0x9162
movq -0x68(%rbp), %rcx
addq %r12, %rcx
movq -0x70(%rbp), %r10
leaq (%r12,%r10), %rbx
addl -0xb8(%rbp), %ecx
imull %edi, %ecx
addl -0x34(%rbp), %ebx
imull %edi, %ebx
addl %r13d, %ecx
movslq %ecx, %rcx
movzbl (%rsi,%rcx), %ecx
addl %r13d, %ebx
movslq %ebx, %r10
movzbl (%rsi,%r10), %r10d
subl %r10d, %ecx
cvtsi2sd %ecx, %xmm1
mulsd %xmm1, %xmm1
mulsd (%r11,%r14,8), %xmm1
addsd %xmm1, %xmm0
incq %r12
incq %r14
subl %edi, %r9d
subl %edi, %eax
jmp 0x90f3
movl -0xd4(%rbp), %r15d
incl %r15d
movq -0x148(%rbp), %rcx
incq %rcx
addq -0x98(%rbp), %r11
movl -0xb0(%rbp), %r14d
movl -0xcc(%rbp), %eax
addl %eax, %r14d
movl -0x30(%rbp), %r10d
addl %eax, %r10d
movq -0x140(%rbp), %rbx
jmp 0x907f
imull -0x2c(%rbp), %edx
movq -0x70(%rbp), %rax
addl %edx, %eax
imull %edi, %eax
addl %r13d, %eax
cltq
movzbl (%rsi,%rax), %eax
cvtsi2sd %eax, %xmm1
movq -0x90(%rbp), %rax
movq -0x138(%rbp), %r14
movsd %xmm1, (%rax,%r14,8)
xorpd 0x7390(%rip), %xmm0     # 0x105a0
divsd -0x120(%rbp), %xmm0
callq 0x22a0
movq %r14, %rcx
movq -0x60(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
movq -0xf8(%rbp), %r8
movq -0x118(%rbp), %rdi
movq -0x68(%rbp), %r9
movq -0x128(%rbp), %rax
movl -0xc8(%rbp), %r10d
movq -0x130(%rbp), %rsi
incq %rcx
incq %rsi
incl %r10d
incq -0x78(%rbp)
jmp 0x8fe4
incl %eax
incl -0x58(%rbp)
jmp 0x8fb2
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq %rax, -0x88(%rbp)
je 0x928a
movq -0x60(%rbp), %rcx
addsd (%rcx,%rax,8), %xmm0
incq %rax
jmp 0x9273
xorpd %xmm1, %xmm1
xorl %eax, %eax
cmpq %rax, -0x88(%rbp)
je 0x92bb
movq -0x60(%rbp), %rcx
movsd (%rcx,%rax,8), %xmm2
divsd %xmm0, %xmm2
movq -0x90(%rbp), %rcx
mulsd (%rcx,%rax,8), %xmm2
addsd %xmm2, %xmm1
incq %rax
jmp 0x9290
cvttsd2si %xmm1, %eax
movq -0x108(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull %ebx, %edx
addl %r9d, %edx
imull 0x10(%rsi), %edx
addl %r13d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r13d
jmp 0x8f74
incq %r9
incl -0x48(%rbp)
incl -0x54(%rbp)
incq -0x80(%rbp)
incq -0xe8(%rbp)
jmp 0x8f60
incl %ebx
incl -0x44(%rbp)
incl -0x50(%rbp)
jmp 0x8f21
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop