package ie.dcu.image.binary; import ie.dcu.image.AbstractImageOp; import ie.dcu.matrix.*; /** * Abstract implementation of the {@link BinaryImageOp} interface. * * Subclasses should implement either the {@link #processImage(ByteMatrix)} or * the {@link #processImage(MatrixProvider)} method. * * @author Kevin McGuinness */ public abstract class AbstractBinaryImageOp extends AbstractImageOp implements BinaryImageOp { /** * The value that should be interpreted by the implementor as a background * pixel. */ protected byte backgroundValue; /** * The value that should be interpreted by the implementor as a foreground * pixel. */ protected byte foregroundValue; /** * Flag indicating that the default implementation of * {@link #processImage(MatrixProvider)} should request a copy of the input * {@link Matrix}. */ private boolean requestInputCopy; /** * Default constructor. Sets the {@link #foregroundValue} and * {@link #backgroundValue} values to * {@link BinaryImageOp#DEFAULT_FOREGROUND_VALUE} and * {@link BinaryImageOp#DEFAULT_BACKGROUND_VALUE} */ public AbstractBinaryImageOp() { backgroundValue = DEFAULT_BACKGROUND_VALUE; foregroundValue = DEFAULT_FOREGROUND_VALUE; requestInputCopy = true; } /** * Set a flag indicating whether the default implementation of * {@link #processImage(MatrixProvider)} should pass a copy of the input to * matrix to {@link #processImage(ByteMatrix)}. * * @param requestInputCopy * true to have {@link #processImage(ByteMatrix)} * request a copy of the input matrix, false otherwise. */ protected void setRequestInputCopy(boolean requestInputCopy) { this.requestInputCopy = requestInputCopy; } public void setBackgroundValue(byte backgroundValue) { this.backgroundValue = backgroundValue; } /** * Returns the value that should be interpreted by the implementor as * a background pixel. */ public byte getBackgroundValue() { return backgroundValue; } public void setForegroundValue(byte foregroundValue) { this.foregroundValue = foregroundValue; } /** * Returns the value that should be interpreted by the implementor as * a foreground pixel. */ public byte getForegroundValue() { return foregroundValue; } protected final Matrix processImage(MatrixProvider inputProvider) { Matrix matrix = inputProvider.getMatrix(Matrix.Type.Byte, requestInputCopy); return processImage((ByteMatrix) matrix); } /** * Process the image and return the results. * * @param input * The input byte matrix. * @return The result of the image operation. */ protected abstract Matrix processImage(ByteMatrix input); }