org.icepdf.core.pobjects
Class ImageUtility

java.lang.Object
  extended by org.icepdf.core.pobjects.ImageUtility

public class ImageUtility
extends java.lang.Object

Utility methods for applying various colour models and masks to image data.

Since:
5.0

Field Summary
protected static int[] GRAY_1_BIT_INDEX_TO_RGB
           
protected static int[] GRAY_1_BIT_INDEX_TO_RGB_REVERSED
           
protected static int[] GRAY_2_BIT_INDEX_TO_RGB
           
protected static int[] GRAY_4_BIT_INDEX_TO_RGB
           
protected static int JPEG_ENC_CMYK
           
protected static int JPEG_ENC_GRAY
           
protected static java.lang.String[] JPEG_ENC_NAMES
           
protected static int JPEG_ENC_RGB
           
protected static int JPEG_ENC_UNKNOWN_PROBABLY_YCbCr
           
protected static int JPEG_ENC_YCbCr
           
protected static int JPEG_ENC_YCCK
           
 
Method Summary
protected static java.awt.image.BufferedImage alterBufferedImage(java.awt.image.BufferedImage bi, java.awt.image.BufferedImage smaskImage, java.awt.image.BufferedImage maskImage, int[] maskMinRGB, int[] maskMaxRGB)
           
protected static java.awt.image.BufferedImage alterRasterCMYK2BGRA(java.awt.image.WritableRaster wr)
           
protected static java.awt.image.BufferedImage alterRasterCMYK2BGRA(java.awt.image.WritableRaster wr, float[] decode)
           
protected static void alterRasterRGB2PColorSpace(java.awt.image.WritableRaster wr, PColorSpace colorSpace)
          Convert a rgb encoded raster to the specified colour space.
protected static java.awt.image.WritableRaster alterRasterRGBA(java.awt.image.WritableRaster wr, java.awt.image.BufferedImage smaskImage, java.awt.image.BufferedImage maskImage, int[] maskMinRGB, int[] maskMaxRGB)
           
protected static void alterRasterY2Gray(java.awt.image.WritableRaster wr, float[] decode)
           
protected static java.awt.image.BufferedImage alterRasterYCbCr2RGBA(java.awt.image.WritableRaster wr, float[] decode)
           
protected static void alterRasterYCbCrA2RGBA(java.awt.image.WritableRaster wr)
           
protected static void alterRasterYCCK2BGRA(java.awt.image.WritableRaster wr, java.awt.image.BufferedImage smaskImage, java.awt.image.BufferedImage maskImage, float[] decode, int bitsPerComponent)
          The basic idea is that we do a fuzzy colour conversion from YCCK to BGRA.
protected static void alterRasterYCCK2CMYK(java.awt.image.WritableRaster wr, float[] decode)
          The basic idea is that we do a fuzzy colour conversion from YCCK to CMYK.
static java.awt.image.BufferedImage applyExplicitMask(java.awt.image.BufferedImage baseImage, java.awt.image.BufferedImage maskImage)
          (see 8.9.6.3, "Explicit Masking") Explicit Masking algorithm, as of PDF 1.3.
protected static java.awt.image.BufferedImage applyExplicitMask(java.awt.image.BufferedImage baseImage, java.awt.Color fill)
          Treats the base image as as mask data applying the specified fill colour to the flagged bytes and a transparency value otherwise.
static java.awt.image.BufferedImage applyExplicitSMask(java.awt.image.BufferedImage baseImage, java.awt.image.BufferedImage sMaskImage)
          (see 11.6.5.3, "Soft-Mask Images") A subsidiary image XObject defining a soft-mask image that shall be used as a source of mask shape or mask opacity values in the transparent imaging model.
protected static java.awt.image.BufferedImage applyIndexColourModel(java.awt.image.BufferedImage image, int width, int height, PColorSpace colourSpace, int bitspercomponent)
          Temporarily pulled out the index colur model application for images from the raw image decode.
static java.awt.image.BufferedImage createBufferedImage(java.awt.Image imageIn)
           
static java.awt.image.BufferedImage createBufferedImage(java.awt.Image imageIn, int imageType)
           
static void displayImage(java.awt.image.BufferedImage bufferedImage, java.lang.String title)
           
 float getBlackRatio()
           
static ImageUtility getInstance()
           
protected static int getJPEGEncoding(byte[] data, int dataLength)
           
protected static void getNormalizedComponents(byte[] pixels, float[] decode, float[] out)
          Apply the Decode Array domain for each colour component.
protected static void getNormalizedComponents(byte[] pixels, float[] decode, int[] out)
          Apply the Decode Array domain for each colour component.
static java.awt.Image getTrilinearScaledInstance(java.awt.image.BufferedImage img, int targetWidth, int targetHeight)
          Applies an iterative scaling method to provide a smooth end result, once complete apply a trilinear blend based on the desired width and height.
