Skip to main content

JP2 Multiple XML Boxes

3 replies [Last post]
jareed
Offline
Joined: 2008-05-30

Hello all,

I was playing around tonight with adding some custom metadata in the form of xml boxes to JPEG-2000 images:

J2KMetadata metadata = new J2KMetadata(its, writer.getDefaultWriteParam(), writer);
metadata.addNode(new XMLBox("<?xml version=\"1.0\" encoding=\"UTF-8\"?>0.01.0\n\n".getBytes()));

This works great, but doesn't seem to work when I try to add multiple XML boxes:

J2KMetadata metadata = new J2KMetadata(its, writer.getDefaultWriteParam(), writer);
metadata.addNode(new XMLBox("<?xml version=\"1.0\" encoding=\"UTF-8\"?>0.01.0\n\n".getBytes()));
metadata.addNode(new XMLBox("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n".getBytes()));

The last one seems to overwrite any previous ones. Based on the spec, I thought I should be able to have multiple XML boxes. A quick peek into J2KMetadataFormat#singleInstance(String) seems to confirm this impression as it is returning false for "JPEG2000XMLBox". If I'm not wrong about multiple XML boxes being supported, then J2KMetadata#addNode(Box) should check the format to see if the added box is a single instance or not, e.g.

public void addNode(Box node) {
if (boxes == null)
boxes = new ArrayList();

String name = Box.getName(node.getType());
if (format.singleInstance(name))
replace(name, node);
else
boxes.add(node);
}

Or what am I missing? Thanks for your time.

Cheers,
Josh Reed

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jareed
Offline
Joined: 2008-05-30

It is possible with the following patch to J2KMetadata:

RCS file: /cvs/jai-imageio-core/src/share/classes/com/sun/media/imageioimpl/plugins/jpeg2000/J2KMetadata.java,v
retrieving revision 1.4
diff -r1.4 J2KMetadata.java
631c631,650
< replace(Box.getName(node.getType()), node);
---
>
> String name = Box.getName(node.getType());
> if (format.singleInstance(name)) {
> replace(Box.getName(node.getType()), node);
> } else if (!boxes.contains(node)) {
> for (int i = boxes.size() - 1; i >= 0; i--) {
> Box box1 = (Box) boxes.get(i);
> if (name.equals(Box.getName(box1.getType()))) {
> boxes.add(i + 1, node);
> return;
> }
> }
> boxes.add(node);
> }
> }

The patch simply checks to see whether the box type can show up multiple times and adds it in the appropriate place if it can.

I'm using this in a little library that I developed for adding/removing/printing metadata in JP2 files: http://hg.psicat.org/jpeg2000/

Cheers,
Josh

jareed
Offline
Joined: 2008-05-30

OK, so I might be wrong about this. I'd love it if someone could either reproduce my error on multiple XML boxes or prove me wrong.

Cheers,
Josh

jareed
Offline
Joined: 2008-05-30

Just a quick follow up on this. When I patch J2KMetadata#addNode(Box) to allow multiple xml boxes, it writes things out but I can no longer read the file back in. Its dying with the following error:

Caused by: java.lang.RuntimeException: An uncaught runtime exception has occurred pos < flushedPos!
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.initializeRead(Unknown Source)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KReadState.(Unknown Source)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.readHeader(Unknown Source)
at com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader.getImageTypes(Unknown Source)
at javax.imageio.ImageReader.getRawImageType(ImageReader.java:665)
at com.sun.media.jai.imageioimpl.ImageReadOpImage.layoutHelper(Unknown Source)
at com.sun.media.jai.imageioimpl.ImageReadOpImage.(Unknown Source)
... 20 more
Caused by: java.lang.IndexOutOfBoundsException: pos < flushedPos!
at javax.imageio.stream.FileImageInputStream.seek(FileImageInputStream.java:113)
at com.sun.media.imageioimpl.plugins.jpeg2000.IISRandomAccessIO.seek(Unknown Source)
at jj2000.j2k.fileformat.reader.FileFormatReader.readFileFormat(Unknown Source)
... 27 more

Cheers,
Josh