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: ImageIOJPEGImageWriter.java 496561 2007-01-16 01:17:01Z cam $ */
019:
020: package org.apache.xmlgraphics.image.writer.imageio;
021:
022: import java.awt.image.RenderedImage;
023:
024: import javax.imageio.ImageWriteParam;
025: import javax.imageio.ImageWriter;
026: import javax.imageio.metadata.IIOInvalidTreeException;
027: import javax.imageio.metadata.IIOMetadata;
028: import javax.imageio.metadata.IIOMetadataNode;
029: import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
030:
031: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
032:
033: /**
034: * ImageWriter that encodes JPEG images using Image I/O.
035: *
036: * @version $Id: ImageIOJPEGImageWriter.java 496561 2007-01-16 01:17:01Z cam $
037: */
038: public class ImageIOJPEGImageWriter extends ImageIOImageWriter {
039:
040: private static final String JPEG_NATIVE_FORMAT = "javax_imageio_jpeg_image_1.0";
041:
042: /**
043: * Main constructor.
044: */
045: public ImageIOJPEGImageWriter() {
046: super ("image/jpeg");
047: }
048:
049: /**
050: * @see ImageIOImageWriter#updateMetadata(javax.imageio.metadata.IIOMetadata, ImageWriterParams)
051: */
052: protected IIOMetadata updateMetadata(IIOMetadata meta,
053: ImageWriterParams params) {
054: //ImageIODebugUtil.dumpMetadata(meta);
055: if (JPEG_NATIVE_FORMAT.equals(meta
056: .getNativeMetadataFormatName())) {
057: meta = addAdobeTransform(meta);
058:
059: IIOMetadataNode root = (IIOMetadataNode) meta
060: .getAsTree(JPEG_NATIVE_FORMAT);
061: //IIOMetadataNode root = new IIOMetadataNode(jpegmeta);
062:
063: IIOMetadataNode jv = getChildNode(root, "JPEGvariety");
064: if (jv == null) {
065: jv = new IIOMetadataNode("JPEGvariety");
066: root.appendChild(jv);
067: }
068: IIOMetadataNode child;
069: if (params.getResolution() != null) {
070: child = getChildNode(jv, "app0JFIF");
071: if (child == null) {
072: child = new IIOMetadataNode("app0JFIF");
073: jv.appendChild(child);
074: }
075: //JPEG gets special treatment because there seems to be a bug in
076: //the JPEG codec in ImageIO converting the pixel size incorrectly
077: //(or not at all) when using standard metadata format.
078: child.setAttribute("majorVersion", null);
079: child.setAttribute("minorVersion", null);
080: child.setAttribute("resUnits", "1"); //dots per inch
081: child.setAttribute("Xdensity", params.getResolution()
082: .toString());
083: child.setAttribute("Ydensity", params.getResolution()
084: .toString());
085: child.setAttribute("thumbWidth", null);
086: child.setAttribute("thumbHeight", null);
087:
088: }
089:
090: /*
091: IIOMetadataNode ms = getChildNode(root, "markerSequence");
092: if (ms == null) {
093: ms = new IIOMetadataNode("markerSequence");
094: root.appendChild(ms);
095: }*/
096:
097: try {
098: meta.setFromTree(JPEG_NATIVE_FORMAT, root);
099: //meta.mergeTree(JPEG_NATIVE_FORMAT, root);
100: } catch (IIOInvalidTreeException e) {
101: throw new RuntimeException(
102: "Cannot update image metadata: "
103: + e.getMessage(), e);
104: }
105:
106: //ImageIODebugUtil.dumpMetadata(meta);
107:
108: //meta = super.updateMetadata(meta, params);
109: //ImageIODebugUtil.dumpMetadata(meta);
110: }
111:
112: return meta;
113: }
114:
115: private static IIOMetadata addAdobeTransform(IIOMetadata meta) {
116: // add the adobe transformation (transform 1 -> to YCbCr)
117: IIOMetadataNode root = (IIOMetadataNode) meta
118: .getAsTree(JPEG_NATIVE_FORMAT);
119:
120: IIOMetadataNode markerSequence = getChildNode(root,
121: "markerSequence");
122: if (markerSequence == null) {
123: throw new RuntimeException("Invalid metadata!");
124: }
125:
126: IIOMetadataNode adobeTransform = getChildNode(markerSequence,
127: "app14Adobe");
128: if (adobeTransform == null) {
129: adobeTransform = new IIOMetadataNode("app14Adobe");
130: adobeTransform.setAttribute("transform", "1"); // convert RGB to YCbCr
131: adobeTransform.setAttribute("version", "101");
132: adobeTransform.setAttribute("flags0", "0");
133: adobeTransform.setAttribute("flags1", "0");
134:
135: markerSequence.appendChild(adobeTransform);
136: } else {
137: adobeTransform.setAttribute("transform", "1");
138: }
139:
140: try {
141: meta.setFromTree(JPEG_NATIVE_FORMAT, root);
142: } catch (IIOInvalidTreeException e) {
143: throw new RuntimeException("Cannot update image metadata: "
144: + e.getMessage(), e);
145: }
146: return meta;
147: }
148:
149: /**
150: * @see ImageIOImageWriter#getDefaultWriteParam(javax.imageio.ImageWriter, java.awt.image.RenderedImage, ImageWriterParams)
151: */
152: protected ImageWriteParam getDefaultWriteParam(
153: ImageWriter iiowriter, RenderedImage image,
154: ImageWriterParams params) {
155: JPEGImageWriteParam param = new JPEGImageWriteParam(iiowriter
156: .getLocale());
157: //ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
158: /*
159: ImageTypeSpecifier type = new ImageTypeSpecifier(
160: image.getColorModel(), image.getSampleModel());
161: */
162: /* didn't work as expected...
163: ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(
164: BufferedImage.TYPE_INT_RGB);
165: param.setDestinationType(type);
166: param.setSourceBands(new int[] {0, 1, 2});
167: */
168: return param;
169: }
170:
171: }
|