1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ------------------------
  25. * ImageEncoderFactory.java
  26. * ------------------------
  27. * (C) Copyright 2004, by Richard Atkinson and Contributors.
  28. *
  29. * Original Author: Richard Atkinson;
  30. * Contributor(s): -;
  31. *
  32. * $Id: ImageEncoderFactory.java,v 1.2 2004/09/13 22:29:38 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 01-Aug-2004 : Initial version (RA);
  37. *
  38. */
  39. package org.jfree.chart.encoders;
  40. import java.util.Hashtable;
  41. /**
  42. * Factory class for returning {@link ImageEncoder}s for different {@link ImageFormat}s.
  43. *
  44. * @author Richard Atkinson
  45. */
  46. public class ImageEncoderFactory {
  47. private static Hashtable encoders = null;
  48. static {
  49. init();
  50. }
  51. /**
  52. * Sets up default encoders (uses Sun PNG Encoder if JDK 1.4+ and the
  53. * SunPNGEncoderAdapter class is available).
  54. */
  55. private static void init() {
  56. encoders = new Hashtable();
  57. encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
  58. try {
  59. // Test for being run under JDK 1.4+
  60. Class.forName("javax.imageio.ImageIO");
  61. // Test for JFreeChart being compiled under JDK 1.4+
  62. Class.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter");
  63. encoders.put("png", "org.jfree.chart.encoders.SunPNGEncoderAdapter");
  64. } catch (ClassNotFoundException e) {
  65. encoders.put("png", "org.jfree.chart.encoders.KeypointPNGEncoderAdapter");
  66. }
  67. }
  68. /**
  69. * Used to set additional encoders or replace default ones.
  70. *
  71. * @param format The image format name.
  72. * @param imageEncoderClassName The name of the ImageEncoder class.
  73. */
  74. public static void setImageEncoder(String format, String imageEncoderClassName) {
  75. encoders.put(format, imageEncoderClassName);
  76. }
  77. /**
  78. * Used to retrieve an ImageEncoder for a specific image format.
  79. *
  80. * @param format The image format required.
  81. * @return The ImageEncoder or null if none available.
  82. */
  83. public static ImageEncoder newInstance(String format) {
  84. ImageEncoder imageEncoder = null;
  85. String className = (String) encoders.get(format);
  86. if (className == null) {
  87. throw new IllegalArgumentException("Unsupported image format - " + format);
  88. }
  89. try {
  90. Class imageEncoderClass = Class.forName(className);
  91. imageEncoder = (ImageEncoder) imageEncoderClass.newInstance();
  92. }
  93. catch (Exception e) {
  94. throw new IllegalArgumentException(e.toString());
  95. }
  96. return imageEncoder;
  97. }
  98. /**
  99. * Used to retrieve an ImageEncoder for a specific image format.
  100. *
  101. * @param format The image format required.
  102. * @param quality The quality to be set before returning.
  103. * @return The ImageEncoder or null if none available.
  104. */
  105. public static ImageEncoder newInstance(String format, float quality) {
  106. ImageEncoder imageEncoder = newInstance(format);
  107. imageEncoder.setQuality(quality);
  108. return imageEncoder;
  109. }
  110. /**
  111. * Used to retrieve an ImageEncoder for a specific image format.
  112. *
  113. * @param format The image format required.
  114. * @param encodingAlpha Sets whether alpha transparency should be encoded.
  115. * @return The ImageEncoder or null if none available.
  116. */
  117. public static ImageEncoder newInstance(String format, boolean encodingAlpha) {
  118. ImageEncoder imageEncoder = newInstance(format);
  119. imageEncoder.setEncodingAlpha(encodingAlpha);
  120. return imageEncoder;
  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. * @param encodingAlpha Sets whether alpha transparency should be encoded.
  128. * @return The ImageEncoder or null if none available.
  129. */
  130. public static ImageEncoder newInstance(String format, float quality, boolean encodingAlpha) {
  131. ImageEncoder imageEncoder = newInstance(format);
  132. imageEncoder.setQuality(quality);
  133. imageEncoder.setEncodingAlpha(encodingAlpha);
  134. return imageEncoder;
  135. }
  136. }