* Hungarian prefix is 'red'.
*
* @author webb
*/
public class GrayReduce extends PipelineStage {
private int cReduceHeight;
private int cReduceWidth;
/** Creates a new instance of GrayReduce.
*
* @param cReduceWidth amount to reduce the width by
* @param cReduceHeight amount to reduce the height by
*/
public GrayReduce(int cReduceWidth, int cReduceHeight) {
setReductionFactor(cReduceWidth, cReduceHeight);
}
/** Reduces a gray image by a factor horizontally and vertically through
* averaging. The reduction factor must be an even multiple of the image
* size.
*
* @param image the input image.
* @throws IllegalArgumentException if the input image is not gray, or
* the reduction factor does not evenly divide the image size.
*/
public void Push(Image image) throws IllegalArgumentException {
if (!(image instanceof Gray8Image)) {
throw new IllegalArgumentException(image.toString() +
" should be a Gray8Image, but isn't");
}
if (image.getWidth() % this.cReduceWidth != 0) {
throw new IllegalArgumentException(image.toString() +
" width should be divisible by " + this.cReduceWidth);
}
if (image.getHeight() % this.cReduceHeight != 0) {
throw new IllegalArgumentException(image.toString() +
" height should be divisible by " + this.cReduceHeight);
}
Gray8Image gray = (Gray8Image) image;
byte[] bIn = gray.getData();
int cReducedHeight = image.getHeight() / this.cReduceHeight;
int cReducedWidth = image.getWidth() / this.cReduceWidth;
Gray8Image result = new Gray8Image(cReducedWidth, cReducedHeight);
byte[] bOut = result.getData();
for (int i=0; i