net.sf.jomic.tools
Class ImageTools

java.lang.Object
  extended by net.sf.jomic.tools.ImageTools

public final class ImageTools
extends java.lang.Object

Utility methods for working with images.

Author:
Thomas Aglassinger

Field Summary
static int DISTINCT_ROTATIONS_COUNT
           
static java.lang.String GAUSSIAN_BLUR
           
static int ROTATE_CLOCKWISE
           
static int ROTATE_COUNTERCLOCKWISE
           
static int ROTATE_NONE
           
static int ROTATE_UPSIDE_DOWN
           
static java.lang.String SCALE_ACTUAL
           
static java.lang.String SCALE_FIT
           
static java.lang.String SCALE_HEIGHT
           
static java.lang.String SCALE_WIDTH
           
static java.lang.String THRESHOLD_BLUR
           
 
Method Summary
 void assertIsValidRotation(int rotation)
           
 void assertIsValidScaleMode(java.lang.String scaleMode)
           
 java.awt.image.RenderedImage createBrokenImage(int width, int height, java.awt.Color background, java.awt.Color foreground)
          Create image to represent a broken item.
 java.awt.image.RenderedImage createBusyImage(int width, int height, java.awt.Color background, java.awt.Color foreground)
          Create image to represent a busy image that is in process of being rendered.
 java.awt.image.BufferedImage createColorBox(int width, int height, java.awt.Color color)
          Create a rectangle of a certain color.
 javax.imageio.stream.ImageInputStream createImageInputStream(java.io.File imageFile)
          Same as ImageIO.createImageInputStream, but throws a FileNotFoundException if imageFile cannot be found (instead of returning null).
 double[] fillColorValues(java.awt.Color color, java.awt.image.ColorModel colorModel)
          Compute fill values for color when it should be used by the "border" operator on an image with a ColorModel of colorModel.
 java.awt.image.BufferedImage getAsBufferedImage(java.awt.image.RenderedImage image)
          Convert image to BufferedImage.
 javax.swing.ImageIcon getAsImageIcon(java.awt.image.RenderedImage image)
          Convert image to ImageIcon.
 java.awt.image.BufferedImage getAsQuickBufferedImage(java.awt.image.RenderedImage image)
          Get image in a form that can be processed quickly by most Java rendering operations.
 java.awt.image.RenderedImage getBluredImage(java.awt.image.RenderedImage image, java.lang.String blurMode)
           
 int getDeltaRotation(int oldRotation, int newRotation)
          Get relative Rotation needed to go from absolute rotation oldRotation to newRotation .
 java.awt.Dimension getImageDimension(java.io.File imageFile)
           
 java.awt.Dimension getImageDimension(javax.imageio.stream.ImageInputStream imageStream)
          Get the image dimension of imageStream or null if no reader can be found.
 java.lang.String getImageFormat(java.io.File imageFile)
          Get the format of imageFile or null if no image reader is available for it.
 java.lang.String getImageFormat(javax.imageio.stream.ImageInputStream imageStream)
          Get the format of imageStream or null if no image stream or reader is available.
 java.util.Map getImageProviderMap()
          Get a map with the keys being an image file suffix, and the value being the name of the provider handling it.
 javax.imageio.ImageReader getImageReader(javax.imageio.stream.ImageInputStream imageStream)
          Get an reader for in the image in imageStream.
 int getLeftRotation(int rotation)
           
 int getNumBands(java.awt.image.RenderedImage image)
          Like JAI's PlanarImage.getNumBands(), but also works on RenderedImage .
 java.lang.String[] getPossibleBlurModes()
           
 java.awt.RenderingHints getRenderingHints()
          RenderingHints used by all render operations within ImageTools.
 int getRightRotation(int rotation)
           
 java.awt.image.RenderedImage getRotatedImage(java.awt.image.RenderedImage image, double angle)
           
 java.awt.image.RenderedImage getRotatedImage(java.awt.image.RenderedImage image, int rotation)
          Rotate image in steps of 90 degrees.
 double getScaleToSqueeze(int areaWidth, int areaHeight, int sourceWidth, int sourceHeight, java.lang.String scaleMode)
          Compute scale to squeeze into an area of size areaWidthxareaHeight an image of size sourceWidthxsourceHeight.
 java.awt.image.RenderedImage getSqueezed(java.awt.image.RenderedImage source, int areaWidth, int areaHeight, java.lang.String scaleMode)
          Get a rescaled version of source that depending on scaleMode possibly fits width and/or height while keeping proportions.
 java.awt.Dimension getSqueezedDimension(int areaWidth, int areaHeight, int sourceWidth, int sourceHeight, java.lang.String scaleMode)
          Get the size of the actual area used by a sourceWidth x sourceHeight image squeezed in an areaWidth x areaHeight area.
static ImageTools instance()
          Get accessor to unique instance.
 boolean isCompressedImageFormat(java.lang.String imageFormat)
          Yield true if imageFormat indicates a compressed reformat.
 boolean isImageFile(java.io.File file)
           
 boolean isImageFile(java.lang.String filePathToCheck)
          Yield true if filePathToCheck indicates an image file.
 boolean isImageSuffix(java.lang.String suffix)
           
 boolean isLandscape(java.awt.Dimension size)
           
 boolean isLandscape(int width, int height)
           
 boolean isLandscape(java.awt.image.RenderedImage image)
           
 boolean isValidBlurMode(java.lang.String some)
           
 boolean isValidRotation(int rotation)
           
 boolean isValidScaleMode(java.lang.String mode)
           
 java.awt.image.RenderedImage readImage(java.io.File imageFile)
          Read the specified imageFile.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DISTINCT_ROTATIONS_COUNT

public static final int DISTINCT_ROTATIONS_COUNT
See Also:
Constant Field Values

GAUSSIAN_BLUR

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

ROTATE_CLOCKWISE

public static final int ROTATE_CLOCKWISE
See Also:
Constant Field Values

ROTATE_COUNTERCLOCKWISE

public static final int ROTATE_COUNTERCLOCKWISE
See Also:
Constant Field Values

ROTATE_NONE

public static final int ROTATE_NONE
See Also:
Constant Field Values

ROTATE_UPSIDE_DOWN

public static final int ROTATE_UPSIDE_DOWN
See Also:
Constant Field Values

SCALE_ACTUAL

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

SCALE_FIT

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

SCALE_HEIGHT

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

SCALE_WIDTH

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

THRESHOLD_BLUR

public static final java.lang.String THRESHOLD_BLUR
See Also:
Constant Field Values
Method Detail

getAsBufferedImage

public java.awt.image.BufferedImage getAsBufferedImage(java.awt.image.RenderedImage image)
Convert image to BufferedImage. This is slow.


getAsImageIcon

public javax.swing.ImageIcon getAsImageIcon(java.awt.image.RenderedImage image)
Convert image to ImageIcon. This is slow.


getAsQuickBufferedImage

public java.awt.image.BufferedImage getAsQuickBufferedImage(java.awt.image.RenderedImage image)
Get image in a form that can be processed quickly by most Java rendering operations.


getBluredImage

public java.awt.image.RenderedImage getBluredImage(java.awt.image.RenderedImage image,
                                                   java.lang.String blurMode)

getDeltaRotation

public int getDeltaRotation(int oldRotation,
                            int newRotation)
Get relative Rotation needed to go from absolute rotation oldRotation to newRotation .


getImageDimension

public java.awt.Dimension getImageDimension(javax.imageio.stream.ImageInputStream imageStream)
                                     throws java.io.IOException
Get the image dimension of imageStream or null if no reader can be found.

Throws:
java.io.IOException

getImageDimension

public java.awt.Dimension getImageDimension(java.io.File imageFile)
                                     throws java.io.IOException
Throws:
java.io.IOException

getImageFormat

public java.lang.String getImageFormat(javax.imageio.stream.ImageInputStream imageStream)
                                throws java.io.IOException
Get the format of imageStream or null if no image stream or reader is available.

Throws:
java.io.IOException
See Also:
ImageIO.getImageReaders(java.lang.Object), ImageIO.createImageInputStream(Object), ImageReader.getFormatName()

getImageFormat

public java.lang.String getImageFormat(java.io.File imageFile)
                                throws java.io.IOException
Get the format of imageFile or null if no image reader is available for it.

Throws:
java.io.IOException
See Also:
ImageIO.getImageReaders(java.lang.Object), ImageReader.getFormatName()

getImageProviderMap

public java.util.Map getImageProviderMap()
Get a map with the keys being an image file suffix, and the value being the name of the provider handling it.


getImageReader

public javax.imageio.ImageReader getImageReader(javax.imageio.stream.ImageInputStream imageStream)
Get an reader for in the image in imageStream.


getLeftRotation

public int getLeftRotation(int rotation)

getNumBands

public int getNumBands(java.awt.image.RenderedImage image)
Like JAI's PlanarImage.getNumBands(), but also works on RenderedImage .


getPossibleBlurModes

public java.lang.String[] getPossibleBlurModes()
See Also:
getBluredImage(RenderedImage, String)

getRenderingHints

public java.awt.RenderingHints getRenderingHints()
RenderingHints used by all render operations within ImageTools.


getRightRotation

public int getRightRotation(int rotation)

getRotatedImage

public java.awt.image.RenderedImage getRotatedImage(java.awt.image.RenderedImage image,
                                                    double angle)

getRotatedImage

public java.awt.image.RenderedImage getRotatedImage(java.awt.image.RenderedImage image,
                                                    int rotation)
Rotate image in steps of 90 degrees.

Parameters:
rotation - -1=counter clockwise, 0=no rotation, 1=clockwise, 2=upside down

getScaleToSqueeze

public double getScaleToSqueeze(int areaWidth,
                                int areaHeight,
                                int sourceWidth,
                                int sourceHeight,
                                java.lang.String scaleMode)
Compute scale to squeeze into an area of size areaWidthxareaHeight an image of size sourceWidthxsourceHeight.


getSqueezed

public java.awt.image.RenderedImage getSqueezed(java.awt.image.RenderedImage source,
                                                int areaWidth,
                                                int areaHeight,
                                                java.lang.String scaleMode)
Get a rescaled version of source that depending on scaleMode possibly fits width and/or height while keeping proportions.

Parameters:
scaleMode - one of: SCALE_ACTUAL, SCALE_FIT, SCALE_WIDTH, SCALE_HEIGHT

getSqueezedDimension

public java.awt.Dimension getSqueezedDimension(int areaWidth,
                                               int areaHeight,
                                               int sourceWidth,
                                               int sourceHeight,
                                               java.lang.String scaleMode)
Get the size of the actual area used by a sourceWidth x sourceHeight image squeezed in an areaWidth x areaHeight area.


isCompressedImageFormat

public boolean isCompressedImageFormat(java.lang.String imageFormat)
Yield true if imageFormat indicates a compressed reformat. Images in such formats will not shrink significantly by compressing them using for example ZIP.

See Also:
getImageFormat(ImageInputStream)

isImageFile

public boolean isImageFile(java.lang.String filePathToCheck)
Yield true if filePathToCheck indicates an image file. The main indicator is the suffix, additionally file names starting with "." are not considered images even if the suffix would match. The latter ensures that files storing the resource fork in ZIP archives created by Mac OS X's "compress" are ignored.


isImageFile

public boolean isImageFile(java.io.File file)

isImageSuffix

public boolean isImageSuffix(java.lang.String suffix)

isLandscape

public boolean isLandscape(int width,
                           int height)

isLandscape

public boolean isLandscape(java.awt.Dimension size)

isLandscape

public boolean isLandscape(java.awt.image.RenderedImage image)

isValidBlurMode

public boolean isValidBlurMode(java.lang.String some)

isValidRotation

public boolean isValidRotation(int rotation)

isValidScaleMode

public boolean isValidScaleMode(java.lang.String mode)

instance

public static ImageTools instance()
Get accessor to unique instance.


assertIsValidRotation

public void assertIsValidRotation(int rotation)

assertIsValidScaleMode

public void assertIsValidScaleMode(java.lang.String scaleMode)

createBrokenImage

public java.awt.image.RenderedImage createBrokenImage(int width,
                                                      int height,
                                                      java.awt.Color background,
                                                      java.awt.Color foreground)
Create image to represent a broken item.


createBusyImage

public java.awt.image.RenderedImage createBusyImage(int width,
                                                    int height,
                                                    java.awt.Color background,
                                                    java.awt.Color foreground)
Create image to represent a busy image that is in process of being rendered.


createColorBox

public java.awt.image.BufferedImage createColorBox(int width,
                                                   int height,
                                                   java.awt.Color color)
Create a rectangle of a certain color.


createImageInputStream

public javax.imageio.stream.ImageInputStream createImageInputStream(java.io.File imageFile)
                                                             throws java.io.IOException
Same as ImageIO.createImageInputStream, but throws a FileNotFoundException if imageFile cannot be found (instead of returning null). Why the original does not work that way already is beyond me.

Note that a non-image (such as a text file) still returns proper stream.

Throws:
java.io.IOException

fillColorValues

public double[] fillColorValues(java.awt.Color color,
                                java.awt.image.ColorModel colorModel)
Compute fill values for color when it should be used by the "border" operator on an image with a ColorModel of colorModel.


readImage

public java.awt.image.RenderedImage readImage(java.io.File imageFile)
                                       throws java.io.IOException
Read the specified imageFile.

Throws:
java.io.IOException