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",
069: "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
070: try {
071: // Test for being run under JDK 1.4+
072: Class.forName("javax.imageio.ImageIO");
073: // Test for JFreeChart being compiled under JDK 1.4+
074: Class
075: .forName("org.jfree.chart.encoders.SunPNGEncoderAdapter");
076: encoders.put("png",
077: "org.jfree.chart.encoders.SunPNGEncoderAdapter");
078: encoders.put("jpeg",
079: "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
080: } catch (ClassNotFoundException e) {
081: encoders
082: .put("png",
083: "org.jfree.chart.encoders.KeypointPNGEncoderAdapter");
084: }
085: }
086:
087: /**
088: * Used to set additional encoders or replace default ones.
089: *
090: * @param format The image format name.
091: * @param imageEncoderClassName The name of the ImageEncoder class.
092: */
093: public static void setImageEncoder(String format,
094: String imageEncoderClassName) {
095: encoders.put(format, imageEncoderClassName);
096: }
097:
098: /**
099: * Used to retrieve an ImageEncoder for a specific image format.
100: *
101: * @param format The image format required.
102: *
103: * @return The ImageEncoder or <code>null</code> if none available.
104: */
105: public static ImageEncoder newInstance(String format) {
106: ImageEncoder imageEncoder = null;
107: String className = (String) encoders.get(format);
108: if (className == null) {
109: throw new IllegalArgumentException(
110: "Unsupported image format - " + format);
111: }
112: try {
113: Class imageEncoderClass = Class.forName(className);
114: imageEncoder = (ImageEncoder) imageEncoderClass
115: .newInstance();
116: } catch (Exception e) {
117: throw new IllegalArgumentException(e.toString());
118: }
119: return imageEncoder;
120: }
121:
122: /**
123: * Used to retrieve an ImageEncoder for a specific image format.
124: *
125: * @param format The image format required.
126: * @param quality The quality to be set before returning.
127: *
128: * @return The ImageEncoder or <code>null</code> if none available.
129: */
130: public static ImageEncoder newInstance(String format, float quality) {
131: ImageEncoder imageEncoder = newInstance(format);
132: imageEncoder.setQuality(quality);
133: return imageEncoder;
134: }
135:
136: /**
137: * Used to retrieve an ImageEncoder for a specific image format.
138: *
139: * @param format The image format required.
140: * @param encodingAlpha Sets whether alpha transparency should be encoded.
141: *
142: * @return The ImageEncoder or <code>null</code> if none available.
143: */
144: public static ImageEncoder newInstance(String format,
145: boolean encodingAlpha) {
146: ImageEncoder imageEncoder = newInstance(format);
147: imageEncoder.setEncodingAlpha(encodingAlpha);
148: return imageEncoder;
149: }
150:
151: /**
152: * Used to retrieve an ImageEncoder for a specific image format.
153: *
154: * @param format The image format required.
155: * @param quality The quality to be set before returning.
156: * @param encodingAlpha Sets whether alpha transparency should be encoded.
157: *
158: * @return The ImageEncoder or <code>null</code> if none available.
159: */
160: public static ImageEncoder newInstance(String format,
161: float quality, boolean encodingAlpha) {
162: ImageEncoder imageEncoder = newInstance(format);
163: imageEncoder.setQuality(quality);
164: imageEncoder.setEncodingAlpha(encodingAlpha);
165: return imageEncoder;
166: }
167:
168: }
|