Improved image filtering routine by reordering loops.