Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class PngCodec

  1. Object
  2. 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 IOExceptionDecodes a PNG file to a non-premultiplied ARGB image.
public static byte[] unfilter(byte[] data, int offset, int row, int h, int bpp) throws IOExceptionReverses 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

data byte[]
file bytes

Returns

whether the data is a PNG file

decode

public static RasterImage decode(byte[] data) throws IOException
Decodes a PNG file to a non-premultiplied ARGB image.

Parameters

data byte[]
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 IOException
Reverses the five PNG row filters.

Parameters

data byte[]
h rows, each a filter type byte followed by row bytes
offset int
where the first row starts in data
row int
bytes per row, without the filter byte
h int
number of rows
bpp int
the filter’s byte distance, at least 1

Returns

the row * h unfiltered bytes

Throws

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

raw byte[]
unfiltered rows
w int
width
h int
height
ctype int
the PNG color type
depth int
the PNG bit depth

Returns

w * h * bands bytes

encode

public static byte[] encode(RasterImage img)
Encodes an image as an 8-bit PNG: RGBA, or RGB when it is opaque.

Parameters

img RasterImage
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 int
width
h int
height
depth int
bit depth
ctype int
color type
plte byte[]
the PLTE chunk body, or null
trns byte[]
the tRNS chunk body, or null
rows byte[]
h rows, each a filter type byte and its samples

Returns

the PNG file