001: /*
002: * @(#)ImageDecoderFactory.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.image;
029:
030: import java.io.InputStream;
031: import java.io.IOException;
032:
033: /**
034: * <code>ImageDecoderFactory</code> allows the peer implementations to
035: * provide alternative image decoder implementations that can be used
036: * image decoding framework. There is always a default instance created
037: * that uses the decoders that are part of the <code>sun.awt.image</code>
038: * package.
039: * <p>
040: * <code>sun.awt.image.InputStreamImageSource.getDecoder()</code> method
041: * gets the instance of <code>ImageDecoderFactory</code> using
042: * <code>ImageDecoderFactory.getInstance()</code> and uses the factory to
043: * create various decoders.
044: * <h3>Plugging Alternative Factories</h3>
045: * The peer implementation can provide alternate implementations for the
046: * image decoders. To do that they need to do the following
047: * <ul>
048: * <li>Subclass <code>sun.awt.image.ImageDecoderFactory</code> and provide
049: * implementation for <code>ImageDecoderFactory.newImageDecoder()</code></li>
050: * <li>Create an instance of the subclass and register it with
051: * <code>sun.awt.image.ImageDecoderFactory.setFactory()</code></li>
052: * </ul>
053: *
054: */
055: public class ImageDecoderFactory {
056: /**
057: * GIF Image format
058: */
059: public static final String IMG_FORMAT_GIF = "gif";
060:
061: /**
062: * JPEG Image format
063: */
064: public static final String IMG_FORMAT_JPG = "jpeg";
065:
066: /**
067: * PNG Image format
068: */
069: public static final String IMG_FORMAT_PNG = "png";
070:
071: /**
072: * XBM Image format
073: */
074: public static final String IMG_FORMAT_XBM = "xbm";
075:
076: /**
077: * The instance of the <code>ImageDecoderFactory</code> configured.
078: */
079: private static ImageDecoderFactory defaultInstance = null;
080:
081: /**
082: * Returns the <code>ImageDecoderFactory</code> instance.
083: */
084: public synchronized static ImageDecoderFactory getInstance() {
085: if (defaultInstance == null) {
086: /* the GUI toolkit has not configured any factory, we will
087: * create a ImageDecoderFactory using the ImageDecoders that
088: * are part of this package
089: */
090: defaultInstance = new ImageDecoderFactory();
091: }
092: return defaultInstance;
093: }
094:
095: /**
096: * Sets the <code>ImageDecoderFactory</code> instance. This can be
097: * called by the peer implementation to change the decoder factory
098: */
099: public synchronized static void setFactory(
100: ImageDecoderFactory factory) {
101: defaultInstance = factory;
102: }
103:
104: /**
105: * Creates an instance of a new <code>ImageDecoder</code> for the
106: * image format specified.
107: */
108: public ImageDecoder newImageDecoder(InputStreamImageSource source,
109: InputStream input, String imgFormat) throws IOException {
110: ImageDecoder decoder = null;
111: if (imgFormat == IMG_FORMAT_GIF) {
112: decoder = new GifImageDecoder(source, input);
113: } else if (imgFormat == IMG_FORMAT_JPG) {
114: decoder = new JPEGImageDecoder(source, input);
115: } else if (imgFormat == IMG_FORMAT_PNG) {
116: decoder = new PNGImageDecoder(source, input);
117: } else if (imgFormat == IMG_FORMAT_XBM) {
118: decoder = new XbmImageDecoder(source, input);
119: } else {
120: throw new IllegalArgumentException(
121: "Unsupported image format:" + imgFormat);
122: }
123:
124: return decoder;
125: }
126: }
|