Save This Page
Home » jcommon-1.0.13 » com.keypoint » [javadoc | source]
com.keypoint
public class: PngEncoder [javadoc | source]
java.lang.Object
   com.keypoint.PngEncoder

Direct Known Subclasses:
    PngEncoderB

PngEncoder takes a Java Image object and creates a byte string which can be saved as a PNG file. The Image is presumed to use the DirectColorModel.

Thanks to Jay Denny at KeyPoint Software http://www.keypoint.com/ who let me develop this code on company time.

You may contact me with (probably very-much-needed) improvements, comments, and bug fixes at:

david@catcode.com

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. A copy of the GNU LGPL may be found at http://www.gnu.org/copyleft/lesser.html

Field Summary
public static final  boolean ENCODE_ALPHA    Constant specifying that alpha channel should be encoded. 
public static final  boolean NO_ALPHA    Constant specifying that alpha channel should not be encoded. 
public static final  int FILTER_NONE    Constants for filter (NONE). 
public static final  int FILTER_SUB    Constants for filter (SUB). 
public static final  int FILTER_UP    Constants for filter (UP). 
public static final  int FILTER_LAST    Constants for filter (LAST). 
protected static final  byte[] IHDR    IHDR tag. 
protected static final  byte[] IDAT    IDAT tag. 
protected static final  byte[] IEND    IEND tag. 
protected static final  byte[] PHYS     
protected  byte[] pngBytes    The png bytes. 
protected  byte[] priorRow    The prior row. 
protected  byte[] leftBytes    The left bytes. 
protected  Image image    The image. 
protected  int width    The width. 
protected  int height    The height. 
protected  int bytePos    The byte position. 
protected  int maxPos    The maximum position. 
protected  CRC32 crc    CRC. 
protected  long crcValue    The CRC value. 
protected  boolean encodeAlpha    Encode alpha? 
protected  int filter    The filter type. 
protected  int bytesPerPixel    The bytes-per-pixel. 
protected  int compressionLevel    The compression level (1 = best speed, 9 = best compression, 0 = no compression). 
Constructor:
 public PngEncoder() 
 public PngEncoder(Image image) 
    Class constructor specifying Image to encode, with no alpha channel encoding.
    Parameters:
    image - A Java Image object which uses the DirectColorModel
    Also see:
    java.awt.Image
 public PngEncoder(Image image,
    boolean encodeAlpha) 
    Class constructor specifying Image to encode, and whether to encode alpha.
    Parameters:
    image - A Java Image object which uses the DirectColorModel
    encodeAlpha - Encode the alpha channel? false=no; true=yes
    Also see:
    java.awt.Image
 public PngEncoder(Image image,
    boolean encodeAlpha,
    int whichFilter) 
    Class constructor specifying Image to encode, whether to encode alpha, and filter to use.
    Parameters:
    image - A Java Image object which uses the DirectColorModel
    encodeAlpha - Encode the alpha channel? false=no; true=yes
    whichFilter - 0=none, 1=sub, 2=up
    Also see:
    java.awt.Image
 public PngEncoder(Image image,
    boolean encodeAlpha,
    int whichFilter,
    int compLevel) 
    Class constructor specifying Image source to encode, whether to encode alpha, filter to use, and compression level.
    Parameters:
    image - A Java Image object
    encodeAlpha - Encode the alpha channel? false=no; true=yes
    whichFilter - 0=none, 1=sub, 2=up
    compLevel - 0..9 (1 = best speed, 9 = best compression, 0 = no compression)
    Also see:
    java.awt.Image
Method from com.keypoint.PngEncoder Summary:
filterSub,   filterUp,   getCompressionLevel,   getEncodeAlpha,   getFilter,   getImage,   getXDpi,   getYDpi,   pngEncode,   pngEncode,   resizeByteArray,   setCompressionLevel,   setDpi,   setEncodeAlpha,   setFilter,   setImage,   setXDpi,   setYDpi,   writeByte,   writeBytes,   writeBytes,   writeEnd,   writeHeader,   writeImageData,   writeInt2,   writeInt4,   writeResolution
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from com.keypoint.PngEncoder Detail:
 protected  void filterSub(byte[] pixels,
    int startPos,
    int width) 
    Perform "sub" filtering on the given row. Uses temporary array leftBytes to store the original values of the previous pixels. The array is 16 bytes long, which will easily hold two-byte samples plus two-byte alpha.
 protected  void filterUp(byte[] pixels,
    int startPos,
    int width) 
    Perform "up" filtering on the given row. Side effect: refills the prior row with current row
 public int getCompressionLevel() 
    Retrieve compression level.
 public boolean getEncodeAlpha() 
    Retrieve alpha encoding status.
 public int getFilter() 
    Retrieve filtering scheme.
 public Image getImage() 
    Returns the image to be encoded.
 public int getXDpi() 
    Get the DPI for the X axis.
 public int getYDpi() 
    Get the DPI for the Y axis.
 public byte[] pngEncode() 
    Creates an array of bytes that is the PNG equivalent of the current image. Alpha encoding is determined by its setting in the constructor.
 public byte[] pngEncode(boolean encodeAlpha) 
    Creates an array of bytes that is the PNG equivalent of the current image, specifying whether to encode alpha or not.
 protected byte[] resizeByteArray(byte[] array,
    int newLength) 
    Increase or decrease the length of a byte array.
 public  void setCompressionLevel(int level) 
    Set the compression level to use.
 public  void setDpi(int xDpi,
    int yDpi) 
    Set the DPI resolution.
 public  void setEncodeAlpha(boolean encodeAlpha) 
    Set the alpha encoding on or off.
 public  void setFilter(int whichFilter) 
    Set the filter to use.
 public  void setImage(Image image) 
    Set the image to be encoded.
 public  void setXDpi(int xDpi) 
    Set the DPI for the X axis.
 public  void setYDpi(int yDpi) 
    Set the DPI for the Y axis.
 protected int writeByte(int b,
    int offset) 
    Write a single byte into the pngBytes array at a given position.
 protected int writeBytes(byte[] data,
    int offset) 
    Write an array of bytes into the pngBytes array. Note: This routine has the side effect of updating maxPos, the largest element written in the array. The array is resized by 1000 bytes or the length of the data to be written, whichever is larger.
 protected int writeBytes(byte[] data,
    int nBytes,
    int offset) 
    Write an array of bytes into the pngBytes array, specifying number of bytes to write. Note: This routine has the side effect of updating maxPos, the largest element written in the array. The array is resized by 1000 bytes or the length of the data to be written, whichever is larger.
 protected  void writeEnd() 
    Write a PNG "IEND" chunk into the pngBytes array.
 protected  void writeHeader() 
    Write a PNG "IHDR" chunk into the pngBytes array.
 protected boolean writeImageData() 
    Write the image data into the pngBytes array. This will write one or more PNG "IDAT" chunks. In order to conserve memory, this method grabs as many rows as will fit into 32K bytes, or the whole image; whichever is less.
 protected int writeInt2(int n,
    int offset) 
    Write a two-byte integer into the pngBytes array at a given position.
 protected int writeInt4(int n,
    int offset) 
    Write a four-byte integer into the pngBytes array at a given position.
 protected  void writeResolution() 
    Write a PNG "pHYs" chunk into the pngBytes array.