// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::gaussian_filter(Image&, Image&, int, float)
void ImageProc::gaussian_filter(Image &img_orig, Image &img_modified, int window_size, float sigma)
{
	int ws = window_size;
	float kernel[ws][ws]; //discrete convolution operator

	//gaussian_kernel(kernel, ws, sigma);
	float kernel_sum = 0.0;
    int kernel_radius = ws / 2;
    //double coeff = 1.0 / (2.0 * M_PI * (sigma * sigma));
    float frac = 0.0;

    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] = 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


	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++)
		 	{
				float values[ws][ws];
				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values[row][col] = 0;
				float values_avg = 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] = 1.0 * img_orig.getvalue((i+m),(j+n),k) * kernel[row][col];
						}
						else
						{
							values[row][col] = 1.0 * img_orig.getvalue((i+m),(j-n),k) * kernel[row][col];
						}
					}

				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values_avg += values[row][col];

				img_modified.setvalue(i,j,k,(int)values_avg);
			}

}

// 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
movq %rcx, %rax
movq %rax, -0xb0(%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, -0xa8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x30(%rbp)
movq %rax, -0x38(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x40(%rbp), %eax
jg 0xe0f6
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jg 0xe0e6
movl -0x48(%rbp), %eax
imull -0x48(%rbp), %eax
movl -0x4c(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0xe48c(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
movss -0x18(%rbp), %xmm1
mulss -0x18(%rbp), %xmm1
cvtss2sd %xmm1, %xmm2
movsd 0xe1e2(%rip), %xmm1     # 0x1c240
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x2380
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rax
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x44(%rbp), %xmm0
movl -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rsi
imulq %rdx, %rsi
shlq $0x2, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movl -0x4c(%rbp), %esi
addl -0x40(%rbp), %esi
movslq %esi, %rsi
movss %xmm0, (%rcx,%rsi,4)
movl -0x48(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movl -0x4c(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
addss -0x3c(%rbp), %xmm0
movss %xmm0, -0x3c(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xe01c
jmp 0xe0e8
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xe008
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe172
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe165
movq -0xa8(%rbp), %rax
movq -0xb0(%rbp), %rdx
movslq -0x50(%rbp), %rsi
imulq %rdx, %rsi
shlq $0x2, %rsi
movq %rax, %rcx
addq %rsi, %rcx
movslq -0x54(%rbp), %rsi
movss (%rcx,%rsi,4), %xmm0
divss -0x3c(%rbp), %xmm0
movslq -0x50(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x54(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xe10c
jmp 0xe167
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xe0fd
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xe54b
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xe53b
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0xe52b
movl -0x1c(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq %rsp, %rdx
movq %rdx, -0x68(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xb8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x70(%rbp)
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe257
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe24a
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rdx
movslq -0x7c(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x80(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax,%rcx,4)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0xe20e
jmp 0xe24c
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xe1ff
xorps %xmm0, %xmm0
movss %xmm0, -0x84(%rbp)
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x40(%rbp), %eax
jg 0xe471
xorl %eax, %eax
subl -0x40(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x40(%rbp), %eax
jg 0xe44c
movl -0x58(%rbp), %eax
addl -0x88(%rbp), %eax
cmpl $0x0, %eax
jl 0xe393
movl -0x58(%rbp), %eax
addl -0x88(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xe393
movl -0x5c(%rbp), %eax
addl -0x90(%rbp), %eax
cmpl $0x0, %eax
jl 0xe393
movl -0x5c(%rbp), %eax
addl -0x90(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xe393
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
addl -0x88(%rbp), %esi
movl -0x5c(%rbp), %edx
addl -0x90(%rbp), %edx
movl -0x60(%rbp), %ecx
callq 0x83b0
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rcx
movq -0xc0(%rbp), %rdx
movl %eax, %esi
movq -0xb8(%rbp), %rax
cvtsi2sd %esi, %xmm1
movsd 0xdf26(%rip), %xmm0     # 0x1c268
mulsd %xmm1, %xmm0
movslq -0x8c(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x2, %rsi
addq %rsi, %rcx
movslq -0x94(%rbp), %rsi
movss (%rcx,%rsi,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0x8c(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x94(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0xe427
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
addl -0x88(%rbp), %esi
movl -0x5c(%rbp), %edx
subl -0x90(%rbp), %edx
movl -0x60(%rbp), %ecx
callq 0x83b0
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rcx
movq -0xc0(%rbp), %rdx
movl %eax, %esi
movq -0xb8(%rbp), %rax
cvtsi2sd %esi, %xmm1
movsd 0xde8d(%rip), %xmm0     # 0x1c268
mulsd %xmm1, %xmm0
movslq -0x8c(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x2, %rsi
addq %rsi, %rcx
movslq -0x94(%rbp), %rsi
movss (%rcx,%rsi,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0x8c(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x94(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0xe429
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xe29b
jmp 0xe44e
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xe277
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe4fb
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xe4e8
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rdx
movslq -0x98(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0x9c(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
addss -0x84(%rbp), %xmm0
movss %xmm0, -0x84(%rbp)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0xe490
jmp 0xe4ea
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xe47b
movq -0x10(%rbp), %rdi
movl -0x58(%rbp), %esi
movl -0x5c(%rbp), %edx
movl -0x60(%rbp), %ecx
cvttss2si -0x84(%rbp), %r8d
callq 0x83f0
movq -0x68(%rbp), %rax
movq %rax, %rsp
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xe1a7
jmp 0xe52d
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xe190
jmp 0xe53d
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xe179
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)