Package org.geotools.image.io
Class ImageIOExt
- Object
-
- ImageIOExt
-
public class ImageIOExt extends Object
Provides an alternative source of image input and output streams that uses optimized behavior.Currently implemented optimizations:
- wrap an OutputStream into a
MemoryCacheImageOutputStreamor aFileCacheImageOutputStreambased on a image size threshold
- Since:
- 2.7.2
- Author:
- Andrea Aime - GeoSolutions
- wrap an OutputStream into a
-
-
Constructor Summary
Constructors Constructor Description ImageIOExt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends ImageReaderWriterSpi>
voidallowNativeCodec(String format, Class<T> category, boolean allowed)Allows or disallows native acceleration for the specified image format.static ImageInputStreamcreateImageInputStream(Object input)Returns aImageOutputStreamsuitable for writing on the specifiedinputstatic ImageOutputStreamcreateImageOutputStream(RenderedImage image, Object destination)Builds aImageOutputStreamwriting todestination, based on logic that involves the image sizestatic FilegetCacheDirectory()Returns the cache directory used by ImageIOExt, either the manually configured one, or the result of callingImageIO.getCacheDirectory()static LonggetFilesystemThreshold()The threshold at which the class will flip fromMemoryCacheImageOutputStreamtoFileCacheImageOutputStream.static ImageInputStreamSpigetImageInputStreamSPI(Object input)Get a properImageInputStreamSpiinstance for the providedObjectinput without trying to create anImageInputStream.static ImageInputStreamSpigetImageInputStreamSPI(Object input, boolean streamCreationCheck)Get a properImageInputStreamSpiinstance for the providedObjectinput.static ImageReadergetImageioReader(ImageInputStream inStream)Look for anImageReaderinstance that is able to read the providedImageInputStream, which must be non null.static booleanisCLibAvailable()Tells me whether or not the native libraries for JAI/ImageIO are active or not.static RenderedImageread(Object input)Reads an image from the given input, working around some JDK reader issues.static BufferedImagereadBufferedImage(Object input)Same asread(Object)but ensures the result is aBufferedImage, eventually transforming it if needs be.static voidsetCacheDirectory(File cache)Sets the directory where cache files are to be created.static voidsetFilesystemThreshold(Long filesystemThreshold)Sets the memory/file usage threshold (or null to have the code fall back on ImageIO behavior)
-
-
-
Method Detail
-
createImageOutputStream
public static ImageOutputStream createImageOutputStream(RenderedImage image, Object destination) throws IOException
Builds aImageOutputStreamwriting todestination, based on logic that involves the image size- Parameters:
image- the image to be written on the destination (can be null)destination- the destination- Throws:
IOException
-
createImageInputStream
public static ImageInputStream createImageInputStream(Object input) throws IOException
Returns aImageOutputStreamsuitable for writing on the specifiedinput- Throws:
IOException
-
getCacheDirectory
public static File getCacheDirectory()
Returns the cache directory used by ImageIOExt, either the manually configured one, or the result of callingImageIO.getCacheDirectory()
-
setCacheDirectory
public static void setCacheDirectory(File cache)
Sets the directory where cache files are to be created. If set to null (the default value)ImageIO.getCacheDirectory()will be used as the value- Parameters:
cache- aFilespecifying a directory.
-
getFilesystemThreshold
public static Long getFilesystemThreshold()
The threshold at which the class will flip fromMemoryCacheImageOutputStreamtoFileCacheImageOutputStream. If the in memory, uncompressed image size is lower than the threshold aMemoryCacheImageOutputStreamwill be returned, otherwise aFileCacheImageOutputStreamwill be used instead
-
setFilesystemThreshold
public static void setFilesystemThreshold(Long filesystemThreshold)
Sets the memory/file usage threshold (or null to have the code fall back on ImageIO behavior)- See Also:
getFilesystemThreshold()
-
allowNativeCodec
public static <T extends ImageReaderWriterSpi> void allowNativeCodec(String format, Class<T> category, boolean allowed)
Allows or disallows native acceleration for the specified image format. By default, the image I/O extension for JAI provides native acceleration for PNG and JPEG. Unfortunatly, those native codec has bug in their 1.0 version. Invoking this method will force the use of standard codec provided in J2SE 1.4.Implementation note: the current implementation assume that JAI codec class name start with "CLib". It work for Sun's 1.0 implementation, but may change in future versions. If this method doesn't recognize the class name, it does nothing.
- Parameters:
format- The format name (e.g. "png").category-ImageReaderSpi.classto set the reader, orImageWriterSpi.classto set the writer.allowed-falseto disallow native acceleration.
-
getImageInputStreamSPI
public static final ImageInputStreamSpi getImageInputStreamSPI(Object input)
Get a properImageInputStreamSpiinstance for the providedObjectinput without trying to create anImageInputStream.- See Also:
getImageInputStreamSPI(Object, boolean)
-
getImageInputStreamSPI
public static final ImageInputStreamSpi getImageInputStreamSPI(Object input, boolean streamCreationCheck)
Get a properImageInputStreamSpiinstance for the providedObjectinput.- Parameters:
input- the input object for which we need to find a properImageInputStreamSpiinstancestreamCreationCheck- iftrue, when a properImageInputStreamSpihave been found for the provided input, use it to try creating anImageInputStreamon top of the input.- Returns:
- an
ImageInputStreamSpiinstance.
-
isCLibAvailable
public static boolean isCLibAvailable()
Tells me whether or not the native libraries for JAI/ImageIO are active or not.- Returns:
falsein case the JAI/ImageIO native libs are not in the path,trueotherwise.
-
getImageioReader
public static ImageReader getImageioReader(ImageInputStream inStream)
Look for anImageReaderinstance that is able to read the providedImageInputStream, which must be non null.In case no reader is found,
nullis returned.- Parameters:
inStream- an instance ofImageInputStreamfor which we need to find a suitableImageReader.- Returns:
- a suitable instance of
ImageReaderornullif one cannot be found.
-
read
public static RenderedImage read(Object input) throws IOException
Reads an image from the given input, working around some JDK reader issues. At the time of writing, this applies a work around for PNGs with RGB (no alpha) and a transparent color configured in the header, that the JDK reader cannot handle.- Parameters:
input- A non null image source, like aFile,URL, orInputStream- Returns:
- A image
- Throws:
IOException
-
readBufferedImage
public static BufferedImage readBufferedImage(Object input) throws IOException
Same asread(Object)but ensures the result is aBufferedImage, eventually transforming it if needs be. Callers that can deal withRenderedImageshould use the other method for efficiency sake.- Returns:
- A image
- Throws:
IOException
-
-