// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::outlier_filter(Image&, Image&, int, float)
void ImageProc::outlier_filter(Image &img_orig, Image &img_modified, int window_size, float threshold)
{
	int ws = window_size;
	int kernel_radius = ws / 2;
	int values[ws][ws];
	int values_sum = 0;
	int new_value = 0;
	int diff;

	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++)
		 	{
				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values[row][col] = 0;

				values_sum = 0;
				new_value = 0;
				diff = 0.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);
							values_sum += values[row][col];
							//std::cout << values[row][col] << std::endl;

						}
						else
						{
							values[row][col] = img_orig.getvalue((i+m),(j-n),k);
							values_sum += values[row][col];
							//std::cout << values[row][col] << std::endl;
						}
					}

				//std::cout << values_sum << std::endl;

				diff = abs((img_orig.getvalue(i,j,k)) - ((1.0/((ws*ws)-1)) * (values_sum - img_orig.getvalue(i,j,k))));
				//std::cout << "diff : " << diff << std::endl;
				//std::cout << "gt : " << (img_orig.getvalue(i,j,k)) << " " << ((1.0/((ws*ws)-1)) * (values_sum - img_orig.getvalue(i,j,k))) << std::endl;
				if(diff > threshold)
				{
					new_value = (1.0/((ws*ws)-1)) * (values_sum - img_orig.getvalue(i,j,k));
					//std::cout << "new_value : " << new_value  << " " << threshold << std::endl;
					img_modified.setvalue(i,j,k,new_value);
				}
				else
					img_modified.setvalue(i,j,k,img_orig.getvalue(i,j,k));
			}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edx, -0x2c(%rsp)
movq %rsi, -0x10(%rsp)
cmpl $0x0, 0xc(%rdi)
jle 0x981d
movl -0x2c(%rsp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
imull %eax, %eax
decl %eax
movl %ecx, %edx
cvtsi2sd %eax, %xmm2
negl %edx
movsd 0x7c35(%rip), %xmm1     # 0x11288
divsd %xmm2, %xmm1
movapd 0x7f01(%rip), %xmm2     # 0x11560
xorpd %xmm1, %xmm2
movl %ecx, %eax
cmovnsl %edx, %eax
movl %eax, -0x20(%rsp)
cmpl %edx, %ecx
movl %edx, %r9d
movl %ecx, -0x24(%rsp)
cmovgl %ecx, %r9d
incl %r9d
movl $0x0, -0x38(%rsp)
movapd 0x7ec4(%rip), %xmm3     # 0x11550
movl %edx, -0x28(%rsp)
movl %edx, -0x34(%rsp)
movq %rdi, -0x18(%rsp)
cmpl $0x0, 0x8(%rdi)
jle 0x9806
xorl %ebx, %ebx
movl -0x24(%rsp), %eax
movl %eax, -0x30(%rsp)
movl 0x10(%rdi), %r15d
testl %r15d, %r15d
jle 0x97f7
xorl %r14d, %r14d
movl $0x0, %r12d
cmpl $-0x1, -0x2c(%rsp)
jl 0x976c
movq -0x18(%rsp), %rax
movl 0x8(%rax), %r13d
movl 0xc(%rax), %ecx
movl %ecx, -0x1c(%rsp)
movq 0x18(%rax), %r11
movl -0x34(%rsp), %eax
imull %r13d, %eax
addl -0x30(%rsp), %eax
xorl %r12d, %r12d
movl -0x28(%rsp), %ecx
movq %rcx, -0x8(%rsp)
addl -0x38(%rsp), %ecx
setns %dil
cmpl -0x1c(%rsp), %ecx
setl %sil
andb %dil, %sil
imull %r13d, %ecx
movl %eax, %edi
movl -0x28(%rsp), %r8d
movl %edi, %ebp
testb %sil, %sil
je 0x973f
movl %r8d, %r10d
addl %ebx, %r10d
setns %bpl
cmpl %r13d, %r10d
setl %dl
andb %bpl, %dl
movl %edi, %ebp
cmpb $0x1, %dl
jne 0x973f
addl %ecx, %r10d
movl %r10d, %ebp
imull %r15d, %ebp
addl %r14d, %ebp
movslq %ebp, %rdx
movzbl (%r11,%rdx), %edx
addl %edx, %r12d
incl %r8d
decl %edi
cmpl %r8d, %r9d
jne 0x9718
movq -0x8(%rsp), %rdx
leal 0x1(%rdx), %ecx
addl %r13d, %eax
cmpl -0x20(%rsp), %edx
jne 0x96f5
movq -0x18(%rsp), %rdi
movq 0x18(%rdi), %rax
movl 0x8(%rdi), %ecx
movl -0x38(%rsp), %r8d
imull %r8d, %ecx
addl %ebx, %ecx
imull %r15d, %ecx
addl %r14d, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
subl %eax, %r12d
xorps %xmm5, %xmm5
cvtsi2sd %r12d, %xmm5
movapd %xmm2, %xmm6
mulsd %xmm5, %xmm6
addsd %xmm4, %xmm6
andpd %xmm3, %xmm6
cvttpd2dq %xmm6, %xmm4
cvtdq2ps %xmm4, %xmm4
ucomiss %xmm0, %xmm4
mulsd %xmm1, %xmm5
cvttsd2si %xmm5, %ecx
cmovbel %eax, %ecx
movq -0x10(%rsp), %rsi
movq 0x18(%rsi), %rax
movl 0x8(%rsi), %edx
imull %r8d, %edx
addl %ebx, %edx
imull 0x10(%rsi), %edx
addl %r14d, %edx
movslq %edx, %rdx
movb %cl, (%rax,%rdx)
incl %r14d
movl 0x10(%rdi), %r15d
cmpl %r15d, %r14d
jl 0x96bd
incl %ebx
incl -0x30(%rsp)
cmpl 0x8(%rdi), %ebx
jl 0x96ad
movl -0x38(%rsp), %eax
incl %eax
incl -0x34(%rsp)
movl %eax, -0x38(%rsp)
cmpl 0xc(%rdi), %eax
jl 0x9699
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq