Class ImageReaderBase
java.lang.Object
javax.imageio.ImageReader
com.twelvemonkeys.imageio.ImageReaderBase
- Direct Known Subclasses:
BMPImageReader, DCXImageReader, DDSImageReader, DIBImageReader, EXIFUtilities.DisplayHelper, HDRImageReader, ICNSImageReader, IFFImageReader, JPEGImageReader, PCXImageReader, PICTImageReader, PNMImageReader, PNTGImageReader, PSDImageReader, SGIImageReader, SVGImageReader, TGAImageReader, ThumbsDBImageReader, TIFFImageReader, WebPImageReader, WMFImageReader, XWDImageReader
Abstract base class for image readers.
- Version:
- $Id: ImageReaderBase.java,v 1.0 Sep 20, 2007 5:28:37 PM haraldk Exp$
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ImageInputStreamFor convenience.private static final longprivate static final StringSystem property ("com.twelvemonkeys.imageio.maxImageBytes") overriding the maximum size, in bytes, of a destination image allocated from declared dimensions when the input length is unknown.private static final PointFields inherited from class ImageReader
availableLocales, ignoreMetadata, input, locale, minIndex, originatingProvider, progressListeners, seekForwardOnly, updateListeners, warningListeners, warningLocales -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedImageReaderBase(ImageReaderSpi provider) Constructs anImageReaderand sets itsoriginatingProviderfield to the supplied value. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidMakes sure input is set.protected voidcheckBounds(int index) Convenience method to make sure image index is within bounds.(package private) static longcomputeByteSize(ImageTypeSpecifier specifier, int width, int height) (package private) static longvoiddispose()protected static BufferedImagefakeAOI(BufferedImage pImage, ImageReadParam pParam) Utility method for getting the area of interest (AOI) of an image.protected static ImagefakeSubsampling(Image pImage, ImageReadParam pParam) Utility method for getting the subsampled image.static BufferedImagegetDestination(ImageReadParam param, Iterator<ImageTypeSpecifier> types, int width, int height) Returns theBufferedImageto which decoded pixel data should be written.getImageMetadata(int imageIndex) Default implementation that always returnsnull.intgetNumImages(boolean allowSearch) Default implementation that always returns1.Default implementation that always returnsnull.protected static booleanhasExplicitDestination(ImageReadParam pParam) Tests if param has explicit destination.static voidvoidreset()protected abstract voidResets all member variables.private static longprivate static longsaturatedMultiply(long a, long b) voidOverridessetInput, to allow easy access to the input, in case it is anImageInputStream.protected static voidshowIt(BufferedImage pImage, String pTitle) static voidvalidateSourceSize(long declaredByteSize, long inputLength, int maxExpansionRatio) Validates that the declared byte size of a source image (typically computed as width * height * samples per pixel * bytes per sample) is reasonable compared to (ie.(package private) static voidvalidateSourceSize(long declaredBytes, long inputLength, int maxExpansionRatio, long maxImageBytes) static voidvalidateSourceSize(ImageTypeSpecifier rawType, int width, int height, long inputLength, int maxExpansionRatio) Validates that the source image size, based on the image type specifier and dimensions, is reasonable compared to (ie.Methods inherited from class ImageReader
abort, abortRequested, addIIOReadProgressListener, addIIOReadUpdateListener, addIIOReadWarningListener, canReadRaster, checkReadParamBandSettings, clearAbortRequest, computeRegions, getAspectRatio, getAvailableLocales, getDefaultReadParam, getFormatName, getHeight, getImageMetadata, getImageTypes, getInput, getLocale, getMinIndex, getNumThumbnails, getOriginatingProvider, getRawImageType, getSourceRegion, getStreamMetadata, getThumbnailHeight, getThumbnailWidth, getTileGridXOffset, getTileGridYOffset, getTileHeight, getTileWidth, getWidth, hasThumbnails, isIgnoringMetadata, isImageTiled, isRandomAccessEasy, isSeekForwardOnly, processImageComplete, processImageProgress, processImageStarted, processImageUpdate, processPassComplete, processPassStarted, processReadAborted, processSequenceComplete, processSequenceStarted, processThumbnailComplete, processThumbnailPassComplete, processThumbnailPassStarted, processThumbnailProgress, processThumbnailStarted, processThumbnailUpdate, processWarningOccurred, processWarningOccurred, read, read, readAll, readAll, readAsRenderedImage, readerSupportsThumbnails, readRaster, readThumbnail, readTile, readTileRaster, removeAllIIOReadProgressListeners, removeAllIIOReadUpdateListeners, removeAllIIOReadWarningListeners, removeIIOReadProgressListener, removeIIOReadUpdateListener, removeIIOReadWarningListener, setInput, setInput, setLocale
-
Field Details
-
ORIGIN
-
MAX_IMAGE_BYTES_PROPERTY
System property ("com.twelvemonkeys.imageio.maxImageBytes") overriding the maximum size, in bytes, of a destination image allocated from declared dimensions when the input length is unknown. Read once at class initialization, so it must be set as a launch-Doption. When unset, the default is half the maximum heap, capped at 512 MB and floored at 64 MB.- See Also:
-
MAX_IMAGE_BYTES
private static final long MAX_IMAGE_BYTES -
imageInput
For convenience. Only set if the input is anImageInputStream.- See Also:
-
-
Constructor Details
-
ImageReaderBase
Constructs anImageReaderand sets itsoriginatingProviderfield to the supplied value.Subclasses that make use of extensions should provide a constructor with signature
(ImageReaderSpi, Object)in order to retrieve the extension object. If the extension object is unsuitable, anIllegalArgumentExceptionshould be thrown.- Parameters:
provider- theImageReaderSpithat is invoking this constructor, ornull.
-
-
Method Details
-
setInput
OverridessetInput, to allow easy access to the input, in case it is anImageInputStream.- Overrides:
setInputin classImageReader- Parameters:
input- theImageInputStreamor otherObjectto use for future decoding.seekForwardOnly- iftrue, images and metadata may only be read in ascending order from this input source.ignoreMetadata- iftrue, metadata may be ignored during reads.- Throws:
IllegalArgumentException- ifinputis not an instance of one of the classes returned by the originating service provider'sgetInputTypesmethod, or is not anImageInputStream.- See Also:
-
dispose
public void dispose()- Overrides:
disposein classImageReader
-
reset
public void reset()- Overrides:
resetin classImageReader
-
resetMembers
protected abstract void resetMembers()Resets all member variables. This method is by default invoked from: -
getImageMetadata
Default implementation that always returnsnull.- Specified by:
getImageMetadatain classImageReader- Parameters:
imageIndex- ignored, unless overridden- Returns:
null, unless overridden- Throws:
IOException- never, unless overridden.
-
getStreamMetadata
Default implementation that always returnsnull.- Specified by:
getStreamMetadatain classImageReader- Returns:
null, unless overridden- Throws:
IOException- never, unless overridden.
-
getNumImages
Default implementation that always returns1.- Specified by:
getNumImagesin classImageReader- Parameters:
allowSearch- ignored, unless overridden- Returns:
1, unless overridden- Throws:
IOException- never, unless overridden
-
checkBounds
Convenience method to make sure image index is within bounds.- Parameters:
index- the image index- Throws:
IOException- if an error occurs during readingIndexOutOfBoundsException- if notminIndex <= index < numImages
-
assertInput
protected void assertInput()Makes sure input is set.- Throws:
IllegalStateException- ifgetInput() == null.
-
getDestination
public static BufferedImage getDestination(ImageReadParam param, Iterator<ImageTypeSpecifier> types, int width, int height) throws IIOException Returns theBufferedImageto which decoded pixel data should be written.As
ImageReader.getDestination(ImageReadParam, Iterator, int, int)but tests if the explicit destination image (if set) is valid according to theImageTypeSpecifiers given intypes.- Parameters:
param- anImageReadParamto be used to get the destination image or image type, ornull.types- anIteratorofImageTypeSpecifiers indicating the legal image types, with the default first.width- the true width of the image or tile begin decoded.height- the true width of the image or tile being decoded.- Returns:
- the
BufferedImageto which decoded pixel data should be written. - Throws:
IIOException- if theImageTypeSpecifierorBufferedImagespecified byparamdoes not match any of the legal ones fromtypes.IllegalArgumentException- iftypesisnullor empty, or if an object not of typeImageTypeSpecifieris retrieved from it. Or, if the resulting image would have a width or height less than 1, or if the product ofwidthandheightof the resulting image is greater thanInteger.MAX_VALUE.
-
computeByteSize
-
validateSourceSize
public static void validateSourceSize(ImageTypeSpecifier rawType, int width, int height, long inputLength, int maxExpansionRatio) throws IIOException Validates that the source image size, based on the image type specifier and dimensions, is reasonable compared to (ie. not larger than) the input length multiplied by a format specific expansion ratio. If the input length is unknown, the source size is compared to a best-effort ceiling from the "com.twelvemonkeys.imageio.maxImageBytes" system property instead.- Parameters:
rawType- the type which most closely represents the "raw" internal format of the source image.width- the width of the source image.height- the height of the source image.inputLength- the length of the input, if known,-1otherwise.maxExpansionRatio- the maximum reasonable expansion ratio for the format. This typically depends on the compression being used.- Throws:
IIOException- if the computed byte size exceeds the allowed size- See Also:
-
validateSourceSize
public static void validateSourceSize(long declaredByteSize, long inputLength, int maxExpansionRatio) throws IIOException Validates that the declared byte size of a source image (typically computed as width * height * samples per pixel * bytes per sample) is reasonable compared to (ie. not larger than) the input length multiplied by a format specific expansion ratio. If the input length is unknown, the source size is compared to a best-effort ceiling from the "com.twelvemonkeys.imageio.maxImageBytes" system property instead.- Parameters:
declaredByteSize- the declared size of the image.inputLength- the length of the input, if known,-1otherwise.maxExpansionRatio- the maximum reasonable expansion ratio for the format. This typically depends on the compression being used.- Throws:
IIOException- if the computed byte size exceeds the allowed size
-
validateSourceSize
static void validateSourceSize(long declaredBytes, long inputLength, int maxExpansionRatio, long maxImageBytes) throws IIOException - Throws:
IIOException
-
resolveMaxImageBytes
private static long resolveMaxImageBytes() -
defaultMaxImageBytes
static long defaultMaxImageBytes() -
saturatedMultiply
private static long saturatedMultiply(long a, long b) -
fakeAOI
Utility method for getting the area of interest (AOI) of an image. The AOI is defined by theIIOParam.setSourceRegion(java.awt.Rectangle)method.Note: If it is possible for the reader to read the AOI directly, such a method should be used instead, for efficiency.
- Parameters:
pImage- the image to get AOI frompParam- the param optionally specifying the AOI- Returns:
- a
BufferedImagecontaining the area of interest (source region), or the original image, if no source region was set, orpParamwasnull
-
fakeSubsampling
Utility method for getting the subsampled image. The subsampling is defined by theIIOParam.setSourceSubsampling(int, int, int, int)method.NOTE: This method does not take the subsampling offsets into consideration.
Note: If it is possible for the reader to subsample directly, such a method should be used instead, for efficiency.
- Parameters:
pImage- the image to subsamplepParam- the param optionally specifying subsampling- Returns:
- an
Imagecontaining the subsampled image, or the original image, if no subsampling was specified, orpParamwasnull
-
hasExplicitDestination
Tests if param has explicit destination.- Parameters:
pParam- the image read parameter, ornull- Returns:
- true if
pParamis non-nulland either itsgetDestination,getDestinationTypereturns a non-nullvalue, orgetDestinationOffsetreturns aPointthat is not the upper left corner(0, 0).
-
main
- Throws:
IOException
-
showIt
-