001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image.codec.imageio;
020:
021: import java.awt.image.RenderedImage;
022:
023: import javax.imageio.ImageWriteParam;
024: import javax.imageio.ImageWriter;
025: import javax.imageio.metadata.IIOInvalidTreeException;
026: import javax.imageio.metadata.IIOMetadata;
027: import javax.imageio.metadata.IIOMetadataNode;
028: import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
029:
030: import org.apache.batik.ext.awt.image.spi.ImageWriterParams;
031:
032: /**
033: * ImageWriter that encodes JPEG images using Image I/O.
034: *
035: * @version $Id: ImageIOJPEGImageWriter.java 502538 2007-02-02 08:52:56Z dvholten $
036: */
037: public class ImageIOJPEGImageWriter extends ImageIOImageWriter {
038:
039: private static final String JPEG_NATIVE_FORMAT = "javax_imageio_jpeg_image_1.0";
040:
041: /**
042: * Main constructor.
043: */
044: public ImageIOJPEGImageWriter() {
045: super ("image/jpeg");
046: }
047:
048: /**
049: * @see ImageIOImageWriter#updateMetadata(javax.imageio.metadata.IIOMetadata, ImageWriterParams)
050: */
051: protected IIOMetadata updateMetadata(IIOMetadata meta,
052: ImageWriterParams params) {
053: //ImageIODebugUtil.dumpMetadata(meta);
054: if (JPEG_NATIVE_FORMAT.equals(meta
055: .getNativeMetadataFormatName())) {
056: meta = addAdobeTransform(meta);
057:
058: IIOMetadataNode root = (IIOMetadataNode) meta
059: .getAsTree(JPEG_NATIVE_FORMAT);
060: //IIOMetadataNode root = new IIOMetadataNode(jpegmeta);
061:
062: IIOMetadataNode jv = getChildNode(root, "JPEGvariety");
063: if (jv == null) {
064: jv = new IIOMetadataNode("JPEGvariety");
065: root.appendChild(jv);
066: }
067: IIOMetadataNode child;
068: if (params.getResolution() != null) {
069: child = getChildNode(jv, "app0JFIF");
070: if (child == null) {
071: child = new IIOMetadataNode("app0JFIF");
072: jv.appendChild(child);
073: }
074: //JPEG gets special treatment because there seems to be a bug in
075: //the JPEG codec in ImageIO converting the pixel size incorrectly
076: //(or not at all) when using standard metadata format.
077: child.setAttribute("majorVersion", null);
078: child.setAttribute("minorVersion", null);
079: child.setAttribute("resUnits", "1"); //dots per inch
080: child.setAttribute("Xdensity", params.getResolution()
081: .toString());
082: child.setAttribute("Ydensity", params.getResolution()
083: .toString());
084: child.setAttribute("thumbWidth", null);
085: child.setAttribute("thumbHeight", null);
086:
087: }
088:
089: /*
090: IIOMetadataNode ms = getChildNode(root, "markerSequence");
091: if (ms == null) {
092: ms = new IIOMetadataNode("markerSequence");
093: root.appendChild(ms);
094: }*/
095:
096: try {
097: meta.setFromTree(JPEG_NATIVE_FORMAT, root);
098: //meta.mergeTree(JPEG_NATIVE_FORMAT, root);
099: } catch (IIOInvalidTreeException e) {
100: throw new RuntimeException(
101: "Cannot update image metadata: "
102: + e.getMessage(), e);
103: }
104:
105: //ImageIODebugUtil.dumpMetadata(meta);
106:
107: //meta = super.updateMetadata(meta, params);
108: //ImageIODebugUtil.dumpMetadata(meta);
109: }
110:
111: return meta;
112: }
113:
114: private static IIOMetadata addAdobeTransform(IIOMetadata meta) {
115: // add the adobe transformation (transform 1 -> to YCbCr)
116: IIOMetadataNode root = (IIOMetadataNode) meta
117: .getAsTree(JPEG_NATIVE_FORMAT);
118:
119: IIOMetadataNode markerSequence = getChildNode(root,
120: "markerSequence");
121: if (markerSequence == null) {
122: throw new RuntimeException("Invalid metadata!");
123: }
124:
125: IIOMetadataNode adobeTransform = getChildNode(markerSequence,
126: "app14Adobe");
127: if (adobeTransform == null) {
128: adobeTransform = new IIOMetadataNode("app14Adobe");
129: adobeTransform.setAttribute("transform", "1"); // convert RGB to YCbCr
130: adobeTransform.setAttribute("version", "101");
131: adobeTransform.setAttribute("flags0", "0");
132: adobeTransform.setAttribute("flags1", "0");
133:
134: markerSequence.appendChild(adobeTransform);
135: } else {
136: adobeTransform.setAttribute("transform", "1");
137: }
138:
139: try {
140: meta.setFromTree(JPEG_NATIVE_FORMAT, root);
141: } catch (IIOInvalidTreeException e) {
142: throw new RuntimeException("Cannot update image metadata: "
143: + e.getMessage(), e);
144: }
145: return meta;
146: }
147:
148: /**
149: * @see ImageIOImageWriter#getDefaultWriteParam(javax.imageio.ImageWriter, java.awt.image.RenderedImage, ImageWriterParams)
150: */
151: protected ImageWriteParam getDefaultWriteParam(
152: ImageWriter iiowriter, RenderedImage image,
153: ImageWriterParams params) {
154: JPEGImageWriteParam param = new JPEGImageWriteParam(iiowriter
155: .getLocale());
156: //ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
157: /*
158: ImageTypeSpecifier type = new ImageTypeSpecifier(
159: image.getColorModel(), image.getSampleModel());
160: */
161: /* didn't work as expected...
162: ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(
163: BufferedImage.TYPE_INT_RGB);
164: param.setDestinationType(type);
165: param.setSourceBands(new int[] {0, 1, 2});
166: */
167: return param;
168: }
169:
170: }
|