// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
movq 0x1255f(%rip), %rdi     # 0x17fc8
leaq 0xc06d(%rip), %rsi      # 0x11add
callq 0x3260
movq %rax, %rdi
callq 0x3110
movq 0x18(%r14), %rax
movq 0x20(%r14), %rcx
movq %rax, 0x18(%rsp)
movl %eax, %eax
movq %rax, 0x58(%rsp)
movq %rcx, 0x10(%rsp)
movl %ecx, %eax
movq %rax, 0x50(%rsp)
movq %r14, 0x68(%rsp)
movq 0x28(%r14), %rax
movq %rax, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x48(%rsp)
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x5abf
movq %rax, 0x8(%rbx)
xorpd %xmm0, %xmm0
andq $0x0, 0x28(%rbx)
movupd %xmm0, 0x18(%rbx)
movslq 0x58(%rsp), %rsi
movslq 0x50(%rsp), %rdx
movslq 0x48(%rsp), %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0x9666
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xa8(%rsp)
movq $0x0, 0x60(%rsp)
xorl %esi, %esi
movq %rbx, 0x70(%rsp)
cmpq 0x48(%rsp), %rsi
je 0x5d97
movq 0x1248a(%rip), %rdi     # 0x17fc8
movq %rsi, 0x20(%rsp)
callq 0x3410
movq %rax, %rdi
leaq 0xc100(%rip), %rsi      # 0x11c52
callq 0x3260
movq %rax, %rdi
callq 0x31b0
xorl %r14d, %r14d
cmpq 0x50(%rsp), %r14
je 0x5d85
xorl %r15d, %r15d
movq %r14, 0x90(%rsp)
cmpq 0x58(%rsp), %r15
je 0x5d7d
movq 0x68(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %rbx
movq %rbx, %rcx
callq 0x70ba
movq 0x28(%rsp), %rcx
movl 0x18(%rcx), %edx
leal (%rdx,%r15), %ecx
movq 0x18(%rsp), %rsi
cmpl %ecx, %esi
cmovll %esi, %ecx
leal (%rdx,%r14), %r10d
movq 0x10(%rsp), %rdi
cmpl %r10d, %edi
cmovll %edi, %r10d
leal (%rdx,%rbx), %edi
movq 0x8(%rsp), %r8
cmpl %edi, %r8d
cmovll %r8d, %edi
subl %edx, %ebx
testl %ebx, %ebx
movl $0x0, %r8d
cmovlel %r8d, %ebx
movq %rbx, 0x98(%rsp)
movl %r14d, %r9d
subl %edx, %r9d
testl %r9d, %r9d
cmovlel %r8d, %r9d
movq %r9, 0x78(%rsp)
movl %r15d, %ebx
subl %edx, %ebx
movzwl %ax, %eax
movl %eax, 0x44(%rsp)
movslq %edi, %rax
movq %rax, 0xb8(%rsp)
testl %ebx, %ebx
cmovlel %r8d, %ebx
movslq %r10d, %rax
movq %rax, 0xa0(%rsp)
movslq %ecx, %rax
movq %rax, 0x88(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %r15, 0x80(%rsp)
cmpq 0x88(%rsp), %rbx
jg 0x5d4c
movq %rbx, %rax
subq %r15, %rax
movq %rax, 0xb0(%rsp)
movq 0x78(%rsp), %r15
cmpq 0xa0(%rsp), %r15
jg 0x5d3c
movq %r15, %r13
subq %r14, %r13
movq 0x98(%rsp), %rbp
cmpq 0xb8(%rsp), %rbp
jg 0x5d2c
movq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x70ba
movzwl %ax, %r12d
movl %r12d, %eax
subl 0x44(%rsp), %eax
cvtsi2sd %eax, %xmm0
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xf0b6
movsd %xmm0, 0xc0(%rsp)
movslq 0x18(%r14), %rax
movq 0xb0(%rsp), %rcx
leaq (%rcx,%rax), %rsi
leaq (%rax,%r13), %rdx
movq 0x60(%rsp), %rcx
addq %rbp, %rcx
addq %rax, %rcx
movq 0xa8(%rsp), %rdi
callq 0xf0fe
mulsd 0xc0(%rsp), %xmm0
cvtsi2sd %r12d, %xmm1
movsd 0x30(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x30(%rsp)
mulsd %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
incq %rbp
jmp 0x5c80
incq %r15
movq 0x90(%rsp), %r14
jmp 0x5c64
incq %rbx
movq 0x80(%rsp), %r15
jmp 0x5c43
movsd 0x38(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movzwl %ax, %r8d
movq 0x70(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
callq 0x9988
incq %r15
jmp 0x5b78
incq %r14
jmp 0x5b62
movq 0x20(%rsp), %rsi
incq %rsi
decq 0x60(%rsp)
jmp 0x5b2c
movq 0x1222a(%rip), %rdi     # 0x17fc8
leaq 0xbd52(%rip), %rsi      # 0x11af7
callq 0x3260
movq %rax, %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3110