Class Jpeg2000

java.lang.Object
com.codename1.util.Jpeg2000

public final class Jpeg2000 extends Object

A pure-Java JPEG 2000 decoder, for platforms whose image APIs cannot read the format (most of them) and for the backend, which has no image support of its own for it.

It reads either a JP2 file (the jp2h header boxes and the jp2c codestream) or a raw J2K codestream, and decodes every tile into one integer plane per component. The decoder covers what PDF producers emit: all five progression orders, multiple tiles and tile-parts, subsampled components, default and custom precincts, every code-block style switch, the 5/3 and 9/7 wavelets, scalar quantization, the RCT and ICT component transforms and bit depths from 1 to 16. Region of interest shifts and progression order changes (POC) are ignored, and High Throughput code-blocks are refused.

Malformed or truncated data never escapes as an exception: decode answers null, or the image as far as it could be decoded. Images above 25 megapixels are refused.

byte[] data = new byte[0]; // the contents of a .jp2 or .j2k file
Jpeg2000 j = Jpeg2000.decode(data);
if (j != null) {
    int[] argb = j.toArgb();
    // on the client: Image.createImage(argb, j.getWidth(), j.getHeight())
}
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Converts colour components the file does not describe itself, as when a PDF gives the image's colour space in its own dictionary.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Jpeg2000
    decode(byte[] data)
    Parses a JP2 file or a raw J2K codestream; null when it cannot be decoded.
    int
    The image height in pixels.
    int
    The image width in pixels.
    int[]
    ARGB pixels, row by row, getWidth() * getHeight() of them; null when the image's components cannot be turned into a colour.
    int[]
    toArgb(Jpeg2000.Colors colors, boolean alpha)
    ARGB pixels as toArgb(), with the colours of colors instead of the file's own.

    Methods inherited from class Object

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

    • Jpeg2000

      public Jpeg2000()
  • Method Details

    • getWidth

      public int getWidth()
      The image width in pixels.
    • getHeight

      public int getHeight()
      The image height in pixels.
    • toArgb

      public int[] toArgb()
      ARGB pixels, row by row, getWidth() * getHeight() of them; null when the image's components cannot be turned into a colour. A JP2 palette is applied, YCC and CMYK are converted, and an alpha channel named by a cdef box, or a component past the colour ones, becomes the alpha.
    • toArgb

      public int[] toArgb(Jpeg2000.Colors colors, boolean alpha)
      ARGB pixels as toArgb(), with the colours of colors instead of the file's own. With alpha false any alpha channel is ignored and the image is opaque.
    • decode

      public static Jpeg2000 decode(byte[] data)
      Parses a JP2 file or a raw J2K codestream; null when it cannot be decoded.