com.github.ojil.algorithm.RgbClip Maven / Gradle / Ivy
Show all versions of ojil-core Show documentation
/*
* RgbClip.java
*
* Created on May 13, 2006, 2:38 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* Copyright 2007 by Jon A. Webb
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser GNU General Public License
* along with this program. If not, see .
*
*/
package com.github.ojil.algorithm;
import com.github.ojil.core.Error;
import com.github.ojil.core.Image;
import com.github.ojil.core.PipelineStage;
import com.github.ojil.core.RgbImage;
import com.github.ojil.core.RgbVal;
/**
* Pipeline stage performs color clipping, setting all pixels that
* do not meet the threshold test to 0, otherwise leaving them alone.
*
* The test is abs(pixel.R - red) + abs(pixel.G - green) + abs(pixel.B - blue)
* < limit.
*
* The test direction can be reversed using the dir parameter.
* @author webb
*/
public class RgbClip extends PipelineStage {
boolean bDir; /* clipping direction */
byte nB; /* blue value */
byte nG; /* green value */
int nLimit; /* the clipping limit */
byte nR; /* red value */
/**
* Creates a new instance of RgbClip. The clip test is defined here.
*
*
* @param r red value
* @param g green value
* @param b value
* @param l the threshold
* @param dir if true pixels that fail test are set to 0; if false
* pixels that pass test are set to 0.
*/
public RgbClip(
byte r,
byte g,
byte b,
int l,
boolean dir) {
this.nR = r;
this.nG = g;
this.nB = b;
this.nLimit = l;
this.bDir = dir;
}
/**
* Creates a new instance of RgbClip. The clip test is defined here.
*
*
* @param rgb int value containg packed RGB
* @param l the threshold
* @param dir if true pixels that fail test are set to 0; if false
* pixels that pass test are set to 0.
*/
public RgbClip(
int rgb,
int l,
boolean dir) {
this.nR = RgbVal.getR(rgb);
this.nG = RgbVal.getG(rgb);
this.nB = RgbVal.getB(rgb);
this.nLimit = l;
this.bDir = dir;
}
/** Clips the RGB image and sets all pixels that fail/pass the
* test (according to bDir) to 0.
*
* @param image the input image.
* @throws com.github.ojil.core.Error if the cropping window
* extends outside the input image, or the input image
* is not an RgbImage.
*/
public void push(Image image) throws com.github.ojil.core.Error {
if (!(image instanceof RgbImage)) {
throw new Error(
Error.PACKAGE.ALGORITHM,
ErrorCodes.IMAGE_NOT_RGBIMAGE,
image.toString(),
null,
null);
}
RgbImage rgbImage = (RgbImage) image;
Integer[] src = rgbImage.getData();
int nWidth = rgbImage.getWidth();
for (int i=0; i