001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * JPEGEncoderAdapter.java
029 * --------------------------
030 * (C) Copyright 2004-2008, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 01-Aug-2004 : Initial version (RA);
038 * 01-Nov-2005 : To remove the dependency on non-supported APIs, use ImageIO
039 *               instead of com.sun.image.codec.jpeg.JPEGImageEncoder - this
040 *               adapter will only be available on JDK 1.4 or later (DG);
041 * ------------- JFREECHART 1.0.x ---------------------------------------------
042 * 20-Jul-2006 : Pass quality setting to ImageIO. Also increased default
043 *               value to 0.95 (DG);
044 *
045 */
046
047package jahuwaldt.image.encoders;
048
049import java.awt.image.BufferedImage;
050import java.io.ByteArrayOutputStream;
051import java.io.IOException;
052import java.io.OutputStream;
053import java.util.Iterator;
054
055import javax.imageio.IIOImage;
056import javax.imageio.ImageIO;
057import javax.imageio.ImageWriteParam;
058import javax.imageio.ImageWriter;
059import javax.imageio.stream.ImageOutputStream;
060
061/**
062 * Adapter class for the Sun JPEG Encoder.
063 */
064public class JPEGEncoderAdapter implements ImageEncoder {
065
066    /** The quality setting (in the range 0.0f to 1.0f). */
067    private float quality = 0.95f;
068
069    /**
070     * Creates a new <code>JPEGEncoderAdapter</code> instance.
071     */
072    public JPEGEncoderAdapter() {
073    }
074
075    /**
076     * Returns the quality of the image encoding, which is a number in the
077     * range 0.0f to 1.0f (higher values give better quality output, but larger
078     * file sizes).  The default value is 0.95f.
079     *
080     * @return A float representing the quality, in the range 0.0f to 1.0f.
081     *
082     * @see #setQuality(float)
083     */
084    @Override
085    public float getQuality() {
086        return this.quality;
087    }
088
089    /**
090     * Set the quality of the image encoding.
091     *
092     * @param quality  A float representing the quality (in the range 0.0f to
093     *     1.0f).
094     *
095     * @see #getQuality()
096     */
097    @Override
098    public void setQuality(float quality) {
099        if (quality < 0.0f || quality > 1.0f) {
100            throw new IllegalArgumentException(
101                    "The 'quality' must be in the range 0.0f to 1.0f");
102        }
103        this.quality = quality;
104    }
105
106    /**
107     * Returns <code>false</code> always, indicating that this encoder does not
108     * encode alpha transparency.
109     *
110     * @return <code>false</code>.
111     */
112    @Override
113    public boolean isEncodingAlpha() {
114        return false;
115    }
116
117    /**
118     * Set whether the encoder should encode alpha transparency (this is not
119     * supported for JPEG, so this method does nothing).
120     *
121     * @param encodingAlpha  ignored.
122     */
123    @Override
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    @Override
141    public byte[] encode(BufferedImage bufferedImage) throws IOException {
142        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
143        encode(bufferedImage, outputStream);
144        return outputStream.toByteArray();
145    }
146
147    /**
148     * Encodes an image in JPEG format and writes it to an output stream.
149     *
150     * @param bufferedImage  the image to be encoded (<code>null</code> not
151     *     permitted).
152     * @param outputStream  the OutputStream to write the encoded image to
153     *     (<code>null</code> not permitted).
154     *
155     * @throws IOException if there is an I/O problem.
156     * @throws NullPointerException if <code>bufferedImage</code> is
157     *     <code>null</code>.
158     */
159    @Override
160    public void encode(BufferedImage bufferedImage, OutputStream outputStream)
161            throws IOException {
162        if (bufferedImage == null) {
163            throw new IllegalArgumentException("Null 'bufferedImage' argument.");
164        }
165        if (outputStream == null) {
166            throw new IllegalArgumentException("Null 'outputStream' argument.");
167        }
168        Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
169        ImageWriter writer = (ImageWriter) iterator.next();
170        ImageWriteParam p = writer.getDefaultWriteParam();
171        p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
172        p.setCompressionQuality(this.quality);
173        ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
174        writer.setOutput(ios);
175        writer.write(null, new IIOImage(bufferedImage, null, null), p);
176        ios.flush();
177        writer.dispose();
178        ios.close();
179    }
180
181}