// Catalano Imaging Library
// The Catalano Framework
//
// Copyright © Diego Catalano, 2012-2016
// diego.catalano at live.com
//
// Copyright © Ivan Kuckir, 2013
// ivan at kuckir.com
//
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

package Catalano.Imaging.Filters;

import Catalano.Imaging.FastBitmap;
import Catalano.Imaging.IApplyInPlace;

/**
 * Gaussian Box Blur.
 * Reference: http://blog.ivank.net/fastest-gaussian-blur.html
 * @author Diego Catalano
 */
public class GaussianBoxBlur implements IApplyInPlace{
    
    private double std;
    private int r;
    
    /**
     * Get Radius.
     * @return Radius.
     */
    public int getRadius(){
        return r;
    }
    
    /**
     * Set Radius.
     * @param radius Radius.
     */
    public void setRadius(int radius){
        this.r = Math.max(1, radius);
    }

    /**
     * Initialize a new instance of the GaussianBoxBlur class.
     */
    public GaussianBoxBlur() {
        this(1);
    }
    
    /**
     * Initialize a new instance of the GaussianBoxBlur class.
     * @param standardDeviation Standard deviation.
     */
    public GaussianBoxBlur(double standardDeviation){
        this(standardDeviation, 3);
    }
    
    /**
     * Initialize a new instance of the GaussianBoxBlur class.
     * @param standartDeviation Standard deviation.
     * @param radius Radius.
     */
    public GaussianBoxBlur(double standartDeviation, int radius){
        this.std = standartDeviation;
        this.r = radius;
    }

    @Override
    public void applyInPlace(FastBitmap fastBitmap) {
        
        FastBitmap copy = new FastBitmap(fastBitmap.getWidth(), fastBitmap.getHeight(), fastBitmap.getColorSpace());
        int[] boxs = BoxesForGauss(std, r);
        
        if(fastBitmap.isGrayscale()){
            BoxBlurGray(fastBitmap, copy, (boxs[0] - 1) / 2);
            BoxBlurGray(copy, fastBitmap, (boxs[1] - 1) / 2);
            BoxBlurGray(fastBitmap, copy, (boxs[2] - 1) / 2);
        }
        else if(fastBitmap.isRGB()){
            BoxBlurRGB(fastBitmap, copy, (boxs[0] - 1) / 2);
            BoxBlurRGB(copy, fastBitmap, (boxs[1] - 1) / 2);
            BoxBlurRGB(fastBitmap, copy, (boxs[2] - 1) / 2);
        }
        
    }
    
    private void BoxBlurGray(FastBitmap source, FastBitmap dest, int r){

        int size = source.getSize();
        for (int i = 0; i < size; i++) {
            dest.setGray(i, source.getGray(i));
        }
        
        BoxBlurH_Gray(dest, source, r);
        BoxBlurT_Gray(source, dest, r);
        
    }
    
    private void BoxBlurRGB(FastBitmap source, FastBitmap dest, int r){
        
        int size = source.getSize();
        
        for (int i = 0; i < size; i++) {
            dest.setRGB(i, source.getRGB(i));
        }
        
        BoxBlurH_RGB(dest, source, r);
        BoxBlurT_RGB(source, dest, r);
        
    }
    
    private void BoxBlurH_Gray (FastBitmap source, FastBitmap dest, int r) {
        
        int w = source.getWidth();
        int h = source.getHeight();
        
        double iarr = 1 / (double)(r+r+1);
        for(int i=0; i 

Ads help maintain this website.