001 /*
002 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.imageio;
027
028 import java.awt.image.BufferedImage;
029 import java.awt.image.Raster;
030 import java.awt.image.RenderedImage;
031 import java.util.List;
032 import javax.imageio.metadata.IIOMetadata;
033
034 /**
035 * A simple container class to aggregate an image, a set of
036 * thumbnail (preview) images, and an object representing metadata
037 * associated with the image.
038 *
039 * <p> The image data may take the form of either a
040 * <code>RenderedImage</code>, or a <code>Raster</code>. Reader
041 * methods that return an <code>IIOImage</code> will always return a
042 * <code>BufferedImage</code> using the <code>RenderedImage</code>
043 * reference. Writer methods that accept an <code>IIOImage</code>
044 * will always accept a <code>RenderedImage</code>, and may optionally
045 * accept a <code>Raster</code>.
046 *
047 * <p> Exactly one of <code>getRenderedImage</code> and
048 * <code>getRaster</code> will return a non-<code>null</code> value.
049 * Subclasses are responsible for ensuring this behavior.
050 *
051 * @see ImageReader#readAll(int, ImageReadParam)
052 * @see ImageReader#readAll(java.util.Iterator)
053 * @see ImageWriter#write(javax.imageio.metadata.IIOMetadata,
054 * IIOImage, ImageWriteParam)
055 * @see ImageWriter#write(IIOImage)
056 * @see ImageWriter#writeToSequence(IIOImage, ImageWriteParam)
057 * @see ImageWriter#writeInsert(int, IIOImage, ImageWriteParam)
058 *
059 * @version 0.5
060 */
061 public class IIOImage {
062
063 /**
064 * The <code>RenderedImage</code> being referenced.
065 */
066 protected RenderedImage image;
067
068 /**
069 * The <code>Raster</code> being referenced.
070 */
071 protected Raster raster;
072
073 /**
074 * A <code>List</code> of <code>BufferedImage</code> thumbnails,
075 * or <code>null</code>. Non-<code>BufferedImage</code> objects
076 * must not be stored in this <code>List</code>.
077 */
078 protected List<? extends BufferedImage> thumbnails = null;
079
080 /**
081 * An <code>IIOMetadata</code> object containing metadata
082 * associated with the image.
083 */
084 protected IIOMetadata metadata;
085
086 /**
087 * Constructs an <code>IIOImage</code> containing a
088 * <code>RenderedImage</code>, and thumbnails and metadata
089 * associated with it.
090 *
091 * <p> All parameters are stored by reference.
092 *
093 * <p> The <code>thumbnails</code> argument must either be
094 * <code>null</code> or contain only <code>BufferedImage</code>
095 * objects.
096 *
097 * @param image a <code>RenderedImage</code>.
098 * @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,
099 * or <code>null</code>.
100 * @param metadata an <code>IIOMetadata</code> object, or
101 * <code>null</code>.
102 *
103 * @exception IllegalArgumentException if <code>image</code> is
104 * <code>null</code>.
105 */
106 public IIOImage(RenderedImage image,
107 List<? extends BufferedImage> thumbnails,
108 IIOMetadata metadata) {
109 if (image == null) {
110 throw new IllegalArgumentException("image == null!");
111 }
112 this .image = image;
113 this .raster = null;
114 this .thumbnails = thumbnails;
115 this .metadata = metadata;
116 }
117
118 /**
119 * Constructs an <code>IIOImage</code> containing a
120 * <code>Raster</code>, and thumbnails and metadata
121 * associated with it.
122 *
123 * <p> All parameters are stored by reference.
124 *
125 * @param raster a <code>Raster</code>.
126 * @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,
127 * or <code>null</code>.
128 * @param metadata an <code>IIOMetadata</code> object, or
129 * <code>null</code>.
130 *
131 * @exception IllegalArgumentException if <code>raster</code> is
132 * <code>null</code>.
133 */
134 public IIOImage(Raster raster,
135 List<? extends BufferedImage> thumbnails,
136 IIOMetadata metadata) {
137 if (raster == null) {
138 throw new IllegalArgumentException("raster == null!");
139 }
140 this .raster = raster;
141 this .image = null;
142 this .thumbnails = thumbnails;
143 this .metadata = metadata;
144 }
145
146 /**
147 * Returns the currently set <code>RenderedImage</code>, or
148 * <code>null</code> if only a <code>Raster</code> is available.
149 *
150 * @return a <code>RenderedImage</code>, or <code>null</code>.
151 *
152 * @see #setRenderedImage
153 */
154 public RenderedImage getRenderedImage() {
155 synchronized (this ) {
156 return image;
157 }
158 }
159
160 /**
161 * Sets the current <code>RenderedImage</code>. The value is
162 * stored by reference. Any existing <code>Raster</code> is
163 * discarded.
164 *
165 * @param image a <code>RenderedImage</code>.
166 *
167 * @exception IllegalArgumentException if <code>image</code> is
168 * <code>null</code>.
169 *
170 * @see #getRenderedImage
171 */
172 public void setRenderedImage(RenderedImage image) {
173 synchronized (this ) {
174 if (image == null) {
175 throw new IllegalArgumentException("image == null!");
176 }
177 this .image = image;
178 this .raster = null;
179 }
180 }
181
182 /**
183 * Returns <code>true</code> if this <code>IIOImage</code> stores
184 * a <code>Raster</code> rather than a <code>RenderedImage</code>.
185 *
186 * @return <code>true</code> if a <code>Raster</code> is
187 * available.
188 */
189 public boolean hasRaster() {
190 synchronized (this ) {
191 return (raster != null);
192 }
193 }
194
195 /**
196 * Returns the currently set <code>Raster</code>, or
197 * <code>null</code> if only a <code>RenderedImage</code> is
198 * available.
199 *
200 * @return a <code>Raster</code>, or <code>null</code>.
201 *
202 * @see #setRaster
203 */
204 public Raster getRaster() {
205 synchronized (this ) {
206 return raster;
207 }
208 }
209
210 /**
211 * Sets the current <code>Raster</code>. The value is
212 * stored by reference. Any existing <code>RenderedImage</code> is
213 * discarded.
214 *
215 * @param raster a <code>Raster</code>.
216 *
217 * @exception IllegalArgumentException if <code>raster</code> is
218 * <code>null</code>.
219 *
220 * @see #getRaster
221 */
222 public void setRaster(Raster raster) {
223 synchronized (this ) {
224 if (raster == null) {
225 throw new IllegalArgumentException("raster == null!");
226 }
227 this .raster = raster;
228 this .image = null;
229 }
230 }
231
232 /**
233 * Returns the number of thumbnails stored in this
234 * <code>IIOImage</code>.
235 *
236 * @return the number of thumbnails, as an <code>int</code>.
237 */
238 public int getNumThumbnails() {
239 return thumbnails == null ? 0 : thumbnails.size();
240 }
241
242 /**
243 * Returns a thumbnail associated with the main image.
244 *
245 * @param index the index of the desired thumbnail image.
246 *
247 * @return a thumbnail image, as a <code>BufferedImage</code>.
248 *
249 * @exception IndexOutOfBoundsException if the supplied index is
250 * negative or larger than the largest valid index.
251 * @exception ClassCastException if a
252 * non-<code>BufferedImage</code> object is encountered in the
253 * list of thumbnails at the given index.
254 *
255 * @see #getThumbnails
256 * @see #setThumbnails
257 */
258 public BufferedImage getThumbnail(int index) {
259 if (thumbnails == null) {
260 throw new IndexOutOfBoundsException(
261 "No thumbnails available!");
262 }
263 return (BufferedImage) thumbnails.get(index);
264 }
265
266 /**
267 * Returns the current <code>List</code> of thumbnail
268 * <code>BufferedImage</code>s, or <code>null</code> if none is
269 * set. A live reference is returned.
270 *
271 * @return the current <code>List</code> of
272 * <code>BufferedImage</code> thumbnails, or <code>null</code>.
273 *
274 * @see #getThumbnail(int)
275 * @see #setThumbnails
276 */
277 public List<? extends BufferedImage> getThumbnails() {
278 return thumbnails;
279 }
280
281 /**
282 * Sets the list of thumbnails to a new <code>List</code> of
283 * <code>BufferedImage</code>s, or to <code>null</code>. The
284 * reference to the previous <code>List</code> is discarded.
285 *
286 * <p> The <code>thumbnails</code> argument must either be
287 * <code>null</code> or contain only <code>BufferedImage</code>
288 * objects.
289 *
290 * @param thumbnails a <code>List</code> of
291 * <code>BufferedImage</code> thumbnails, or <code>null</code>.
292 *
293 * @see #getThumbnail(int)
294 * @see #getThumbnails
295 */
296 public void setThumbnails(List<? extends BufferedImage> thumbnails) {
297 this .thumbnails = thumbnails;
298 }
299
300 /**
301 * Returns a reference to the current <code>IIOMetadata</code>
302 * object, or <code>null</code> is none is set.
303 *
304 * @return an <code>IIOMetadata</code> object, or <code>null</code>.
305 *
306 * @see #setMetadata
307 */
308 public IIOMetadata getMetadata() {
309 return metadata;
310 }
311
312 /**
313 * Sets the <code>IIOMetadata</code> to a new object, or
314 * <code>null</code>.
315 *
316 * @param metadata an <code>IIOMetadata</code> object, or
317 * <code>null</code>.
318 *
319 * @see #getMetadata
320 */
321 public void setMetadata(IIOMetadata metadata) {
322 this.metadata = metadata;
323 }
324 }
|