001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ImageIOTIFFImageWriter.java 496561 2007-01-16 01:17:01Z cam $ */
019:
020: package org.apache.xmlgraphics.image.writer.imageio;
021:
022: import javax.imageio.metadata.IIOInvalidTreeException;
023: import javax.imageio.metadata.IIOMetadata;
024: import javax.imageio.metadata.IIOMetadataNode;
025:
026: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
027:
028: /**
029: * ImageWriter that encodes TIFF images using Image I/O.
030: *
031: * @version $Id: ImageIOTIFFImageWriter.java 496561 2007-01-16 01:17:01Z cam $
032: */
033: public class ImageIOTIFFImageWriter extends ImageIOImageWriter {
034:
035: private static final String SUN_TIFF_NATIVE_FORMAT = "com_sun_media_imageio_plugins_tiff_image_1.0";
036:
037: /**
038: * Main constructor.
039: */
040: public ImageIOTIFFImageWriter() {
041: super ("image/tiff");
042: }
043:
044: /**
045: * @see org.apache.xmlgraphics.image.writer.imageio.ImageIOImageWriter#updateMetadata(javax.imageio.metadata.IIOMetadata, org.apache.xmlgraphics.image.writer.ImageWriterParams)
046: */
047: protected IIOMetadata updateMetadata(IIOMetadata meta,
048: ImageWriterParams params) {
049: IIOMetadata ret = super .updateMetadata(meta, params);
050:
051: //We set the resolution manually using the native format since it appears that
052: //it doesn't work properly through the standard metadata. Haven't figured out why
053: //that happens.
054: if (params.getResolution() != null) {
055: if (SUN_TIFF_NATIVE_FORMAT.equals(meta
056: .getNativeMetadataFormatName())) {
057:
058: //IIOMetadataNode root = (IIOMetadataNode)meta.getAsTree(SUN_TIFF_NATIVE_FORMAT);
059: IIOMetadataNode root = new IIOMetadataNode(
060: SUN_TIFF_NATIVE_FORMAT);
061:
062: IIOMetadataNode ifd = getChildNode(root, "TIFFIFD");
063: if (ifd == null) {
064: ifd = new IIOMetadataNode("TIFFIFD");
065: ifd
066: .setAttribute("tagSets",
067: "com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet");
068: root.appendChild(ifd);
069: }
070: ifd.appendChild(createResolutionField(282,
071: "XResolution", params));
072: ifd.appendChild(createResolutionField(283,
073: "YResolution", params));
074:
075: //ResolutionUnit
076: IIOMetadataNode field, arrayNode, valueNode;
077: field = new IIOMetadataNode("TIFFField");
078: field.setAttribute("number", Integer.toString(296));
079: field.setAttribute("name", "ResolutionUnit");
080: arrayNode = new IIOMetadataNode("TIFFShorts");
081: field.appendChild(arrayNode);
082: valueNode = new IIOMetadataNode("TIFFShort");
083: valueNode.setAttribute("value", Integer.toString(3));
084: valueNode.setAttribute("description", "Centimeter");
085: arrayNode.appendChild(valueNode);
086:
087: try {
088: meta.mergeTree(SUN_TIFF_NATIVE_FORMAT, root);
089: } catch (IIOInvalidTreeException e) {
090: throw new RuntimeException(
091: "Cannot update image metadata: "
092: + e.getMessage(), e);
093: }
094: }
095: }
096:
097: return ret;
098: }
099:
100: private IIOMetadataNode createResolutionField(int number,
101: String name, ImageWriterParams params) {
102: IIOMetadataNode field, arrayNode, valueNode;
103: field = new IIOMetadataNode("TIFFField");
104: field.setAttribute("number", Integer.toString(number));
105: field.setAttribute("name", name);
106: arrayNode = new IIOMetadataNode("TIFFRationals");
107: field.appendChild(arrayNode);
108: valueNode = new IIOMetadataNode("TIFFRational");
109: arrayNode.appendChild(valueNode);
110:
111: // Set target resolution
112: float pixSzMM = 25.4f / params.getResolution().floatValue();
113: // num Pixs in 100 Meters
114: int numPix = (int) (((1000 * 100) / pixSzMM) + 0.5);
115: int denom = 100 * 100; // Centimeters per 100 Meters;
116: valueNode.setAttribute("value", numPix + "/" + denom);
117: return field;
118: }
119:
120: }
|