package pl.wroc.pwr.imagechannel.basic; import pl.wroc.pwr.IOperator; import pl.wroc.pwr.IOperator1P; import pl.wroc.pwr.imagechannel.IImageChannel; import pl.wroc.pwr.imagechannel.factory.ImageChannelFactory; public class Limit implements IOperator1P { private boolean inPlace; final private float upperValue; final private float lowerValue; public Limit() { this(0,1,false); } public Limit(boolean inPlace) { this(0,1,inPlace); } public Limit(float lowerValue, float upperValue, boolean inPlace) { this.lowerValue = lowerValue; this.upperValue = upperValue; this.inPlace = inPlace; } public IImageChannel apply(IImageChannel inputChannel) { if (inputChannel == null) { return null; } IImageChannel outputChannel = this.inPlace ? inputChannel : new ImageChannelFactory(inputChannel).apply(); final int maxIndex = inputChannel.getMaxIndex(); float [] inputRaster = inputChannel.getRaster(); float [] outputRaster = outputChannel.getRaster(); if ((inputRaster != null)&&(outputRaster != null)) { for (int i = 0; i < maxIndex; i++) { outputRaster[i] = Math.max(Math.min(inputRaster[i],this.upperValue),this.lowerValue); } } else { for (int i = 0; i < maxIndex; i++) { outputChannel.setValue(i, Math.max(Math.min(inputChannel.getValue(i), this.upperValue), this.lowerValue)); } } return outputChannel; } }