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