package pl.wroc.pwr.imagechannel.basic; import pl.wroc.pwr.IOperator; import pl.wroc.pwr.imagechannel.IImageChannel; import pl.wroc.pwr.imagechannel.factory.ImageChannelFactory; public class Threshold implements IOperator< IImageChannel > { private IImageChannel inputChannel; private float lowerValue; private float upperValue; private float thresholdValue; private boolean inPlace; public Threshold( IImageChannel channel, float thresholdValue ) { this(channel, thresholdValue, 0, 1, false); } public Threshold( IImageChannel channel, float thresholdValue, boolean inPlace ) { this(channel, thresholdValue, 0, 1, inPlace); } public Threshold( IImageChannel channel, float thresholdValue, float lowerValue, float upperValue, boolean inPlace ) { this.inputChannel = channel; this.thresholdValue = thresholdValue; this.lowerValue = lowerValue; this.upperValue = upperValue; this.inPlace = inPlace; //System.out.println("Threshold on " + this.inputChannel + " with thresholdValue " + this.thresholdValue + //$NON-NLS-1$ //$NON-NLS-2$ // ", lowerValue " + this.lowerValue + ", upperValue " + this.upperValue); //$NON-NLS-1$ //$NON-NLS-2$ } public IImageChannel apply( ) { IImageChannel outputChannel = this.inPlace ? this.inputChannel : new ImageChannelFactory(this.inputChannel).apply(); final int maxIndex = this.inputChannel.getMaxIndex(); float [] inputRaster = this.inputChannel.getRaster(); float [] outputRaster = outputChannel.getRaster(); if ((inputRaster != null)&&(outputRaster != null)) { for ( int i = 0; i < maxIndex; i++ ) { outputRaster[i] = inputRaster[i]<=this.thresholdValue?this.lowerValue:this.upperValue; } } else { for ( int i = 0; i < maxIndex; i++ ) { outputChannel.setValue( i, this.inputChannel.getValue( i ) <= this.thresholdValue ? this.lowerValue : this.upperValue ); } } return outputChannel; } }