protected static boolean hasAlpha(java.awt.Image image)
           
protected static java.awt.image.BufferedImage jbig2Decode(byte[] data, java.util.HashMap decodeParms, Stream globalsStream)
           
protected static java.awt.image.BufferedImage makeGrayBufferedImage(java.awt.image.WritableRaster wr)
           
protected static java.awt.image.BufferedImage makeImageWithRasterFromBytes(PColorSpace colourSpace, java.awt.Color fill, int width, int height, int colorSpaceCompCount, int bitspercomponent, boolean imageMask, float[] decode, java.awt.image.BufferedImage smaskImage, java.awt.image.BufferedImage maskImage, int[] maskMinRGB, int[] maskMaxRGB, int maskMinIndex, int maskMaxIndex, byte[] data, int dataLength)
           
protected static java.awt.image.BufferedImage makeRGBABufferedImage(java.awt.image.WritableRaster wr)
          Utility to build an RGBA buffered image using the specified raster and a Transparency.OPAQUE transparency model.
protected static java.awt.image.BufferedImage makeRGBABufferedImage(java.awt.image.WritableRaster wr, int transparency)
          Utility to build an RGBA buffered image using the specified raster and transparency type.
protected static java.awt.image.BufferedImage makeRGBABufferedImageFromImage(java.awt.Image image)
           
protected static java.awt.image.BufferedImage makeRGBBufferedImage(java.awt.image.WritableRaster wr)
           
protected static java.awt.image.BufferedImage makeRGBBufferedImage(java.awt.image.WritableRaster wr, float[] decode, PColorSpace colorSpace)
           
protected static java.awt.image.BufferedImage makeRGBtoRGBABuffer(java.awt.image.WritableRaster wr, int width, int height)
           
protected static java.awt.image.BufferedImage proJbig2Decode(javax.imageio.stream.ImageInputStream imageInputStream, java.util.HashMap decodeParms, Stream globalsStream)
           
static java.awt.image.BufferedImage[] scaleImagesToSameSize(java.awt.image.BufferedImage baseImage, java.awt.image.BufferedImage maskImage)
          Utility method to scale the two provided images.
 void setBlackRatio(float blackRatio)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GRAY_1_BIT_INDEX_TO_RGB_REVERSED

protected static final int[] GRAY_1_BIT_INDEX_TO_RGB_REVERSED

GRAY_1_BIT_INDEX_TO_RGB

protected static final int[] GRAY_1_BIT_INDEX_TO_RGB

GRAY_2_BIT_INDEX_TO_RGB

protected static final int[] GRAY_2_BIT_INDEX_TO_RGB

GRAY_4_BIT_INDEX_TO_RGB

protected static final int[] GRAY_4_BIT_INDEX_TO_RGB

JPEG_ENC_UNKNOWN_PROBABLY_YCbCr

protected static final int JPEG_ENC_UNKNOWN_PROBABLY_YCbCr
See Also:
Constant Field Values

JPEG_ENC_RGB

protected static final int JPEG_ENC_RGB
See Also:
Constant Field Values

JPEG_ENC_CMYK

protected static final int JPEG_ENC_CMYK
See Also:
Constant Field Values

JPEG_ENC_YCbCr

protected static final int JPEG_ENC_YCbCr
See Also:
Constant Field Values

JPEG_ENC_YCCK

protected static final int JPEG_ENC_YCCK
See Also:
Constant Field Values

JPEG_ENC_GRAY

protected static final int JPEG_ENC_GRAY
See Also:
Constant Field Values

JPEG_ENC_NAMES

protected static java.lang.String[] JPEG_ENC_NAMES
Method Detail

getInstance

public static ImageUtility getInstance()

getBlackRatio

public float getBlackRatio()

setBlackRatio

public void setBlackRatio(float blackRatio)

alterBufferedImage

protected static java.awt.image.BufferedImage alterBufferedImage(java.awt.image.BufferedImage bi,
                                                                 java.awt.image.BufferedImage smaskImage,
                                                                 java.awt.image.BufferedImage maskImage,
                                                                 int[] maskMinRGB,
                                                                 int[] maskMaxRGB)

alterRasterCMYK2BGRA

protected static java.awt.image.BufferedImage alterRasterCMYK2BGRA(java.awt.image.WritableRaster wr,
                                                                   float[] decode)

alterRasterCMYK2BGRA

protected static java.awt.image.BufferedImage alterRasterCMYK2BGRA(java.awt.image.WritableRaster wr)

displayImage

public static void displayImage(java.awt.image.BufferedImage bufferedImage,
                                java.lang.String title)

makeRGBABufferedImage

protected static java.awt.image.BufferedImage makeRGBABufferedImage(java.awt.image.WritableRaster wr)
Utility to build an RGBA buffered image using the specified raster and a Transparency.OPAQUE transparency model.

