ch.systemsx.cisd.openbis.dss.generic.shared.utils
Class ImageUtil

java.lang.Object
  extended by ch.systemsx.cisd.openbis.dss.generic.shared.utils.ImageUtil

public class ImageUtil
extends java.lang.Object

Utility function on images.


Field Summary
static float DEFAULT_IMAGE_OPTIMAL_RESCALING_FACTOR
          When a grayscale image with color depth > 8 bits has to be displayed and user has not decided how it should be converted, then this threshold will be used.
static java.lang.String GIF_FILE
           
static java.lang.String JPEG_FILE
           
static java.lang.String PNG_FILE
           
static java.lang.String TIFF_FILE
           
 
Constructor Summary
ImageUtil()
           
 
Method Summary
static java.awt.image.BufferedImage convertForDisplayIfNecessary(java.awt.image.BufferedImage image)
          If the specified image uses grayscale with color depth larger then 8 bits, conversion to 8 bits grayscale is done.
static java.awt.image.BufferedImage convertForDisplayIfNecessary(java.awt.image.BufferedImage image, float threshold)
          If the specified image uses grayscale with color depth larger then 8 bits, conversion to 8 bits grayscale is done.
static java.awt.image.BufferedImage createThumbnailForDisplay(java.awt.image.BufferedImage image, int maxWidth, int maxHeight)
          Re-scales the image to be the biggest one which fits into a (0,0,maxWidth, maxHeight) rectangle.
static byte[] imageToPng(java.awt.image.BufferedImage image)
          Converts the given image to a PNG image.
static byte[] imageToPng(java.awt.image.BufferedImage image, ar.com.hjg.pngj.PngFilterType filterType, int compressionLevel)
          Converts the given image to a PNG image.
static byte[] imageToPngFast(java.awt.image.BufferedImage image)
          Converts the given image to a PNG image.
static boolean isGif(java.lang.String fileTypeOrNull)
          Returns true if the fileTypeOrNull is a gif file.
static boolean isImageFile(java.io.File file)
          Returns true if the specified file is a supported image file.
static boolean isJpeg(java.lang.String fileTypeOrNull)
          Returns true if the fileTypeOrNull is a jpeg file.
static boolean isPng(java.lang.String fileTypeOrNull)
          Returns true if the fileTypeOrNull is a png file.
static boolean isTiff(java.lang.String fileTypeOrNull)
          Returns true if the fileTypeOrNull is a tiff file.
static int loadColorDepthJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle, ch.systemsx.cisd.imagereaders.ImageID imageID)
          For experts only! Loads some kinds of TIFF images handled by JAI library.
static java.awt.Dimension loadDimensionJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle, ch.systemsx.cisd.imagereaders.ImageID imageID)
          For experts only! Loads some kinds of TIFF images handled by JAI library.
static java.awt.image.BufferedImage loadImageForDisplay(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode)
          Loads an image from specified file node.
static java.awt.image.BufferedImage loadJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle, ch.systemsx.cisd.imagereaders.ImageID imageID)
          For experts only! Loads some kinds of TIFF images handled by JAI library.
static java.awt.image.BufferedImage loadUnchangedImage(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode, java.lang.String imageIdOrNull, java.lang.String imageLibraryNameOrNull, java.lang.String imageLibraryReaderNameOrNull, ch.systemsx.cisd.imagereaders.IReadParams params)
          Loads the image specified by imageIdOrNull from the given inputStream.
static int loadUnchangedImageColorDepth(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode, java.lang.String imageIdOrNull, java.lang.String imageLibraryNameOrNull, java.lang.String imageLibraryReaderNameOrNull)
          Loads the color depth of image specified by imageIdOrNull from the given inputStream.
static java.awt.Dimension loadUnchangedImageDimension(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode, java.lang.String imageIdOrNull, java.lang.String imageLibraryNameOrNull, java.lang.String imageLibraryReaderNameOrNull)
          Loads the size of image specified by imageIdOrNull from the given inputStream.
static ch.systemsx.cisd.imagereaders.ImageID parseImageID(java.lang.String imageIdOrNull, ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode)
          Parses specified string representation of an ImageID.
