1 /* IIOImage.java --
2    Copyright (C) 2003 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package javax.imageio;
40 
41 import java.awt.image.BufferedImage;
42 import java.awt.image.Raster;
43 import java.awt.image.RenderedImage;
44 import java.util.List;
45 
46 import javax.imageio.metadata.IIOMetadata;
47 
48 /**
49  * IIOImage is a container class for components of an image file that
50  * stores image data, image metadata and thumbnails.
51  *
52  * The image data can be either a RenderedImage or a Raster but not
53  * both.  Image readers that produce IIOImages will always produce
54  * BufferedImages from the RenderedImage field.  Image writers that
55  * accept IIOImages will always accept RenderedImages and may
56  * optionally accept Rasters.
57  *
58  * @author Thomas Fitzsimmons (fitzsim@redhat.com)
59  */
60 public class IIOImage
61 {
62   /**
63    * Image data as a RenderedImage.  null if this IIOImage uses the
64    * Raster representation.
65    */
66   protected RenderedImage image;
67 
68   /**
69    * Image metadata.
70    */
71   protected IIOMetadata metadata;
72 
73   /**
74    * Image data as a Raster.  null if this IIOImage uses the
75    * RenderedImage representation.
76    */
77   protected Raster raster;
78 
79   /**
80    * A list of BufferedImage thumbnails of this image.
81    */
82   // for 1.5 these lists are List<? extends BufferedImage>
83   protected List thumbnails;
84 
85   /**
86    * Construct an IIOImage containing raster image data, thumbnails
87    * and metadata.
88    *
89    * @param raster image data
90    * @param thumbnails a list of BufferedImage thumbnails or null
91    * @param metadata image metadata or null
92    *
93    * @exception IllegalArgumentException if raster is null
94    */
IIOImage(Raster raster, List thumbnails, IIOMetadata metadata)95   public IIOImage (Raster raster, List thumbnails, IIOMetadata metadata)
96   {
97     if (raster == null)
98       throw new IllegalArgumentException ("raster may not be null");
99 
100     this.raster = raster;
101     this.thumbnails = thumbnails;
102     this.metadata = metadata;
103   }
104 
105   /**
106    * Construct an IIOImage containing rendered image data, thumbnails
107    * and metadata.
108    *
109    * @param image rendered image data
110    * @param thumbnails a list of BufferedImage thumbnails or null
111    * @param metadata image metadata or null
112    *
113    * @exception IllegalArgumentException if image is null
114    */
IIOImage(RenderedImage image, List thumbnails, IIOMetadata metadata)115   public IIOImage (RenderedImage image, List thumbnails, IIOMetadata metadata)
116   {
117     if (image == null)
118       throw new IllegalArgumentException ("image may not be null");
119 
120     this.image = image;
121     this.thumbnails = thumbnails;
122     this.metadata = metadata;
123   }
124 
125   /**
126    * Retrieve the image metadata or null if there is no metadata
127    * associated with this IIOImage.
128    *
129    * @return image metadata or null
130    */
getMetadata()131   public IIOMetadata getMetadata()
132   {
133     return metadata;
134   }
135 
136   /**
137    * Retrieve the number of thumbnails in this IIOImage.
138    *
139    * @return the number of thumbnails
140    */
getNumThumbnails()141   public int getNumThumbnails()
142   {
143     return thumbnails == null ? 0 : thumbnails.size();
144   }
145 
146   /**
147    * Retrieve the raster image data stored in this IIOImage or null if
148    * this image stores data using the RenderedImage representation.
149    *
150    * @return the raster image data or null
151    */
getRaster()152   public Raster getRaster()
153   {
154     return raster;
155   }
156 
157   /**
158    * Retrieve the rendered image data stored in this IIOImage or null
159    * if this image stores data using the Raster representation.
160    *
161    * @return the rendered image data or null
162    */
getRenderedImage()163   public RenderedImage getRenderedImage()
164   {
165     return image;
166   }
167 
168   /**
169    * Retrieve the thumbnail stored at the specified index in the
170    * thumbnails list.
171    *
172    * @param index the index of the thumbnail to retrieve
173    *
174    * @return the buffered image thumbnail
175    *
176    * @exception IndexOutOfBoundsException if index is out-of-bounds
177    * @exception ClassCastException if the object returned from the
178    * thumbnails list is not a BufferedImage
179    */
getThumbnail(int index)180   public BufferedImage getThumbnail (int index)
181   {
182     // This throws a ClassCastException if the returned object is not
183     // a BufferedImage or an IndexOutOfBoundsException if index is
184     // out-of-bounds.
185     return (BufferedImage) thumbnails.get (index);
186   }
187 
188   /**
189    * Retrieve the list of thumbnails or null if there are no
190    * thumbnails associated with this IIOImage.  The returned reference
191    * can be used to update the thumbnails list.
192    *
193    * @return a list of thumbnails or null
194    */
getThumbnails()195   public List getThumbnails()
196   {
197     return thumbnails;
198   }
199 
200   /**
201    * Check whether this IIOImage stores its image data as a Raster or
202    * as a RenderedImage.
203    *
204    * @return true if this IIOImage uses the Raster representation,
205    * false if it uses the RenderedImage representation.
206    */
hasRaster()207   public boolean hasRaster()
208   {
209     return raster != null;
210   }
211 
212   /**
213    * Set this IIOImage's metadata.
214    *
215    * @param metadata the image metadata
216    */
setMetadata(IIOMetadata metadata)217   public void setMetadata (IIOMetadata metadata)
218   {
219     this.metadata = metadata;
220   }
221 
222   /**
223    * Set the raster data for this image.  This disposes of any
224    * existing rendered image data stored in this IIOImage.
225    *
226    * @param raster the image raster data
227    *
228    * @exception IllegalArgumentException if raster is null
229    */
setRaster(Raster raster)230   public void setRaster (Raster raster)
231   {
232     if (raster == null)
233       throw new IllegalArgumentException ("raster may not be null");
234 
235     this.image = null;
236     this.raster = raster;
237   }
238 
239   /**
240    * Set the rendered image data for this image.  This disposes of any
241    * existing raster data stored in this IIOImage.
242    *
243    * @param image the rendered image data
244    *
245    * @exception IllegalArgumentException if image is null
246    */
setRenderedImage(RenderedImage image)247   public void setRenderedImage (RenderedImage image)
248   {
249     if (image == null)
250       throw new IllegalArgumentException ("image may not be null");
251 
252     this.image = image;
253     this.raster = null;
254   }
255 
256   /**
257    * Set the list of thumbnails for this IIOImage to a new list of
258    * BufferedImages or to null.  Any existing thumbnails list is
259    * disposed.
260    *
261    * @param thumbnails a new list of thumbnails or null
262    */
setThumbnails(List thumbnails)263   public void setThumbnails (List thumbnails)
264   {
265     this.thumbnails = thumbnails;
266   }
267 }
268