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 * EncoderUtil.java
029 * ----------------
030 * (C) Copyright 2004-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.encoders;
038
039import java.awt.image.BufferedImage;
040import java.io.IOException;
041import java.io.OutputStream;
042
043/**
044 * A collection of utility methods for encoding images and returning them as a
045 * byte[] or writing them directly to an OutputStream.
046 */
047public class EncoderUtil {
048
049    /**
050     * Encode the image in a specific format.
051     *
052     * @param image  The image to be encoded.
053     * @param format  The {@link ImageFormat} to use.
054     *
055     * @return The byte[] that is the encoded image.
056     * @throws IOException if there is an IO problem.
057     */
058    public static byte[] encode(BufferedImage image, String format)
059            throws IOException {
060        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
061        return imageEncoder.encode(image);
062    }
063
064    /**
065     * Encode the image in a specific format.
066     *
067     * @param image  The image to be encoded.
068     * @param format  The {@link ImageFormat} to use.
069     * @param encodeAlpha  Whether to encode alpha transparency (not supported
070     *                     by all ImageEncoders).
071     * @return The byte[] that is the encoded image.
072     * @throws IOException if there is an IO problem.
073     */
074    public static byte[] encode(BufferedImage image, String format,
075            boolean encodeAlpha) throws IOException {
076        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
077                encodeAlpha);
078        return imageEncoder.encode(image);
079    }
080
081    /**
082     * Encode the image in a specific format.
083     *
084     * @param image  The image to be encoded.
085     * @param format  The {@link ImageFormat} to use.
086     * @param quality  The quality to use for the image encoding (not supported
087     *                 by all ImageEncoders).
088     * @return The byte[] that is the encoded image.
089     * @throws IOException if there is an IO problem.
090     */
091    public static byte[] encode(BufferedImage image, String format,
092            float quality) throws IOException {
093        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
094                quality);
095        return imageEncoder.encode(image);
096    }
097
098    /**
099     * Encode the image in a specific format.
100     *
101     * @param image  The image to be encoded.
102     * @param format  The {@link ImageFormat} to use.
103     * @param quality  The quality to use for the image encoding (not supported
104     *                 by all ImageEncoders).
105     * @param encodeAlpha  Whether to encode alpha transparency (not supported
106     *                     by all ImageEncoders).
107     * @return The byte[] that is the encoded image.
108     * @throws IOException if there is an IO problem.
109     */
110    public static byte[] encode(BufferedImage image, String format,
111            float quality, boolean encodeAlpha) throws IOException {
112        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
113                quality, encodeAlpha);
114        return imageEncoder.encode(image);
115    }
116
117    /**
118     * Encode the image in a specific format and write it to an OutputStream.
119     *
120     * @param image  The image to be encoded.
121     * @param format  The {@link ImageFormat} to use.
122     * @param outputStream  The OutputStream to write the encoded image to.
123     * @throws IOException if there is an IO problem.
124     */
125    public static void writeBufferedImage(BufferedImage image, String format,
126            OutputStream outputStream) throws IOException {
127        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
128        imageEncoder.encode(image, outputStream);
129    }
130
131    /**
132     * Encode the image in a specific format and write it to an OutputStream.
133     *
134     * @param image  The image to be encoded.
135     * @param format  The {@link ImageFormat} to use.
136     * @param outputStream  The OutputStream to write the encoded image to.
137     * @param quality  The quality to use for the image encoding (not
138     *                 supported by all ImageEncoders).
139     * @throws IOException if there is an IO problem.
140     */
141    public static void writeBufferedImage(BufferedImage image, String format,
142        OutputStream outputStream, float quality) throws IOException {
143        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
144                quality);
145        imageEncoder.encode(image, outputStream);
146    }
147
148    /**
149     * Encode the image in a specific format and write it to an OutputStream.
150     *
151     * @param image  The image to be encoded.
152     * @param format  The {@link ImageFormat} to use.
153     * @param outputStream  The OutputStream to write the encoded image to.
154     * @param encodeAlpha  Whether to encode alpha transparency (not
155     *                     supported by all ImageEncoders).
156     * @throws IOException if there is an IO problem.
157     */
158    public static void writeBufferedImage(BufferedImage image, String format,
159            OutputStream outputStream, boolean encodeAlpha) throws IOException {
160        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
161                encodeAlpha);
162        imageEncoder.encode(image, outputStream);
163    }
164
165    /**
166     * Encode the image in a specific format and write it to an OutputStream.
167     *
168     * @param image  The image to be encoded.
169     * @param format  The {@link ImageFormat} to use.
170     * @param outputStream  The OutputStream to write the encoded image to.
171     * @param quality  The quality to use for the image encoding (not
172     *                 supported by all ImageEncoders).
173     * @param encodeAlpha  Whether to encode alpha transparency (not supported
174     *                     by all ImageEncoders).
175     * @throws IOException if there is an IO problem.
176     */
177    public static void writeBufferedImage(BufferedImage image, String format,
178            OutputStream outputStream, float quality, boolean encodeAlpha)
179            throws IOException {
180        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
181                quality, encodeAlpha);
182        imageEncoder.encode(image, outputStream);
183    }
184
185}