com.objectplanet.image
Class PngEncoder

java.lang.Object
  extended by com.objectplanet.image.PngEncoder

public class PngEncoder
extends java.lang.Object

This PngEncoder class converts a java image to a PNG image and writes it to a specified output stream. It is compatible with JDK 1.1 and later.

The encoder supports 8 bit indexed images with 256 colors, 16 bit grayscale images and 24 bit RGB images. Full alpha channel is supported for truecolor and grayscale images while indexed images can only contain fully opaque or fully transparent pixels. Indexed images are generally smaller in size than truecolor and grayscale images.

    FileOutputStream out = new FileOutputStream("image.png");
    PngEncoder encoder = new PngEncoder();
    encoder.encode(image,out);
 
Version: 2.0

Author:
Bjorn J. Kvande., Philipp Kolibaba.

Field Summary
static int BEST_COMPRESSION
          The best compression level.
static int BEST_SPEED
          The compression level with the best speed.
static int COLOR_GRAYSCALE
          Encode the image as 16bit grayscale image.
static int COLOR_GRAYSCALE_ALPHA
          Encode the image as 16bit grayscale image with alpha.
static int COLOR_INDEXED
          Encode the image as a palette indexed image with maximum 256 colors.
static int COLOR_INDEXED_ALPHA
          Encode the image as a palette indexed image with maximum 256 colors.
static int COLOR_TRUECOLOR
          Encode the image as 24bit truecolor RGB image with 16,7 million colors.
static int COLOR_TRUECOLOR_ALPHA
          Encode the image as 24bit truecolor image with alpha.
static int DEFAULT_COMPRESSION
          The default compression level.
static int INDEXED_COLORS_AUTO
          The PngEncoder checks automatically if it should use original colors or convert to the pre-set palette.
static int INDEXED_COLORS_CONVERT
          The original colors are used for the indexed PNG output.
static int INDEXED_COLORS_ORIGINAL
          The original colors are converted to the pre-set palette.
 
Constructor Summary
PngEncoder()
          Creates a new PngEncoder for truecolor images and default compression.
PngEncoder(int colorType)
          Creates a new PngEncoder with the specified color type and default compression.
PngEncoder(int colorType, int compression)
          Creates a new PngEncoder with the specified color type and compression.
 
Method Summary
 void encode(java.awt.Image image, java.io.OutputStream out)
          Encodes an image to an output stream as a PNG image.
 int getColorType()
          Gets the current image type.
 int getIndexedColorMode()
          Gets current mode of processing original image to indexed PNG.
static java.lang.String getVersion()
          Gets the version number.
static void main(java.lang.String[] argv)
          Prints out the version number.
 void setColorType(int colorType)
          Sets the encoded image type.
 void setCompression(int level)
          Sets the image compression level.
 void setIndexedColorMode(int mode)
          Sets mode to process the original image to indexed PNG output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BEST_COMPRESSION

public static final int BEST_COMPRESSION
The best compression level.

See Also:
Constant Field Values

BEST_SPEED

public static final int BEST_SPEED
The compression level with the best speed.

See Also:
Constant Field Values

COLOR_GRAYSCALE

public static final int COLOR_GRAYSCALE
Encode the image as 16bit grayscale image.

See Also:
Constant Field Values

COLOR_GRAYSCALE_ALPHA

public static final int COLOR_GRAYSCALE_ALPHA
Encode the image as 16bit grayscale image with alpha.

See Also:
Constant Field Values

COLOR_INDEXED

public static final int COLOR_INDEXED
Encode the image as a palette indexed image with maximum 256 colors. This image type takes less space and is sometimes more efficiently encoded.

See Also:
Constant Field Values

COLOR_INDEXED_ALPHA

public static final int COLOR_INDEXED_ALPHA
Encode the image as a palette indexed image with maximum 256 colors.

See Also:
Constant Field Values

COLOR_TRUECOLOR

public static final int COLOR_TRUECOLOR
Encode the image as 24bit truecolor RGB image with 16,7 million colors.

