001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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    private EncoderUtil() {
050        // no requirement to instantiate
051    }
052
053    /**
054     * Encode the image in a specific format.
055     *
056     * @param image  The image to be encoded.
057     * @param format  The {@link ImageFormat} to use.
058     *
059     * @return The byte[] that is the encoded image.
060     * @throws IOException if there is an IO problem.
061     */
062    public static byte[] encode(BufferedImage image, String format)
063            throws IOException {
064        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
065        return imageEncoder.encode(image);
066    }
067
068    /**
069     * Encode the image in a specific format.
070     *
071     * @param image  The image to be encoded.
072     * @param format  The {@link ImageFormat} to use.
073     * @param encodeAlpha  Whether to encode alpha transparency (not supported
074     *                     by all ImageEncoders).
075     * @return The byte[] that is the encoded image.
076     * @throws IOException if there is an IO problem.
077     */
078    public static byte[] encode(BufferedImage image, String format,
079            boolean encodeAlpha) throws IOException {
080        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
081                encodeAlpha);
082        return imageEncoder.encode(image);
083    }
084
085    /**
086     * Encode the image in a specific format.
087     *
088     * @param image  The image to be encoded.
089     * @param format  The {@link ImageFormat} to use.
090     * @param quality  The quality to use for the image encoding (not supported
091     *                 by all ImageEncoders).
092     * @return The byte[] that is the encoded image.
093     * @throws IOException if there is an IO problem.
094     */
095    public static byte[] encode(BufferedImage image, String format,
096            float quality) throws IOException {
097        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format,
098                quality);
099        return imageEncoder.encode(image);
100    }
101
102    /**
103     * Encode the image in a specific format.
104     *
105     * @param image  The image to be encoded.
106     * @param format  The {@link ImageFormat} to use.
107     * @param quality  The quality to use for the image encoding (not supported
108     *                 by all ImageEncoders).
109     * @param encodeAlpha  Whether to encode alpha transparency (not supported
110     *                     by all ImageEncoders).
111     * @return The byte[] that is the encoded image.
112     * @throws IOException if there is an IO problem.
113     */
114    public static byte[] encode(BufferedImage image, String format,
115            float quality, boolean encodeAlpha) throws IOException {
116        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, 
117                quality, encodeAlpha);
118        return imageEncoder.encode(image);
119    }
120
121    /**
122     * Encode the image in a specific format and write it to an OutputStream.
123     *
124     * @param image  The image to be encoded.
125     * @param format  The {@link ImageFormat} to use.
126     * @param outputStream  The OutputStream to write the encoded image to.
127     * @throws IOException if there is an IO problem.
128     */
129    public static void writeBufferedImage(BufferedImage image, String format,
130            OutputStream outputStream) throws IOException {
131        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
132        imageEncoder.encode(image, outputStream);
133    }
134
135    /**
136     * Encode the image in a specific format and write it to an OutputStream.
137     *
138     * @param image  The image to be encoded.
139     * @param format  The {@link ImageFormat} to use.
140     * @param outputStream  The OutputStream to write the encoded image to.
141     * @param quality  The quality to use for the image encoding (not
142     *                 supported by all ImageEncoders).
143     * @throws IOException if there is an IO problem.
144     */
145    public static void writeBufferedImage(BufferedImage image, String format,
146        OutputStream outputStream, float quality) throws IOException {
147        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, quality);
148        imageEncoder.encode(image, outputStream);
149    }
150
151    /**
152     * Encode the image in a specific format and write it to an OutputStream.
153     *
154     * @param image  The image to be encoded.
155     * @param format  The {@link ImageFormat} to use.
156     * @param outputStream  The OutputStream to write the encoded image to.
157     * @param encodeAlpha  Whether to encode alpha transparency (not
158     *                     supported by all ImageEncoders).
159     * @throws IOException if there is an IO problem.
160     */
161    public static void writeBufferedImage(BufferedImage image, String format,
162            OutputStream outputStream, boolean encodeAlpha) throws IOException {
163        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, encodeAlpha);
164        imageEncoder.encode(image, outputStream);
165    }
166
167    /**
168     * Encode the image in a specific format and write it to an OutputStream.
169     *
170     * @param image  The image to be encoded.
171     * @param format  The {@link ImageFormat} to use.
172     * @param outputStream  The OutputStream to write the encoded image to.
173     * @param quality  The quality to use for the image encoding (not
174     *                 supported by all ImageEncoders).
175     * @param encodeAlpha  Whether to encode alpha transparency (not supported
176     *                     by all ImageEncoders).
177     * @throws IOException if there is an IO problem.
178     */
179    public static void writeBufferedImage(BufferedImage image, String format,
180            OutputStream outputStream, float quality, boolean encodeAlpha)
181            throws IOException {
182        ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
183        imageEncoder.encode(image, outputStream);
184    }
185
186}