Skip to content

Commit

Permalink
Merge pull request #156 from aaime/jp2_gml
Browse files Browse the repository at this point in the history
Kakadu stream metadata XMLBoxMetadataNode fails to return the actual XML #155
  • Loading branch information
aaime authored May 9, 2018
2 parents b26e64d + 577e289 commit 3c2c4cd
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import it.geosolutions.imageio.plugins.jp2k.box.BoxUtilities;
import it.geosolutions.imageio.plugins.jp2k.box.ContiguousCodestreamBox;
import it.geosolutions.imageio.plugins.jp2k.box.JP2KFileBox;
import it.geosolutions.imageio.plugins.jp2k.box.LabelBox;
import it.geosolutions.imageio.plugins.jp2k.box.LabelBoxMetadataNode;
import it.geosolutions.imageio.plugins.jp2k.box.UUIDBox;
import it.geosolutions.imageio.plugins.jp2k.box.UUIDBoxMetadataNode;
import it.geosolutions.imageio.plugins.jp2k.box.XMLBox;
Expand Down Expand Up @@ -116,6 +118,9 @@ private Node buildTree(final JP2KBoxMetadata node) {
case ASOCBox.BOX_TYPE:
mdNode = new ASOCBoxMetadataNode(node);
break;
case LabelBox.BOX_TYPE:
mdNode = new LabelBoxMetadataNode(node);
break;
case JP2KFileBox.BOX_TYPE:
mdNode = node.getNativeNode();
mdNode.setAttribute(NUM_CODESTREAMS, Integer.toString(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package it.geosolutions.imageio.plugins.jp2k.box;

import it.geosolutions.imageio.plugins.jp2k.JP2KBox;
import it.geosolutions.util.KakaduUtilities;

import javax.imageio.metadata.IIOMetadataNode;

/**
Expand All @@ -34,9 +37,9 @@ public String getText() {
return text;
}

LabelBoxMetadataNode(final LabelBox box) {
public LabelBoxMetadataNode(final JP2KBox box) {
super(box);
text = box.getText();
text = KakaduUtilities.readTerminatedString(box.getContent());
try {
IIOMetadataNode child = new IIOMetadataNode("text");
child.setNodeValue(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import it.geosolutions.imageio.plugins.jp2k.JP2KBox;

import it.geosolutions.util.KakaduUtilities;
import org.w3c.dom.DOMException;

import com.sun.media.imageioimpl.common.ImageUtil;
Expand Down Expand Up @@ -51,7 +52,7 @@ public XMLBoxMetadataNode(final JP2KBox box) {
}

public String getXml() {
return ImageUtil.convertObjectToString(getContent());
return KakaduUtilities.readTerminatedString(getContent());
}

public ByteArrayInputStream getRawXml() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,29 @@ public static void computeRegions(final Rectangle sourceBounds,
public static boolean notEqual(double value, double reference) {
return (Math.abs(value - reference) > KakaduUtilities.DOUBLE_TOLERANCE);
}

/**
* Helper that reads a "terminated" string applying some work arounds for invalid data.
* Assumes all the bytes should be used, skips the eventual last terminator, and turns
* all ones in the middle in newlines, see also https://trac.osgeo.org/gdal/ticket/5760
*
* @param contents
* @return
*/
public static String readTerminatedString(byte[] contents) {
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < contents.length; i++) {
byte c = contents[i];
if ((c == 0 || c == -1)) {
if (i == contents.length - 1) {
break;
} else {
c = '\n';
}
}
builder.append((char) c);
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package it.geosolutions.imageio.plugins.jp2k;

import it.geosolutions.imageio.imageioimpl.imagereadmt.ImageReadDescriptorMT;
import it.geosolutions.imageio.plugins.jp2k.box.XMLBox;
import it.geosolutions.imageio.plugins.jp2k.box.XMLBoxMetadataNode;
import it.geosolutions.imageio.utilities.ImageIOUtilities;
import it.geosolutions.resources.TestData;

Expand All @@ -25,9 +27,13 @@
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;

import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.FileImageOutputStream;
import javax.media.jai.Histogram;
import javax.media.jai.ImageLayout;
Expand All @@ -39,6 +45,10 @@

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;

/**
* Testing reading capabilities for {@link JP2KKakaduImageReader} leveraging on
Expand Down Expand Up @@ -190,5 +200,25 @@ public static void displayStatistics(boolean b, RenderedImage source) {
frame.pack();
frame.show();
}

@Test
public void testXMLBoxReading() throws Exception {
if (!runTests)
return;

final File file = TestData.file(this, "bogota_gml.jp2");
final ImageReader reader = new JP2KKakaduImageReaderSpi().createReaderInstance();
reader.setInput(file);
Assert.assertEquals(1,reader.getNumImages(false));
final JP2KStreamMetadata metadata = (JP2KStreamMetadata) reader.getStreamMetadata();
final List<IIOMetadataNode> boxes = metadata.searchOccurrencesNode(XMLBox.BOX_TYPE);
assertTrue(boxes != null);
assertEquals(1, boxes.size());
final XMLBoxMetadataNode xmlBox = (XMLBoxMetadataNode) boxes.get(0);
String xml = xmlBox.getXml();
assertTrue(xml.startsWith("<gml:FeatureCollection"));
assertTrue(xml.contains("gml:RectifiedGridCoverage"));
assertTrue(xml.endsWith("</gml:FeatureCollection>\n"));
}

}
Binary file not shown.

0 comments on commit 3c2c4cd

Please sign in to comment.