See Also:
Constant Field Values

COLOR_TRUECOLOR_ALPHA

public static final int COLOR_TRUECOLOR_ALPHA
Encode the image as 24bit truecolor image with alpha.

See Also:
Constant Field Values

DEFAULT_COMPRESSION

public static final int DEFAULT_COMPRESSION
The default compression level.

See Also:
Constant Field Values

INDEXED_COLORS_AUTO

public static final int INDEXED_COLORS_AUTO
The PngEncoder checks automatically if it should use original colors or convert to the pre-set palette. Should be used when it is unknow if there is more or less then 256 colors in original image. Encoding in this mode might take more time then in other ones. This is the default mode.

See Also:
Constant Field Values

INDEXED_COLORS_CONVERT

public static final int INDEXED_COLORS_CONVERT
The original colors are used for the indexed PNG output. Should be set when there is less then 256 colors in original image.

See Also:
Constant Field Values

INDEXED_COLORS_ORIGINAL

public static final int INDEXED_COLORS_ORIGINAL
The original colors are converted to the pre-set palette. Should be used when there is more then 256 colors in originalimage

See Also:
Constant Field Values
Constructor Detail

PngEncoder

public PngEncoder()
Creates a new PngEncoder for truecolor images and default compression.


PngEncoder

public PngEncoder(int colorType)
Creates a new PngEncoder with the specified color type and default compression.

Parameters:
colorType - COLOR_TRUECOLOR, COLOR_INDEXED or COLOR_GRAYSCALE.
Throws:
java.lang.IllegalArgumentException - If the color type is invalid.

PngEncoder

public PngEncoder(int colorType,
                  int compression)
Creates a new PngEncoder with the specified color type and compression. The less compression, the faster the encoding will be and larger the image.

Parameters:
colorType - COLOR_TRUECOLOR, COLOR_INDEXED, COLOR_GRAYSCALE, COLOR_TRUECOLOR_ALPHA, COLOR_INDEXED_ALPHA, COLOR_GRAYSCALE_ALPHA.
compression - The compression level (0-9 or DEFAULT_COMPRESSION, BEST_SPEED, BEST_COMPRESSION).
Method Detail

encode

public void encode(java.awt.Image image,
                   java.io.OutputStream out)
            throws java.io.IOException
Encodes an image to an output stream as a PNG image. It does not close the output stream, this must be done by the caller.

Parameters:
image - the buffered image to encode.
out - the output stream to write the encoded data to.
Throws:
java.io.IOException

getColorType

public int getColorType()
Gets the current image type.

Returns:
COLOR_INDEXED, COLOR_TRUECOLOR or COLOR_GRAYSCALE.

getIndexedColorMode

public int getIndexedColorMode()
Gets current mode of processing original image to indexed PNG.

Returns:
Returns current

getVersion

public static java.lang.String getVersion()
Gets the version number.


main

public static void main(java.lang.String[] argv)
Prints out the version number.


setColorType

public void setColorType(int colorType)
Sets the encoded image type. Palette indexed images are smaller but can contain a maximum of 256 colors. Truecolor images uses one byte per pixel and can contain 16,7 million colors, but are larger.

Parameters:
colorType - COLOR_INDEXED, COLOR_TRUECOLOR or COLOR_GRAYSCALE.
Throws:
java.lang.IllegalArgumentException - If the color type is invalid.

setCompression

public void setCompression(int level)
Sets the image compression level. The better the compression the smaller the image and longer the encoding time.

Parameters:
level - 0-9 or DEFAULT_COMPRESSION, BEST_SPEED, or BEST_COMPRESSION.
Throws:
java.lang.IllegalArgumentException - If the compression level is invalid.

setIndexedColorMode

public void setIndexedColorMode(int mode)
Sets mode to process the original image to indexed PNG output.

Parameters:
mode - One of the following: INDEXED_COLORS_AUTO, INDEXED_COLORS_ORIGINAL, INDEXED_COLORS_CONVERT.