Class PngCodec
Reads and writes PNG files in pure Java.
[#decode(byte[])] accepts every PNG color type (gray, RGB, palette, gray
with alpha, RGBA), bit depths 1 to 16, tRNS transparency and Adam7
interlacing. 16-bit samples are reduced to their high byte. encode(RasterImage)
writes 8-bit RGBA, or RGB when every pixel is opaque.
The lower-level [#unfilter(byte[], int, int, int, int)] and
[#expand(byte[], int, int, int, int)] keep the file's own sample layout
instead of converting to ARGB; the Python runtime's PIL uses them to
keep a palette image a palette image.
-
Method Summary
Modifier and TypeMethodDescriptionstatic RasterImagedecode(byte[] data) Decodes a PNG file to a non-premultiplied ARGB image.static byte[]encode(RasterImage img) Encodes an image as an 8-bit PNG: RGBA, or RGB when it is opaque.static byte[]encodeRows(int w, int h, int depth, int ctype, byte[] plte, byte[] trns, byte[] rows) Assembles a PNG file from rows that already carry their filter bytes.static byte[]expand(byte[] raw, int w, int h, int ctype, int depth) Unpacks unfiltered rows to one byte per sample: gray to 1 band, RGB to 3, gray with alpha to 2, RGBA to 4 and palette to its 1-band indices.static booleanisPng(byte[] data) Returns true whendatastarts with the PNG signature.static byte[]unfilter(byte[] data, int offset, int row, int h, int bpp) Reverses the five PNG row filters.
-
Method Details
-
isPng
public static boolean isPng(byte[] data) Returns true when
datastarts with the PNG signature.Parameters
data: file bytes
Returns
whether the data is a PNG file
-
decode
Decodes a PNG file to a non-premultiplied ARGB image.
Parameters
data: the file
Returns
the image
Throws
IOException: when the data is not a PNG file this codec can read
- Throws:
IOException
-
unfilter
Reverses the five PNG row filters.
Parameters
data:hrows, each a filter type byte followed byrowbytesoffset: where the first row starts indatarow: bytes per row, without the filter byteh: number of rowsbpp: the filter's byte distance, at least 1
Returns
the
row * hunfiltered bytesThrows
IOException: on truncated data or an unknown filter type
- Throws:
IOException
-
expand
public static byte[] expand(byte[] raw, int w, int h, int ctype, int depth) Unpacks unfiltered rows to one byte per sample: gray to 1 band, RGB to 3, gray with alpha to 2, RGBA to 4 and palette to its 1-band indices. Gray below 8 bits is scaled to 0..255, 16-bit samples keep their high byte.
Parameters
raw: unfiltered rowsw: widthh: heightctype: the PNG color typedepth: the PNG bit depth
Returns
w * h * bandsbytes -
encode
Encodes an image as an 8-bit PNG: RGBA, or RGB when it is opaque.
Parameters
img: the image
Returns
the PNG file
-
encodeRows
public static byte[] encodeRows(int w, int h, int depth, int ctype, byte[] plte, byte[] trns, byte[] rows) Assembles a PNG file from rows that already carry their filter bytes.
Parameters
w: widthh: heightdepth: bit depthctype: color typeplte: thePLTEchunk body, or nulltrns: thetRNSchunk body, or nullrows:hrows, each a filter type byte and its samples
Returns
the PNG file
-