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