001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * --------------------------
028 * SunJPEGEncoderAdapter.java
029 * --------------------------
030 * (C) Copyright 2004-2006, by Richard Atkinson and Contributors.
031 *
032 * Original Author: Richard Atkinson;
033 * Contributor(s): David Gilbert (for Object Refinery Limited);
034 *
035 * $Id: SunJPEGEncoderAdapter.java,v 1.3.2.3 2006/07/20 14:27:00 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 01-Aug-2004 : Initial version (RA);
040 * 01-Nov-2005 : To remove the dependency on non-supported APIs, use ImageIO
041 * instead of com.sun.image.codec.jpeg.JPEGImageEncoder - this
042 * adapter will only be available on JDK 1.4 or later (DG);
043 * ------------- JFREECHART 1.0.0 ---------------------------------------------
044 * 20-Jul-2006 : Pass quality setting to ImageIO. Also increased default
045 * value to 0.95 (DG);
046 *
047 */
048
049 package org.jfree.chart.encoders;
050
051 import java.awt.image.BufferedImage;
052 import java.io.ByteArrayOutputStream;
053 import java.io.IOException;
054 import java.io.OutputStream;
055 import java.util.Iterator;
056
057 import javax.imageio.IIOImage;
058 import javax.imageio.ImageIO;
059 import javax.imageio.ImageWriteParam;
060 import javax.imageio.ImageWriter;
061 import javax.imageio.stream.ImageOutputStream;
062
063 /**
064 * Adapter class for the Sun JPEG Encoder. The {@link ImageEncoderFactory}
065 * will only return a reference to this class by default if the library has
066 * been compiled under a JDK 1.4+ and is being run using a JRE 1.4+.
067 */
068 public class SunJPEGEncoderAdapter implements ImageEncoder {
069
070 /** The quality setting (in the range 0.0f to 1.0f). */
071 private float quality = 0.95f;
072
073 /**
074 * Creates a new <code>SunJPEGEncoderAdapter</code> instance.
075 */
076 public SunJPEGEncoderAdapter() {
077 }
078
079 /**
080 * Returns the quality of the image encoding, which is a number in the
081 * range 0.0f to 1.0f (higher values give better quality output, but larger
082 * file sizes). The default value is 0.95f.
083 *
084 * @return A float representing the quality, in the range 0.0f to 1.0f.
085 *
086 * @see #setQuality(float)
087 */
088 public float getQuality() {
089 return this.quality;
090 }
091
092 /**
093 * Set the quality of the image encoding.
094 *
095 * @param quality A float representing the quality (in the range 0.0f to
096 * 1.0f).
097 *
098 * @see #getQuality()
099 */
100 public void setQuality(float quality) {
101 if (quality < 0.0f || quality > 1.0f) {
102 throw new IllegalArgumentException(
103 "The 'quality' must be in the range 0.0f to 1.0f");
104 }
105 this.quality = quality;
106 }
107
108 /**
109 * Returns <code>false</code> always, indicating that this encoder does not
110 * encode alpha transparency.
111 *
112 * @return <code>false</code>.
113 */
114 public boolean isEncodingAlpha() {
115 return false;
116 }
117
118 /**
119 * Set whether the encoder should encode alpha transparency (this is not
120 * supported for JPEG, so this method does nothing).
121 *
122 * @param encodingAlpha ignored.
123 */
124 public void setEncodingAlpha(boolean encodingAlpha) {
125 // No op
126 }
127
128 /**
129 * Encodes an image in JPEG format.
130 *
131 * @param bufferedImage the image to be encoded (<code>null</code> not
132 * permitted).
133 *
134 * @return The byte[] that is the encoded image.
135 *
136 * @throws IOException if there is an I/O problem.
137 * @throws NullPointerException if <code>bufferedImage</code> is
138 * <code>null</code>.
139 */
140 public byte[] encode(BufferedImage bufferedImage) throws IOException {
141 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
142 encode(bufferedImage, outputStream);
143 return outputStream.toByteArray();
144 }
145
146 /**
147 * Encodes an image in JPEG format and writes it to an output stream.
148 *
149 * @param bufferedImage the image to be encoded (<code>null</code> not
150 * permitted).
151 * @param outputStream the OutputStream to write the encoded image to
152 * (<code>null</code> not permitted).
153 *
154 * @throws IOException if there is an I/O problem.
155 * @throws NullPointerException if <code>bufferedImage</code> is
156 * <code>null</code>.
157 */
158 public void encode(BufferedImage bufferedImage, OutputStream outputStream)
159 throws IOException {
160 if (bufferedImage == null) {
161 throw new IllegalArgumentException("Null 'image' argument.");
162 }
163 if (outputStream == null) {
164 throw new IllegalArgumentException("Null 'outputStream' argument.");
165 }
166 Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
167 ImageWriter writer = (ImageWriter) iterator.next();
168 ImageWriteParam p = writer.getDefaultWriteParam();
169 p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
170 p.setCompressionQuality(this.quality);
171 ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
172 writer.setOutput(ios);
173 writer.write(null, new IIOImage(bufferedImage, null, null), p);
174 ios.flush();
175 writer.dispose();
176 ios.close();
177 }
178
179 }