// File: /workspace/github_cpp_makefile_star2_O2/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, %ecx
movq %rsi, -0x8(%rsp)
movl $0x2, %esi
movl %edx, %eax
cltd
idivl %esi
movl %eax, %edx
negl %edx
imull %ecx, %ecx
decl %ecx
cvtsi2sd %ecx, %xmm2
movsd 0x81b8(%rip), %xmm1     # 0x10288
divsd %xmm2, %xmm1
movapd 0x84c4(%rip), %xmm2     # 0x105a0
xorpd %xmm1, %xmm2
leal 0x1(%rax), %ecx
cmpl %ecx, %edx
cmovgl %edx, %ecx
movl %ecx, -0x20(%rsp)
movq %rax, -0x18(%rsp)
leal 0x1(,%rax,2), %eax
movl %eax, -0x2c(%rsp)
xorl %r8d, %r8d
movapd 0x83e9(%rip), %xmm3     # 0x104f0
movl %edx, -0x3c(%rsp)
movl %edx, -0x38(%rsp)
movq %rdi, -0x10(%rsp)
cmpl 0xc(%rdi), %r8d
jge 0x8294
xorl %r11d, %r11d
movl -0x3c(%rsp), %eax
movl %eax, -0x30(%rsp)
movq -0x18(%rsp), %rax
movl %eax, -0x34(%rsp)
movl %r8d, -0x28(%rsp)
cmpl 0x8(%rdi), %r11d
jge 0x8288
xorl %r14d, %r14d
movl 0x10(%rdi), %r15d
cmpl %r15d, %r14d
jge 0x8278
movl 0x8(%rdi), %r12d
movl 0xc(%rdi), %eax
movl %eax, -0x24(%rsp)
movq 0x18(%rdi), %r13
movl -0x38(%rsp), %eax
imull %r12d, %eax
addl -0x34(%rsp), %eax
xorl %r10d, %r10d
movl -0x3c(%rsp), %esi
cmpl -0x20(%rsp), %esi
je 0x8203
movl %esi, %edx
addl %r8d, %edx
setns %cl
cmpl -0x24(%rsp), %edx
setl %dil
andb %cl, %dil
imull %r12d, %edx
movl -0x30(%rsp), %ebx
movl %eax, -0x1c(%rsp)
movl %eax, %r8d
movl -0x2c(%rsp), %r9d
movl -0x3c(%rsp), %eax
movl %eax, %ecx
subl $0x1, %r9d
jb 0x81f0
movl %r8d, %ebp
testb %dil, %dil
je 0x81d4
testl %ebx, %ebx
setns %bpl
cmpl %r12d, %ebx
setl %al
andb %bpl, %al
movl %r8d, %ebp
cmpb $0x1, %al
jne 0x81d4
leal (%rcx,%r11), %ebp
addl %edx, %ebp
imull %r15d, %ebp
addl %r14d, %ebp
movslq %ebp, %rax
movzbl (%r13,%rax), %eax
addl %eax, %r10d
incl %ecx
decl %r8d
incl %ebx
jmp 0x81aa
incl %esi
movl -0x1c(%rsp), %eax
addl %r12d, %eax
movl -0x28(%rsp), %r8d
jmp 0x8173
imull %r8d, %r12d
addl %r11d, %r12d
imull %r15d, %r12d
addl %r14d, %r12d
movslq %r12d, %rax
movzbl (%r13,%rax), %eax
cvtsi2sd %eax, %xmm4
subl %eax, %r10d
cvtsi2sd %r10d, %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 -0x8(%rsp), %rsi
movq 0x18(%rsi), %rax
movl 0x8(%rsi), %edx
imull %r8d, %edx
addl %r11d, %edx
imull 0x10(%rsi), %edx
addl %r14d, %edx
movslq %edx, %rdx
movb %cl, (%rax,%rdx)
incl %r14d
movq -0x10(%rsp), %rdi
jmp 0x8144
incl %r11d
incl -0x34(%rsp)
incl -0x30(%rsp)
jmp 0x8137
incl %r8d
incl -0x38(%rsp)
jmp 0x8114
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop