Source for org.jfree.chart.encoders.ImageEncoderFactory

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ------------------------
  28:  * ImageEncoderFactory.java
  29:  * ------------------------
  30:  * (C) Copyright 2004-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ImageEncoderFactory.java,v 1.3.2.3 2007/02/02 14:51:22 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 01-Aug-2004 : Initial version (RA);
  40:  * 01-Nov-2005 : Now using ImageIO for JPEG encoding, so we no longer have a
  41:  *               dependency on com.sun.* which isn't available on all 
  42:  *               implementations (DG);
  43:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.chart.encoders;
  48: 
  49: import java.util.Hashtable;
  50: 
  51: /**
  52:  * Factory class for returning {@link ImageEncoder}s for different 
  53:  * {@link ImageFormat}s.
  54:  */
  55: public class ImageEncoderFactory {
  56:     private static Hashtable encoders = null;
  57: 
  58:     static {
  59:         init();
  60:     }
  61: 
  62:     /**
  63:      * Sets up default encoders (uses Sun PNG Encoder if JDK 1.4+ and the
  64:      * SunPNGEncoderAdapter class is available).
  65:      */
  66:     private static void init() {
  67:         encoders = new Hashtable();
  68:         encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
  69:         try {
  70:             //  Test for being run under JDK 1.4+
  71:             Class.forName("javax.imageio.ImageIO");
  72:             //  Test for JFreeChart being compiled under JDK 1.4+
  73:             Class.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter");
  74:             encoders.put("png", 
  75:                     "org.jfree.chart.encoders.SunPNGEncoderAdapter");
  76:             encoders.put("jpeg",
  77:                     "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
  78:         } 
  79:         catch (ClassNotFoundException e) {
  80:             encoders.put("png", 
  81:                     "org.jfree.chart.encoders.KeypointPNGEncoderAdapter");
  82:         }
  83:     }
  84: 
  85:     /**
  86:      * Used to set additional encoders or replace default ones.
  87:      *
  88:      * @param format  The image format name.
  89:      * @param imageEncoderClassName  The name of the ImageEncoder class.
  90:      */
  91:     public static void setImageEncoder(String format, 
  92:                                        String imageEncoderClassName) {
  93:         encoders.put(format, imageEncoderClassName);
  94:     }
  95: 
  96:     /**
  97:      * Used to retrieve an ImageEncoder for a specific image format.
  98:      *
  99:      * @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: }