// File: rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::bilateral_filter(Image&, Image&, int, int, int)
void ImageProc::bilateral_filter(Image &img_orig, Image &img_modified, int window_size, int sigma_dist, int sigma_simi)
{
	int ws = window_size;
	double kernel[ws][ws], intensity_diff[ws][ws]; //discrete convolution operator
	double normalization_factor_sum = 0.0;
    int kernel_radius = ws / 2;
    double 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_dist * sigma_dist)));
            kernel[row + kernel_radius][col + kernel_radius] = frac;
        }


	for(int i = kernel_radius; i < img_orig.rows - kernel_radius; i++)
	 	for(int j = kernel_radius; j < img_orig.cols - kernel_radius; 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;
						intensity_diff[row][col] = 0.0;
					}
				int value = 0;
				normalization_factor_sum = 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))
						{
							intensity_diff[row][col] = exp(-1.0 * ((pow((img_orig.getvalue((i+m),(j+n),k) - img_orig.getvalue(i,j,k)),2.0))
													   /(2 * (sigma_simi*sigma_simi))));
						}
						else
						{
							intensity_diff[row][col] = exp(-1.0 * ((pow((img_orig.getvalue((i-m),(j-n),k) - img_orig.getvalue(i,j,k)),2.0))
													   /(2 * (sigma_simi*sigma_simi))));
						}
//std::cout << intensity_diff[row][col] << " " << img_orig.getvalue(i+m,j+n,k) << " " << img_orig(i,j,k) << " " << i+m << " " << j+n << " " << k << " " << std::endl;
					}

				for(int row = 0; row < ws; row++)
				 	for(int col = 0; col < ws; col++)
				 		normalization_factor_sum += kernel[row][col] * intensity_diff[row][col];
