public class PngEncoder extends Object implements 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
Modifier and Type | Field and Description |
---|---|
static boolean |
ENCODE_ALPHA
Constant specifying that alpha channel should be encoded.
|
static int |
FILTER_LAST
Constants for filter (LAST).
|
static int |
FILTER_NONE
Constants for filter (NONE).
|
static int |
FILTER_SUB
Constants for filter (SUB).
|
static int |
FILTER_UP
Constants for filter (UP).
|
static boolean |
NO_ALPHA
Constant specifying that alpha channel should not be encoded.
|
Constructor and Description |
---|
PngEncoder()
Class 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.
|
Modifier and Type | Method and Description |
---|---|
void |
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.
|
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.
|
Image |
getImage()
Returns the image to be encoded.
|
String |
getMimeType()
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[] |
pngEncode()
Creates an array of bytes that is the PNG equivalent of the current image, specifying whether to encode alpha or
not.
|
byte[] |
pngEncode(boolean encodeAlpha)
Deprecated.
Use the other pngEncode method and select the alpha-encoding via the constructor or setter.
|
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 |
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 |
setImage(Image image)
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.
|
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 void |
writeResolution()
Write a PNG "pHYs" chunk into the pngBytes array.
|
public static final boolean ENCODE_ALPHA
public static final boolean NO_ALPHA
public static final int FILTER_NONE
public static final int FILTER_SUB
public static final int FILTER_UP
public static final int FILTER_LAST
public PngEncoder()
public PngEncoder(Image image)
image
- A Java Image object which uses the DirectColorModelImage
public PngEncoder(Image image, boolean encodeAlpha)
image
- A Java Image object which uses the DirectColorModelencodeAlpha
- Encode the alpha channel? false=no; true=yesImage
public PngEncoder(Image image, boolean encodeAlpha, int whichFilter)
image
- A Java Image object which uses the DirectColorModelencodeAlpha
- Encode the alpha channel? false=no; true=yeswhichFilter
- 0=none, 1=sub, 2=upImage
public PngEncoder(Image image, boolean encodeAlpha, int whichFilter, int compLevel)
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)Image
public void setImage(Image image)
image
- A Java Image object which uses the DirectColorModelImage
,
DirectColorModel
public Image getImage()
public byte[] pngEncode(boolean encodeAlpha)
encodeAlpha
- boolean false=no alpha, true=encode alphapublic byte[] pngEncode()
public void setEncodeAlpha(boolean encodeAlpha)
encodeAlpha
- false=no, true=yespublic boolean getEncodeAlpha()
public void setFilter(int whichFilter)
whichFilter
- from constant listpublic int getFilter()
public void setCompressionLevel(int level)
level
- the compression level (1 = best speed, 9 = best compression, 0 = no compression)public int getCompressionLevel()
protected byte[] resizeByteArray(byte[] array, int newLength)
array
- The original array.newLength
- The length you wish the new array to have.protected int writeBytes(byte[] data, int offset)
data
- The data to be written into pngBytes.offset
- The starting point to write to.protected int writeBytes(byte[] data, int nBytes, int offset)
data
- The data to be written into pngBytes.nBytes
- The number of bytes to be written.offset
- The starting point to write to.protected int writeInt2(int n, int offset)
n
- The integer to be written into pngBytes.offset
- The starting point to write to.protected int writeInt4(int n, int offset)
n
- The integer to be written into pngBytes.offset
- The starting point to write to.protected int writeByte(int b, int offset)
b
- The integer to be written into pngBytes.offset
- The starting point to write to.protected void writeHeader()
protected void filterSub(byte[] pixels, int startPos, int width)
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.protected void filterUp(byte[] pixels, int startPos, int width)
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.protected boolean writeImageData()
protected void writeEnd()
public void setXDpi(int xDpi)
xDpi
- The number of dots per inchpublic int getXDpi()
public void setYDpi(int yDpi)
yDpi
- The number of dots per inchpublic int getYDpi()
public void setDpi(int xDpi, int yDpi)
xDpi
- The number of dots per inch for the X axis.yDpi
- The number of dots per inch for the Y axis.protected void writeResolution()
public void encodeImage(Image image, OutputStream outputStream, float quality, boolean encodeAlpha) throws IOException, UnsupportedEncoderException
ImageEncoder
encodeImage
in interface ImageEncoder
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.IOException
- if there was an IO error while generating or writing the image data.UnsupportedEncoderException
- if the encoder is not supported.public String getMimeType()
ImageEncoder
getMimeType
in interface ImageEncoder
Copyright © 2002–2017 Pentaho Corporation. All rights reserved.