I want to load a jpeg2000 image (.jp2) using Java JAI.
I'm already using an older JAI version which still supports the jpeg2000. I downloaded these images from scihub.copernicus.eu. These files are rather big (up to 100 MB). I tried several different approaches:
With java the standard way works like that:
public class Main {
public static void main(String[] args) throws IOException {
String path = "C:\\temp\\B2.jp2";
File inputFile = new File(path);
Image result = ImageIO.read(inputFile);
}
}
The exception occurs with the following stacktrace:
Exception in thread "main" java.lang.RuntimeException: An uncaught runtime exception has occurred
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.initializeRead(J2KReadState.java:708)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.<init>(J2KReadState.java:209)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.read(J2KImageReader.java:449)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1308)
at Main.main(Main.java:16)
Caused by: java.io.IOException: File too long.
at jj2000.j2k.fileformat.reader.FileFormatReader.readFileFormat(FileFormatReader.java:207)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.initializeRead(J2KReadState.java:418)
... 5 more
I expected to get a BufferedImage of the JPEP2000 File.
This is the default way of the JAI to load pictures.
public class Main {
public static void main(String[] args) throws IOException {
String path = "C:\\temp\\B2.jp2";
File inputFile = new File(path);
RenderedOp image = JAI.create("fileload", inputFile.getPath());
Image result = image.getAsBufferedImage();
}
}
However the variable image doesn't contain any data: The method getAsBufferedImage() throws the following exception:
Exception in thread "main" java.lang.RuntimeException: - Unable to render RenderedOp for this operation.
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
at javax.media.jai.RenderedOp.getColorModel(RenderedOp.java:2242)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2498)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2546)
at Main.main(Main.java:15)
Any other picture format works fine. What JAI libs are you using in order to load JPEG2000 files? Javax or the libs provided by github or any other? What does your code look like?
I think the "problem" is the specific file(s) that you are trying to read. And it's not really the file size that is too large, but one of the "boxes" size.
The JPEG 2000 file format is based on ISO base media file format (ISO BMFF), which is a container format made up of "boxes" of data. It seems that your specific JPEG 2000 file contains a "box" type (a box with extended length) that the original authors of JJ2000 (the Java JPEG 2000 Reference Implementation that JAI and most, if not all, Java based decoders are based upon) didn't bother to support (alternatively, this type of box is not really allowed in this part of the file, I haven't read the spec in such detail).
You might have better luck with the native JAI plugins for JPEG 2000, or another (native) implementation.
As the ISO BMFF is well documented, it might be possible to fix or patch the JAI FileFormatReader
class to support these extended boxes.