// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movss %xmm0, -0x18(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0x78(%rbp)
movq %rsp, %rdx
movq %rdx, -0x28(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movq %rcx, -0x30(%rbp)
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xf123
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xf113
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0xf103
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xee16
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xee09
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rdx
movslq -0x54(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x58(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xedd4
jmp 0xee0b
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xedc5
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
xorl %eax, %eax
subl -0x20(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0xef7b
xorl %eax, %eax
subl -0x20(%rbp), %eax
movl %eax, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x20(%rbp), %eax
jg 0xef62
movl -0x48(%rbp), %eax
addl -0x5c(%rbp), %eax
cmpl $0x0, %eax
jl 0xeef2
movl -0x48(%rbp), %eax
addl -0x5c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xeef2
movl -0x4c(%rbp), %eax
addl -0x64(%rbp), %eax
cmpl $0x0, %eax
jl 0xeef2
movl -0x4c(%rbp), %eax
addl -0x64(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xeef2
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
addl -0x5c(%rbp), %esi
movl -0x4c(%rbp), %edx
addl -0x64(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
movq -0x78(%rbp), %rdx
movl %eax, %edi
movq -0x70(%rbp), %rax
movslq -0x60(%rbp), %rsi
imulq %rdx, %rsi
shlq $0x2, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movslq -0x68(%rbp), %rsi
movl %edi, (%rcx,%rsi,4)
movslq -0x60(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xef49
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
addl -0x5c(%rbp), %esi
movl -0x4c(%rbp), %edx
subl -0x64(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
movq -0x78(%rbp), %rdx
movl %eax, %edi
movq -0x70(%rbp), %rax
movslq -0x60(%rbp), %rsi
imulq %rdx, %rsi
shlq $0x2, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movslq -0x68(%rbp), %rsi
movl %edi, (%rcx,%rsi,4)
movslq -0x60(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xef4b
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xee55
jmp 0xef64
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xee3a
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x88(%rbp)
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0xd2b9(%rip), %xmm0     # 0x1c268
divsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
movsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
movl %eax, %ecx
movl -0x7c(%rbp), %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x1b2f0
cvttsd2si %xmm0, %eax
movl %eax, -0x44(%rbp)
cvtsi2ssl -0x44(%rbp), %xmm0
ucomiss -0x18(%rbp), %xmm0
jbe 0xf09a
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0xd22e(%rip), %xmm0     # 0x1c268
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
movsd -0xa0(%rbp), %xmm0
movl %eax, %ecx
movl -0x94(%rbp), %eax
subl %ecx, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
movl -0x40(%rbp), %r8d
callq 0x83f0
jmp 0xf0f3
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0xa4(%rbp)
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %ecx
callq 0x83b0
movq -0xb8(%rbp), %rdi
movl -0xac(%rbp), %esi
movl -0xa8(%rbp), %edx
movl -0xa4(%rbp), %ecx
movl %eax, %r8d
callq 0x83f0
jmp 0xf0f5
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xedae
jmp 0xf105
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xed97
jmp 0xf115
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xed80
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop