// Catalano Imaging Library
// The Catalano Framework
//
// Copyright © Diego Catalano, 2012-2016
// diego.catalano at live.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;

/**
 * Subtract filter - subtract pixel values of one or two images.
 * @author Diego Catalano
 */
public class Subtract implements IApplyInPlace{
    
    private FastBitmap overlayImage;
    private int red = 0, green = 0, blue = 0, gray = 0;
    private boolean isOverlay = false;

    /**
     * Initialize a new instance of the Subtract class.
     */
    public Subtract() {}
    
    /**
     * Initialize a new instance of the Subtract class.
     * @param gray Gray value.
     */
    public Subtract(int gray){
        this.gray = Math.abs(gray);
    }
    
    /**
     * Initialize a new instance of the Subtract class.
     * @param r Red value.
     * @param g Green value.
     * @param b Blue value.
     */
    public Subtract(int r, int g, int b){
        this.red = Math.abs(r);
        this.green = Math.abs(g);
        this.blue = Math.abs(b);
    }

    /**
     * Initialize a new instance of the Subtract class.
     * @param overlayImage Overlay image.
     */
    public Subtract(FastBitmap overlayImage) {
        this.overlayImage = overlayImage;
        this.isOverlay = true;
    }

    /**
     * Sets an overlay image, which will be used as the second image required to process source image.
     * @param overlayImage Overlay image.
     */
    public void setOverlayImage(FastBitmap overlayImage) {
        this.overlayImage = overlayImage;
        this.isOverlay = true;
    }
    
    @Override
    public void applyInPlace(FastBitmap sourceImage){
        if (isOverlay) {
            ApplyInPlaceImage(sourceImage);
        }
        else{
            ApplyInPlaceValues(sourceImage);
        }
    }
    
    private void ApplyInPlaceValues(FastBitmap sourceImage){
       
        int size = sourceImage.getSize();
        if (sourceImage.isGrayscale()){
                int l;
                for (int i = 0; i < size; i++) {
                    l = sourceImage.getGray(i) - gray;
                    l = l < 0 ? 0 : l;
                    sourceImage.setGray(i, l);
                }
        }
        else if (sourceImage.isRGB()){
                int r,g,b;
                for (int i = 0; i < size; i++) {
                    r = sourceImage.getRed(i) - red;
                    g = sourceImage.getGreen(i) - green;
                    b = sourceImage.getBlue(i) - blue;

                    r = r < 0 ? 0 : r;
                    g = g < 0 ? 0 : g;
                    b = b < 0 ? 0 : b;
                    sourceImage.setRGB(i, r, g, b);
                }
            }
    }
    
    private void ApplyInPlaceImage(FastBitmap sourceImage){
        
        int size = sourceImage.getSize();
        int sizeDestination = overlayImage.getWidth() * overlayImage.getHeight();
        if ((sourceImage.isGrayscale()) && (overlayImage.isGrayscale())) {
            if (size == sizeDestination) {
                int l;
                for (int i = 0; i < size; i++) {
                    l = sourceImage.getGray(i) - overlayImage.getGray(i);
                    l = l < 0 ? 0 : l;
                    sourceImage.setGray(i, l);
                }
            }
        }
        else if ((sourceImage.isRGB()) && (overlayImage.isRGB())){
            if (size == sizeDestination) {
                int r,g,b;
                for (int i = 0; i < size; i++) {
                    r = sourceImage.getRed(i) - overlayImage.getRed(i);
                    g = sourceImage.getGreen(i) - overlayImage.getGreen(i);
                    b = sourceImage.getBlue(i) - overlayImage.getBlue(i);

                    r = r < 0 ? 0 : r;
                    g = g < 0 ? 0 : g;
                    b = b < 0 ? 0 : b;
                    sourceImage.setRGB(i, r, g, b);
                }
            }
        }
    }
}
 

Ads help maintain this website.