IIOImage.java 7.6 KB

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