// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %r8d, -0x58(%rbp)
movq %rsi, -0x108(%rbp)
movl %edx, %r8d
movq %r8, %rax
imulq %r8, %rax
movq %rsp, %r15
movq %rax, -0x100(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, -0x78(%rbp)
movq %rsi, %rsp
pushq $0x2
popq %rsi
movq %rdx, -0x48(%rbp)
movl %edx, %eax
cltd
idivl %esi
movl %eax, %edx
negl %edx
imull %ecx, %ecx
addl %ecx, %ecx
cvtsi2sd %rcx, %xmm0
movsd %xmm0, -0x40(%rbp)
movl %edx, -0x64(%rbp)
movslq %edx, %r12
movq %rax, -0x90(%rbp)
cltq
leal 0x1(%rax), %ecx
cmpl %ecx, %r12d
cmovgl %r12d, %ecx
movq %rcx, -0x80(%rbp)
movslq %ecx, %rcx
movq %rcx, -0x38(%rbp)
movq %r8, -0xa8(%rbp)
leaq (,%r8,8), %rcx
movq %rcx, -0x50(%rbp)
movq %rax, -0xf0(%rbp)
leaq 0x1(%rax,%rax), %r14
movq %r15, -0xd0(%rbp)
movq %r12, -0xd8(%rbp)
movq %rdi, -0xc0(%rbp)
cmpq -0x38(%rbp), %r12
je 0x8762
movq %r12, %r13
imulq %r12, %r13
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x8759
movq -0xd8(%rbp), %rax
addq %rbx, %rax
imull %eax, %eax
addl %r13d, %eax
cvtsi2sd %eax, %xmm0
xorpd 0x7e63(%rip), %xmm0     # 0x105a0
divsd -0x40(%rbp), %xmm0
callq 0x22a0
movq -0xc0(%rbp), %rdi
movsd %xmm0, (%r15,%rbx,8)
incq %rbx
jmp 0x871c
incq %r12
addq -0x50(%rbp), %r15
jmp 0x870d
movl -0x58(%rbp), %eax
imull %eax, %eax
addl %eax, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x118(%rbp)
xorl %edx, %edx
movq -0x48(%rbp), %rax
testl %eax, %eax
movl $0x0, %ecx
movq %rcx, -0x70(%rbp)
cmovlel %edx, %eax
movq %rax, -0x48(%rbp)
movq -0x90(%rbp), %rax
leal (%rax,%rax), %edx
movq -0x80(%rbp), %rcx
addl %eax, %ecx
movq %rcx, -0x80(%rbp)
movq -0xa8(%rbp), %r8
leaq (,%r8,4), %rcx
movq %rcx, -0xb0(%rbp)
leaq 0x8(,%rdx,8), %rcx
movq %rcx, -0x128(%rbp)
leaq 0x1(%rdx), %rcx
movq %rcx, -0x120(%rbp)
movslq %eax, %rcx
movq %rcx, -0xf8(%rbp)
movq %rdx, -0xe8(%rbp)
movl %edx, -0x5c(%rbp)
movl %eax, %r9d
movl 0xc(%rdi), %eax
subl -0x90(%rbp), %eax
cmpl %eax, %r9d
jge 0x8c07
movq $0x0, -0xb8(%rbp)
movq -0xe8(%rbp), %rax
movl %eax, -0x60(%rbp)
movq -0xf0(%rbp), %rcx
movq %r9, -0x58(%rbp)
movslq 0x8(%rdi), %rax
subq -0xf8(%rbp), %rax
cmpq %rax, %rcx
jge 0x8bf2
movq %rcx, -0x40(%rbp)
xorl %r12d, %r12d
cmpl 0x10(%rdi), %r12d
jge 0x8bdc
movq %rsp, -0x110(%rbp)
movq %rsp, %rbx
movq -0x100(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rbx, %rax
movq -0x78(%rbp), %rcx
xorl %edx, %edx
cmpq -0x48(%rbp), %rdx
je 0x8897
xorl %esi, %esi
cmpq %rsi, %r8
je 0x8887
andl $0x0, (%rax,%rsi,4)
andq $0x0, (%rcx,%rsi,8)
incq %rsi
jmp 0x8874
incq %rdx
addq -0x50(%rbp), %rcx
addq -0xb0(%rbp), %rax
jmp 0x886c
movq -0x70(%rbp), %rax
movl %eax, -0x88(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x38(%rbp)
xorl %ecx, %ecx
movl -0x64(%rbp), %eax
movl %eax, %edx
movq %rbx, -0xe0(%rbp)
cmpq -0x80(%rbp), %rcx
je 0x89fc
movq %rcx, -0xa0(%rbp)
movq %rdx, -0x98(%rbp)
leal (%rdx,%r9), %r14d
movq -0xb8(%rbp), %rax
movl %eax, %r13d
movq -0x90(%rbp), %r15
xorl %ebx, %ebx
cmpq %rbx, -0x128(%rbp)
je 0x89c1
movl 0x8(%rdi), %eax
testl %r14d, %r14d
js 0x8942
cmpl 0xc(%rdi), %r14d
jge 0x8942
testl %r13d, %r13d
setns %cl
cmpl %eax, %r13d
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x8942
movq 0x18(%rdi), %rcx
movl 0x10(%rdi), %esi
movl -0x88(%rbp), %edx
imull %eax, %edx
addl %r13d, %edx
imull %esi, %edx
addl %r12d, %edx
movslq %edx, %rdx
addq %rcx, %rdx
imull %r9d, %eax
addl -0x40(%rbp), %eax
jmp 0x896c
movq 0x18(%rdi), %rcx
movl 0x10(%rdi), %esi
movl -0x2c(%rbp), %edx
imull %eax, %edx
movq -0x40(%rbp), %r8
addl %r15d, %edx
addl %r8d, %edx
imull %esi, %edx
addl %r12d, %edx
movslq %edx, %rdx
addq %rcx, %rdx
imull %r9d, %eax
addl %r8d, %eax
imull %esi, %eax
movzbl (%rdx), %edx
addl %r12d, %eax
cltq
movzbl (%rcx,%rax), %eax
subl %eax, %edx
cvtsi2sd %edx, %xmm1
movapd %xmm1, %xmm0
xorpd 0x7c13(%rip), %xmm0     # 0x105a0
mulsd %xmm1, %xmm0
divsd -0x118(%rbp), %xmm0
callq 0x22a0
movq -0x38(%rbp), %rax
movsd %xmm0, (%rax,%rbx)
addq $0x8, %rbx
decl %r15d
incl %r13d
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %r9
jmp 0x88ec
movq -0x98(%rbp), %rdx
incl %edx
movq -0xa0(%rbp), %rcx
incq %rcx
movq -0x38(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
decl -0x2c(%rbp)
incl -0x88(%rbp)
movq -0xa8(%rbp), %r8
movq -0xe0(%rbp), %rbx
jmp 0x88bd
xorpd %xmm0, %xmm0
movq -0xd0(%rbp), %rax
movq -0x78(%rbp), %rcx
xorl %edx, %edx
cmpq -0x48(%rbp), %rdx
je 0x8a3c
xorl %esi, %esi
cmpq %rsi, %r8
je 0x8a2d
movsd (%rax,%rsi,8), %xmm1
mulsd (%rcx,%rsi,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
jmp 0x8a15
incq %rdx
movq -0x50(%rbp), %rsi
addq %rsi, %rcx
addq %rsi, %rax
jmp 0x8a0d
movl 0xc(%rdi), %eax
movl %eax, -0x98(%rbp)
movl 0x10(%rdi), %ecx
movslq 0x8(%rdi), %rdx
movq 0x18(%rdi), %rsi
movq -0x70(%rbp), %rax
movl %eax, %edi
imull %edx, %edi
addl -0x60(%rbp), %edi
imull %ecx, %edi
movq %rdx, -0x38(%rbp)
movl %edx, %eax
imull %ecx, %eax
movl %eax, -0xc4(%rbp)
movq %rbx, %r9
movq -0x78(%rbp), %r10
movq -0xd0(%rbp), %r11
xorl %edx, %edx
movl -0x64(%rbp), %r15d
cmpq -0x80(%rbp), %rdx
je 0x8b71
movq %rdx, -0x88(%rbp)
movl %r15d, -0xa0(%rbp)
addl -0x58(%rbp), %r15d
setns %al
cmpl -0x98(%rbp), %r15d
setl %r13b
andb %al, %r13b
imull -0x38(%rbp), %r15d
movl %edi, -0x2c(%rbp)
xorl %r8d, %r8d
movq -0xd8(%rbp), %rax
cmpq %r8, -0x120(%rbp)
je 0x8b30
movl %edi, %ebx
testb %r13b, %r13b
je 0x8afe
movq -0xb8(%rbp), %rbx
addq %r8, %rbx
setns %r14b
cmpq -0x38(%rbp), %rbx
setl %dl
andb %r14b, %dl
movl %edi, %ebx
cmpb $0x1, %dl
jne 0x8afe
movq -0x40(%rbp), %rdx
leaq (%rax,%rdx), %rbx
addl %r15d, %ebx
imull %ecx, %ebx
addl %r12d, %ebx
movslq %ebx, %rdx
movzbl (%rsi,%rdx), %edx
cvtsi2sd %edx, %xmm1
mulsd (%r11,%r8,8), %xmm1
mulsd (%r10,%r8,8), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r9,%r8,4)
incq %rax
incq %r8
subl %ecx, %edi
jmp 0x8ac1
movl -0xa0(%rbp), %r15d
incl %r15d
movq -0x88(%rbp), %rdx
incq %rdx
movq -0x50(%rbp), %rax
addq %rax, %r11
addq %rax, %r10
addq -0xb0(%rbp), %r9
movl -0x2c(%rbp), %edi
addl -0xc4(%rbp), %edi
movq -0xa8(%rbp), %r8
movq -0xe0(%rbp), %rbx
jmp 0x8a82
xorl %ecx, %ecx
xorl %eax, %eax
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %r9
cmpq -0x48(%rbp), %rcx
je 0x8bab
xorl %edx, %edx
cmpq %rdx, %r8
je 0x8b9f
cvtsi2ss %eax, %xmm0
addss (%rbx,%rdx,4), %xmm0
cvttss2si %xmm0, %eax
incq %rdx
jmp 0x8b88
incq %rcx
addq -0xb0(%rbp), %rbx
jmp 0x8b80
movq -0x108(%rbp), %rdx
movl 0x8(%rdx), %ecx
imull %r9d, %ecx
addl -0x40(%rbp), %ecx
imull 0x10(%rdx), %ecx
movq 0x18(%rdx), %rdx
addl %r12d, %ecx
movslq %ecx, %rcx
movb %al, (%rdx,%rcx)
movq -0x110(%rbp), %rsp
incl %r12d
jmp 0x8836
movq -0x40(%rbp), %rcx
incq %rcx
incq -0xb8(%rbp)
incl -0x60(%rbp)
jmp 0x881b
incl %r9d
incl -0x5c(%rbp)
movq -0x70(%rbp), %rax
incl %eax
movq %rax, -0x70(%rbp)
jmp 0x87e9
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq