Program: 149509
Project: 149509
Buggy source:
/** 
 * Blur an image in one direction (x or y) by a Gaussian.
 * @param pixels The input/output data (packed in YX order)
 * @param roi The region to blur
 * @param width The width of the data
 * @param height The height of the data
 * @param sigma Standard deviation of the Gaussian
 * @param xDirection True for bluring in x direction, false for y direction
 * @param extraLines Number of lines (parallel to the blurring direction) below and above the roi bounds that should be processed.
 */
private void blur1Direction(final float[] pixels,Rectangle roi,final int width,final int height,final double sigma,final boolean xDirection,final int extraLines){
  final int UPSCALE_K_RADIUS=2;
  final double MIN_DOWNSCALED_SIGMA=4.;
  final int length=xDirection ? width : height;
  final int pointInc=xDirection ? 1 : width;
  final int lineInc=xDirection ? width : 1;
  final int lineFromA=(xDirection ? roi.y : roi.x) - extraLines;
  final int lineFrom;
  if (lineFromA < 0)   lineFrom=0;
 else   lineFrom=lineFromA;
  final int lineToA=(xDirection ? roi.y + roi.height : roi.x + roi.width) + extraLines;
  final int lineTo;
  if (lineToA > (xDirection ? height : width))   lineTo=(xDirection ? height : width);
 else   lineTo=lineToA;
  final int writeFrom=xDirection ? roi.x : roi.y;
  final int writeTo=xDirection ? roi.x + roi.width : roi.y + roi.height;
  final boolean doDownscaling=sigma > 2 * MIN_DOWNSCALED_SIGMA + 0.5;
  final int reduceBy=doDownscaling ? Math.min((int)Math.floor(sigma / MIN_DOWNSCALED_SIGMA),length) : 1;
  final double sigmaGauss=doDownscaling ? Math.sqrt(sigma * sigma / (reduceBy * reduceBy) - 1. / 3. - 1. / 4.) : sigma;
  final int maxLength=doDownscaling ? (length + reduceBy - 1) / reduceBy + 2 * (UPSCALE_K_RADIUS + 1) : length;
  final float[][] gaussKernel=makeGaussianKernel(sigmaGauss,maxLength);
  final int kRadius=gaussKernel[0].length * reduceBy;
  final int readFrom=(writeFrom - kRadius < 0) ? 0 : writeFrom - kRadius;
  final int readTo=(writeTo + kRadius > length) ? length : writeTo + kRadius;
  final int newLength=doDownscaling ? (readTo - readFrom + reduceBy - 1) / reduceBy + 2 * (UPSCALE_K_RADIUS + 1) : length;
  final int unscaled0=readFrom - (UPSCALE_K_RADIUS + 1) * reduceBy;
  if (doDownscaling)   createScalingKernels(reduceBy);
  final float[] cache1=new float[newLength];
  final float[] cache2=doDownscaling ? new float[newLength] : null;
  int pixel0=lineFrom * lineInc;
  for (int line=lineFrom; line < lineTo; line+=1, pixel0+=lineInc) {
    if (doDownscaling) {
      downscaleLine(pixels,cache1,downscaleKernel,reduceBy,pixel0,unscaled0,length,pointInc,newLength);
      convolveLine(cache1,cache2,gaussKernel,0,newLength,1,newLength - 1,0,1);
      upscaleLine(cache2,pixels,upscaleKernel,reduceBy,pixel0,unscaled0,writeFrom,writeTo,pointInc);
    }
 else {
      int p=pixel0 + readFrom * pointInc;
      for (int i=readFrom; i < readTo; i++, p+=pointInc)       cache1[i]=pixels[p];
      convolveLine(cache1,pixels,gaussKernel,readFrom,readTo,writeFrom,writeTo,pixel0,pointInc);
    }
  }
}

Fixed version (ground truth):
/** 
 * Blur an image in one direction (x or y) by a Gaussian.
 * @param pixels The input/output data (packed in YX order)
 * @param roi The region to blur
 * @param width The width of the data
 * @param height The height of the data
 * @param sigma Standard deviation of the Gaussian
 * @param xDirection True for blurring in x direction, false for y direction
 * @param extraLines Number of lines (parallel to the blurring direction) below and above the roi bounds that should be processed.
 */
private void blur1Direction(final float[] pixels,Rectangle roi,final int width,final int height,final double sigma,final boolean xDirection,final int extraLines){
  final int UPSCALE_K_RADIUS=2;
  final double MIN_DOWNSCALED_SIGMA=4.;
  final int length=xDirection ? width : height;
  final int pointInc=xDirection ? 1 : width;
  final int lineInc=xDirection ? width : 1;
  final int lineFromA=(xDirection ? roi.y : roi.x) - extraLines;
  final int lineFrom;
  if (lineFromA < 0)   lineFrom=0;
 else   lineFrom=lineFromA;
  final int lineToA=(xDirection ? roi.y + roi.height : roi.x + roi.width) + extraLines;
  final int lineTo;
  if (lineToA > (xDirection ? height : width))   lineTo=(xDirection ? height : width);
 else   lineTo=lineToA;
  final int writeFrom=xDirection ? roi.x : roi.y;
  final int writeTo=xDirection ? roi.x + roi.width : roi.y + roi.height;
  final boolean doDownscaling=sigma > 2 * MIN_DOWNSCALED_SIGMA + 0.5;
  final int reduceBy=doDownscaling ? Math.min((int)Math.floor(sigma / MIN_DOWNSCALED_SIGMA),length) : 1;
  final double sigmaGauss=doDownscaling ? Math.sqrt(sigma * sigma / (reduceBy * reduceBy) - 1. / 3. - 1. / 4.) : sigma;
  final int maxLength=doDownscaling ? (length + reduceBy - 1) / reduceBy + 2 * (UPSCALE_K_RADIUS + 1) : length;
  final float[][] gaussKernel=makeGaussianKernel(sigmaGauss,maxLength);
  final int kRadius=gaussKernel[0].length * reduceBy;
  final int readFrom=(writeFrom - kRadius < 0) ? 0 : writeFrom - kRadius;
  final int readTo=(writeTo + kRadius > length) ? length : writeTo + kRadius;
  final int newLength=doDownscaling ? (readTo - readFrom + reduceBy - 1) / reduceBy + 2 * (UPSCALE_K_RADIUS + 1) : length;
  final int unscaled0=readFrom - (UPSCALE_K_RADIUS + 1) * reduceBy;
  if (doDownscaling)   createScalingKernels(reduceBy);
  final float[] cache1=new float[newLength];
  final float[] cache2=doDownscaling ? new float[newLength] : null;
  int pixel0=lineFrom * lineInc;
  for (int line=lineFrom; line < lineTo; line+=1, pixel0+=lineInc) {
    if (doDownscaling) {
      downscaleLine(pixels,cache1,downscaleKernel,reduceBy,pixel0,unscaled0,length,pointInc,newLength);
      convolveLine(cache1,cache2,gaussKernel,0,newLength,1,newLength - 1,0,1);
      upscaleLine(cache2,pixels,upscaleKernel,reduceBy,pixel0,unscaled0,writeFrom,writeTo,pointInc);
    }
 else {
      int p=pixel0 + readFrom * pointInc;
      for (int i=readFrom; i < readTo; i++, p+=pointInc)       cache1[i]=pixels[p];
      convolveLine(cache1,pixels,gaussKernel,readFrom,readTo,writeFrom,writeTo,pixel0,pointInc);
    }
  }
}