001: /*
002: * $RCSfile: ImageDecoderImpl.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:30 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codec;
013:
014: import java.awt.image.Raster;
015: import java.awt.image.RenderedImage;
016: import java.io.InputStream;
017: import java.io.IOException;
018:
019: /**
020: * A partial implementation of the <code>ImageDecoder</code> interface
021: * useful for subclassing.
022: *
023: * <p><b> This class is not a committed part of the JAI API. It may
024: * be removed or changed in future releases of JAI.</b>
025: */
026: public abstract class ImageDecoderImpl implements ImageDecoder {
027:
028: /**
029: * The <code>SeekableStream</code> associcted with this
030: * <code>ImageEncoder</code>.
031: */
032: protected SeekableStream input;
033:
034: /**
035: * The <code>ImageDecodeParam</code> object associated with this
036: * <code>ImageEncoder</code>.
037: */
038: protected ImageDecodeParam param;
039:
040: /**
041: * Constructs an <code>ImageDecoderImpl</code> with a given
042: * <code>SeekableStream</code> and <code>ImageDecodeParam</code>
043: * instance.
044: */
045: public ImageDecoderImpl(SeekableStream input, ImageDecodeParam param) {
046: this .input = input;
047: this .param = param;
048: }
049:
050: /**
051: * Constructs an <code>ImageDecoderImpl</code> with a given
052: * <code>InputStream</code> and <code>ImageDecodeParam</code>
053: * instance. The <code>input</code> parameter will be used to
054: * construct a <code>ForwardSeekableStream</code>; if the ability
055: * to seek backwards is required, the caller should construct
056: * an instance of <code>SeekableStream</code> and
057: * make use of the other contructor.
058: */
059: public ImageDecoderImpl(InputStream input, ImageDecodeParam param) {
060: this .input = new ForwardSeekableStream(input);
061: this .param = param;
062: }
063:
064: /**
065: * Returns the current parameters as an instance of the
066: * <code>ImageDecodeParam</code> interface. Concrete
067: * implementations of this interface will return corresponding
068: * concrete implementations of the <code>ImageDecodeParam</code>
069: * interface. For example, a <code>JPEGImageDecoder</code> will
070: * return an instance of <code>JPEGDecodeParam</code>.
071: */
072: public ImageDecodeParam getParam() {
073: return param;
074: }
075:
076: /**
077: * Sets the current parameters to an instance of the
078: * <code>ImageDecodeParam</code> interface. Concrete
079: * implementations of <code>ImageDecoder</code> may throw a
080: * <code>RuntimeException</code> if the <code>param</code>
081: * argument is not an instance of the appropriate subclass or
082: * subinterface. For example, a <code>JPEGImageDecoder</code>
083: * will expect <code>param</code> to be an instance of
084: * <code>JPEGDecodeParam</code>.
085: */
086: public void setParam(ImageDecodeParam param) {
087: this .param = param;
088: }
089:
090: /**
091: * Returns the <code>SeekableStream</code> associated with
092: * this <code>ImageDecoder</code>.
093: */
094: public SeekableStream getInputStream() {
095: return input;
096: }
097:
098: /**
099: * Returns the number of pages present in the current stream.
100: * By default, the return value is 1. Subclasses that deal with
101: * multi-page formats should override this method.
102: */
103: public int getNumPages() throws IOException {
104: return 1;
105: }
106:
107: /**
108: * Returns a <code>Raster</code> that contains the decoded
109: * contents of the <code>SeekableStream</code> associated
110: * with this <code>ImageDecoder</code>. Only
111: * the first page of a multi-page image is decoded.
112: */
113: public Raster decodeAsRaster() throws IOException {
114: return decodeAsRaster(0);
115: }
116:
117: /**
118: * Returns a <code>Raster</code> that contains the decoded
119: * contents of the <code>SeekableStream</code> associated
120: * with this <code>ImageDecoder</code>.
121: * The given page of a multi-page image is decoded. If
122: * the page does not exist, an IOException will be thrown.
123: * Page numbering begins at zero.
124: *
125: * @param page The page to be decoded.
126: */
127: public Raster decodeAsRaster(int page) throws IOException {
128: RenderedImage im = decodeAsRenderedImage(page);
129: return im.getData();
130: }
131:
132: /**
133: * Returns a <code>RenderedImage</code> that contains the decoded
134: * contents of the <code>SeekableStream</code> associated
135: * with this <code>ImageDecoder</code>. Only
136: * the first page of a multi-page image is decoded.
137: */
138: public RenderedImage decodeAsRenderedImage() throws IOException {
139: return decodeAsRenderedImage(0);
140: }
141:
142: /**
143: * Returns a <code>RenderedImage</code> that contains the decoded
144: * contents of the <code>SeekableStream</code> associated
145: * with this <code>ImageDecoder</code>.
146: * The given page of a multi-page image is decoded. If
147: * the page does not exist, an IOException will be thrown.
148: * Page numbering begins at zero.
149: *
150: * @param page The page to be decoded.
151: */
152: public abstract RenderedImage decodeAsRenderedImage(int page)
153: throws IOException;
154: }
|