You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by ss...@apache.org on 2018/06/18 10:08:46 UTC

svn commit: r1833700 - in /xmlgraphics/commons/trunk/src: main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java

Author: ssteiner
Date: Mon Jun 18 10:08:45 2018
New Revision: 1833700

URL: http://svn.apache.org/viewvc?rev=1833700&view=rev
Log:
XGC-112: TIFF resolution wrong on Java 10

Modified:
    xmlgraphics/commons/trunk/src/main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java
    xmlgraphics/commons/trunk/src/test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java

Modified: xmlgraphics/commons/trunk/src/main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java?rev=1833700&r1=1833699&r2=1833700&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java (original)
+++ xmlgraphics/commons/trunk/src/main/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java Mon Jun 18 10:08:45 2018
@@ -46,8 +46,12 @@ public class ImageIOTIFFImageWriter exte
 
     private static final String SUN_TIFF_NATIVE_FORMAT
             = "com_sun_media_imageio_plugins_tiff_image_1.0";
+    private static final String JAVA_TIFF_NATIVE_FORMAT
+            = "javax_imageio_tiff_image_1.0";
     private static final String SUN_TIFF_NATIVE_STREAM_FORMAT
             = "com_sun_media_imageio_plugins_tiff_stream_1.0";
+    private static final String JAVA_TIFF_NATIVE_STREAM_FORMAT
+            = "javax_imageio_tiff_stream_1.0";
 
     /**
      * Main constructor.
@@ -65,14 +69,12 @@ public class ImageIOTIFFImageWriter exte
         //it doesn't work properly through the standard metadata. Haven't figured out why
         //that happens.
         if (params.getResolution() != null) {
-            if (SUN_TIFF_NATIVE_FORMAT.equals(meta.getNativeMetadataFormatName())) {
-                //IIOMetadataNode root = (IIOMetadataNode)meta.getAsTree(SUN_TIFF_NATIVE_FORMAT);
-                IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_NATIVE_FORMAT);
+            if (SUN_TIFF_NATIVE_FORMAT.equals(meta.getNativeMetadataFormatName())
+                    || JAVA_TIFF_NATIVE_FORMAT.equals(meta.getNativeMetadataFormatName())) {
+                IIOMetadataNode root = new IIOMetadataNode(meta.getNativeMetadataFormatName());
                 IIOMetadataNode ifd = getChildNode(root, "TIFFIFD");
                 if (ifd == null) {
                     ifd = new IIOMetadataNode("TIFFIFD");
-                    ifd.setAttribute("tagSets",
-                                "com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet");
                     root.appendChild(ifd);
                 }
                 ifd.appendChild(createResolutionUnitField(params));
@@ -85,7 +87,7 @@ public class ImageIOTIFFImageWriter exte
                         "RowsPerStrip", Integer.toString(rows)));
 
                 try {
-                    meta.mergeTree(SUN_TIFF_NATIVE_FORMAT, root);
+                    meta.mergeTree(meta.getNativeMetadataFormatName(), root);
                 } catch (IIOInvalidTreeException e) {
                     throw new RuntimeException("Cannot update image metadata: "
                                 + e.getMessage(), e);
@@ -221,20 +223,27 @@ public class ImageIOTIFFImageWriter exte
 
         //Try changing the Byte Order
         IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(writeParam);
-        Set<String> names = new java.util.HashSet<String>(
-                Arrays.asList(streamMetadata.getMetadataFormatNames()));
-        if (names.contains(SUN_TIFF_NATIVE_STREAM_FORMAT)) {
-            Node root = streamMetadata.getAsTree(SUN_TIFF_NATIVE_STREAM_FORMAT);
+        if (streamMetadata != null) {
+            Set<String> names = new java.util.HashSet<String>(
+                    Arrays.asList(streamMetadata.getMetadataFormatNames()));
+            setFromTree(names, streamMetadata, endian, SUN_TIFF_NATIVE_STREAM_FORMAT);
+            setFromTree(names, streamMetadata, endian, JAVA_TIFF_NATIVE_STREAM_FORMAT);
+        }
+        return streamMetadata;
+    }
+
+    private void setFromTree(Set<String> names, IIOMetadata streamMetadata, Endianness endian, String format) {
+        if (names.contains(format)) {
+            Node root = streamMetadata.getAsTree(format);
             root.getFirstChild().getAttributes().item(0).setNodeValue(endian.toString());
             try {
-                streamMetadata.setFromTree(SUN_TIFF_NATIVE_STREAM_FORMAT, root);
+                streamMetadata.setFromTree(format, root);
             } catch (IIOInvalidTreeException e) {
                 //This should not happen since we check if the format is supported.
                 throw new IllegalStateException(
                         "Could not replace TIFF stream metadata: " + e.getMessage(), e);
             }
         }
-        return streamMetadata;
     }
 
 }

Modified: xmlgraphics/commons/trunk/src/test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java?rev=1833700&r1=1833699&r2=1833700&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java (original)
+++ xmlgraphics/commons/trunk/src/test/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriterTestCase.java Mon Jun 18 10:08:45 2018
@@ -27,9 +27,14 @@ import java.awt.geom.Ellipse2D;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
 
+import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.metadata.IIOMetadataNode;
+
 import org.junit.Assert;
 import org.junit.Test;
 
+import org.w3c.dom.Node;
+
 import org.apache.commons.io.output.ByteArrayOutputStream;
 
 import org.apache.xmlgraphics.image.loader.ImageSize;
@@ -126,7 +131,7 @@ public class ImageIOTIFFImageWriterTestC
         ByteArrayOutputStream getByteArrayOutput();
     }
 
-    private class TestImageWriter implements ImageWriterHelper {
+    private static class TestImageWriter implements ImageWriterHelper {
         private ImageWriter writer;
         private ByteArrayOutputStream baout;
 
@@ -149,7 +154,7 @@ public class ImageIOTIFFImageWriterTestC
         }
     }
 
-    private class TestMultiImageWriter implements ImageWriterHelper {
+    private static class TestMultiImageWriter implements ImageWriterHelper {
         private MultiImageWriter writer;
         private ByteArrayOutputStream baout;
 
@@ -176,4 +181,32 @@ public class ImageIOTIFFImageWriterTestC
         }
     }
 
+    @Test
+    public void testNewMetadataFormat() {
+        ImageWriterParams params = new ImageWriterParams();
+        params.setResolution(92);
+        MyIIOMetadata metadata = new MyIIOMetadata();
+        new ImageIOTIFFImageWriter().updateMetadata(null, metadata, params);
+        Assert.assertEquals(metadata.mergeNode, "javax_imageio_tiff_image_1.0");
+    }
+
+    static class MyIIOMetadata extends IIOMetadata {
+        String mergeNode;
+        MyIIOMetadata() {
+            super(true, "javax_imageio_tiff_image_1.0", null, null, null);
+        }
+        public boolean isReadOnly() {
+            return false;
+        }
+        public Node getAsTree(String formatName) {
+            IIOMetadataNode node = new IIOMetadataNode();
+            node.appendChild(new IIOMetadataNode("Dimension"));
+            return node;
+        }
+        public void mergeTree(String formatName, Node root) {
+            mergeNode = root.getNodeName();
+        }
+        public void reset() {
+        }
+    };
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org