Class ImageEncodingHelper


  • public class ImageEncodingHelper
    extends java.lang.Object
    Helper class for encoding bitmap images.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageEncodingHelper​(java.awt.image.RenderedImage image)
      Main constructor
      ImageEncodingHelper​(java.awt.image.RenderedImage image, boolean enableCMYK)
      Main constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ImageEncoder createRenderedImageEncoder​(java.awt.image.RenderedImage img)
      Create an ImageEncoder for the given RenderImage instance.
      protected void determineEncodedColorModel()
      Determines the color model used for encoding the image.
      void encode​(java.io.OutputStream out)
      Encodes the image and writes everything to the given OutputStream.
      void encodeAlpha​(java.io.OutputStream out)
      Encodes the image's alpha channel.
      static void encodePackedColorComponents​(java.awt.image.RenderedImage image, java.io.OutputStream out)
      Writes all pixels (color components only) of a RenderedImage to an OutputStream.
      static void encodeRenderedImageAsRGB​(java.awt.image.RenderedImage image, java.io.OutputStream out)  
      static void encodeRenderedImageAsRGB​(java.awt.image.RenderedImage image, java.io.OutputStream out, boolean outputbw, boolean bwinvert)
      Writes a RenderedImage to an OutputStream by converting it to RGB.
      static boolean encodeRenderedImageWithDirectColorModelAsRGB​(java.awt.image.RenderedImage image, java.io.OutputStream out)
      Writes a RenderedImage to an OutputStream.
      static void encodeRGBAsGrayScale​(byte[] raw, int width, int height, int bitsPerPixel, java.io.OutputStream out)
      Converts a byte array containing 24 bit RGB image data to a grayscale image.
      java.awt.image.ColorModel getEncodedColorModel()
      Returns the effective ColorModel used to encode the image.
      java.awt.image.RenderedImage getImage()
      Returns the associated image.
      java.awt.image.ColorModel getNativeColorModel()
      Returns the native ColorModel used by the image.
      private static java.awt.image.Raster getRaster​(java.awt.image.RenderedImage image)  
      boolean hasAlpha()
      Indicates whether the image has an alpha channel.
      boolean isConverted()
      Indicates whether the image is converted during encodation.
      protected boolean isMultiTile()
      Indicates whether the image consists of multiple tiles.
      private boolean optimizedWriteTo​(java.io.OutputStream out)  
      void setBWInvert​(boolean v)  
      private void writeRGBTo​(java.io.OutputStream out)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_RGB_COLOR_MODEL

        private static final java.awt.image.ColorModel DEFAULT_RGB_COLOR_MODEL
      • image

        private final java.awt.image.RenderedImage image
      • encodedColorModel

        private java.awt.image.ColorModel encodedColorModel
      • firstTileDump

        private boolean firstTileDump
      • enableCMYK

        private boolean enableCMYK
      • isBGR

        private boolean isBGR
      • isKMYC

        private boolean isKMYC
      • outputbw

        private boolean outputbw
      • bwinvert

        private boolean bwinvert
    • Constructor Detail

      • ImageEncodingHelper

        public ImageEncodingHelper​(java.awt.image.RenderedImage image)
        Main constructor
        Parameters:
        image - the image
      • ImageEncodingHelper

        public ImageEncodingHelper​(java.awt.image.RenderedImage image,
                                   boolean enableCMYK)
        Main constructor
        Parameters:
        image - the image
        enableCMYK - true to enable CMYK, false to disable
    • Method Detail

      • getImage

        public java.awt.image.RenderedImage getImage()
        Returns the associated image.
        Returns:
        the image
      • getNativeColorModel

        public java.awt.image.ColorModel getNativeColorModel()
        Returns the native ColorModel used by the image.
        Returns:
        the native color model
      • getEncodedColorModel

        public java.awt.image.ColorModel getEncodedColorModel()
        Returns the effective ColorModel used to encode the image. If this is different from the value returned by getNativeColorModel() this means that the image is converted in order to encode it because no native encoding is currently possible.
        Returns:
        the effective color model
      • hasAlpha

        public boolean hasAlpha()
        Indicates whether the image has an alpha channel.
        Returns:
        true if the image has an alpha channel
      • isConverted

        public boolean isConverted()
        Indicates whether the image is converted during encodation.
        Returns:
        true if the image cannot be encoded in its native format
      • writeRGBTo

        private void writeRGBTo​(java.io.OutputStream out)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • encodeRenderedImageAsRGB

        public static void encodeRenderedImageAsRGB​(java.awt.image.RenderedImage image,
                                                    java.io.OutputStream out)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • encodeRenderedImageAsRGB

        public static void encodeRenderedImageAsRGB​(java.awt.image.RenderedImage image,
                                                    java.io.OutputStream out,
                                                    boolean outputbw,
                                                    boolean bwinvert)
                                             throws java.io.IOException
        Writes a RenderedImage to an OutputStream by converting it to RGB.
        Parameters:
        image - the image
        out - the OutputStream to write the pixels to
        Throws:
        java.io.IOException - if an I/O error occurs
      • encodeRenderedImageWithDirectColorModelAsRGB

        public static boolean encodeRenderedImageWithDirectColorModelAsRGB​(java.awt.image.RenderedImage image,
                                                                           java.io.OutputStream out)
                                                                    throws java.io.IOException
        Writes a RenderedImage to an OutputStream. This method optimizes the encoding of the DirectColorModel as it is returned by ColorModel.getRGBdefault().
        Parameters:
        image - the image
        out - the OutputStream to write the pixels to
        Returns:
        true if this method encoded this image, false if the image is incompatible
        Throws:
        java.io.IOException - if an I/O error occurs
      • getRaster

        private static java.awt.image.Raster getRaster​(java.awt.image.RenderedImage image)
      • encodeRGBAsGrayScale

        public static void encodeRGBAsGrayScale​(byte[] raw,
                                                int width,
                                                int height,
                                                int bitsPerPixel,
                                                java.io.OutputStream out)
                                         throws java.io.IOException
        Converts a byte array containing 24 bit RGB image data to a grayscale image.
        Parameters:
        raw - the buffer containing the RGB image data
        width - the width of the image in pixels
        height - the height of the image in pixels
        bitsPerPixel - the number of bits to use per pixel
        out - the OutputStream to write the pixels to
        Throws:
        java.io.IOException - if an I/O error occurs
      • optimizedWriteTo

        private boolean optimizedWriteTo​(java.io.OutputStream out)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • isMultiTile

        protected boolean isMultiTile()
        Indicates whether the image consists of multiple tiles.
        Returns:
        true if there are multiple tiles
      • determineEncodedColorModel

        protected void determineEncodedColorModel()
        Determines the color model used for encoding the image.
      • encode

        public void encode​(java.io.OutputStream out)
                    throws java.io.IOException
        Encodes the image and writes everything to the given OutputStream.
        Parameters:
        out - the OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs
      • encodeAlpha

        public void encodeAlpha​(java.io.OutputStream out)
                         throws java.io.IOException
        Encodes the image's alpha channel. If it doesn't have an alpha channel, an IllegalStateException is thrown.
        Parameters:
        out - the OutputStream
        Throws:
        java.io.IOException - if an I/O error occurs
      • encodePackedColorComponents

        public static void encodePackedColorComponents​(java.awt.image.RenderedImage image,
                                                       java.io.OutputStream out)
                                                throws java.io.IOException
        Writes all pixels (color components only) of a RenderedImage to an OutputStream.
        Parameters:
        image - the image to be encoded
        out - the OutputStream to write to
        Throws:
        java.io.IOException - if an I/O error occurs
      • createRenderedImageEncoder

        public static ImageEncoder createRenderedImageEncoder​(java.awt.image.RenderedImage img)
        Create an ImageEncoder for the given RenderImage instance.
        Parameters:
        img - the image
        Returns:
        the requested ImageEncoder
      • setBWInvert

        public void setBWInvert​(boolean v)