    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     * ImageEncoderFactory.java
    029     * ------------------------
    030     * (C) Copyright 2004-2007, by Richard Atkinson and Contributors.
    031     *
    032     * Original Author:  Richard Atkinson;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * $Id: ImageEncoderFactory.java,v 1.3.2.3 2007/02/02 14:51:22 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 01-Aug-2004 : Initial version (RA);
    040     * 01-Nov-2005 : Now using ImageIO for JPEG encoding, so we no longer have a
    041     *               dependency on com.sun.* which isn't available on all
    042     *               implementations (DG);
    043     * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
    044     *
    045     */
    046
    047    package org.jfree.chart.encoders;
    048
    049    import java.util.Hashtable;
    050
    051    /**
    052     * Factory class for returning {@link ImageEncoder}s for different
    053     * {@link ImageFormat}s.
    054     */
    055    public class ImageEncoderFactory {
    056        private static Hashtable encoders = null;
    057
    058        static {
    059            init();
    060        }
    061
    062        /**
    063         * Sets up default encoders (uses Sun PNG Encoder if JDK 1.4+ and the
    064         * SunPNGEncoderAdapter class is available).
    065         */
    066        private static void init() {
    067            encoders = new Hashtable();
    068            encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
    069            try {
    070                //  Test for being run under JDK 1.4+
    071                Class.forName("javax.imageio.ImageIO");
    072                //  Test for JFreeChart being compiled under JDK 1.4+
    073                Class.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter");
    074                encoders.put("png",
    075                        "org.jfree.chart.encoders.SunPNGEncoderAdapter");
    076                encoders.put("jpeg",
    077                        "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
    078            }
    079            catch (ClassNotFoundException e) {
    080                encoders.put("png",
    081                        "org.jfree.chart.encoders.KeypointPNGEncoderAdapter");
    082            }
    083        }
    084
    085        /**
    086         * Used to set additional encoders or replace default ones.
    087         *
    088         * @param format  The image format name.
    089         * @param imageEncoderClassName  The name of the ImageEncoder class.
    090         */
    091        public static void setImageEncoder(String format,
    092                                           String imageEncoderClassName) {
    093            encoders.put(format, imageEncoderClassName);
    094        }
    095
    096        /**
    097         * Used to retrieve an ImageEncoder for a specific image format.
    098         *
    099         * @param format  The image format required.
    100         *
    101         * @return The ImageEncoder or <code>null</code> if none available.
    102         */
    103        public static ImageEncoder newInstance(String format) {
    104            ImageEncoder imageEncoder = null;
    105            String className = (String) encoders.get(format);
    106            if (className == null) {
    107                throw new IllegalArgumentException("Unsupported image format - "
    108                        + format);
    109            }
    110            try {
    111                Class imageEncoderClass = Class.forName(className);
    112                imageEncoder = (ImageEncoder) imageEncoderClass.newInstance();
    113            }
    114            catch (Exception e) {
    115                throw new IllegalArgumentException(e.toString());
    116            }
    117            return imageEncoder;
    118        }
    119
    120        /**
    121         * Used to retrieve an ImageEncoder for a specific image format.
    122         *
    123         * @param format  The image format required.
    124         * @param quality  The quality to be set before returning.
    125         *
    126         * @return The ImageEncoder or <code>null</code> if none available.
    127         */
    128        public static ImageEncoder newInstance(String format, float quality) {
    129            ImageEncoder imageEncoder = newInstance(format);
    130            imageEncoder.setQuality(quality);
    131            return imageEncoder;
    132        }
    133
    134        /**
    135         * Used to retrieve an ImageEncoder for a specific image format.
    136         *
    137         * @param format  The image format required.
    138         * @param encodingAlpha  Sets whether alpha transparency should be encoded.
    139         *
    140         * @return The ImageEncoder or <code>null</code> if none available.
    141         */
    142        public static ImageEncoder newInstance(String format,
    143                                               boolean encodingAlpha) {
    144            ImageEncoder imageEncoder = newInstance(format);
    145            imageEncoder.setEncodingAlpha(encodingAlpha);
    146            return imageEncoder;
    147        }
    148
    149        /**
    150         * Used to retrieve an ImageEncoder for a specific image format.
    151         *
    152         * @param format  The image format required.
    153         * @param quality  The quality to be set before returning.
    154         * @param encodingAlpha  Sets whether alpha transparency should be encoded.
    155         *
    156         * @return The ImageEncoder or <code>null</code> if none available.
    157         */
    158        public static ImageEncoder newInstance(String format, float quality,
    159                                               boolean encodingAlpha) {
    160            ImageEncoder imageEncoder = newInstance(format);
    161            imageEncoder.setQuality(quality);
    162            imageEncoder.setEncodingAlpha(encodingAlpha);
    163            return imageEncoder;
    164        }
    165
    166    }