// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq 0x15e11(%rip), %rbx     # 0x1cfc8
leaq 0x10994(%rip), %rsi     # 0x17b52
movl $0x19, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31e0
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq %rax, 0x48(%rsp)
movl %eax, %eax
movq %rax, 0x58(%rsp)
movq %rcx, 0x38(%rsp)
movl %ecx, %eax
movq %rax, 0x50(%rsp)
movq %r15, 0x78(%rsp)
movq 0x28(%r15), %rax
movq %rax, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x7234
movq %rax, 0x8(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x18(%r14)
movq $0x0, 0x28(%r14)
movslq 0x58(%rsp), %rsi
movslq 0x50(%rsp), %rdx
movslq 0x40(%rsp), %rcx
movq %r14, %rdi
xorl %r8d, %r8d
callq 0xceda
cmpl $0x0, 0x8(%rsp)
je 0x759a
movq 0x48(%rsp), %rax
decl %eax
movl %eax, 0x20(%rsp)
movq 0x38(%rsp), %rax
decl %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
leaq 0x20(%r12), %rax
movq %rax, 0xb0(%rsp)
movq $0x0, 0x70(%rsp)
xorl %r15d, %r15d
movq %r14, 0x80(%rsp)
movq 0x15d13(%rip), %rdi     # 0x1cfc8
movl %r15d, %esi
callq 0x3430
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x109f8(%rip), %rsi     # 0x17cc7
callq 0x3300
movq %rbx, %rdi
callq 0x31e0
cmpl $0x0, 0x38(%rsp)
je 0x7587
xorl %ebx, %ebx
movq %r15, 0x88(%rsp)
cmpl $0x0, 0x48(%rsp)
je 0x7579
xorl %r13d, %r13d
movq %rbx, 0x68(%rsp)
movq 0x78(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x9144
movl 0x18(%r12), %edx
leal (%rdx,%r13), %ecx
movl 0x20(%rsp), %esi
cmpl %ecx, %esi
cmovll %esi, %ecx
leal (%rdx,%r15), %edi
movq 0x8(%rsp), %rsi
cmpl %edi, %esi
cmovll %esi, %edi
subl %edx, %r15d
testl %r15d, %r15d
movl $0x0, %esi
cmovgl %r15d, %esi
movl %esi, 0x2c(%rsp)
leal (%rdx,%rbx), %r8d
movl 0x1c(%rsp), %esi
cmpl %r8d, %esi
cmovll %esi, %r8d
subl %edx, %ebx
testl %ebx, %ebx
movl $0x0, %esi
cmovgl %ebx, %esi
movl %esi, 0x24(%rsp)
movzwl %ax, %eax
movl %eax, 0x34(%rsp)
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
movq %r15, 0xa0(%rsp)
movl %edi, 0x30(%rsp)
movslq %edi, %rsi
movq %rsi, 0xb8(%rsp)
testl %ebx, %ebx
cmovlel %eax, %ebx
movq %rbx, 0x90(%rsp)
movq %r13, 0x60(%rsp)
movl %r13d, %r14d
subl %edx, %r14d
testl %r14d, %r14d
cmovlel %eax, %r14d
movq %r14, 0x10(%rsp)
movl %r8d, 0x28(%rsp)
movslq %r8d, %rax
movq %rax, 0xa8(%rsp)
movslq %ecx, %rax
movq %rax, 0x98(%rsp)
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
movl 0x24(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x751b
movq 0x10(%rsp), %r14
subq 0x60(%rsp), %r14
movq 0x90(%rsp), %r15
movl 0x2c(%rsp), %eax
cmpl 0x30(%rsp), %eax
jg 0x7506
movq %r15, %rbp
subq 0x68(%rsp), %rbp
movq 0xa0(%rsp), %rbx
movsd %xmm0, 0xc8(%rsp)
movsd %xmm2, 0xd0(%rsp)
movq 0x78(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x9144
movzwl %ax, %r13d
movl %r13d, %eax
subl 0x34(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
andpd 0xfc77(%rip), %xmm0     # 0x170d0
movsd 0x10(%r12), %xmm1
mulsd 0xfbc0(%rip), %xmm1     # 0x17028
xorpd %xmm2, %xmm2
ucomisd %xmm1, %xmm0
jae 0x7494
divsd 0xfbb6(%rip), %xmm1     # 0x17030
divsd %xmm1, %xmm0
callq 0x3400
cvttsd2si %xmm0, %rax
movl %eax, %eax
movq 0x50(%r12), %rcx
movsd (%rcx,%rax,8), %xmm2
movsd %xmm2, 0xc0(%rsp)
movslq 0x18(%r12), %rax
leaq (%r14,%rax), %rsi
leaq (%rax,%rbp), %rdx
movq 0x70(%rsp), %rcx
addq %rbx, %rcx
addq %rax, %rcx
movq 0xb0(%rsp), %rdi
callq 0x146e4
mulsd 0xc0(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm2
addsd %xmm0, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
mulsd %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm0
addsd %xmm1, %xmm0
leaq 0x1(%rbx), %rax
cmpq 0xb8(%rsp), %rbx
movq %rax, %rbx
jl 0x7418
leaq 0x1(%r15), %rax
cmpq 0xa8(%rsp), %r15
movq %rax, %r15
jl 0x73fa
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rax
cmpq 0x98(%rsp), %rcx
movq %rax, 0x10(%rsp)
jl 0x73da
divsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movzwl %ax, %r8d
movq 0x80(%rsp), %rdi
movq 0x60(%rsp), %r13
movq %r13, %rsi
movq 0x68(%rsp), %rbx
movq %rbx, %rdx
movq 0x88(%rsp), %r15
movq %r15, %rcx
callq 0xd394
incq %r13
cmpq 0x58(%rsp), %r13
jne 0x7304
incq %rbx
cmpq 0x50(%rsp), %rbx
jne 0x72f1
incq %r15
decq 0x70(%rsp)
cmpq 0x40(%rsp), %r15
jne 0x72ae
leaq 0x105cb(%rip), %rsi     # 0x17b6c
movl $0x5, %edx
movq 0x15a1b(%rip), %rbx     # 0x1cfc8
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0
nop