// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, %r8d
movq %rsi, -0xb8(%rbp)
movl %edx, %r13d
movq %r13, %rax
imulq %r13, %rax
movq %rsp, %r15
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0xa8(%rbp)
subq %rax, %r15
movq %r15, %rsp
pushq $0x2
popq %rcx
movl %edx, %eax
cltd
idivl %ecx
movl %eax, %ecx
negl %ecx
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movl %ecx, -0x3c(%rbp)
movslq %ecx, %r12
movq %rax, -0x90(%rbp)
cltq
leal 0x1(%rax), %edx
cmpl %edx, %r12d
cmovgl %r12d, %edx
addsd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq %rdx, -0x78(%rbp)
movslq %edx, %rdx
movq %rdx, -0x50(%rbp)
leaq (,%r13,4), %rdx
movq %rdx, -0x68(%rbp)
leaq 0x1(%rax,%rax), %r14
xorps %xmm1, %xmm1
movq %r15, -0x98(%rbp)
movq %r12, -0x70(%rbp)
movq %rdi, -0xa0(%rbp)
movq %r8, -0x60(%rbp)
cmpq -0x50(%rbp), %r12
je 0x7a14
movss %xmm1, -0x30(%rbp)
movq %r12, -0x58(%rbp)
imulq %r12, %r12
xorl %ebx, %ebx
cmpq %rbx, %r14
movq -0x70(%rbp), %rax
je 0x79f7
addq %rbx, %rax
imull %eax, %eax
addl %r12d, %eax
cvtsi2sd %eax, %xmm0
xorpd 0x8bd0(%rip), %xmm0     # 0x105a0
divsd -0x38(%rbp), %xmm0
callq 0x22a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rbx,4)
movss -0x30(%rbp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, -0x30(%rbp)
incq %rbx
jmp 0x79b2
movq -0x58(%rbp), %r12
incq %r12
addq -0x68(%rbp), %r15
movq -0x60(%rbp), %r8
movq -0xa0(%rbp), %rdi
movss -0x30(%rbp), %xmm1
jmp 0x799d
xorl %eax, %eax
testl %r8d, %r8d
cmovlel %eax, %r8d
movq -0x98(%rbp), %rcx
movq -0x68(%rbp), %rsi
cmpq %r8, %rax
je 0x7a4f
xorl %edx, %edx
cmpq %rdx, %r13
je 0x7a47
movss (%rcx,%rdx,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
jmp 0x7a2f
incq %rax
addq %rsi, %rcx
jmp 0x7a28
movq -0x90(%rbp), %rax
leal (%rax,%rax), %r9d
movq -0x78(%rbp), %rcx
addl %eax, %ecx
movq %rcx, -0x78(%rbp)
incq %r9
xorl %r10d, %r10d
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
movq %r8, -0x60(%rbp)
movq %r13, -0xb0(%rbp)
cmpl 0xc(%rdi), %r10d
jge 0x7c9d
movq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x90(%rbp), %rax
movl %eax, -0x44(%rbp)
xorl %r11d, %r11d
movq %r10, -0xc8(%rbp)
movslq 0x8(%rdi), %rax
cmpq %rax, %r11
jge 0x7c92
xorl %ebx, %ebx
movq %r11, -0x50(%rbp)
movl %ebx, -0x38(%rbp)
cmpl 0x10(%rdi), %ebx
jge 0x7c83
movq %rsp, -0xc0(%rbp)
movq %rsp, %rbx
subq -0xa8(%rbp), %rbx
movq %rbx, %rsp
movq %rbx, %rcx
xorl %edx, %edx
cmpq %r8, %rdx
je 0x7af6
xorl %eax, %eax
cmpq %rax, %r13
je 0x7aee
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0x7ae0
incq %rdx
addq %rsi, %rcx
jmp 0x7ad9
movl 0xc(%rdi), %eax
movl %eax, -0x80(%rbp)
movl 0x10(%rdi), %r13d
movslq 0x8(%rdi), %rax
movq 0x18(%rdi), %r12
movl -0x40(%rbp), %r14d
imull %eax, %r14d
addl -0x44(%rbp), %r14d
imull %r13d, %r14d
movq %rax, -0x30(%rbp)
imull %r13d, %eax
movl %eax, -0x7c(%rbp)
movq %rbx, %r11
movq -0x98(%rbp), %rdx
xorl %eax, %eax
movl -0x3c(%rbp), %r15d
movl -0x38(%rbp), %edi
cmpq -0x78(%rbp), %rax
je 0x7c13
movq %rax, -0xd0(%rbp)
movl %r15d, -0x84(%rbp)
addl %r10d, %r15d
setns %al
cmpl -0x80(%rbp), %r15d
setl %cl
andb %al, %cl
imull -0x30(%rbp), %r15d
movl %r14d, -0x88(%rbp)
xorl %r8d, %r8d
movq -0x70(%rbp), %rsi
cmpq %r8, %r9
je 0x7bde
movl %r14d, %eax
testb %cl, %cl
je 0x7bb2
movq -0x58(%rbp), %rax
addq %r8, %rax
setns %r10b
cmpq -0x30(%rbp), %rax
movq %r9, %rdi
setl %r9b
andb %r10b, %r9b
movl %r14d, %eax
cmpb $0x1, %r9b
movq %rdi, %r9
movl -0x38(%rbp), %edi
jne 0x7bb2
movq -0x50(%rbp), %rax
addq %rsi, %rax
addl %r15d, %eax
imull %r13d, %eax
addl %edi, %eax
cltq
movzbl (%r12,%rax), %eax
cvtsi2sd %eax, %xmm0
cvtss2sd (%rdx,%r8,4), %xmm1
mulsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r11,%r8,4)
incq %rsi
incq %r8
subl %r13d, %r14d
jmp 0x7b70
movl -0x84(%rbp), %r15d
incl %r15d
movq -0xd0(%rbp), %rax
incq %rax
movq -0x68(%rbp), %rsi
addq %rsi, %rdx
addq %rsi, %r11
movl -0x88(%rbp), %r14d
addl -0x7c(%rbp), %r14d
movq -0xc8(%rbp), %r10
jmp 0x7b36
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq -0x60(%rbp), %r8
movq -0xa0(%rbp), %rdi
movq -0xb0(%rbp), %r13
movq -0x50(%rbp), %r11
cmpq %r8, %rcx
je 0x7c4d
xorl %eax, %eax
cmpq %rax, %r13
je 0x7c45
addss (%rbx,%rax,4), %xmm0
incq %rax
jmp 0x7c36
incq %rcx
addq %rsi, %rbx
jmp 0x7c2f
cvttss2si %xmm0, %eax
movq -0xb8(%rbp), %rdx
movl 0x8(%rdx), %ecx
imull %r10d, %ecx
addl %r11d, %ecx
imull 0x10(%rdx), %ecx
movq 0x18(%rdx), %rdx
movl -0x38(%rbp), %ebx
addl %ebx, %ecx
movslq %ecx, %rcx
movb %al, (%rdx,%rcx)
movq -0xc0(%rbp), %rsp
incl %ebx
jmp 0x7ab4
incq %r11
incl -0x44(%rbp)
incq -0x58(%rbp)
jmp 0x7aa1
incl %r10d
incl -0x40(%rbp)
jmp 0x7a7b
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq