KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > ImageDecoderImpl


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.codec;
19
20 import java.awt.image.Raster JavaDoc;
21 import java.awt.image.RenderedImage JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * A partial implementation of the <code>ImageDecoder</code> interface
27  * useful for subclassing.
28  *
29  */

30 public abstract class ImageDecoderImpl implements ImageDecoder {
31
32     /**
33      * The <code>SeekableStream</code> associcted with this
34      * <code>ImageEncoder</code>.
35      */

36     protected SeekableStream input;
37
38     /**
39      * The <code>ImageDecodeParam</code> object associated with this
40      * <code>ImageEncoder</code>.
41      */

42     protected ImageDecodeParam param;
43
44     /**
45      * Constructs an <code>ImageDecoderImpl</code> with a given
46      * <code>SeekableStream</code> and <code>ImageDecodeParam</code>
47      * instance.
48      */

49     public ImageDecoderImpl(SeekableStream input,
50                             ImageDecodeParam param) {
51         this.input = input;
52         this.param = param;
53     }
54
55     /**
56      * Constructs an <code>ImageDecoderImpl</code> with a given
57      * <code>InputStream</code> and <code>ImageDecodeParam</code>
58      * instance. The <code>input</code> parameter will be used to
59      * construct a <code>ForwardSeekableStream</code>; if the ability
60      * to seek backwards is required, the caller should construct
61      * an instance of <code>SeekableStream</code> and
62      * make use of the other contructor.
63      */

64     public ImageDecoderImpl(InputStream JavaDoc input,
65                             ImageDecodeParam param) {
66         this.input = new ForwardSeekableStream(input);
67         this.param = param;
68     }
69
70     /**
71      * Returns the current parameters as an instance of the
72      * <code>ImageDecodeParam</code> interface. Concrete
73      * implementations of this interface will return corresponding
74      * concrete implementations of the <code>ImageDecodeParam</code>
75      * interface. For example, a <code>JPEGImageDecoder</code> will
76      * return an instance of <code>JPEGDecodeParam</code>.
77      */

78     public ImageDecodeParam getParam() {
79         return param;
80     }
81
82     /**
83      * Sets the current parameters to an instance of the
84      * <code>ImageDecodeParam</code> interface. Concrete
85      * implementations of <code>ImageDecoder</code> may throw a
86      * <code>RuntimeException</code> if the <code>param</code>
87      * argument is not an instance of the appropriate subclass or
88      * subinterface. For example, a <code>JPEGImageDecoder</code>
89      * will expect <code>param</code> to be an instance of
90      * <code>JPEGDecodeParam</code>.
91      */

92     public void setParam(ImageDecodeParam param) {
93         this.param = param;
94     }
95
96     /**
97      * Returns the <code>SeekableStream</code> associated with
98      * this <code>ImageDecoder</code>.
99      */

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 JavaDoc {
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 JavaDoc decodeAsRaster() throws IOException JavaDoc {
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 JavaDoc decodeAsRaster(int page) throws IOException JavaDoc {
134         RenderedImage JavaDoc 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 JavaDoc decodeAsRenderedImage() throws IOException JavaDoc {
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 JavaDoc decodeAsRenderedImage(int page)
159         throws IOException JavaDoc;
160 }
161
Popular Tags