Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class Jpeg2000

  1. Object
  2. Jpeg2000

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 types

interface Jpeg2000.ColorsConverts colour components the file does not describe itself, as when a PDF gives the image’s colour space in its own dictionary.

Constructors

public Jpeg2000()

Methods

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

Inherited methods

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.