Class PngEncoder
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
- Version:
- 1.5, 19 Oct 2003 CHANGES: -------- 19-Nov-2002 : CODING STYLE CHANGES ONLY (by David Gilbert for Object Refinery Limited); 19-Sep-2003 : Fix for platforms using EBCDIC (contributed by Paulo Soares); 19-Oct-2003 : Change private fields to protected fields so that PngEncoderB can inherit them (JDE) Fixed bug with calculation of nRows 15-Aug-2008 : Added scrunch.end() in writeImageData() method - see JFreeChart bug report 2037930 (David Gilbert);
- Author:
- J. David Eisenberg
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intThe byte position.protected intThe bytes-per-pixel.protected intThe compression level (1 = best speed, 9 = best compression, 0 = no compression).protected CRC32CRC.protected longThe CRC value.static final booleanConstant specifying that alpha channel should be encoded.protected booleanEncode alpha?protected intThe filter type.static final intConstants for filter (LAST).static final intConstants for filter (NONE).static final intConstants for filter (SUB).static final intConstants for filter (UP).protected intThe height.protected static final byte[]IDAT tag.protected static final byte[]IEND tag.protected static final byte[]IHDR tag.protected ImageThe image.protected byte[]The left bytes.protected intThe maximum position.static final booleanConstant specifying that alpha channel should not be encoded.protected static final byte[]PHYS tag.protected byte[]The png bytes.protected byte[]The prior row.protected intThe width. -
Constructor Summary
ConstructorsConstructorDescriptionClass constructor.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
Modifier and TypeMethodDescriptionprotected voidfilterSub(byte[] pixels, int startPos, int width) Perform "sub" filtering on the given row.protected voidfilterUp(byte[] pixels, int startPos, int width) Perform "up" filtering on the given row.intRetrieve compression level.booleanRetrieve alpha encoding status.intRetrieve filtering scheme.getImage()Returns the image to be encoded.intgetXDpi()Get the DPI for the X axis.intgetYDpi()Get the DPI for the Y axis.byte[]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.voidsetCompressionLevel(int level) Set the compression level to use.voidsetDpi(int xDpi, int yDpi) Set the DPI resolution.voidsetEncodeAlpha(boolean encodeAlpha) Set the alpha encoding on or off.voidsetFilter(int whichFilter) Set the filter to use.voidSet the image to be encoded.voidsetXDpi(int xDpi) Set the DPI for the X axis.voidsetYDpi(int yDpi) Set the DPI for the Y axis.protected intwriteByte(int b, int offset) Write a single byte into the pngBytes array at a given position.protected intwriteBytes(byte[] data, int offset) Write an array of bytes into the pngBytes array.protected intwriteBytes(byte[] data, int nBytes, int offset) Write an array of bytes into the pngBytes array, specifying number of bytes to write.protected voidwriteEnd()Write a PNG "IEND" chunk into the pngBytes array.protected voidWrite a PNG "IHDR" chunk into the pngBytes array.protected booleanWrite the image data into the pngBytes array.protected intwriteInt2(int n, int offset) Write a two-byte integer into the pngBytes array at a given position.protected intwriteInt4(int n, int offset) Write a four-byte integer into the pngBytes array at a given position.protected voidWrite a PNG "pHYs" chunk into the pngBytes array.
-
Field Details
-
ENCODE_ALPHA
Constant specifying that alpha channel should be encoded.- See Also:
-
NO_ALPHA
Constant specifying that alpha channel should not be encoded.- See Also:
-
FILTER_NONE
-
FILTER_SUB
-
FILTER_UP
-
FILTER_LAST
-
IHDR
IHDR tag. -
IDAT
IDAT tag. -
IEND
IEND tag. -
PHYS
PHYS tag. -
pngBytes
The png bytes. -
priorRow
The prior row. -
leftBytes
The left bytes. -
image
-
width
The width. -
height
The height. -
bytePos
The byte position. -
maxPos
The maximum position. -
crc
-
crcValue
The CRC value. -
encodeAlpha
Encode alpha? -
filter
The filter type. -
bytesPerPixel
The bytes-per-pixel. -
compressionLevel
The compression level (1 = best speed, 9 = best compression, 0 = no compression).
-
-
Constructor Details
-
PngEncoder
public PngEncoder()Class constructor. -
PngEncoder
Class constructor specifying Image to encode, with no alpha channel encoding.- Parameters:
image- A Java Image object which uses the DirectColorModel- See Also:
-
PngEncoder
Class constructor specifying Image to encode, and whether to encode alpha.- Parameters:
image- A Java Image object which uses the DirectColorModelencodeAlpha- Encode the alpha channel? false=no; true=yes- See Also:
-
PngEncoder
Class constructor specifying Image to encode, whether to encode alpha, and filter to use.- Parameters:
image- A Java Image object which uses the DirectColorModelencodeAlpha- Encode the alpha channel? false=no; true=yeswhichFilter- 0=none, 1=sub, 2=up- See Also:
-
PngEncoder
Class constructor specifying Image source to encode, whether to encode alpha, filter to use, and compression level.- Parameters:
image- A Java Image objectencodeAlpha- Encode the alpha channel? false=no; true=yeswhichFilter- 0=none, 1=sub, 2=upcompLevel- 0..9 (1 = best speed, 9 = best compression, 0 = no compression)- See Also:
-
-
Method Details
-
setImage
-
getImage
-
pngEncode
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
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
Set the alpha encoding on or off.- Parameters:
encodeAlpha- false=no, true=yes
-
getEncodeAlpha
-
setFilter
Set the filter to use.- Parameters:
whichFilter- from constant list
-
getFilter
-
setCompressionLevel
Set the compression level to use.- Parameters:
level- the compression level (1 = best speed, 9 = best compression, 0 = no compression)
-
getCompressionLevel
Retrieve compression level.- Returns:
- int (1 = best speed, 9 = best compression, 0 = no compression)
-
resizeByteArray
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
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
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
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
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
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.
-
writeHeader
Write a PNG "IHDR" chunk into the pngBytes array. -
filterSub
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 builtstartPos- Starting position within pixels of bytes to be filtered.width- Width of a scanline in pixels.
-
filterUp
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 builtstartPos- Starting position within pixels of bytes to be filtered.width- Width of a scanline in pixels.
-
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
Write a PNG "IEND" chunk into the pngBytes array. -
setXDpi
Set the DPI for the X axis.- Parameters:
xDpi- The number of dots per inch
-
getXDpi
-
setYDpi
Set the DPI for the Y axis.- Parameters:
yDpi- The number of dots per inch
-
getYDpi
-
setDpi
Set the DPI resolution.- Parameters:
xDpi- The number of dots per inch for the X axis.yDpi- The number of dots per inch for the Y axis.
-
writeResolution
Write a PNG "pHYs" chunk into the pngBytes array.
-