Parameters:
wr - writable raster of image.
Returns:
constructed image.

makeRGBABufferedImage

protected static java.awt.image.BufferedImage makeRGBABufferedImage(java.awt.image.WritableRaster wr,
                                                                    int transparency)
Utility to build an RGBA buffered image using the specified raster and transparency type.

Parameters:
wr - writable raster of image.
transparency - any valid Transparency interface type. Bitmask, opaque and translucent.
Returns:
constructed image.

makeRGBtoRGBABuffer

protected static java.awt.image.BufferedImage makeRGBtoRGBABuffer(java.awt.image.WritableRaster wr,
                                                                  int width,
                                                                  int height)

makeRGBBufferedImage

protected static java.awt.image.BufferedImage makeRGBBufferedImage(java.awt.image.WritableRaster wr)

makeGrayBufferedImage

protected static java.awt.image.BufferedImage makeGrayBufferedImage(java.awt.image.WritableRaster wr)

makeRGBBufferedImage

protected static java.awt.image.BufferedImage makeRGBBufferedImage(java.awt.image.WritableRaster wr,
                                                                   float[] decode,
                                                                   PColorSpace colorSpace)

makeRGBABufferedImageFromImage

protected static java.awt.image.BufferedImage makeRGBABufferedImageFromImage(java.awt.Image image)

hasAlpha

protected static boolean hasAlpha(java.awt.Image image)

alterRasterYCCK2CMYK

protected static void alterRasterYCCK2CMYK(java.awt.image.WritableRaster wr,
                                           float[] decode)
The basic idea is that we do a fuzzy colour conversion from YCCK to CMYK. The conversion is not perfect but when converted again from CMYK to RGB the result is much better then going directly from YCCK to RGB. NOTE: no masking here, as it is done later in the call to alterRasterCMYK2BGRA

Parameters:
wr - writable raster to alter.
decode - decode vector.

getNormalizedComponents

protected static void getNormalizedComponents(byte[] pixels,
                                              float[] decode,
                                              float[] out)
Apply the Decode Array domain for each colour component. Assumes output range is 0-1f for each value in out.

Parameters:
pixels - colour to process by decode
decode - decode array for colour space
out - return value always (2bitsPerComponent - 1).

getNormalizedComponents

protected static void getNormalizedComponents(byte[] pixels,
                                              float[] decode,
                                              int[] out)
Apply the Decode Array domain for each colour component. Assumes output range is 0-255 for each value in out.

Parameters:
pixels - colour to process by decode
decode - decode array for colour space
out - return value always (2bitsPerComponent - 1).

alterRasterRGB2PColorSpace

protected static void alterRasterRGB2PColorSpace(java.awt.image.WritableRaster wr,
                                                 PColorSpace colorSpace)
Convert a rgb encoded raster to the specified colour space.

Parameters:
wr - writable rasters in rgb.
colorSpace - colour space to convert colours too.

alterRasterRGBA

protected static java.awt.image.WritableRaster alterRasterRGBA(java.awt.image.WritableRaster wr,
                                                               java.awt.image.BufferedImage smaskImage,
                                                               java.awt.image.BufferedImage maskImage,
                                                               int[] maskMinRGB,
                                                               int[] maskMaxRGB)

alterRasterY2Gray

protected static void alterRasterY2Gray(java.awt.image.WritableRaster wr,
                                        float[] decode)

alterRasterYCbCr2RGBA

protected static java.awt.image.BufferedImage alterRasterYCbCr2RGBA(java.awt.image.WritableRaster wr,
                                                                    float[] decode)

alterRasterYCCK2BGRA

protected static void alterRasterYCCK2BGRA(java.awt.image.WritableRaster wr,
                                           java.awt.image.BufferedImage smaskImage,
                                           java.awt.image.BufferedImage maskImage,
                                           float[] decode,
                                           int bitsPerComponent)
The basic idea is that we do a fuzzy colour conversion from YCCK to BGRA. The conversion is not perfect giving a bit of a greenish hue to the image in question. I've tweaked the core Adobe algorithm ot give slightly "better" colour representation but it does seem to make red a little light.

Parameters:
wr - image stream to convert colour space.
smaskImage - smask used to apply alpha values.
maskImage - maks image for drop out.

alterRasterYCbCrA2RGBA

protected static void alterRasterYCbCrA2RGBA(java.awt.image.WritableRaster wr)

applyExplicitMask

public static java.awt.image.BufferedImage applyExplicitMask(java.awt.image.BufferedImage baseImage,
                                                             java.awt.image.BufferedImage maskImage)
(see 8.9.6.3, "Explicit Masking") Explicit Masking algorithm, as of PDF 1.3. The entry in an image dictionary may be an image mask, as described under "Stencil Masking", which serves as an explicit mask for the primary or base image. The base image and the image mask need not have the same resolution (width, height), but since all images are defined on the unit square in user space, their boundaries on the page will conincide; that is, they will overlay each other.