static java.awt.image.BufferedImage rescale(java.awt.image.BufferedImage image, int maxWidth, int maxHeight, boolean enlargeIfNecessary, boolean highQuality8Bit)
          Re-scales the image to be the biggest one which fits into a (0,0,maxWidth, maxHeight) rectangle.
static java.lang.String tryToFigureOutFileTypeOf(ch.systemsx.cisd.base.io.IRandomAccessFile handle)
          Tries to figure out the file type of the specified binary content.
static void writeImageToPng(java.awt.image.BufferedImage image, java.io.OutputStream out)
          Converts the given image to a PNG image and writes it to the given output stream.
static void writeImageToPng(java.awt.image.BufferedImage image, java.io.OutputStream out, ar.com.hjg.pngj.PngFilterType filterType, int compressionLevel)
          Converts the given image to a PNG image and writes it to the given output stream.
static void writeImageToPngFast(java.awt.image.BufferedImage image, java.io.OutputStream out)
          Converts the given image to a PNG image and writes it to the given output stream.
static void writeImageUsingImageIO(java.awt.image.BufferedImage image, java.io.OutputStream out, java.lang.String format)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TIFF_FILE

public static final java.lang.String TIFF_FILE
See Also:
Constant Field Values

PNG_FILE

public static final java.lang.String PNG_FILE
See Also:
Constant Field Values

JPEG_FILE

public static final java.lang.String JPEG_FILE
See Also:
Constant Field Values

GIF_FILE

public static final java.lang.String GIF_FILE
See Also:
Constant Field Values

DEFAULT_IMAGE_OPTIMAL_RESCALING_FACTOR

public static final float DEFAULT_IMAGE_OPTIMAL_RESCALING_FACTOR
When a grayscale image with color depth > 8 bits has to be displayed and user has not decided how it should be converted, then this threshold will be used.

See Also:
Constant Field Values
Constructor Detail

ImageUtil

public ImageUtil()
Method Detail

loadJavaAdvancedImagingTiff

public static java.awt.image.BufferedImage loadJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle,
                                                                       ch.systemsx.cisd.imagereaders.ImageID imageID)
                                                                throws EnvironmentFailureException
For experts only! Loads some kinds of TIFF images handled by JAI library.

Throws:
EnvironmentFailureException

loadDimensionJavaAdvancedImagingTiff

public static java.awt.Dimension loadDimensionJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle,
                                                                      ch.systemsx.cisd.imagereaders.ImageID imageID)
                                                               throws EnvironmentFailureException
For experts only! Loads some kinds of TIFF images handled by JAI library.

Throws:
EnvironmentFailureException

loadColorDepthJavaAdvancedImagingTiff

public static int loadColorDepthJavaAdvancedImagingTiff(ch.systemsx.cisd.base.io.IRandomAccessFile handle,
                                                        ch.systemsx.cisd.imagereaders.ImageID imageID)
                                                 throws EnvironmentFailureException
For experts only! Loads some kinds of TIFF images handled by JAI library.

Throws:
EnvironmentFailureException

isImageFile

public static boolean isImageFile(java.io.File file)
Returns true if the specified file is a supported image file. Supported formats are GIF, JPG, PNG, TIFF. Only file type is taken into account for figuring out the image format. Following file types are recognized: .gif, .jpg, .jpeg, .png, .tif, .tiff


loadUnchangedImage

public static java.awt.image.BufferedImage loadUnchangedImage(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode,
                                                              java.lang.String imageIdOrNull,
                                                              java.lang.String imageLibraryNameOrNull,
                                                              java.lang.String imageLibraryReaderNameOrNull,
                                                              ch.systemsx.cisd.imagereaders.IReadParams params)
Loads the image specified by imageIdOrNull from the given inputStream. Supported images formats are GIF, JPG, PNG, and TIFF. The input stream will be closed after loading.

Note that the original color depth will be kept, so e.g. 12 or 16 bit grayscale images will not be converted to RGB.

