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