The image mask indicates indicates which places on the page are to be painted and which are to be masked out (left unchanged). Unmasked areas are painted with the corresponding portions of the base image; masked areas are not.

Parameters:
baseImage - base image in which the mask weill be applied to
maskImage - image mask to be applied to base image.

applyExplicitSMask

public static java.awt.image.BufferedImage applyExplicitSMask(java.awt.image.BufferedImage baseImage,
                                                              java.awt.image.BufferedImage sMaskImage)
(see 11.6.5.3, "Soft-Mask Images") A subsidiary image XObject defining a soft-mask image that shall be used as a source of mask shape or mask opacity values in the transparent imaging model. The alpha source parameter in the graphics state determines whether the mask values shall be interpreted as shape or opacity.

If present, this entry shall override the current soft mask in the graphics state, as well as the image’s Mask entry, if any. However, the other transparency-related graphics state parameters—blend mode and alpha constant—shall remain in effect. If SMask is absent, the image shall have no associated soft mask (although the current soft mask in the graphics state may still apply).

Parameters:
baseImage - base image in which the mask weill be applied to

applyExplicitMask

protected static java.awt.image.BufferedImage applyExplicitMask(java.awt.image.BufferedImage baseImage,
                                                                java.awt.Color fill)
Treats the base image as as mask data applying the specified fill colour to the flagged bytes and a transparency value otherwise. This method creates a new BufferedImage with a transparency model so it will cause a memory spike.

Parameters:
baseImage - masking image.
fill - fill value to apply to mask.
Returns:
masked image encoded with the fill colour and transparency.

applyIndexColourModel

protected static java.awt.image.BufferedImage applyIndexColourModel(java.awt.image.BufferedImage image,
                                                                    int width,
                                                                    int height,
                                                                    PColorSpace colourSpace,
                                                                    int bitspercomponent)
Temporarily pulled out the index colur model application for images from the raw image decode. This method is only called from JPEG2000 code for now but will be consolidate as we move to to 5.0


proJbig2Decode

protected static java.awt.image.BufferedImage proJbig2Decode(javax.imageio.stream.ImageInputStream imageInputStream,
                                                             java.util.HashMap decodeParms,
                                                             Stream globalsStream)
                                                      throws java.lang.reflect.InvocationTargetException,
                                                             java.lang.IllegalAccessException,
                                                             java.lang.NoSuchMethodException,
                                                             java.io.IOException,
                                                             java.lang.ClassNotFoundException,
                                                             java.lang.InstantiationException
Throws:
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
java.lang.NoSuchMethodException
java.io.IOException
java.lang.ClassNotFoundException
java.lang.InstantiationException

jbig2Decode

protected static java.awt.image.BufferedImage jbig2Decode(byte[] data,
                                                          java.util.HashMap decodeParms,
                                                          Stream globalsStream)

getJPEGEncoding

protected static int getJPEGEncoding(byte[] data,
                                     int dataLength)

makeImageWithRasterFromBytes

protected static java.awt.image.BufferedImage makeImageWithRasterFromBytes(PColorSpace colourSpace,
                                                                           java.awt.Color fill,
                                                                           int width,
                                                                           int height,
                                                                           int colorSpaceCompCount,
                                                                           int bitspercomponent,
                                                                           boolean imageMask,
                                                                           float[] decode,
                                                                           java.awt.image.BufferedImage smaskImage,
                                                                           java.awt.image.BufferedImage maskImage,
                                                                           int[] maskMinRGB,
                                                                           int[] maskMaxRGB,
                                                                           int maskMinIndex,
                                                                           int maskMaxIndex,
                                                                           byte[] data,
                                                                           int dataLength)

createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(java.awt.Image imageIn)

createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(java.awt.Image imageIn,
                                                               int imageType)

scaleImagesToSameSize

public static java.awt.image.BufferedImage[] scaleImagesToSameSize(java.awt.image.BufferedImage baseImage,
                                                                   java.awt.image.BufferedImage maskImage)
Utility method to scale the two provided images. There are two modes based on the system property "". The d

Parameters:
baseImage - base image that mask will be applied to
maskImage - mask image that will be applied to base image.
Returns:
array of altered baseImage and maskImage, should be same size on return.

getTrilinearScaledInstance

public static java.awt.Image getTrilinearScaledInstance(java.awt.image.BufferedImage img,
                                                        int targetWidth,
                                                        int targetHeight)
Applies an iterative scaling method to provide a smooth end result, once complete apply a trilinear blend based on the desired width and height. Technique derived from Jim Graham example code.

Parameters:
img - image to scale
targetWidth - target width
targetHeight - target height
Returns:
scaled instance.