<commit_before>// __BEGIN_LICENSE__
// 
// Copyright (C) 2008 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA).  All Rights Reserved.
// 
// Copyright 2008 <NAME>. All rights reserved.
// 
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3.  The NOSA has been approved by the Open Source
// Initiative.  See the file COPYING at the top of the distribution
// directory tree for the complete NOSA document.
// 
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
//
// __END_LICENSE__

/// \file StereoSession.h
///

#ifndef __MEDIAN_FILTER_H__
#define __MEDIAN_FILTER_H__

typedef uint8 CalcPixelT;
#define CALC_PIXEL_NUM_VALS 256

CalcPixelT find_median_in_histogram(Vector<int, CALC_PIXEL_NUM_VALS> histogram, int kernSize) {
  int acc = 0;
  int acc_limit = kernSize * kernSize / 2;

  CalcPixelT i = 0;

  for (;;) {
    acc += histogram(i);

    if (acc >= acc_limit)
      break;

    i++;
  }
  
  return i;
}

template<class ImageT>
ImageView<typename ImageT::pixel_type> fast_median_filter(ImageViewBase<ImageT> const& img,  int kernSize) {
  typedef typename ImageT::pixel_type PixelT;
 
  ImageView<CalcPixelT> src = pixel_cast_rescale<CalcPixelT>(img.impl());
  ImageView<CalcPixelT> result(src.cols(), src.rows());

  Vector<int, CALC_PIXEL_NUM_VALS> histogram;

  // Seed histogram
  for (int x = 0; x < kernSize; x++) {
    for (int y = 0; y < kernSize; y++) {
      histogram(src(x, y))++;
    }
  }
  
  bool goingRight = true;
  int x = 0, y = 0;
  while (y <= src.rows() - kernSize) {
    result(x + kernSize / 2, y + kernSize / 2) = find_median_in_histogram(histogram, kernSize);

    if (goingRight) {
      if (x < src.cols() - kernSize) {
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x, y + i))--;
          histogram(src(x + kernSize, y + i))++;
        }
        x++;
      }
      else {
        // Reached the right edge
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x + i, y))--;
          histogram(src(x + i, y + kernSize))++;
        }
        goingRight = false; 
        y++;  
      }
    }
    else {
      if (x > 0) {
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x - 1, y + i))++;
          histogram(src(x + kernSize - 1, y + i))--;
        }
        x--;
      }
      else {
        // Reached the left edge
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x + i, y))--;
          histogram(src(x + i, y + kernSize))++;
        }
        goingRight = true;
        y++;
      }
    }
  }
 
  ImageView<PixelT> result_scaled = pixel_cast_rescale<PixelT>(result);

  return result_scaled;
   
}
template<class PixelT>
class MedianFilterFunctor:public ReturnFixedType<PixelT>
{
   int m_kernel_width;
   int m_kernel_height;
   
public: 
   MedianFilterFunctor(int kernel_width, int kernel_height):
   m_kernel_width(kernel_width),
   m_kernel_height(kernel_height){}

   BBox2i work_area() const{ return BBox2i(Vector2i(-m_kernel_width/2, -m_kernel_height/2),
                                           Vector2i(m_kernel_width/2, m_kernel_height/2));  }

   template<class PixelAccessorT>
   typename PixelAccessorT::pixel_type operator()(PixelAccessorT const& acc) const{
            
   Vector<int, CALC_PIXEL_NUM_VALS> histogram;

   // Seed histogram
   for (int x = 0; x < m_kernel_width; x++) {
     for (int y = 0; y < m_kernel_height; y++) {
          histogram(*acc)++;
     }
   }
   *acc = find_median_in_histogram(histogram, m_kernel_width);
 
   /*
   bool goingRight = true;
   int x = 0, y = 0;
   while (y <= src.rows() - kernSize) {
      
      	result(x + m_kernel_width / 2, y + m_kernel_height / 2) = find_median_in_histogram(histogram, m_kernel_width);
        
    	if (goingRight) {
      	  if (x < src.cols() - kernSize) {
             for (int i = 0; i < kernSize; i++) {
          	histogram(src(x, y + i))--;
          	histogram(src(x + kernSize, y + i))++;
             }
             x++;
          }
      	  else {
             // Reached the right edge
             for (int i = 0; i < kernSize; i++) {
                 histogram(src(x + i, y))--;
                 histogram(src(x + i, y + kernSize))++;
             }
             goingRight = false; 
             y++;  
          }
       }
       else {
          if (x > 0) {
             for (int i = 0; i < kernSize; i++) {
                histogram(src(x - 1, y + i))++;
                histogram(src(x + kernSize - 1, y + i))--;
             }<commit_msg>bug fix that crashed apollo and mi-stereo<commit_after>// __BEGIN_LICENSE__
// 
// Copyright (C) 2008 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA).  All Rights Reserved.
// 
// Copyright 2008 Carnegie Mellon University. All rights reserved.
// 
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3.  The NOSA has been approved by the Open Source
// Initiative.  See the file COPYING at the top of the distribution
// directory tree for the complete NOSA document.
// 
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
//
// __END_LICENSE__

/// \file StereoSession.h
///

#ifndef __MEDIAN_FILTER_H__
#define __MEDIAN_FILTER_H__

typedef uint8 CalcPixelT;
#define CALC_PIXEL_NUM_VALS 256

CalcPixelT find_median_in_histogram(Vector<int, CALC_PIXEL_NUM_VALS> histogram, int kernSize) {
  int acc = 0;
  int acc_limit = kernSize * kernSize / 2;

  CalcPixelT i = 0;

  for (;;) {
    acc += histogram(i);

    if (acc >= acc_limit)
      break;

    i++;
  }
  
  return i;
}

template<class ImageT>
ImageView<typename ImageT::pixel_type> fast_median_filter(ImageViewBase<ImageT> const& img,  int kernSize) {
  typedef typename ImageT::pixel_type PixelT;
 
  ImageView<CalcPixelT> src = pixel_cast_rescale<CalcPixelT>(img.impl());
  ImageView<CalcPixelT> result(src.cols(), src.rows());

  Vector<int, CALC_PIXEL_NUM_VALS> histogram;

  // Seed histogram
  for (int x = 0; x < kernSize; x++) {
    for (int y = 0; y < kernSize; y++) {
      histogram(src(x, y))++;
    }
  }
  
  bool goingRight = true;
  int x = 0, y = 0;
  //while (y <=src.rows() - kernSize) { //this was a bug
  while (y < src.rows() - kernSize) {
     
    result(x + kernSize / 2, y + kernSize / 2) = find_median_in_histogram(histogram, kernSize);

    if (goingRight) {
      if (x < src.cols() - kernSize) {
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x, y + i))--;
          histogram(src(x + kernSize, y + i))++;
        }
        x++;
       
      }
      else {
        // Reached the right edge
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x + i, y))--;
          histogram(src(x + i, y + kernSize))++;
        }
        goingRight = false; 
        y++;  
      }
    }
    else {
      if (x > 0) {
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x - 1, y + i))++;
          histogram(src(x + kernSize - 1, y + i))--;
        }
        x--;
      }
      else {
        // Reached the left edge
        for (int i = 0; i < kernSize; i++) {
          histogram(src(x + i, y))--;
          histogram(src(x + i, y + kernSize))++;
        }
        goingRight = true;
        y++;
      }
    }
  }
 
  ImageView<PixelT> result_scaled = pixel_cast_rescale<PixelT>(result);

  return result_scaled;
   
}
template<class PixelT>
class MedianFilterFunctor:public ReturnFixedType<PixelT>
{
   int m_kernel_width;
   int m_kernel_height;
   
public: 
   MedianFilterFunctor(int kernel_width, int kernel_height):
   m_kernel_width(kernel_width),
   m_kernel_height(kernel_height){}

   BBox2i work_area() const{ return BBox2i(Vector2i(-m_kernel_width/2, -m_kernel_height/2),
                                           Vector2i(m_kernel_width/2, m_kernel_height/2));  }

   template<class PixelAccessorT>
   typename PixelAccessorT::pixel_type operator()(PixelAccessorT const& acc) const{
            
   Vector<int, CALC_PIXEL_NUM_VALS> histogram;

   // Seed histogram
   for (int x = 0; x < m_kernel_width; x++) {
     for (int y = 0; y < m_kernel_height; y++) {
          histogram(*acc)++;
     }
   }
   *acc = find_median_in_histogram(histogram, m_kernel_width);
 
   /*
   bool goingRight = true;
   int x = 0, y = 0;
   while (y < src.rows() - kernSize) {
      
      	result(x + m_kernel_width / 2, y + m_kernel_height / 2) = find_median_in_histogram(histogram, m_kernel_width);
        
    	if (goingRight) {
      	  if (x < src.cols() - kernSize) {
             for (int i = 0; i < kernSize; i++) {
          	histogram(src(x, y + i))--;
          	histogram(src(x + kernSize, y + i))++;
             }
             x++;
          }
      	  else {
             // Reached the right edge
             for (int i = 0; i < kernSize; i++) {
                 histogram(src(x + i, y))--;
                 histogram(src(x + i, y + kernSize))++;
             }
             goingRight = false; 
             y++;  
          }
       }
       else {
          if (x > 0) {
             for (int i = 0; i < kernSize; i++) {
                histogram(src(x - 1, y + i))++;
                histogram(src(x + kernSize - 1, y + i))--;
             }
{'fraction_non_alphanumeric': 0.12599352369738004, 'fraction_numerical': 0.009812579727210283, 'mean_word_length': 2.175077881619938, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '67709', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3350, 'n_words': 954}