pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1577931 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util: ImageIOUtil.java JPEGUtil.java MetaUtil.java TIFFUtil.java
Date Sat, 15 Mar 2014 20:07:44 GMT
Author: tilman
Date: Sat Mar 15 20:07:44 2014
New Revision: 1577931

URL: http://svn.apache.org/r1577931
Log:
PDFBOX-1975: expand JPEGUtil for missing nodes in metadata; some refactoring

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/TIFFUtil.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java?rev=1577931&r1=1577930&r2=1577931&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/ImageIOUtil.java Sat Mar 15 20:07:44
2014
@@ -32,6 +32,7 @@ import javax.imageio.metadata.IIOInvalid
 import javax.imageio.metadata.IIOMetadata;
 import javax.imageio.metadata.IIOMetadataNode;
 import javax.imageio.stream.ImageOutputStream;
+import static org.apache.pdfbox.util.MetaUtil.STANDARD_METADATA_FORMAT;
 import org.w3c.dom.NodeList;
 
 /**
@@ -39,8 +40,6 @@ import org.w3c.dom.NodeList;
  */
 public class ImageIOUtil
 {
-    private static final String STANDARD_METADATA_FORMAT = "javax_imageio_1.0";
-    
     private ImageIOUtil()
     {
     }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java?rev=1577931&r1=1577930&r2=1577931&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/JPEGUtil.java Sat Mar 15 20:07:44
2014
@@ -17,7 +17,11 @@ package org.apache.pdfbox.util;
 
 import javax.imageio.metadata.IIOInvalidTreeException;
 import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.metadata.IIOMetadataNode;
+import static org.apache.pdfbox.util.MetaUtil.JPEG_NATIVE_FORMAT;
+import static org.apache.pdfbox.util.MetaUtil.debugLogMetadata;
 import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 /**
  *
@@ -25,25 +29,63 @@ import org.w3c.dom.Element;
  */
 class JPEGUtil
 {
-    static final String JPEG_NATIVE_FORMAT = "javax_imageio_jpeg_image_1.0";
-
     /**
-     * Set dpi ina JPEG file
-     * 
+     * Set dpi in a JPEG file
+     *
      * @param metadata the meta data
      * @param dpi the dpi
-     * 
+     *
      * @throws IIOInvalidTreeException if something goes wrong
      */
     static void updateMetadata(IIOMetadata metadata, int dpi) throws IIOInvalidTreeException
     {
-        // https://svn.apache.org/repos/asf/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOJPEGImageWriter.java
+        debugLogMetadata(metadata, JPEG_NATIVE_FORMAT);
+
+        // https://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOJPEGImageWriter.java
         // http://docs.oracle.com/javase/6/docs/api/javax/imageio/metadata/doc-files/jpeg_metadata.html
         Element root = (Element) metadata.getAsTree(JPEG_NATIVE_FORMAT);
-        Element child = (Element) root.getElementsByTagName("app0JFIF").item(0);
-        child.setAttribute("resUnits", "1"); // inch
-        child.setAttribute("Xdensity", Integer.toString(dpi));
-        child.setAttribute("Ydensity", Integer.toString(dpi));
-        metadata.mergeTree(JPEG_NATIVE_FORMAT, root);
+        NodeList jvarNodeList = root.getElementsByTagName("JPEGvariety");
+        Element jvarChild;
+        if (jvarNodeList.getLength() == 0)
+        {
+            jvarChild = new IIOMetadataNode("JPEGvariety");
+            root.appendChild(jvarChild);
+        }
+        else
+        {
+            jvarChild = (Element) jvarNodeList.item(0);
+        }
+
+        NodeList jfifNodeList = jvarChild.getElementsByTagName("app0JFIF");
+        Element jfifChild;
+        if (jfifNodeList.getLength() == 0)
+        {
+            jfifChild = new IIOMetadataNode("app0JFIF");
+            jvarChild.appendChild(jfifChild);
+        }
+        else
+        {
+            jfifChild = (Element) jfifNodeList.item(0);
+        }
+        if (jfifChild.getAttribute("majorVersion").isEmpty())
+        {
+            jfifChild.setAttribute("majorVersion", "1");
+        }
+        if (jfifChild.getAttribute("minorVersion").isEmpty())
+        {
+            jfifChild.setAttribute("minorVersion", "2");
+        }
+        jfifChild.setAttribute("resUnits", "1"); // inch
+        jfifChild.setAttribute("Xdensity", Integer.toString(dpi));
+        jfifChild.setAttribute("Ydensity", Integer.toString(dpi));
+        if (jfifChild.getAttribute("thumbWidth").isEmpty())
+        {
+            jfifChild.setAttribute("thumbWidth", "0");
+        }
+        if (jfifChild.getAttribute("thumbHeight").isEmpty())
+        {
+            jfifChild.setAttribute("thumbHeight", "0");
+        }
+        metadata.setFromTree(JPEG_NATIVE_FORMAT, root); // mergeTree doesn't work for ARGB
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java?rev=1577931&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java Sat Mar 15 20:07:44
2014
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2014 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pdfbox.util;
+
+import java.io.StringWriter;
+import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.metadata.IIOMetadataNode;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+public class MetaUtil
+{
+    private static final Log LOG = LogFactory.getLog(TIFFUtil.class);
+
+    static final String SUN_TIFF_FORMAT = "com_sun_media_imageio_plugins_tiff_image_1.0";
+    static final String JPEG_NATIVE_FORMAT = "javax_imageio_jpeg_image_1.0";
+    static final String STANDARD_METADATA_FORMAT = "javax_imageio_1.0";
+    
+
+    // logs metadata as an XML tree if debug is enabled
+    static void debugLogMetadata(IIOMetadata metadata, String format)
+    {
+        if (LOG.isDebugEnabled())
+        {
+            return;
+        }
+
+        // see http://docs.oracle.com/javase/7/docs/api/javax/imageio/
+        //     metadata/doc-files/standard_metadata.html
+        IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(format);
+        try
+        {
+            StringWriter xmlStringWriter = new StringWriter();
+            StreamResult streamResult = new StreamResult(xmlStringWriter);
+            Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            // see http://stackoverflow.com/a/1264872/535646
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
+            DOMSource domSource = new DOMSource(root);
+            transformer.transform(domSource, streamResult);
+            LOG.debug("\n" + xmlStringWriter);
+        }
+        catch (IllegalArgumentException ex)
+        {
+            LOG.error(ex, ex);
+        }
+        catch (TransformerException ex)
+        {
+            LOG.error(ex, ex);
+        }
+    }
+
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/MetaUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/TIFFUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/TIFFUtil.java?rev=1577931&r1=1577930&r2=1577931&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/TIFFUtil.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/TIFFUtil.java Sat Mar 15 20:07:44
2014
@@ -23,13 +23,9 @@ import javax.imageio.ImageWriteParam;
 import javax.imageio.metadata.IIOInvalidTreeException;
 import javax.imageio.metadata.IIOMetadata;
 import javax.imageio.metadata.IIOMetadataNode;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
 import java.awt.image.BufferedImage;
-import java.io.StringWriter;
+import static org.apache.pdfbox.util.MetaUtil.SUN_TIFF_FORMAT;
+import static org.apache.pdfbox.util.MetaUtil.debugLogMetadata;
 
 /**
  * Used by ImageIOUtil to write TIFF files.
@@ -38,7 +34,6 @@ import java.io.StringWriter;
 class TIFFUtil
 {
     private static final Log LOG = LogFactory.getLog(TIFFUtil.class);
-    private static final String SUN_TIFF_FORMAT = "com_sun_media_imageio_plugins_tiff_image_1.0";
 
     /**
      * Sets the ImageIO parameter compression type based on the given image.
@@ -73,7 +68,7 @@ class TIFFUtil
      */
     public static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi)
     {
-        debugLogMetadata(metadata);
+        debugLogMetadata(metadata, SUN_TIFF_FORMAT);
 
         if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName()))
         {
@@ -121,7 +116,7 @@ class TIFFUtil
             throw new RuntimeException(e);
         }
 
-        debugLogMetadata(metadata);
+        debugLogMetadata(metadata, SUN_TIFF_FORMAT);
     }
 
     private static IIOMetadataNode createShortField(int tiffTagNumber, String name, int val)
@@ -181,33 +176,4 @@ class TIFFUtil
         return field;
     }
 
-    // logs metadata as an XML tree if debug is enabled
-
-    private static void debugLogMetadata(IIOMetadata metadata)
-    {
-        if (LOG.isDebugEnabled())
-        {
-            return;
-        }
-
-        // see http://docs.oracle.com/javase/7/docs/api/javax/imageio/
-        //     metadata/doc-files/standard_metadata.html
-        IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree("javax_imageio_1.0");
-        try
-        {
-            StringWriter xmlStringWriter = new StringWriter();
-            StreamResult streamResult = new StreamResult(xmlStringWriter);
-            Transformer transformer = TransformerFactory.newInstance().newTransformer();
-            // see http://stackoverflow.com/a/1264872/535646
-            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
-            DOMSource domSource = new DOMSource(root);
-            transformer.transform(domSource, streamResult);
-            LOG.debug("\n" + xmlStringWriter);
-        }
-        catch (Exception ex)
-        {
-            LOG.error(ex, ex);
-        }
-    }
 }



Mime
View raw message