Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class PngCodec
- Object
- 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.
Methods
public static boolean isPng(byte[] data) | Returns true when data starts with the PNG signature. |
public static RasterImage decode(byte[] data)
throws IOException | Decodes a PNG file to a non-premultiplied ARGB image. |
public static byte[] unfilter(byte[] data, int offset, int row, int h, int bpp)
throws IOException | Reverses the five PNG row filters. |
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. |
public static byte[] encode(RasterImage img) | Encodes an image as an 8-bit PNG: RGBA, or RGB when it is opaque. |
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. |
Inherited methods
Method details
isPng
public static boolean isPng(byte[] data)Returns true when
data starts with the PNG signature.Parameters
databyte[]- file bytes
Returns
whether the data is a PNG file
decode
public static RasterImage decode(byte[] data)
throws IOExceptionDecodes a PNG file to a non-premultiplied ARGB image.
Parameters
databyte[]- the file
Returns
the image
Throws
IOException- when the data is not a PNG file this codec can read
unfilter
public static byte[] unfilter(byte[] data, int offset, int row, int h, int bpp)
throws IOExceptionReverses the five PNG row filters.
Parameters
databyte[]hrows, each a filter type byte followed byrowbytesoffsetint- where the first row starts in
data rowint- bytes per row, without the filter byte
hint- number of rows
bppint- the filter’s byte distance, at least 1
Returns
the
row * h unfiltered bytesThrows
IOException- on truncated data or an unknown filter type
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
rawbyte[]- unfiltered rows
wint- width
hint- height
ctypeint- the PNG color type
depthint- the PNG bit depth
Returns
w * h * bands bytesencode
public static byte[] encode(RasterImage img)Encodes an image as an 8-bit PNG: RGBA, or RGB when it is opaque.
Parameters
imgRasterImage- 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
wint- width
hint- height
depthint- bit depth
ctypeint- color type
pltebyte[]- the
PLTEchunk body, or null trnsbyte[]- the
tRNSchunk body, or null rowsbyte[]hrows, each a filter type byte and its samples
Returns
the PNG file