//std::cout << "\nstart" << std::endl;
				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) * kernel[row][col] * intensity_diff[row][col] / normalization_factor_sum;
						else
							values[row][col] = img_orig.getvalue((i+m),(j-n),k) * kernel[row][col] * intensity_diff[row][col] / normalization_factor_sum;
						//std::cout << values[row][col] << " " << img_orig((i+m),(j+n),k) << " " << kernel[row][col] << " " << intensity_diff[row][col] << " " << normalization_factor_sum << std::endl;
					}

				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
					{
						//std::cout << values[row][col] << std::endl;
						value += values[row][col];
					}

					//std::cout << value << std::endl;
					//std::cout << img_orig.getvalue(i,j,k) << "\n " << std::endl;

				img_modified.setvalue(i,j,k,value);
			}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq %rax, -0xf0(%rbp)
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rcx
imulq %rcx, %rcx
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe8(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movq %rax, -0x38(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,8), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xd8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x40(%rbp)
movq %rax, -0x48(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x20(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x54(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xf7f7
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xf7e7
movl -0x64(%rbp), %eax
imull -0x64(%rbp), %eax
movl -0x68(%rbp), %ecx
imull -0x68(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0xcd55(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
movl -0x18(%rbp), %eax
imull -0x18(%rbp), %eax
cvtsi2sd %eax, %xmm2
movsd 0xcaae(%rip), %xmm1     # 0x1c240
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
callq 0x2380
movq -0xf0(%rbp), %rdx
movq -0xe8(%rbp), %rax
movsd %xmm0, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
movl -0x64(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movl -0x68(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xf753
jmp 0xf7e9
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xf73f
movl -0x54(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0xc(%rcx), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jge 0xff61
movl -0x54(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x8(%rcx), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jge 0xff51
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0xff41
movl -0x20(%rbp), %eax
movl %eax, %ecx
movq %rcx, %rax
movq %rax, -0x100(%rbp)
movq %rsp, %rdx
movq %rdx, -0x80(%rbp)
movq %rcx, %rdx
imulq %rdx, %rdx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xf8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x88(%rbp)
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xf93e
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xf928
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rdx
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rdi
movslq -0x94(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x2, %rsi
addq %rsi, %rcx
movslq -0x98(%rbp), %rsi
xorps %xmm0, %xmm0
movss %xmm0, (%rcx,%rsi,4)
movslq -0x94(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0x98(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xf8ae
jmp 0xf92a
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xf895
movl $0x0, -0x9c(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xfb6f
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl -0xa8(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xfb4a
movl -0x6c(%rbp), %eax
addl -0xa0(%rbp), %eax
cmpl $0x0, %eax
jl 0xfa89
movl -0x6c(%rbp), %eax
addl -0xa0(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xfa89
movl -0x70(%rbp), %eax
addl -0xa8(%rbp), %eax
cmpl $0x0, %eax
jl 0xfa89
movl -0x70(%rbp), %eax
addl -0xa8(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xfa89
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
addl -0xa0(%rbp), %esi
movl -0x70(%rbp), %edx
addl -0xa8(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movl %eax, -0x104(%rbp)
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movl -0x104(%rbp), %edi
subl %eax, %edi
movsd 0xc812(%rip), %xmm0     # 0x1c240
callq 0x1b370
movaps %xmm0, %xmm1
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
shll %eax
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd 0xca81(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
callq 0x2380
movq -0xe0(%rbp), %rdx
movq -0xd8(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xac(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xfb25
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
subl -0xa0(%rbp), %esi
movl -0x70(%rbp), %edx
subl -0xa8(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movl %eax, -0x108(%rbp)
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movl -0x108(%rbp), %edi
subl %eax, %edi
movsd 0xc771(%rip), %xmm0     # 0x1c240
callq 0x1b370
movaps %xmm0, %xmm1
movl -0x1c(%rbp), %eax
imull -0x1c(%rbp), %eax
shll %eax
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd 0xc9e0(%rip), %xmm0     # 0x1c4d0
mulsd %xmm1, %xmm0
callq 0x2380
movq -0xe0(%rbp), %rdx
movq -0xd8(%rbp), %rax
movslq -0xa4(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xac(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0xfb27
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xf989
jmp 0xfb4c
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0xf965
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xfc31
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xfc1b
movq -0xd8(%rbp), %rax
movq -0xe0(%rbp), %rdx
movq -0xe8(%rbp), %rcx
movq -0xf0(%rbp), %rdi
movslq -0xb0(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x3, %rsi
addq %rsi, %rcx
movslq -0xb4(%rbp), %rsi
movsd (%rcx,%rsi,8), %xmm0
movslq -0xb0(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq -0xb4(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xfb92
jmp 0xfc1d
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xfb79
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xfe80
xorl %eax, %eax
subl -0x54(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc0(%rbp), %eax
cmpl -0x54(%rbp), %eax
jg 0xfe5b
movl -0x6c(%rbp), %eax
addl -0xb8(%rbp), %eax
cmpl $0x0, %eax
jl 0xfd82
movl -0x6c(%rbp), %eax
addl -0xb8(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xfd82
movl -0x70(%rbp), %eax
addl -0xc0(%rbp), %eax
cmpl $0x0, %eax
jl 0xfd82
movl -0x70(%rbp), %eax
addl -0xc0(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xfd82
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
addl -0xb8(%rbp), %esi
movl -0x70(%rbp), %edx
addl -0xc0(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movq -0xf0(%rbp), %r9
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rcx
movq -0x100(%rbp), %rdx
movl %eax, %r8d
movq -0xf8(%rbp), %rax
cvtsi2sd %r8d, %xmm0
movslq -0xbc(%rbp), %r8
imulq %r9, %r8
shlq $0x3, %r8
addq %r8, %rsi
movslq -0xc4(%rbp), %r8
mulsd (%rsi,%r8,8), %xmm0
movslq -0xbc(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x3, %rsi
addq %rsi, %rcx
movslq -0xc4(%rbp), %rsi
mulsd (%rcx,%rsi,8), %xmm0
divsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0xbc(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0xc4(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0xfe36
movq -0x8(%rbp), %rdi
movl -0x6c(%rbp), %esi
addl -0xb8(%rbp), %esi
movl -0x70(%rbp), %edx
subl -0xc0(%rbp), %edx
movl -0x74(%rbp), %ecx
callq 0x83b0
movq -0xf0(%rbp), %r9
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movq -0xd8(%rbp), %rcx
movq -0x100(%rbp), %rdx
movl %eax, %r8d
movq -0xf8(%rbp), %rax
cvtsi2sd %r8d, %xmm0
movslq -0xbc(%rbp), %r8
imulq %r9, %r8
shlq $0x3, %r8
addq %r8, %rsi
movslq -0xc4(%rbp), %r8
mulsd (%rsi,%r8,8), %xmm0
movslq -0xbc(%rbp), %rsi
imulq %rdi, %rsi
shlq $0x3, %rsi
addq %rsi, %rcx
movslq -0xc4(%rbp), %rsi
mulsd (%rcx,%rsi,8), %xmm0
divsd -0x50(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0xbc(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0xc4(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0xfe38
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xfc6a
jmp 0xfe5d
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xfc46
movl $0x0, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xff13
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0xfefd
movq -0xf8(%rbp), %rax
movq -0x100(%rbp), %rdx
movslq -0xc8(%rbp), %rcx
imulq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0xcc(%rbp), %rcx
movss (%rax,%rcx,4), %xmm1
cvtsi2ssl -0x9c(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x9c(%rbp)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0xfe9f
jmp 0xfeff
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0xfe8a
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movl -0x74(%rbp), %ecx
movl -0x9c(%rbp), %r8d
callq 0x83f0
movq -0x80(%rbp), %rax
movq %rax, %rsp
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xf834
jmp 0xff43
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xf818
jmp 0xff53
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xf7fd
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)