You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2006/09/18 09:32:49 UTC
svn commit: r447306 - in
/xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics:
image/writer/imageio/ImageIOImageWriter.java
image/writer/imageio/ImageIOTIFFImageWriter.java util/ImageIODebugUtil.java
Author: jeremias
Date: Mon Sep 18 00:32:48 2006
New Revision: 447306
URL: http://svn.apache.org/viewvc?view=rev&rev=447306
Log:
Setting the image resolution through the standard metadata for TIFF images doesn't seem to work properly. The values come out wrong. So I set the resolution manually over the native format.
Modified:
xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java
xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java
xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/util/ImageIODebugUtil.java
Modified: xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java?view=diff&rev=447306&r1=447305&r2=447306
==============================================================================
--- xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java (original)
+++ xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java Mon Sep 18 00:32:48 2006
@@ -48,6 +48,8 @@
*/
public class ImageIOImageWriter implements ImageWriter, IIOWriteWarningListener {
+ private static final String STANDARD_METADATA_FORMAT = "javax_imageio_1.0";
+
private String targetMIME;
/**
@@ -142,9 +144,8 @@
* @return the updated metadata
*/
protected IIOMetadata updateMetadata(IIOMetadata meta, ImageWriterParams params) {
- final String stdmeta = "javax_imageio_1.0";
if (meta.isStandardMetadataFormatSupported()) {
- IIOMetadataNode root = (IIOMetadataNode)meta.getAsTree(stdmeta);
+ IIOMetadataNode root = (IIOMetadataNode)meta.getAsTree(STANDARD_METADATA_FORMAT);
IIOMetadataNode dim = getChildNode(root, "Dimension");
IIOMetadataNode child;
if (params.getResolution() != null) {
@@ -164,7 +165,7 @@
Double.toString(params.getResolution().doubleValue() / 25.4));
}
try {
- meta.mergeTree(stdmeta, root);
+ meta.mergeTree(STANDARD_METADATA_FORMAT, root);
} catch (IIOInvalidTreeException e) {
throw new RuntimeException("Cannot update image metadata: "
+ e.getMessage());
Modified: xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java?view=diff&rev=447306&r1=447305&r2=447306
==============================================================================
--- xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java (original)
+++ xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java Mon Sep 18 00:32:48 2006
@@ -19,6 +19,12 @@
package org.apache.xmlgraphics.image.writer.imageio;
+import javax.imageio.metadata.IIOInvalidTreeException;
+import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.metadata.IIOMetadataNode;
+
+import org.apache.xmlgraphics.image.writer.ImageWriterParams;
+
/**
* ImageWriter that encodes TIFF images using Image I/O.
*
@@ -26,11 +32,82 @@
*/
public class ImageIOTIFFImageWriter extends ImageIOImageWriter {
+ private static final String SUN_TIFF_NATIVE_FORMAT
+ = "com_sun_media_imageio_plugins_tiff_image_1.0";
+
/**
* Main constructor.
*/
public ImageIOTIFFImageWriter() {
super("image/tiff");
+ }
+
+ /**
+ * @see org.apache.xmlgraphics.image.writer.imageio.ImageIOImageWriter#updateMetadata(javax.imageio.metadata.IIOMetadata, org.apache.xmlgraphics.image.writer.ImageWriterParams)
+ */
+ protected IIOMetadata updateMetadata(IIOMetadata meta, ImageWriterParams params) {
+ IIOMetadata ret = super.updateMetadata(meta, params);
+
+ //We set the resolution manually using the native format since it appears that
+ //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);
+
+ 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(createResolutionField(282, "XResolution", params));
+ ifd.appendChild(createResolutionField(283, "YResolution", params));
+
+ //ResolutionUnit
+ IIOMetadataNode field, arrayNode, valueNode;
+ field = new IIOMetadataNode("TIFFField");
+ field.setAttribute("number", Integer.toString(296));
+ field.setAttribute("name", "ResolutionUnit");
+ arrayNode = new IIOMetadataNode("TIFFShorts");
+ field.appendChild(arrayNode);
+ valueNode = new IIOMetadataNode("TIFFShort");
+ valueNode.setAttribute("value", Integer.toString(3));
+ valueNode.setAttribute("description", "Centimeter");
+ arrayNode.appendChild(valueNode);
+
+ try {
+ meta.mergeTree(SUN_TIFF_NATIVE_FORMAT, root);
+ } catch (IIOInvalidTreeException e) {
+ throw new RuntimeException("Cannot update image metadata: "
+ + e.getMessage(), e);
+ }
+ }
+ }
+
+ return ret;
+ }
+
+ private IIOMetadataNode createResolutionField(int number, String name, ImageWriterParams params) {
+ IIOMetadataNode field, arrayNode, valueNode;
+ field = new IIOMetadataNode("TIFFField");
+ field.setAttribute("number", Integer.toString(number));
+ field.setAttribute("name", name);
+ arrayNode = new IIOMetadataNode("TIFFRationals");
+ field.appendChild(arrayNode);
+ valueNode = new IIOMetadataNode("TIFFRational");
+ arrayNode.appendChild(valueNode);
+
+ // Set target resolution
+ float pixSzMM = 25.4f / params.getResolution().floatValue();
+ // num Pixs in 100 Meters
+ int numPix = (int)(((1000 * 100) / pixSzMM) + 0.5);
+ int denom = 100 * 100; // Centimeters per 100 Meters;
+ valueNode.setAttribute("value", numPix + "/" + denom);
+ return field;
}
}
Modified: xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/util/ImageIODebugUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/util/ImageIODebugUtil.java?view=diff&rev=447306&r1=447305&r2=447306
==============================================================================
--- xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/util/ImageIODebugUtil.java (original)
+++ xmlgraphics/commons/trunk/src/java-1.4/org/apache/xmlgraphics/util/ImageIODebugUtil.java Mon Sep 18 00:32:48 2006
@@ -20,6 +20,8 @@
package org.apache.xmlgraphics.util;
import javax.imageio.metadata.IIOMetadata;
+import javax.imageio.metadata.IIOMetadataFormatImpl;
+import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
@@ -36,8 +38,13 @@
*/
public class ImageIODebugUtil {
- public static void dumpMetadata(IIOMetadata meta) {
- String format = meta.getNativeMetadataFormatName();
+ public static void dumpMetadata(IIOMetadata meta, boolean nativeFormat) {
+ String format;
+ if (nativeFormat) {
+ format = meta.getNativeMetadataFormatName();
+ } else {
+ format = IIOMetadataFormatImpl.standardMetadataFormatName;
+ }
Node node = meta.getAsTree(format);
dumpNode(node);
}
@@ -46,6 +53,7 @@
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
+ t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source src = new DOMSource(node);
Result res = new StreamResult(System.out);
t.transform(src, res);
---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org