// File: /workspace/github_cpp_makefile_star2_O3/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)
movl 0xc(%rdi), %edx
testl %edx, %edx
jle 0x98aa
movl -0x2c(%rsp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
imull %eax, %eax
decl %eax
movl %ecx, %esi
negl %esi
cvtsi2sd %eax, %xmm2
movsd 0x7ba4(%rip), %xmm1     # 0x11288
divsd %xmm2, %xmm1
movapd 0x7ef0(%rip), %xmm2     # 0x115e0
xorpd %xmm1, %xmm2
movl %ecx, %eax
cmovnsl %esi, %eax
movl %eax, -0x20(%rsp)
movl 0x8(%rdi), %eax
cmpl %esi, %ecx
movl %esi, %r9d
movl %ecx, -0x24(%rsp)
cmovgl %ecx, %r9d
incl %r9d
movl $0x0, -0x38(%rsp)
movapd 0x7dd0(%rip), %xmm3     # 0x114f0
movl %esi, -0x28(%rsp)
movl %esi, -0x34(%rsp)
movq %rdi, -0x18(%rsp)
testl %eax, %eax
jle 0x9894
movl 0x10(%rdi), %r15d
xorl %ebx, %ebx
movl -0x24(%rsp), %ecx
movl %ecx, -0x30(%rsp)
testl %r15d, %r15d
jle 0x9883
xorl %r14d, %r14d
movq 0x18(%rdi), %r12
movl 0x8(%rdi), %r13d
movl $0x0, %eax
cmpl $-0x1, -0x2c(%rsp)
jl 0x97fe
movq -0x18(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x1c(%rsp)
movl -0x34(%rsp), %r11d
imull %r13d, %r11d
addl -0x30(%rsp), %r11d
xorl %eax, %eax
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 %r11d, %edi
movl -0x28(%rsp), %r8d
movl %edi, %ebp
testb %sil, %sil
je 0x97d2
movl %r8d, %r10d
addl %ebx, %r10d
setns %bpl
cmpl %r13d, %r10d
setl %dl
andb %bpl, %dl
movl %edi, %ebp
cmpb $0x1, %dl
jne 0x97d2
addl %ecx, %r10d
movl %r10d, %ebp
imull %r15d, %ebp
addl %r14d, %ebp
movslq %ebp, %rdx
movzbl (%r12,%rdx), %edx
addl %edx, %eax
incl %r8d
decl %edi
cmpl %r8d, %r9d
jne 0x97ab
movq -0x8(%rsp), %rdx
leal 0x1(%rdx), %ecx
addl %r13d, %r11d
cmpl -0x20(%rsp), %edx
jne 0x9787
movl -0x38(%rsp), %edi
imull %edi, %r13d
addl %ebx, %r13d
imull %r15d, %r13d
addl %r14d, %r13d
movslq %r13d, %rcx
movzbl (%r12,%rcx), %ecx
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
subl %ecx, %eax
xorps %xmm5, %xmm5
cvtsi2sd %eax, %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, %eax
cmovbel %ecx, %eax
movq -0x10(%rsp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull %edi, %edx
addl %ebx, %edx
imull 0x10(%rsi), %edx
addl %r14d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r14d
movq -0x18(%rsp), %rdi
movl 0x10(%rdi), %r15d
cmpl %r15d, %r14d
jl 0x974f
movl 0x8(%rdi), %eax
incl %ebx
incl -0x30(%rsp)
cmpl %eax, %ebx
jl 0x9743
movl 0xc(%rdi), %edx
movl -0x38(%rsp), %ecx
incl %ecx
incl -0x34(%rsp)
movl %ecx, -0x38(%rsp)
cmpl %edx, %ecx
jl 0x972d
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop