001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------------------
028 * SunJPEGEncoderAdapter.java
029 * --------------------------
030 * (C) Copyright 2004-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   David Gilbert;
034 *
035 */
036
037package org.jfree.chart.encoders;
038
039import java.awt.image.BufferedImage;
040import java.io.ByteArrayOutputStream;
041import java.io.IOException;
042import java.io.OutputStream;
043import java.util.Iterator;
044
045import javax.imageio.IIOImage;
046import javax.imageio.ImageIO;
047import javax.imageio.ImageWriteParam;
048import javax.imageio.ImageWriter;
049import javax.imageio.stream.ImageOutputStream;
050import org.jfree.chart.util.Args;
051
052/**
053 * Adapter class for the Sun JPEG Encoder.  The {@link ImageEncoderFactory}
054 * will only return a reference to this class by default if the library has
055 * been compiled under a JDK 1.4+ and is being run using a JRE 1.4+.
056 */
057public class SunJPEGEncoderAdapter implements ImageEncoder {
058
059    /** The quality setting (in the range 0.0f to 1.0f). */
060    private float quality = 0.95f;
061
062    /**
063     * Creates a new {@code SunJPEGEncoderAdapter} instance.
064     */
065    public SunJPEGEncoderAdapter() {
066    }
067
068    /**
069     * Returns the quality of the image encoding, which is a number in the
070     * range 0.0f to 1.0f (higher values give better quality output, but larger
071     * file sizes).  The default value is 0.95f.
072     *
073     * @return A float representing the quality, in the range 0.0f to 1.0f.
074     *
075     * @see #setQuality(float)
076     */
077    @Override
078    public float getQuality() {
079        return this.quality;
080    }
081
082    /**
083     * Set the quality of the image encoding.
084     *
085     * @param quality  A float representing the quality (in the range 0.0f to
086     *     1.0f).
087     *
088     * @see #getQuality()
089     */
090    @Override
091    public void setQuality(float quality) {
092        if (quality < 0.0f || quality > 1.0f) {
093            throw new IllegalArgumentException(
094                    "The 'quality' must be in the range 0.0f to 1.0f");
095        }
096        this.quality = quality;
097    }
098
099    /**
100     * Returns {@code false} always, indicating that this encoder does not
101     * encode alpha transparency.
102     *
103     * @return {@code false}.
104     */
105    @Override
106    public boolean isEncodingAlpha() {
107        return false;
108    }
109
110    /**
111     * Set whether the encoder should encode alpha transparency (this is not
112     * supported for JPEG, so this method does nothing).
113     *
114     * @param encodingAlpha  ignored.
115     */
116    @Override
117    public void setEncodingAlpha(boolean encodingAlpha) {
118        //  No op
119    }
120
121    /**
122     * Encodes an image in JPEG format.
123     *
124     * @param bufferedImage  the image to be encoded ({@code null} not
125     *     permitted).
126     *
127     * @return The byte[] that is the encoded image.
128     *
129     * @throws IOException if there is an I/O problem.
130     * @throws NullPointerException if {@code bufferedImage} is
131     *     {@code null}.
132     */
133    @Override
134    public byte[] encode(BufferedImage bufferedImage) throws IOException {
135        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
136        encode(bufferedImage, outputStream);
137        return outputStream.toByteArray();
138    }
139
140    /**
141     * Encodes an image in JPEG format and writes it to an output stream.
142     *
143     * @param bufferedImage  the image to be encoded ({@code null} not
144     *     permitted).
145     * @param outputStream  the OutputStream to write the encoded image to
146     *     ({@code null} not permitted).
147     *
148     * @throws IOException if there is an I/O problem.
149     * @throws NullPointerException if {@code bufferedImage} is {@code null}.
150     */
151    @Override
152    public void encode(BufferedImage bufferedImage, OutputStream outputStream)
153            throws IOException {
154        Args.nullNotPermitted(bufferedImage, "bufferedImage");
155        Args.nullNotPermitted(outputStream, "outputStream");
156        Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg");
157        ImageWriter writer = (ImageWriter) iterator.next();
158        ImageWriteParam p = writer.getDefaultWriteParam();
159        p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
160        p.setCompressionQuality(this.quality);
161        ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
162        writer.setOutput(ios);
163        writer.write(null, new IIOImage(bufferedImage, null, null), p);
164        ios.flush();
165        writer.dispose();
166        ios.close();
167    }
168
169}