Listing 2: STL-compliant median filter.

// functor
template<size_t SubWindowWidth> struct MedianFiltering {  
  template<typename SubWindow>
  inline unsigned char operator()(SubWindow &in) {
    std::copy(in.begin(), in.end(), tmp.begin());
    std::nth_element(tmp.begin(), 
                     tmp.begin() + (SubWindowWidth * SubWindowWidth) / 2, 
                     tmp.end());
    return data[(SubWindowWidth * SubWindowWidth) / 2];
  }
  unsigned char tmp[SubWindowWidth * SubWindowWidth];
};
// process all pixels
int main(...) {
  std::transform(windowsBegin(in), windowsEnd(in),
                 sequentialBegin(out), MedianFiltering());
}