Source code: org/ydp/jai/ImageDecoderImpl.java
1 /*
2 * The contents of this file are subject to the JAVA ADVANCED IMAGING
3 * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE License
4 * Version 1.0 (the "License"); You may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 * http://www.sun.com/software/imaging/JAI/index.html
7 *
8 * Software distributed under the License is distributed on an "AS IS"
9 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 * the License for the specific language governing rights and limitations
11 * under the License.
12 *
13 * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS
14 * AND WIDGET HANDLING SOURCE CODE.
15 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
16 * Portions created by: _______________________________________
17 * are Copyright (C): _______________________________________
18 * All Rights Reserved.
19 * Contributor(s): _______________________________________
20 */
21
22 package org.ydp.jai;
23
24 import java.awt.image.Raster;
25 import java.awt.image.RenderedImage;
26 import java.io.InputStream;
27 import java.io.IOException;
28
29 /**
30 * A partial implementation of the <code>ImageDecoder</code> interface
31 * useful for subclassing.
32 *
33 * <p><b> This class is not a committed part of the JAI API. It may
34 * be removed or changed in future releases of JAI.</b>
35 */
36 public abstract class ImageDecoderImpl implements ImageDecoder {
37
38 /**
39 * The <code>SeekableStream</code> associcted with this
40 * <code>ImageEncoder</code>.
41 */
42 protected SeekableStream input;
43
44 /**
45 * The <code>ImageDecodeParam</code> object associated with this
46 * <code>ImageEncoder</code>.
47 */
48 protected ImageDecodeParam param;
49
50 /**
51 * Constructs an <code>ImageDecoderImpl</code> with a given
52 * <code>SeekableStream</code> and <code>ImageDecodeParam</code>
53 * instance.
54 */
55 public ImageDecoderImpl(SeekableStream input,
56 ImageDecodeParam param) {
57 this.input = input;
58 this.param = param;
59 }
60
61 /**
62 * Constructs an <code>ImageDecoderImpl</code> with a given
63 * <code>InputStream</code> and <code>ImageDecodeParam</code>
64 * instance. The <code>input</code> parameter will be used to
65 * construct a <code>ForwardSeekableStream</code>; if the ability
66 * to seek backwards is required, the caller should construct
67 * an instance of <code>SeekableStream</code> and
68 * make use of the other contructor.
69 */
70 public ImageDecoderImpl(InputStream input,
71 ImageDecodeParam param) {
72 this.input = new ForwardSeekableStream(input);
73 this.param = param;
74 }
75
76 /**
77 * Returns the current parameters as an instance of the
78 * <code>ImageDecodeParam</code> interface. Concrete
79 * implementations of this interface will return corresponding
80 * concrete implementations of the <code>ImageDecodeParam</code>
81 * interface. For example, a <code>JPEGImageDecoder</code> will
82 * return an instance of <code>JPEGDecodeParam</code>.
83 */
84 public ImageDecodeParam getParam() {
85 return param;
86 }
87
88 /**
89 * Sets the current parameters to an instance of the
90 * <code>ImageDecodeParam</code> interface. Concrete
91 * implementations of <code>ImageDecoder</code> may throw a
92 * <code>RuntimeException</code> if the <code>param</code>
93 * argument is not an instance of the appropriate subclass or
94 * subinterface. For example, a <code>JPEGImageDecoder</code>
95 * will expect <code>param</code> to be an instance of
96 * <code>JPEGDecodeParam</code>.
97 */
98 public void setParam(ImageDecodeParam param) {
99 this.param = param;
100 }
101
102 /**
103 * Returns the <code>SeekableStream</code> associated with
104 * this <code>ImageDecoder</code>.
105 */
106 public SeekableStream getInputStream() {
107 return input;
108 }
109
110 /**
111 * Returns the number of pages present in the current stream.
112 * By default, the return value is 1. Subclasses that deal with
113 * multi-page formats should override this method.
114 */
115 public int getNumPages() throws IOException {
116 return 1;
117 }
118
119 /**
120 * Returns a <code>Raster</code> that contains the decoded
121 * contents of the <code>SeekableStream</code> associated
122 * with this <code>ImageDecoder</code>. Only
123 * the first page of a multi-page image is decoded.
124 */
125 public Raster decodeAsRaster() throws IOException {
126 return decodeAsRaster(0);
127 }
128
129 /**
130 * Returns a <code>Raster</code> that contains the decoded
131 * contents of the <code>SeekableStream</code> associated
132 * with this <code>ImageDecoder</code>.
133 * The given page of a multi-page image is decoded. If
134 * the page does not exist, an IOException will be thrown.
135 * Page numbering begins at zero.
136 *
137 * @param page The page to be decoded.
138 */
139 public Raster decodeAsRaster(int page) throws IOException {
140 RenderedImage im = decodeAsRenderedImage(page);
141 return im.getData();
142 }
143
144 /**
145 * Returns a <code>RenderedImage</code> that contains the decoded
146 * contents of the <code>SeekableStream</code> associated
147 * with this <code>ImageDecoder</code>. Only
148 * the first page of a multi-page image is decoded.
149 */
150 public RenderedImage decodeAsRenderedImage() throws IOException {
151 return decodeAsRenderedImage(0);
152 }
153
154 /**
155 * Returns a <code>RenderedImage</code> that contains the decoded
156 * contents of the <code>SeekableStream</code> associated
157 * with this <code>ImageDecoder</code>.
158 * The given page of a multi-page image is decoded. If
159 * the page does not exist, an IOException will be thrown.
160 * Page numbering begins at zero.
161 *
162 * @param page The page to be decoded.
163 */
164 public abstract RenderedImage decodeAsRenderedImage(int page)
165 throws IOException;
166 }