001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, 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, 2005, 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.2 2005/11/01 16:37:01 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 * 044 */ 045 046 package org.jfree.chart.encoders; 047 048 import java.util.Hashtable; 049 050 /** 051 * Factory class for returning {@link ImageEncoder}s for different 052 * {@link ImageFormat}s. 053 * 054 * @author Richard Atkinson 055 */ 056 public class ImageEncoderFactory { 057 private static Hashtable encoders = null; 058 059 static { 060 init(); 061 } 062 063 /** 064 * Sets up default encoders (uses Sun PNG Encoder if JDK 1.4+ and the 065 * SunPNGEncoderAdapter class is available). 066 */ 067 private static void init() { 068 encoders = new Hashtable(); 069 encoders.put("jpeg", "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.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter"); 075 encoders.put("png", 076 "org.jfree.chart.encoders.SunPNGEncoderAdapter"); 077 encoders.put("jpeg", 078 "org.jfree.chart.encoders.SunJPEGEncoderAdapter"); 079 } 080 catch (ClassNotFoundException e) { 081 encoders.put("png", 082 "org.jfree.chart.encoders.KeypointPNGEncoderAdapter"); 083 } 084 } 085 086 /** 087 * Used to set additional encoders or replace default ones. 088 * 089 * @param format The image format name. 090 * @param imageEncoderClassName The name of the ImageEncoder class. 091 */ 092 public static void setImageEncoder(String format, 093 String imageEncoderClassName) { 094 encoders.put(format, imageEncoderClassName); 095 } 096 097 /** 098 * Used to retrieve an ImageEncoder for a specific image format. 099 * 100 * @param format The image format required. 101 * 102 * @return The ImageEncoder or <code>null</code> if none available. 103 */ 104 public static ImageEncoder newInstance(String format) { 105 ImageEncoder imageEncoder = null; 106 String className = (String) encoders.get(format); 107 if (className == null) { 108 throw new IllegalArgumentException("Unsupported image format - " 109 + format); 110 } 111 try { 112 Class imageEncoderClass = Class.forName(className); 113 imageEncoder = (ImageEncoder) imageEncoderClass.newInstance(); 114 } 115 catch (Exception e) { 116 throw new IllegalArgumentException(e.toString()); 117 } 118 return imageEncoder; 119 } 120 121 /** 122 * Used to retrieve an ImageEncoder for a specific image format. 123 * 124 * @param format The image format required. 125 * @param quality The quality to be set before returning. 126 * 127 * @return The ImageEncoder or <code>null</code> if none available. 128 */ 129 public static ImageEncoder newInstance(String format, float quality) { 130 ImageEncoder imageEncoder = newInstance(format); 131 imageEncoder.setQuality(quality); 132 return imageEncoder; 133 } 134 135 /** 136 * Used to retrieve an ImageEncoder for a specific image format. 137 * 138 * @param format The image format required. 139 * @param encodingAlpha Sets whether alpha transparency should be encoded. 140 * 141 * @return The ImageEncoder or <code>null</code> if none available. 142 */ 143 public static ImageEncoder newInstance(String format, 144 boolean encodingAlpha) { 145 ImageEncoder imageEncoder = newInstance(format); 146 imageEncoder.setEncodingAlpha(encodingAlpha); 147 return imageEncoder; 148 } 149 150 /** 151 * Used to retrieve an ImageEncoder for a specific image format. 152 * 153 * @param format The image format required. 154 * @param quality The quality to be set before returning. 155 * @param encodingAlpha Sets whether alpha transparency should be encoded. 156 * 157 * @return The ImageEncoder or <code>null</code> if none available. 158 */ 159 public static ImageEncoder newInstance(String format, float quality, 160 boolean encodingAlpha) { 161 ImageEncoder imageEncoder = newInstance(format); 162 imageEncoder.setQuality(quality); 163 imageEncoder.setEncodingAlpha(encodingAlpha); 164 return imageEncoder; 165 } 166 167 }