001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ImageDecoderImpl.java 447277 2006-09-18 06:19:34Z jeremias $ */
019:
020: package org.apache.xmlgraphics.image.codec.util;
021:
022: import java.awt.image.Raster;
023: import java.awt.image.RenderedImage;
024: import java.io.IOException;
025: import java.io.InputStream;
026:
027: /**
028: * A partial implementation of the <code>ImageDecoder</code> interface
029: * useful for subclassing.
030: *
031: */
032: public abstract class ImageDecoderImpl implements ImageDecoder {
033:
034: /**
035: * The <code>SeekableStream</code> associcted with this
036: * <code>ImageEncoder</code>.
037: */
038: protected SeekableStream input;
039:
040: /**
041: * The <code>ImageDecodeParam</code> object associated with this
042: * <code>ImageEncoder</code>.
043: */
044: protected ImageDecodeParam param;
045:
046: /**
047: * Constructs an <code>ImageDecoderImpl</code> with a given
048: * <code>SeekableStream</code> and <code>ImageDecodeParam</code>
049: * instance.
050: */
051: public ImageDecoderImpl(SeekableStream input, ImageDecodeParam param) {
052: this .input = input;
053: this .param = param;
054: }
055:
056: /**
057: * Constructs an <code>ImageDecoderImpl</code> with a given
058: * <code>InputStream</code> and <code>ImageDecodeParam</code>
059: * instance. The <code>input</code> parameter will be used to
060: * construct a <code>ForwardSeekableStream</code>; if the ability
061: * to seek backwards is required, the caller should construct
062: * an instance of <code>SeekableStream</code> and
063: * make use of the other contructor.
064: */
065: public ImageDecoderImpl(InputStream input, ImageDecodeParam param) {
066: this .input = new ForwardSeekableStream(input);
067: this .param = param;
068: }
069:
070: /**
071: * Returns the current parameters as an instance of the
072: * <code>ImageDecodeParam</code> interface. Concrete
073: * implementations of this interface will return corresponding
074: * concrete implementations of the <code>ImageDecodeParam</code>
075: * interface. For example, a <code>JPEGImageDecoder</code> will
076: * return an instance of <code>JPEGDecodeParam</code>.
077: */
078: public ImageDecodeParam getParam() {
079: return param;
080: }
081:
082: /**
083: * Sets the current parameters to an instance of the
084: * <code>ImageDecodeParam</code> interface. Concrete
085: * implementations of <code>ImageDecoder</code> may throw a
086: * <code>RuntimeException</code> if the <code>param</code>
087: * argument is not an instance of the appropriate subclass or
088: * subinterface. For example, a <code>JPEGImageDecoder</code>
089: * will expect <code>param</code> to be an instance of
090: * <code>JPEGDecodeParam</code>.
091: */
092: public void setParam(ImageDecodeParam param) {
093: this .param = param;
094: }
095:
096: /**
097: * Returns the <code>SeekableStream</code> associated with
098: * this <code>ImageDecoder</code>.
099: */
100: public SeekableStream getInputStream() {
101: return input;
102: }
103:
104: /**
105: * Returns the number of pages present in the current stream.
106: * By default, the return value is 1. Subclasses that deal with
107: * multi-page formats should override this method.
108: */
109: public int getNumPages() throws IOException {
110: return 1;
111: }
112:
113: /**
114: * Returns a <code>Raster</code> that contains the decoded
115: * contents of the <code>SeekableStream</code> associated
116: * with this <code>ImageDecoder</code>. Only
117: * the first page of a multi-page image is decoded.
118: */
119: public Raster decodeAsRaster() throws IOException {
120: return decodeAsRaster(0);
121: }
122:
123: /**
124: * Returns a <code>Raster</code> that contains the decoded
125: * contents of the <code>SeekableStream</code> associated
126: * with this <code>ImageDecoder</code>.
127: * The given page of a multi-page image is decoded. If
128: * the page does not exist, an IOException will be thrown.
129: * Page numbering begins at zero.
130: *
131: * @param page The page to be decoded.
132: */
133: public Raster decodeAsRaster(int page) throws IOException {
134: RenderedImage im = decodeAsRenderedImage(page);
135: return im.getData();
136: }
137:
138: /**
139: * Returns a <code>RenderedImage</code> that contains the decoded
140: * contents of the <code>SeekableStream</code> associated
141: * with this <code>ImageDecoder</code>. Only
142: * the first page of a multi-page image is decoded.
143: */
144: public RenderedImage decodeAsRenderedImage() throws IOException {
145: return decodeAsRenderedImage(0);
146: }
147:
148: /**
149: * Returns a <code>RenderedImage</code> that contains the decoded
150: * contents of the <code>SeekableStream</code> associated
151: * with this <code>ImageDecoder</code>.
152: * The given page of a multi-page image is decoded. If
153: * the page does not exist, an IOException will be thrown.
154: * Page numbering begins at zero.
155: *
156: * @param page The page to be decoded.
157: */
158: public abstract RenderedImage decodeAsRenderedImage(int page)
159: throws IOException;
160: }
|