Throws:
java.lang.IllegalArgumentException - if the input stream doesn't start with a magic number identifying supported image format.

loadUnchangedImageDimension

public static java.awt.Dimension loadUnchangedImageDimension(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode,
                                                             java.lang.String imageIdOrNull,
                                                             java.lang.String imageLibraryNameOrNull,
                                                             java.lang.String imageLibraryReaderNameOrNull)
Loads the size of image specified by imageIdOrNull from the given inputStream. Supported images formats are GIF, JPG, PNG, and TIFF. The input stream will be closed after loading.

Throws:
java.lang.IllegalArgumentException - if the input stream doesn't start with a magic number identifying supported image format.

loadUnchangedImageColorDepth

public static int loadUnchangedImageColorDepth(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode,
                                               java.lang.String imageIdOrNull,
                                               java.lang.String imageLibraryNameOrNull,
                                               java.lang.String imageLibraryReaderNameOrNull)
Loads the color depth of image specified by imageIdOrNull from the given inputStream. Supported images formats are GIF, JPG, PNG, and TIFF. The input stream will be closed after loading.

Throws:
java.lang.IllegalArgumentException - if the input stream doesn't start with a magic number identifying supported image format.

imageToPngFast

public static byte[] imageToPngFast(java.awt.image.BufferedImage image)
Converts the given image to a PNG image. Uses fast parameters for the filter and deflate level (no filter and no deflation).

This method is about 7 times faster than ImageIO.write(java.awt.image.RenderedImage, String, java.io.OutputStream) and should be preferred whenever speed is important.

Parameters:
image - The image to convert to the PNG byte[].
Returns:
The bytes of the uncompressed PNG.

writeImageToPngFast

public static void writeImageToPngFast(java.awt.image.BufferedImage image,
                                       java.io.OutputStream out)
Converts the given image to a PNG image and writes it to the given output stream. Uses fast parameters for the filter and deflate level (no filter and no deflation).

This method is about 7 times faster than ImageIO.write(java.awt.image.RenderedImage, String, java.io.OutputStream) and should be preferred whenever speed is important.

Parameters:
image - The image to write to the output stream.
out - The output stream to write the png converted image to.

imageToPng

public static byte[] imageToPng(java.awt.image.BufferedImage image)
Converts the given image to a PNG image. Uses default parameters for the filter and deflate level.

This method is about 7 times faster than ImageIO.write(java.awt.image.RenderedImage, String, java.io.OutputStream) and should be preferred whenever speed is important.

Parameters:
image - The image to convert to the PNG byte[].
Returns:
The bytes of the uncompressed PNG.

writeImageToPng

public static void writeImageToPng(java.awt.image.BufferedImage image,
                                   java.io.OutputStream out)
Converts the given image to a PNG image and writes it to the given output stream. Uses default parameters for the filter and deflate level.

This method is about 3 times faster than ImageIO.write(java.awt.image.RenderedImage, String, java.io.OutputStream) and should be preferred whenever speed is important.

Parameters:
image - The image to write to the output stream.
out - The output stream to write the png converted image to.

imageToPng

public static byte[] imageToPng(java.awt.image.BufferedImage image,
                                ar.com.hjg.pngj.PngFilterType filterType,
                                int compressionLevel)
Converts the given image to a PNG image.

Parameters:
image - The image to write to the output stream.
filterType - The type of the filter (see PNG filters) to apply when converting to PNG, null means PngFilterType.FILTER_DEFAULT.
compressionLevel - the compression level for the deflation filter of the PNG conversion, from -1 to 9. 0 means no compression, 9 means maximal compression, -1 means 6 which is the default deflation level.
Returns:
The bytes of the uncompressed PNG.

writeImageToPng

public static void writeImageToPng(java.awt.image.BufferedImage image,
                                   java.io.OutputStream out,
                                   ar.com.hjg.pngj.PngFilterType filterType,
                                   int compressionLevel)
Converts the given image to a PNG image and writes it to the given output stream.

