1. /*
  2. * @(#)IIOImage.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.Raster;
  10. import java.awt.image.RenderedImage;
  11. import java.util.List;
  12. import javax.imageio.metadata.IIOMetadata;
  13. /**
  14. * A simple container class to aggregate an image, a set of
  15. * thumbnail (preview) images, and an object representing metadata
  16. * associated with the image.
  17. *
  18. * <p> The image data may take the form of either a
  19. * <code>RenderedImage</code>, or a <code>Raster</code>. Reader
  20. * methods that return an <code>IIOImage</code> will always return a
  21. * <code>BufferedImage</code> using the <code>RenderedImage</code>
  22. * reference. Writer methods that accept an <code>IIOImage</code>
  23. * will always accept a <code>RenderedImage</code>, and may optionally
  24. * accept a <code>Raster</code>.
  25. *
  26. * <p> Exactly one of <code>getRenderedImage</code> and
  27. * <code>getRaster</code> will return a non-<code>null</code> value.
  28. * Subclasses are responsible for ensuring this behavior.
  29. *
  30. * @see ImageReader#readAll(int, ImageReadParam)
  31. * @see ImageReader#readAll(java.util.Iterator)
  32. * @see ImageWriter#write(javax.imageio.metadata.IIOMetadata,
  33. * IIOImage, ImageWriteParam)
  34. * @see ImageWriter#write(IIOImage)
  35. * @see ImageWriter#writeToSequence(IIOImage, ImageWriteParam)
  36. * @see ImageWriter#writeInsert(int, IIOImage, ImageWriteParam)
  37. *
  38. * @version 0.5
  39. */
  40. public class IIOImage {
  41. /**
  42. * The <code>RenderedImage</code> being referenced.
  43. */
  44. protected RenderedImage image;
  45. /**
  46. * The <code>Raster</code> being referenced.
  47. */
  48. protected Raster raster;
  49. /**
  50. * A <code>List</code> of <code>BufferedImage</code> thumbnails,
  51. * or <code>null</code>. Non-<code>BufferedImage</code> objects
  52. * must not be stored in this <code>List</code>.
  53. */
  54. protected List thumbnails = null;
  55. /**
  56. * An <code>IIOMetadata</code> object containing metadata
  57. * associated with the image.
  58. */
  59. protected IIOMetadata metadata;
  60. /**
  61. * Constructs an <code>IIOImage</code> containing a
  62. * <code>RenderedImage</code>, and thumbnails and metadata
  63. * associated with it.
  64. *
  65. * <p> All parameters are stored by reference.
  66. *
  67. * <p> The <code>thumbnails</code> argument must either be
  68. * <code>null</code> or contain only <code>BufferedImage</code>
  69. * objects.
  70. *
  71. * @param image a <code>RenderedImage</code>.
  72. * @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,
  73. * or <code>null</code>.
  74. * @param metadata an <code>IIOMetadata</code> object, or
  75. * <code>null</code>.
  76. *
  77. * @exception IllegalArgumentException if <code>image</code> is
  78. * <code>null</code>.
  79. */
  80. public IIOImage(RenderedImage image,
  81. List thumbnails,
  82. IIOMetadata metadata) {
  83. if (image == null) {
  84. throw new IllegalArgumentException("image == null!");
  85. }
  86. this.image = image;
  87. this.raster = null;
  88. this.thumbnails = thumbnails;
  89. this.metadata = metadata;
  90. }
  91. /**
  92. * Constructs an <code>IIOImage</code> containing a
  93. * <code>Raster</code>, and thumbnails and metadata
  94. * associated with it.
  95. *
  96. * <p> All parameters are stored by reference.
  97. *
  98. * @param raster a <code>Raster</code>.
  99. * @param thumbnails a <code>List</code> of <code>BufferedImage</code>s,
  100. * or <code>null</code>.
  101. * @param metadata an <code>IIOMetadata</code> object, or
  102. * <code>null</code>.
  103. *
  104. * @exception IllegalArgumentException if <code>raster</code> is
  105. * <code>null</code>.
  106. */
  107. public IIOImage(Raster raster,
  108. List thumbnails,
  109. IIOMetadata metadata) {
  110. if (raster == null) {
  111. throw new IllegalArgumentException("raster == null!");
  112. }
  113. this.raster = raster;
  114. this.image = null;
  115. this.thumbnails = thumbnails;
  116. this.metadata = metadata;
  117. }
  118. /**
  119. * Returns the currently set <code>RenderedImage</code>, or
  120. * <code>null</code> if only a <code>Raster</code> is available.
  121. *
  122. * @return a <code>RenderedImage</code>, or <code>null</code>.
  123. *
  124. * @see #setRenderedImage
  125. */
  126. public RenderedImage getRenderedImage() {
  127. synchronized(this) {
  128. return image;
  129. }
  130. }
  131. /**
  132. * Sets the current <code>RenderedImage</code>. The value is
  133. * stored by reference. Any existing <code>Raster</code> is
  134. * discarded.
  135. *
  136. * @param image a <code>RenderedImage</code>.
  137. *
  138. * @exception IllegalArgumentException if <code>image</code> is
  139. * <code>null</code>.
  140. *
  141. * @see #getRenderedImage
  142. */
  143. public void setRenderedImage(RenderedImage image) {
  144. synchronized(this) {
  145. if (image == null) {
  146. throw new IllegalArgumentException("image == null!");
  147. }
  148. this.image = image;
  149. this.raster = null;
  150. }
  151. }
  152. /**
  153. * Returns <code>true</code> if this <code>IIOImage</code> stores
  154. * a <code>Raster</code> rather than a <code>RenderedImage</code>.
  155. *
  156. * @return <code>true</code> if a <code>Raster</code> is
  157. * available.
  158. */
  159. public boolean hasRaster() {
  160. synchronized(this) {
  161. return (raster != null);
  162. }
  163. }
  164. /**
  165. * Returns the currently set <code>Raster</code>, or
  166. * <code>null</code> if only a <code>RenderedImage</code> is
  167. * available.
  168. *
  169. * @return a <code>Raster</code>, or <code>null</code>.
  170. *
  171. * @see #setRaster
  172. */
  173. public Raster getRaster() {
  174. synchronized(this) {
  175. return raster;
  176. }
  177. }
  178. /**
  179. * Sets the current <code>Raster</code>. The value is
  180. * stored by reference. Any existing <code>RenderedImage</code> is
  181. * discarded.
  182. *
  183. * @param raster a <code>Raster</code>.
  184. *
  185. * @exception IllegalArgumentException if <code>raster</code> is
  186. * <code>null</code>.
  187. *
  188. * @see #getRaster
  189. */
  190. public void setRaster(Raster raster) {
  191. synchronized(this) {
  192. if (raster == null) {
  193. throw new IllegalArgumentException("raster == null!");
  194. }
  195. this.raster = raster;
  196. this.image = null;
  197. }
  198. }
  199. /**
  200. * Returns the number of thumbnails stored in this
  201. * <code>IIOImage</code>.
  202. *
  203. * @return the number of thumbnails, as an <code>int</code>.
  204. */
  205. public int getNumThumbnails() {
  206. return thumbnails == null ? 0 : thumbnails.size();
  207. }
  208. /**
  209. * Returns a thumbnail associated with the main image.
  210. *
  211. * @param index the index of the desired thumbnail image.
  212. *
  213. * @return a thumbnail image, as a <code>BufferedImage</code>.
  214. *
  215. * @exception IndexOutOfBoundsException if the supplied index is
  216. * negative or larger than the largest valid index.
  217. * @exception ClassCastException if a
  218. * non-<code>BufferedImage</code> object is encountered in the
  219. * list of thumbnails at the given index.
  220. *
  221. * @see #getThumbnails
  222. * @see #setThumbnails
  223. */
  224. public BufferedImage getThumbnail(int index) {
  225. if (thumbnails == null) {
  226. throw new IndexOutOfBoundsException("No thumbnails available!");
  227. }
  228. return (BufferedImage)thumbnails.get(index);
  229. }
  230. /**
  231. * Returns the current <code>List</code> of thumbnail
  232. * <code>BufferedImage</code>s, or <code>null</code> if none is
  233. * set. A live reference is returned.
  234. *
  235. * @return the current <code>List</code> of
  236. * <code>BufferedImage</code> thumbnails, or <code>null</code>.
  237. *
  238. * @see #getThumbnail(int)
  239. * @see #setThumbnails
  240. */
  241. public List getThumbnails() {
  242. return thumbnails;
  243. }
  244. /**
  245. * Sets the list of thumbnails to a new <code>List</code> of
  246. * <code>BufferedImage</code>s, or to <code>null</code>. The
  247. * reference to the previous <code>List</code> is discarded.
  248. *
  249. * <p> The <code>thumbnails</code> argument must either be
  250. * <code>null</code> or contain only <code>BufferedImage</code>
  251. * objects.
  252. *
  253. * @param thumbnails a <code>List</code> of
  254. * <code>BufferedImage</code> thumbnails, or <code>null</code>.
  255. *
  256. * @see #getThumbnail(int)
  257. * @see #getThumbnails
  258. */
  259. public void setThumbnails(List thumbnails) {
  260. this.thumbnails = thumbnails;
  261. }
  262. /**
  263. * Returns a reference to the current <code>IIOMetadata</code>
  264. * object, or <code>null</code> is none is set.
  265. *
  266. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  267. *
  268. * @see #setMetadata
  269. */
  270. public IIOMetadata getMetadata() {
  271. return metadata;
  272. }
  273. /**
  274. * Sets the <code>IIOMetadata</code> to a new object, or
  275. * <code>null</code>.
  276. *
  277. * @param metadata an <code>IIOMetadata</code> object, or
  278. * <code>null</code>.
  279. *
  280. * @see #getMetadata
  281. */
  282. public void setMetadata(IIOMetadata metadata) {
  283. this.metadata = metadata;
  284. }
  285. }