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