    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     * Changes
    036     * -------
    037     * 01-Aug-2004 : Initial version (RA);
    038     * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
    039     *
    040     */
    041
    042    package org.jfree.chart.encoders;
    043
    044    import java.awt.image.BufferedImage;
    045    import java.io.IOException;
    046    import 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     */
    052    public 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
    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
    078         */
    079        public static byte[] encode(BufferedImage image, String format,
    080                                    boolean encodeAlpha) throws IOException {
    081            ImageEncoder imageEncoder
    082                = ImageEncoderFactory.newInstance(format, 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
    095         */
    096        public static byte[] encode(BufferedImage image, String format,
    097                                    float quality) throws IOException {
    098            ImageEncoder imageEncoder
    099                = ImageEncoderFactory.newInstance(format, 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
    114         */
    115        public static byte[] encode(BufferedImage image, String format,
    116                                    float quality, boolean encodeAlpha)
    117            throws IOException {
    118            ImageEncoder imageEncoder
    119                = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
    120            return imageEncoder.encode(image);
    121        }
    122
    123        /**
    124         * Encode the image in a specific format and write it to an OutputStream.
    125         *
    126         * @param image  The image to be encoded.
    127         * @param format  The {@link ImageFormat} to use.
    128         * @param outputStream  The OutputStream to write the encoded image to.
    129         * @throws IOException
    130         */
    131        public static void writeBufferedImage(BufferedImage image, String format,
    132            OutputStream outputStream) throws IOException {
    133            ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
    134            imageEncoder.encode(image, outputStream);
    135        }
    136
    137        /**
    138         * Encode the image in a specific format and write it to an OutputStream.
    139         *
    140         * @param image  The image to be encoded.
    141         * @param format  The {@link ImageFormat} to use.
    142         * @param outputStream  The OutputStream to write the encoded image to.
    143         * @param quality  The quality to use for the image encoding (not
    144         *                 supported by all ImageEncoders).
    145         * @throws IOException
    146         */
    147        public static void writeBufferedImage(BufferedImage image, String format,
    148            OutputStream outputStream, float quality) throws IOException {
    149            ImageEncoder imageEncoder
    150                = ImageEncoderFactory.newInstance(format, quality);
    151            imageEncoder.encode(image, outputStream);
    152        }
    153
    154        /**
    155         * Encode the image in a specific format and write it to an OutputStream.
    156         *
    157         * @param image  The image to be encoded.
    158         * @param format  The {@link ImageFormat} to use.
    159         * @param outputStream  The OutputStream to write the encoded image to.
    160         * @param encodeAlpha  Whether to encode alpha transparency (not
    161         *                     supported by all ImageEncoders).
    162         * @throws IOException
    163         */
    164        public static void writeBufferedImage(BufferedImage image, String format,
    165            OutputStream outputStream, boolean encodeAlpha) throws IOException {
    166            ImageEncoder imageEncoder
    167                = ImageEncoderFactory.newInstance(format, encodeAlpha);
    168            imageEncoder.encode(image, outputStream);
    169        }
    170
    171        /**
    172         * Encode the image in a specific format and write it to an OutputStream.
    173         *
    174         * @param image  The image to be encoded.
    175         * @param format  The {@link ImageFormat} to use.
    176         * @param outputStream  The OutputStream to write the encoded image to.
    177         * @param quality  The quality to use for the image encoding (not
    178         *                 supported by all ImageEncoders).
    179         * @param encodeAlpha  Whether to encode alpha transparency (not supported
    180         *                     by all ImageEncoders).
    181         * @throws IOException
    182         */
    183        public static void writeBufferedImage(BufferedImage image, String format,
    184            OutputStream outputStream, float quality, boolean encodeAlpha)
    185            throws IOException {
    186            ImageEncoder imageEncoder
    187                = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
    188            imageEncoder.encode(image, outputStream);
    189        }
    190
    191    }