Package org.opengis.test.coverage.image
Class ImageWriterTestCase
- Object
-
- TestCase
-
- ImageIOTestCase
-
- ImageWriterTestCase
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public abstract class ImageWriterTestCase extends ImageIOTestCase implements Closeable
Base class for testingImageWriter
implementations. This test writes different regions and bands of an image at different sub-sampling levels, then read back the images and compare the sample values.To use this test, subclasses need to set the
writer
field to a non-null value in theprepareImageWriter(boolean)
method. Example:
The writer shall accept at least one of the following output types, in preference order:public class MyImageWriterTest extends ImageWriterTestCase { @Override protected void prepareImageWriter(boolean optionallySetOutput) throws IOException { if (writer == null) { writer = new MyImageWriter(); } } }
ImageOutputStream
- mandatory according Image I/O specification.File
- fallback if the writer doesn't supportImageOutputStream
. This fallback exists becauseImageOutputStream
is hard to support when the writer is implemented by a native library.
- Since:
- 3.1
-
-
Field Summary
Fields Modifier and Type Field Description protected ImageReader
reader
The reader to use for verifying the writer output.protected ImageWriter
writer
The image writer to test.-
Fields inherited from class ImageIOTestCase
isSourceBandsSupported, isSubregionSupported, isSubsamplingOffsetSupported, isSubsamplingSupported, sampleToleranceThreshold
-
Fields inherited from class TestCase
configurationTip, listener, units, validators
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ImageWriterTestCase()
Creates a new test case using a default random number generator.protected
ImageWriterTestCase(long seed)
Creates a new test case using a random number generator initialized to the given seed.
-
Method Summary
Modifier and Type Method Description void
close()
protected void
completeImageMetadata(IIOMetadata metadata, RenderedImage image)
Completes stream or image metadata to be given to the tested writer.protected abstract void
prepareImageWriter(boolean optionallySetOutput)
Invoked when the image writer is about to be used for the first time.void
testOneByteBand()
Tests theImageWriter.write
method for a single band of byte values.void
testOneDoubleBand()
Same test thantestOneByteBand()
, but using the signeddouble
type.void
testOneFloatBand()
Same test thantestOneByteBand()
, but using the signedfloat
type.void
testOneIntBand()
Same test thantestOneByteBand()
, but using the signedint
type.void
testOneShortBand()
Same test thantestOneByteBand()
, but using the signedshort
type.void
testOneUnsignedShortBand()
Same test thantestOneByteBand()
, but using the unsignedshort
type.void
testThreeByteBands()
Same test thantestOneByteBand()
, but using RGB values in three bands.-
Methods inherited from class TestCase
configuration, factories, factories, getEnabledFlags
-
-
-
-
Field Detail
-
writer
protected ImageWriter writer
The image writer to test. This field must be set by subclasses in theprepareImageWriter(boolean)
method.
-
reader
protected ImageReader reader
The reader to use for verifying the writer output. By default, this field isnull
until a reader is first needed, in which case the field is assigned to a reader instance created byImageIO.getImageReader(ImageWriter)
. Subclasses can set explicitely a value to this field if they need the tests to use an other reader instead.ImageWriterTestCase
will use only theImageReader.read(int)
method. Consequently, this reader doesn't need to supportImageReadParam
usage.
-
-
Constructor Detail
-
ImageWriterTestCase
protected ImageWriterTestCase()
Creates a new test case using a default random number generator. The sub-regions, sub-samplings and source bands will be different for every test execution. If reproducible subsetting sequences are needed, use theImageWriterTestCase(long)
constructor instead.
-
ImageWriterTestCase
protected ImageWriterTestCase(long seed)
Creates a new test case using a random number generator initialized to the given seed.- Parameters:
seed
- the initial seed for the random numbers generator. Use a constant value if the tests need to be reproduced with the same sequence of image write parameters.
-
-
Method Detail
-
prepareImageWriter
protected abstract void prepareImageWriter(boolean optionallySetOutput) throws IOException
Invoked when the image writer is about to be used for the first time. Subclasses need to create a newImageWriter
instance if needed.If the
optionallySetOutput
argument istrue
, then subclasses can optionally set the output to a temporary file or other object suitable to the writer. This operation is optional: if no output has been explicitely set,ImageWriterTestCase
will automatically set the output to an in-memory stream or to a temporary file.Example:
This method may be invoked with a@Override protected void prepareImageWriter(boolean optionallySetOutput) throws IOException { if (writer == null) { writer = new MyImageWriter(); } if (optionallySetOutput) { writer.setOutput(output); // Optional operation. } }
false
argument value when the methods to be tested do not need an output, for exampleImageWriter.canWriteRasters()
.- Parameters:
optionallySetOutput
-true
if this method can set the writer output (optional operation), orfalse
if this is not yet necessary.- Throws:
IOException
- if an error occurred while preparing the writer.
-
completeImageMetadata
protected void completeImageMetadata(IIOMetadata metadata, RenderedImage image) throws IOException
Completes stream or image metadata to be given to the tested writer. This method is invoked after the default metadata have been created, and before they are given to the tested image writer, as below:For stream metadata:
IIOMetadata metadata = writer.getDefaultStreamMetadata(param); if (metadata != null) { completeImageMetadata(metadata, null); }
For image metadata:
IIOMetadata metadata = writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(image), param); if (metadata != null) { completeImageMetadata(metadata, image); }
The default implementation does nothing (note: this may change in a future version). Subclasses can override this method for providing custom metadata.- Parameters:
metadata
- the stream or image metadata to complete before to be given to the tested image writer.image
- the image for which to create image metadata, ornull
for stream metadata.- Throws:
IOException
- if the implementation needs to perform an I/O operation and that operation failed.- See Also:
ImageWriter.getDefaultStreamMetadata(ImageWriteParam)
,ImageWriter.getDefaultImageMetadata(ImageTypeSpecifier, ImageWriteParam)
-
testOneByteBand
public void testOneByteBand() throws IOException
Tests theImageWriter.write
method for a single band of byte values. First, this method creates an single-banded image filled with random byte values. Then, this method invokes write the image an arbitrary amount of time for the following configurations (note: anyisXXXSupported
field which was set tofalse
prior the execution of this test will stayfalse
):- Writes the full image once (all
isXXXSupported
fields set tofalse
). - Writes various sub-regions (only
isSubregionSupported
may betrue
) - Writes at various sub-sampling (only
isSubsamplingSupported
may betrue
) - Reads various bands (only
isSourceBandsSupported
may betrue
) - A mix of sub-regions, sub-sampling and source bands
- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
- Writes the full image once (all
-
testThreeByteBands
public void testThreeByteBands() throws IOException
Same test thantestOneByteBand()
, but using RGB values in three bands.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
testOneShortBand
public void testOneShortBand() throws IOException
Same test thantestOneByteBand()
, but using the signedshort
type.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
testOneUnsignedShortBand
public void testOneUnsignedShortBand() throws IOException
Same test thantestOneByteBand()
, but using the unsignedshort
type.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
testOneIntBand
public void testOneIntBand() throws IOException
Same test thantestOneByteBand()
, but using the signedint
type.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
testOneFloatBand
public void testOneFloatBand() throws IOException
Same test thantestOneByteBand()
, but using the signedfloat
type.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
testOneDoubleBand
public void testOneDoubleBand() throws IOException
Same test thantestOneByteBand()
, but using the signeddouble
type.- Throws:
IOException
- if an error occurred while writing the image or or reading it back.
-
close
public void close() throws IOException
Disposes the reader and the writer (if non-null) after each test. The default implementation performs the following cleanup:- If the writer output is closeable, closes it.
- Invokes
ImageWriter.reset()
for clearing the output and listeners. - Invokes
ImageWriter.dispose()
for performing additional resource disposal, if any. - Sets the
writer
field tonull
for preventing accidental use. - Performs the same steps than above for the reader, if non-null.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if an error occurred while closing the output stream.- See Also:
ImageWriter.reset()
,ImageWriter.dispose()
-
-