Parameters:
image - The image to write to the output stream.
out - The output stream to write the png converted image to
filterType - The type of the filter (see PNG filters) to apply when converting to PNG, null means PngFilterType.FILTER_DEFAULT.
compressionLevel - the compression level for the deflation filter of the PNG conversion, from -1 to 9. 0 means no compression, 9 means maximal compression, -1 means 6 which is the default deflation level.

writeImageUsingImageIO

public static void writeImageUsingImageIO(java.awt.image.BufferedImage image,
                                          java.io.OutputStream out,
                                          java.lang.String format)
                                   throws ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked
Throws:
ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked

parseImageID

public static ch.systemsx.cisd.imagereaders.ImageID parseImageID(java.lang.String imageIdOrNull,
                                                                 ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode)
Parses specified string representation of an ImageID. If the argument is null ImageID.NULL will be returned.


loadImageForDisplay

public static java.awt.image.BufferedImage loadImageForDisplay(ch.systemsx.cisd.openbis.common.io.hierarchical_content.api.IHierarchicalContentNode contentNode)
Loads an image from specified file node. Supported file formats are GIF, JPG, PNG, and TIFF.

Throws:
java.lang.IllegalArgumentException - if the file isn't a valid image file.

createThumbnailForDisplay

public static java.awt.image.BufferedImage createThumbnailForDisplay(java.awt.image.BufferedImage image,
                                                                     int maxWidth,
                                                                     int maxHeight)
Re-scales the image to be the biggest one which fits into a (0,0,maxWidth, maxHeight) rectangle. Preserves the aspect ratio. If the rectangle is bigger than the image does nothing.

If the specified image uses grayscale with color depth larger then 8 bits, conversion to 8 bits grayscale is done.

Parameters:
maxWidth - Maximum width of the result image.
maxHeight - Maximum height of the result image.

convertForDisplayIfNecessary

public static java.awt.image.BufferedImage convertForDisplayIfNecessary(java.awt.image.BufferedImage image)
If the specified image uses grayscale with color depth larger then 8 bits, conversion to 8 bits grayscale is done. Otherwise the original image is returned.


convertForDisplayIfNecessary

public static java.awt.image.BufferedImage convertForDisplayIfNecessary(java.awt.image.BufferedImage image,
                                                                        float threshold)
If the specified image uses grayscale with color depth larger then 8 bits, conversion to 8 bits grayscale is done. Otherwise the original image is returned.


rescale

public static java.awt.image.BufferedImage rescale(java.awt.image.BufferedImage image,
                                                   int maxWidth,
                                                   int maxHeight,
                                                   boolean enlargeIfNecessary,
                                                   boolean highQuality8Bit)
Re-scales the image to be the biggest one which fits into a (0,0,maxWidth, maxHeight) rectangle. Preserves the aspect ratio. If the rectangle is bigger than the image and 'enlargeIfNecessary' is false then nothing is done.

Parameters:
maxWidth - Maximum width of the result image.
maxHeight - Maximum height of the result image.
enlargeIfNecessary - if false and the image has smaller width and height than the specified limit, then the image is not changed.
highQuality8Bit - if true thumbnails will be of higher quality, but rescaling will take longer and the image will be converted to 8 bit.

tryToFigureOutFileTypeOf

public static java.lang.String tryToFigureOutFileTypeOf(ch.systemsx.cisd.base.io.IRandomAccessFile handle)
Tries to figure out the file type of the specified binary content. It uses the first few bytes as a finger print (so-called 'magic numbers') as a heuristic to get the type of content. Currently only the following types are recognized: gif, jpg, png, tif.

Parameters:
handle - IRandomAccessFile which supports marking.
Returns:
null if file type couldn't be figured out.

isTiff

public static boolean isTiff(java.lang.String fileTypeOrNull)
Returns true if the fileTypeOrNull is a tiff file.


isJpeg

public static boolean isJpeg(java.lang.String fileTypeOrNull)
Returns true if the fileTypeOrNull is a jpeg file.


isPng

public static boolean isPng(java.lang.String fileTypeOrNull)
Returns true if the fileTypeOrNull is a png file.


isGif

public static boolean isGif(java.lang.String fileTypeOrNull)
Returns true if the fileTypeOrNull is a gif file.