Class PngEncoder
- All Implemented Interfaces:
ImageEncoder
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
- Author:
- J. David Eisenberg
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean
Constant specifying that alpha channel should be encoded.static final int
Constants for filter (LAST).static final int
Constants for filter (NONE).static final int
Constants for filter (SUB).static final int
Constants for filter (UP).static final boolean
Constant specifying that alpha channel should not be encoded. -
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 TypeMethodDescriptionvoid
encodeImage
(Image image, OutputStream outputStream, float quality, boolean encodeAlpha) Encodes the given image using the given encoder-specific quality and alpha-channel settings and writes the encoded image-data to the given stream.int
Retrieve compression level.boolean
Retrieve alpha encoding status.int
Retrieve filtering scheme.getImage()
Returns the image to be encoded.Returns the mime-type of the encoded data.int
getXDpi()
Get the DPI for the X axis.int
getYDpi()
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.void
setCompressionLevel
(int level) Set the compression level to use.void
setDpi
(int xDpi, int yDpi) Set the DPI resolution.void
setEncodeAlpha
(boolean encodeAlpha) Set the alpha encoding on or off.void
setFilter
(int whichFilter) Set the filter to use.void
Set the image to be encoded.void
setXDpi
(int xDpi) Set the DPI for the X axis.void
setYDpi
(int yDpi) Set the DPI for the Y axis.
-
Field Details
-
ENCODE_ALPHA
public static final boolean ENCODE_ALPHAConstant specifying that alpha channel should be encoded.- See Also:
-
NO_ALPHA
public static final boolean NO_ALPHAConstant specifying that alpha channel should not be encoded.- See Also:
-
FILTER_NONE
public static final int FILTER_NONEConstants for filter (NONE).- See Also:
-
FILTER_SUB
public static final int FILTER_SUBConstants for filter (SUB).- See Also:
-
FILTER_UP
public static final int FILTER_UPConstants for filter (UP).- See Also:
-
FILTER_LAST
public static final int FILTER_LASTConstants for filter (LAST).- See Also:
-
-
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
Set the image to be encoded.- Parameters:
image
- A Java Image object which uses the DirectColorModel- See Also:
-
getImage
Returns the image to be encoded.- Returns:
- the image to be encoded.
-
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
- the compression level (1 = best speed, 9 = best compression, 0 = no compression)
-
getCompressionLevel
public int getCompressionLevel()Retrieve compression level.- Returns:
- int (1 = best speed, 9 = best compression, 0 = no compression)
-
setXDpi
public void setXDpi(int xDpi) Set the DPI for the X axis.- Parameters:
xDpi
- The number of dots per inch
-
getXDpi
public int getXDpi()Get the DPI for the X axis.- Returns:
- The number of dots per inch
-
setYDpi
public void setYDpi(int yDpi) Set the DPI for the Y axis.- Parameters:
yDpi
- The number of dots per inch
-
getYDpi
public int getYDpi()Get the DPI for the Y axis.- Returns:
- The number of dots per inch
-
setDpi
public void setDpi(int xDpi, int yDpi) 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.
-
encodeImage
public void encodeImage(Image image, OutputStream outputStream, float quality, boolean encodeAlpha) throws IOException, UnsupportedEncoderException Description copied from interface:ImageEncoder
Encodes the given image using the given encoder-specific quality and alpha-channel settings and writes the encoded image-data to the given stream.- Specified by:
encodeImage
in interfaceImageEncoder
- Parameters:
image
- the image to be encoded.outputStream
- the output stream, where to write the image data to.quality
- the quality of the encoding.encodeAlpha
- a flag controlling whether the alpha-channel should be encoded as well.- Throws:
IOException
- if there was an IO error while generating or writing the image data.UnsupportedEncoderException
- if the encoder is not supported.
-
getMimeType
Description copied from interface:ImageEncoder
Returns the mime-type of the encoded data.- Specified by:
getMimeType
in interfaceImageEncoder
- Returns:
- the mime-type.
-