// File: /workspace/github_cpp_makefile_star2_O3/spatialfruitsalad[P]volume2position/./bilateral.hpp
// Function: void bilateralFilter::Process<unsigned short>(tomogram3d<unsigned short>&, tomogram3d<unsigned short>&)
void Process(tomogram3d<VALUE>& matrix_in, tomogram3d<VALUE>& matrix_out)
    {
        std::cout << "applying bilateral filter" << std::endl;

        int sx = matrix_in.get_sx();
        int sy = matrix_in.get_sy();
        int sz = matrix_in.get_sz();

        matrix_out.clear();
        matrix_out.initialise (sx, sy, sz, 0);

        // outer loop over the whole image
        for (int k = 0; k != sz; ++k)
        {
            std::cout << k  << " " << std::flush;
            for (int j = 0; j != sy; ++j)
            {
                for (int i = 0; i != sx; ++i)
                {
                    // original value from the old image
                    VALUE orig = matrix_in.get(i,j,k);

                    // calculate boundaries so I don't read from otside the image
                    int imin = std::max (i-tol, 0);
                    int imax = std::min (i+tol, int (sx-1));
                    int jmin = std::max (j-tol, 0);
                    int jmax = std::min (j+tol, int (sy-1));
                    int kmin = std::max (k-tol, 0);
                    int kmax = std::min (k+tol, int (sz-1));

                    double new_val = 0;
                    double sum = 0;
                    // inner loop over the filter kernel
                    for (int ii = imin; ii <= imax; ++ii)
                    {
                        for (int jj = jmin; jj <= jmax; ++jj)
                        {
                            for (int kk = kmin; kk <= kmax; ++kk)
                            {
                                VALUE other = matrix_in.get(ii,jj,kk);  
                                double similarityFactor = getGauss( other - orig);
                                double closenessFactor = mask.get(ii-i+tol, jj-j+tol, kk-k+tol);
                                double currentWeight = closenessFactor * similarityFactor;
                                sum += currentWeight;
                                new_val += other * currentWeight;
                            }
                        }
                    }
                    matrix_out.set(i,j,k,new_val/sum);
                }
            }
        }
        std::cout << "\ndone" << std::endl;
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0x15ec9(%rip), %rbx     # 0x1cfc8
leaq 0x10a3c(%rip), %rsi     # 0x17b42
movl $0x19, %edx
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq %rax, 0x38(%rsp)
movl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rcx, 0x28(%rsp)
movl %ecx, %eax
movq %rax, 0x40(%rsp)
movq %r15, 0x68(%rsp)
movq 0x28(%r15), %rax
movq %rax, (%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x717b
movq %rax, 0x8(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%r14)
movq $0x0, 0x28(%r14)
movslq 0x48(%rsp), %rsi
movslq 0x40(%rsp), %rdx
movslq 0x30(%rsp), %rcx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0xcd8e
cmpl $0x0, (%rsp)
je 0x74b0
movq 0x38(%rsp), %rax
decl %eax
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %rax
decl %eax
movl %eax, 0x14(%rsp)
movq (%rsp), %rax
decl %eax
movq %rax, (%rsp)
leaq 0x20(%r12), %rax
movq %rax, 0xa0(%rsp)
movq $0x0, 0x60(%rsp)
xorl %r15d, %r15d
movq %r14, 0x70(%rsp)
movq 0x15dd2(%rip), %rdi     # 0x1cfc8
movl %r15d, %esi
callq 0x3420
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x10aa7(%rip), %rsi     # 0x17cb7
callq 0x32f0
movq %rbx, %rdi
callq 0x31e0
cmpl $0x0, 0x28(%rsp)
je 0x749d
xorl %ebx, %ebx
movq %r15, 0x78(%rsp)
cmpl $0x0, 0x38(%rsp)
je 0x748f
xorl %r13d, %r13d
movq %rbx, 0x58(%rsp)
movq 0x68(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x8fee
movl 0x18(%r12), %ecx
movl %r13d, %edx
subl %ecx, %edx
testl %edx, %edx
movl $0x0, %esi
cmovlel %esi, %edx
movq %rdx, 0x8(%rsp)
movq %r13, 0x50(%rsp)
leal (%rcx,%r13), %edi
movl 0x18(%rsp), %edx
cmpl %edi, %edx
cmovll %edx, %edi
movq %rdi, 0x88(%rsp)
leal (%rcx,%r15), %edi
movq (%rsp), %rdx
cmpl %edi, %edx
cmovll %edx, %edi
subl %ecx, %r15d
testl %r15d, %r15d
cmovlel %esi, %r15d
leal (%rcx,%rbx), %r8d
movl 0x14(%rsp), %edx
cmpl %r8d, %edx
cmovll %edx, %r8d
movl %ebx, %r13d
subl %ecx, %r13d
testl %r13d, %r13d
cmovlel %esi, %r13d
movzwl %ax, %eax
movl %eax, 0x24(%rsp)
movl %edi, 0x20(%rsp)
movslq %edi, %rax
movq %rax, 0xa8(%rsp)
movl %r8d, 0x1c(%rsp)
movslq %r8d, %rax
movq %rax, 0x90(%rsp)
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
movq %r15, 0x98(%rsp)
movq %r13, 0x80(%rsp)
cmpl 0x1c(%rsp), %r13d
jg 0x742f
movq 0x8(%rsp), %r14
subq 0x50(%rsp), %r14
cmpl 0x20(%rsp), %r15d
jg 0x7412
movq %r13, %rbp
subq 0x58(%rsp), %rbp
movq %r15, %rbx
movsd %xmm0, 0xb8(%rsp)
movsd %xmm2, 0xc0(%rsp)
movq 0x68(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x8fee
movzwl %ax, %r15d
movl %r15d, %eax
subl 0x24(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
andpd 0xfd6b(%rip), %xmm0     # 0x170d0
movsd 0x10(%r12), %xmm1
mulsd 0xfcb4(%rip), %xmm1     # 0x17028
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm0
jae 0x73a0
divsd 0xfcaa(%rip), %xmm1     # 0x17030
divsd %xmm1, %xmm0
callq 0x33f0
cvttsd2si %xmm0, %rax
movl %eax, %eax
movq 0x50(%r12), %rcx
movsd (%rcx,%rax,8), %xmm2
movsd %xmm2, 0xb0(%rsp)
movslq 0x18(%r12), %rax
leaq (%r14,%rax), %rsi
leaq (%rax,%rbp), %rdx
movq 0x60(%rsp), %rcx
addq %rbx, %rcx
addq %rax, %rcx
movq 0xa0(%rsp), %rdi
callq 0x1459a
mulsd 0xb0(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm2
addsd %xmm0, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
mulsd %xmm0, %xmm1
movsd 0xb8(%rsp), %xmm0
addsd %xmm1, %xmm0
leaq 0x1(%rbx), %rax
cmpq 0xa8(%rsp), %rbx
movq %rax, %rbx
jl 0x7324
leaq 0x1(%r13), %rax
cmpq 0x90(%rsp), %r13
movq %rax, %r13
movq 0x98(%rsp), %r15
jl 0x730e
movq 0x8(%rsp), %rcx
leaq 0x1(%rcx), %rax
cmpq 0x88(%rsp), %rcx
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %r13
jb 0x72f9
divsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movzwl %ax, %r8d
movq 0x70(%rsp), %rdi
movq 0x50(%rsp), %r13
movq %r13, %rsi
movq 0x58(%rsp), %rbx
movq %rbx, %rdx
movq 0x78(%rsp), %r15
movq %r15, %rcx
callq 0xd25a
incq %r13
cmpq 0x48(%rsp), %r13
jne 0x7242
incq %rbx
cmpq 0x40(%rsp), %rbx
jne 0x722f
incq %r15
decq 0x60(%rsp)
cmpq 0x30(%rsp), %r15
jne 0x71ef
leaq 0x106a5(%rip), %rsi     # 0x17b5c
movl $0x5, %edx
movq 0x15b05(%rip), %rbx     # 0x1cfc8
movq %rbx, %rdi
callq 0x32f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0
nop