Class PngCodec

java.lang.Object
com.codename1.backend.image.PngCodec

public final class PngCodec extends Object

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 Type
    Method
    Description
    decode(byte[] data)
    Decodes a PNG file to a non-premultiplied ARGB image.
    static byte[]
    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 boolean
    isPng(byte[] data)
    Returns true when data starts with the PNG signature.
    static byte[]
    unfilter(byte[] data, int offset, int row, int h, int bpp)
    Reverses the five PNG row filters.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isPng

      public static boolean isPng(byte[] data)

      Returns true when data starts with the PNG signature.

      Parameters
      • data: 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: the file
      Returns

      the image

      Throws
      • IOException: when the data is not a PNG file this codec can read
      Throws:
      IOException
    • 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: h rows, each a filter type byte followed by row bytes
      • offset: where the first row starts in data
      • row: bytes per row, without the filter byte
      • h: number of rows
      • bpp: the filter's byte distance, at least 1
      Returns

      the row * h unfiltered bytes

      Throws
      • 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 rows
      • w: width
      • h: height
      • ctype: the PNG color type
      • depth: 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: 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: width
      • h: height
      • depth: bit depth
      • ctype: color type
      • plte: the PLTE chunk body, or null
      • trns: the tRNS chunk body, or null
      • rows: h rows, each a filter type byte and its samples
      Returns

      the PNG file