package ie.dcu.image;
import ie.dcu.matrix.*;
import ie.dcu.matrix.Matrix.Type;
/**
* Abstract implementation of an {@link ImageOp}. Subclasses should implement
* the {@link #processImage(MatrixProvider)} method.
*
*
* @author Kevin McGuinness
*/
public abstract class AbstractImageOp implements ImageOp {
private MatrixProvider inputProvider;
private Matrix outputMatrix;
/**
* Default constructor (does nothing).
*/
public AbstractImageOp() {
}
/**
* Returns true if the output matrix is available.
*/
public boolean isProcessingComplete() {
return outputMatrix != null;
}
/**
* Returns true if the input {@link MatrixProvider} has been
* set.
*/
public boolean isInputProviderSet() {
return inputProvider != null;
}
/**
* Set the input provider. Clears the output {@link Matrix}.
*
* @see ImageOp#setInputProvider(MatrixProvider)
*/
public void setInputProvider(MatrixProvider provider) {
this.inputProvider = provider;
this.outputMatrix = null;
}
/**
* Returns the input {@link MatrixProvider}.
*
* @return The input {@link MatrixProvider} or null if it has
* not been set.
*/
public MatrixProvider getInputProvider() {
return inputProvider;
}
/**
* Returns the output {@link Matrix}.
*
* @return The output {@link Matrix} or null if it has not been
* set.
*/
protected Matrix getOutputMatrix() {
if (!isInputProviderSet()) {
throw new IllegalStateException("input provider not set");
}
if (!isProcessingComplete()) {
outputMatrix = processImage(getInputProvider());
}
return outputMatrix;
}
public Matrix getMatrix(boolean alwaysCopy) {
return getMatrix(null, false);
}
public Matrix getMatrix(Type type, boolean alwaysCopy) {
return getOutputMatrix().getMatrix(type, alwaysCopy);
}
/**
* Process the image and return the results.
*
* @param imageProvider
* A class that can be used to obtain the input image.
* @return The result of the image operation.
*/
protected abstract Matrix processImage(MatrixProvider imageProvider);
}