FreeHEP API
Version current

org.freehep.graphicsio.png
Class PNGEncoder

java.lang.Object
  extended byorg.freehep.graphicsio.png.PNGEncoder
All Implemented Interfaces:
ImageObserver

public class PNGEncoder
extends Object
implements ImageObserver

Source Code:
PNGEncoder.java

Field Summary
protected  int bytePos
           
protected  int bytesPerPixel
           
protected  int compressionLevel
           
protected  CRC32 crc
           
protected  long crcValue
           
protected  int dataPos
           
static boolean ENCODE_ALPHA
          Constant specifying that alpha channel should be encoded.
protected  boolean encodeAlpha
           
protected  int endPos
           
protected  int filter
           
static int FILTER_LAST
           
static int FILTER_NONE
          Constants for filters
static int FILTER_SUB
           
static int FILTER_UP
           
protected  int hdrPos
           
protected  int height
           
protected  Image image
           
protected  List keys
           
protected  byte[] leftBytes
           
protected  int maxPos
           
static boolean NO_ALPHA
          Constant specifying that alpha channel should not be encoded.
protected  byte[] pngBytes
           
protected  byte[] priorRow
           
protected  List text
           
protected  int width
           
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
PNGEncoder()
           
PNGEncoder(Image image)
          Class constructor specifying Image to encode, with no alpha channel encoding.
PNGEncoder(Image image, boolean encodeAlpha)
          Class constructor specifying Image to encode, and whether to encode alpha.
PNGEncoder(Image image, boolean encodeAlpha, int whichFilter)
          Class constructor specifying Image to encode, whether to encode alpha, and filter to use.
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.
 
Method Summary
 void addText(String key, String value)
           
protected  void filterSub(byte[] pixels, int startPos, int width)
          Perform "sub" filtering on the given row.
protected  void filterUp(byte[] pixels, int startPos, int width)
          Perform "up" filtering on the given row.
 int getCompressionLevel()
          Retrieve compression level
 boolean getEncodeAlpha()
          Retrieve alpha encoding status.
 int getFilter()
          Retrieve filtering scheme
 boolean imageUpdate(Image image, int flags, int x, int y, int width, int height)
           
 byte[] pngEncode()
          Creates an array of bytes that is the PNG equivalent of the current image.
 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.
 void setCompressionLevel(int level)
          Set the compression level to use
 void setEncodeAlpha(boolean encodeAlpha)
          Set the alpha encoding on or off.
 void setFilter(int whichFilter)
          Set the filter to use
 void setImage(Image image)
          Set the image to be encoded
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.
protected  int writeBytes(byte[] data, int nBytes, int offset)
          Write an array of bytes into the pngBytes array, specifying number of bytes to write.
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.
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  int writeString(String s, int offset)
          Write a string into the pngBytes array at a given position.
protected  void writeText(String key, String value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODE_ALPHA

public static final boolean ENCODE_ALPHA
Constant specifying that alpha channel should be encoded.

See Also:
Constant Field Values

NO_ALPHA

public static final boolean NO_ALPHA
Constant specifying that alpha channel should not be encoded.

See Also:
Constant Field Values

FILTER_NONE

public static final int FILTER_NONE
Constants for filters

See Also:
Constant Field Values

FILTER_SUB

public static final int FILTER_SUB
See Also:
Constant Field Values

FILTER_UP

public static final int FILTER_UP
See Also:
Constant Field Values

FILTER_LAST

public static final int FILTER_LAST
See Also:
Constant Field Values

pngBytes

protected byte[] pngBytes

priorRow

protected byte[] priorRow

leftBytes

protected byte[] leftBytes

image

protected Image image

width

protected int width

height

protected int height

bytePos

protected int bytePos

maxPos

protected int maxPos

hdrPos

protected int hdrPos

dataPos

protected int dataPos

endPos

protected int endPos

crc

protected CRC32 crc

crcValue

protected long crcValue

encodeAlpha

protected boolean encodeAlpha

filter

protected int filter

bytesPerPixel

protected int bytesPerPixel

compressionLevel

protected int compressionLevel

keys

protected List keys

text

protected List text
Constructor Detail

PNGEncoder

public PNGEncoder()

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
See Also:
Image

PNGEncoder

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
See Also:
Image

PNGEncoder

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
See Also:
Image

PNGEncoder

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
See Also:
Image
Method Detail

addText

public void addText(String key,
                    String value)

setImage

public void setImage(Image image)
Set the image to be encoded

Parameters:
image - A Java Image object which uses the DirectColorModel
See Also:
Image

imageUpdate

public boolean imageUpdate(Image image,
                           int flags,
                           int x,
                           int y,
                           int width,
                           int height)
Specified by:
imageUpdate in interface ImageObserver

pngEncode

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.

Parameters:
encodeAlpha - boolean false=no alpha, true=encode alpha
Returns:
an array of bytes, or null if there was a problem

pngEncode

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.

Returns:
an array of bytes, or null if there was a problem

setEncodeAlpha

public void setEncodeAlpha(boolean encodeAlpha)
Set the alpha encoding on or off.

Parameters:
encodeAlpha - false=no, true=yes

getEncodeAlpha

public boolean getEncodeAlpha()
Retrieve alpha encoding status.

Returns:
boolean false=no, true=yes

setFilter

public void setFilter(int whichFilter)
Set the filter to use

Parameters:
whichFilter - from constant list

getFilter

public int getFilter()
Retrieve filtering scheme

Returns:
int (see constant list)

setCompressionLevel

public void setCompressionLevel(int level)
Set the compression level to use

Parameters:
level - 0 through 9

getCompressionLevel

public int getCompressionLevel()
Retrieve compression level

Returns:
int in range 0-9

resizeByteArray

protected byte[] resizeByteArray(byte[] array,
                                 int newLength)
Increase or decrease the length of a byte array.

Parameters:
array - The original array.
newLength - The length you wish the new array to have.
Returns:
Array of newly desired length. If shorter than the original, the trailing elements are truncated.

writeBytes

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.

Parameters:
data - The data to be written into pngBytes.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.

writeBytes

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.

Parameters:
data - The data to be written into pngBytes.
nBytes - The number of bytes to be written.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.

writeInt2

protected int writeInt2(int n,
                        int offset)
Write a two-byte integer into the pngBytes array at a given position.

Parameters:
n - The integer to be written into pngBytes.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.

writeInt4

protected int writeInt4(int n,
                        int offset)
Write a four-byte integer into the pngBytes array at a given position.

Parameters:
n - The integer to be written into pngBytes.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.

writeByte

protected int writeByte(int b,
                        int offset)
Write a single byte into the pngBytes array at a given position.

Parameters:
b - The integer to be written into pngBytes.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.

writeString

protected int writeString(String s,
                          int offset)
Write a string into the pngBytes array at a given position. This uses the getBytes method, so the encoding used will be its default.

Parameters:
s - The String to be written into pngBytes.
offset - The starting point to write to.
Returns:
The next place to be written to in the pngBytes array.
See Also:
String.getBytes()

writeHeader

protected void writeHeader()
Write a PNG "IHDR" chunk into the pngBytes array.


writeText

protected void writeText(String key,
                         String value)

filterSub

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.

Parameters:
pixels - The array holding the scan lines being built
startPos - Starting position within pixels of bytes to be filtered.
width - Width of a scanline in pixels.

filterUp

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

Parameters:
pixels - The array holding the scan lines being built
startPos - Starting position within pixels of bytes to be filtered.
width - Width of a scanline in pixels.

writeImageData

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.

Returns:
true if no errors; false if error grabbing pixels

writeEnd

protected void writeEnd()
Write a PNG "IEND" chunk into the pngBytes array.


FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.