1. /*
  2. * @(#)ImageReader.java 1.138 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.Point;
  9. import java.awt.Rectangle;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.Raster;
  12. import java.awt.image.RenderedImage;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.Locale;
  18. import java.util.MissingResourceException;
  19. import java.util.ResourceBundle;
  20. import java.util.Set;
  21. import javax.imageio.spi.ImageReaderSpi;
  22. import javax.imageio.event.IIOReadWarningListener;
  23. import javax.imageio.event.IIOReadProgressListener;
  24. import javax.imageio.event.IIOReadUpdateListener;
  25. import javax.imageio.metadata.IIOMetadata;
  26. import javax.imageio.metadata.IIOMetadataFormatImpl;
  27. import javax.imageio.stream.ImageInputStream;
  28. /**
  29. * An abstract superclass for parsing and decoding of images. This
  30. * class must be subclassed by classes that read in images in the
  31. * context of the Java Image I/O framework.
  32. *
  33. * <p> <code>ImageReader</code> objects are normally instantiated by
  34. * the service provider interface (SPI) class for the specific format.
  35. * Service provider classes (e.g., instances of
  36. * <code>ImageReaderSpi</code>) are registered with the
  37. * <code>IIORegistry</code>, which uses them for format recognition
  38. * and presentation of available format readers and writers.
  39. *
  40. * <p> When an input source is set (using the <code>setInput</code>
  41. * method), it may be marked as "seek forward only". This setting
  42. * means that images contained within the input source will only be
  43. * read in order, possibly allowing the reader to avoid caching
  44. * portions of the input containing data associated with images that
  45. * have been read previously.
  46. *
  47. * @see ImageWriter
  48. * @see javax.imageio.spi.IIORegistry
  49. * @see javax.imageio.spi.ImageReaderSpi
  50. *
  51. * @version 0.5
  52. */
  53. public abstract class ImageReader {
  54. /**
  55. * The <code>ImageReaderSpi</code> that instantiated this object,
  56. * or <code>null</code> if its identity is not known or none
  57. * exists. By default it is initialized to <code>null</code>.
  58. */
  59. protected ImageReaderSpi originatingProvider;
  60. /**
  61. * The <code>ImageInputStream</code> or other
  62. * <code>Object</code> by <code>setInput</code> and retrieved
  63. * by <code>getInput</code>. By default it is initialized to
  64. * <code>null</code>.
  65. */
  66. protected Object input = null;
  67. /**
  68. * <code>true</code> if the current input source has been marked
  69. * as allowing only forward seeking by <code>setInput</code>. By
  70. * default, the value is <code>false</code>.
  71. *
  72. * @see #minIndex
  73. * @see #setInput
  74. */
  75. protected boolean seekForwardOnly = false;
  76. /**
  77. * <code>true</code> if the current input source has been marked
  78. * as allowing metadata to be ignored by <code>setInput</code>.
  79. * By default, the value is <code>false</code>.
  80. *
  81. * @see #setInput
  82. */
  83. protected boolean ignoreMetadata = false;
  84. /**
  85. * The smallest valid index for reading, initially 0. When
  86. * <code>seekForwardOnly</code> is <code>true</code>, various methods
  87. * may throw an <code>IndexOutOfBoundsException</code> on an
  88. * attempt to access data associate with an image having a lower
  89. * index.
  90. *
  91. * @see #seekForwardOnly
  92. * @see #setInput
  93. */
  94. protected int minIndex = 0;
  95. /**
  96. * An array of <code>Locale</code>s which may be used to localize
  97. * warning messages, or <code>null</code> if localization is not
  98. * supported.
  99. */
  100. protected Locale[] availableLocales = null;
  101. /**
  102. * The current <code>Locale</code> to be used for localization, or
  103. * <code>null</code> if none has been set.
  104. */
  105. protected Locale locale = null;
  106. /**
  107. * A <code>List</code> of currently registered
  108. * <code>IIOReadWarningListener</code>s, initialized by default to
  109. * <code>null</code>, which is synonymous with an empty
  110. * <code>List</code>.
  111. */
  112. protected List warningListeners = null;
  113. /**
  114. * A <code>List</code> of the <code>Locale</code>s associated with
  115. * each currently registered <code>IIOReadWarningListener</code>,
  116. * initialized by default to <code>null</code>, which is
  117. * synonymous with an empty <code>List</code>.
  118. */
  119. protected List warningLocales = null;
  120. /**
  121. * A <code>List</code> of currently registered
  122. * <code>IIOReadProgressListener</code>s, initialized by default
  123. * to <code>null</code>, which is synonymous with an empty
  124. * <code>List</code>.
  125. */
  126. protected List progressListeners = null;
  127. /**
  128. * A <code>List</code> of currently registered
  129. * <code>IIOReadUpdateListener</code>s, initialized by default to
  130. * <code>null</code>, which is synonymous with an empty
  131. * <code>List</code>.
  132. */
  133. protected List updateListeners = null;
  134. /**
  135. * If <code>true</code>, the current read operation should be
  136. * aborted.
  137. */
  138. private boolean abortFlag = false;
  139. /**
  140. * Constructs an <code>ImageReader</code> and sets its
  141. * <code>originatingProvider</code> field to the supplied value.
  142. *
  143. * <p> Subclasses that make use of extensions should provide a
  144. * constructor with signature <code>(ImageReaderSpi,
  145. * Object)</code> in order to retrieve the extension object. If
  146. * the extension object is unsuitable, an
  147. * <code>IllegalArgumentException</code> should be thrown.
  148. *
  149. * @param originatingProvider the <code>ImageReaderSpi</code> that is
  150. * invoking this constructor, or <code>null</code>.
  151. */
  152. protected ImageReader(ImageReaderSpi originatingProvider) {
  153. this.originatingProvider = originatingProvider;
  154. }
  155. /**
  156. * Returns a <code>String</code> identifying the format of the
  157. * input source.
  158. *
  159. * <p> The default implementation returns
  160. * <code>originatingProvider.getFormatNames()[0]</code>.
  161. * Implementations that may not have an originating service
  162. * provider, or which desire a different naming policy should
  163. * override this method.
  164. *
  165. * @exception IOException if an error occurs reading the
  166. * information from the input source.
  167. *
  168. * @return the format name, as a <code>String</code>.
  169. */
  170. public String getFormatName() throws IOException {
  171. return originatingProvider.getFormatNames()[0];
  172. }
  173. /**
  174. * Returns the <code>ImageReaderSpi</code> that was passed in on
  175. * the constructor. Note that this value may be <code>null</code>.
  176. *
  177. * @return an <code>ImageReaderSpi</code>, or <code>null</code>.
  178. *
  179. * @see ImageReaderSpi
  180. */
  181. public ImageReaderSpi getOriginatingProvider() {
  182. return originatingProvider;
  183. }
  184. /**
  185. * Sets the input source to use to the given
  186. * <code>ImageInputStream</code> or other <code>Object</code>.
  187. * The input source must be set before any of the query or read
  188. * methods are used. If <code>input</code> is <code>null</code>,
  189. * any currently set input source will be removed. In any case,
  190. * the value of <code>minIndex</code> will be initialized to 0.
  191. *
  192. * <p> The <code>seekForwardOnly</code> parameter controls whether
  193. * the value returned by <code>getMinIndex</code> will be
  194. * increased as each image (or thumbnail, or image metadata) is
  195. * read. If <code>seekForwardOnly</code> is true, then a call to
  196. * <code>read(index)</code> will throw an
  197. * <code>IndexOutOfBoundsException</code> if <code>index <
  198. * this.minIndex</code> otherwise, the value of
  199. * <code>minIndex</code> will be set to <code>index</code>. If
  200. * <code>seekForwardOnly</code> is <code>false</code>, the value of
  201. * <code>minIndex</code> will remain 0 regardless of any read
  202. * operations.
  203. *
  204. * <p> The <code>ignoreMetadata</code> parameter, if set to
  205. * <code>true</code>, allows the reader to disregard any metadata
  206. * encountered during the read. Subsequent calls to the
  207. * <code>getStreamMetadata</code> and
  208. * <code>getImageMetadata</code> methods may return
  209. * <code>null</code>, and an <code>IIOImage</code> returned from
  210. * <code>readAll</code> may return <code>null</code> from their
  211. * <code>getMetadata</code> method. Setting this parameter may
  212. * allow the reader to work more efficiently. The reader may
  213. * choose to disregard this setting and return metadata normally.
  214. *
  215. * <p> Subclasses should take care to remove any cached
  216. * information based on the previous stream, such as header
  217. * information or partially decoded image data.
  218. *
  219. * <p> Use of a general <code>Object</code> other than an
  220. * <code>ImageInputStream</code> is intended for readers that
  221. * interact directly with a capture device or imaging protocol.
  222. * The set of legal classes is advertised by the reader's service
  223. * provider's <code>getInputTypes</code> method; most readers
  224. * will return a single-element array containing only
  225. * <code>ImageInputStream.class</code> to indicate that they
  226. * accept only an <code>ImageInputStream</code>.
  227. *
  228. * <p> The default implementation checks the <code>input</code>
  229. * argument against the list returned by
  230. * <code>originatingProvider.getInputTypes()</code> and fails
  231. * if the argument is not an instance of one of the classes
  232. * in the list. If the originating provider is set to
  233. * <code>null</code>, the input is accepted only if it is an
  234. * <code>ImageInputStream</code>.
  235. *
  236. * @param input the <code>ImageInputStream</code> or other
  237. * <code>Object</code> to use for future decoding.
  238. * @param seekForwardOnly if <code>true</code>, images and metadata
  239. * may only be read in ascending order from this input source.
  240. * @param ignoreMetadata if <code>true</code>, metadata
  241. * may be ignored during reads.
  242. *
  243. * @exception IllegalArgumentException if <code>input</code> is
  244. * not an instance of one of the classes returned by the
  245. * originating service provider's <code>getInputTypes</code>
  246. * method, or is not an <code>ImageInputStream</code>.
  247. *
  248. * @see ImageInputStream
  249. * @see #getInput
  250. * @see javax.imageio.spi.ImageReaderSpi#getInputTypes
  251. */
  252. public void setInput(Object input,
  253. boolean seekForwardOnly,
  254. boolean ignoreMetadata) {
  255. if (input != null) {
  256. boolean found = false;
  257. if (originatingProvider != null) {
  258. Class[] classes = originatingProvider.getInputTypes();
  259. for (int i = 0; i < classes.length; i++) {
  260. if (classes[i].isInstance(input)) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. } else {
  266. if (input instanceof ImageInputStream) {
  267. found = true;
  268. }
  269. }
  270. if (!found) {
  271. throw new IllegalArgumentException("Incorrect input type!");
  272. }
  273. this.seekForwardOnly = seekForwardOnly;
  274. this.ignoreMetadata = ignoreMetadata;
  275. this.minIndex = 0;
  276. }
  277. this.input = input;
  278. }
  279. /**
  280. * Sets the input source to use to the given
  281. * <code>ImageInputStream</code> or other <code>Object</code>.
  282. * The input source must be set before any of the query or read
  283. * methods are used. If <code>input</code> is <code>null</code>,
  284. * any currently set input source will be removed. In any case,
  285. * the value of <code>minIndex</code> will be initialized to 0.
  286. *
  287. * <p> The <code>seekForwardOnly</code> parameter controls whether
  288. * the value returned by <code>getMinIndex</code> will be
  289. * increased as each image (or thumbnail, or image metadata) is
  290. * read. If <code>seekForwardOnly</code> is true, then a call to
  291. * <code>read(index)</code> will throw an
  292. * <code>IndexOutOfBoundsException</code> if <code>index <
  293. * this.minIndex</code> otherwise, the value of
  294. * <code>minIndex</code> will be set to <code>index</code>. If
  295. * <code>seekForwardOnly</code> is <code>false</code>, the value of
  296. * <code>minIndex</code> will remain 0 regardless of any read
  297. * operations.
  298. *
  299. * <p> This method is equivalent to <code>setInput(input,
  300. * seekForwardOnly, false)</code>.
  301. *
  302. * @param input the <code>ImageInputStream</code> or other
  303. * <code>Object</code> to use for future decoding.
  304. * @param seekForwardOnly if <code>true</code>, images and metadata
  305. * may only be read in ascending order from this input source.
  306. *
  307. * @exception IllegalArgumentException if <code>input</code> is
  308. * not an instance of one of the classes returned by the
  309. * originating service provider's <code>getInputTypes</code>
  310. * method, or is not an <code>ImageInputStream</code>.
  311. *
  312. * @see #getInput
  313. */
  314. public void setInput(Object input,
  315. boolean seekForwardOnly) {
  316. setInput(input, seekForwardOnly, false);
  317. }
  318. /**
  319. * Sets the input source to use to the given
  320. * <code>ImageInputStream</code> or other <code>Object</code>.
  321. * The input source must be set before any of the query or read
  322. * methods are used. If <code>input</code> is <code>null</code>,
  323. * any currently set input source will be removed. In any case,
  324. * the value of <code>minIndex</code> will be initialized to 0.
  325. *
  326. * <p> This method is equivalent to <code>setInput(input, false,
  327. * false)</code>.
  328. *
  329. * @param input the <code>ImageInputStream</code> or other
  330. * <code>Object</code> to use for future decoding.
  331. *
  332. * @exception IllegalArgumentException if <code>input</code> is
  333. * not an instance of one of the classes returned by the
  334. * originating service provider's <code>getInputTypes</code>
  335. * method, or is not an <code>ImageInputStream</code>.
  336. *
  337. * @see #getInput
  338. */
  339. public void setInput(Object input) {
  340. setInput(input, false, false);
  341. }
  342. /**
  343. * Returns the <code>ImageInputStream</code> or other
  344. * <code>Object</code> previously set as the input source. If the
  345. * input source has not been set, <code>null</code> is returned.
  346. *
  347. * @return the <code>Object</code> that will be used for future
  348. * decoding, or <code>null</code>.
  349. *
  350. * @see ImageInputStream
  351. * @see #setInput
  352. */
  353. public Object getInput() {
  354. return input;
  355. }
  356. /**
  357. * Returns <code>true</code> if the current input source has been
  358. * marked as seek forward only by passing <code>true</code> as the
  359. * <code>seekForwardOnly</code> argument to the
  360. * <code>setInput</code> method.
  361. *
  362. * @return <code>true</code> if the input source is seek forward
  363. * only.
  364. *
  365. * @see #setInput
  366. */
  367. public boolean isSeekForwardOnly() {
  368. return seekForwardOnly;
  369. }
  370. /**
  371. * Returns <code>true</code> if the current input source has been
  372. * marked as allowing metadata to be ignored by passing
  373. * <code>true</code> as the <code>ignoreMetadata</code> argument
  374. * to the <code>setInput</code> method.
  375. *
  376. * @return <code>true</code> if the metadata may be ignored.
  377. *
  378. * @see #setInput
  379. */
  380. public boolean isIgnoringMetadata() {
  381. return ignoreMetadata;
  382. }
  383. /**
  384. * Returns the lowest valid index for reading an image, thumbnail,
  385. * or image metadata. If <code>seekForwardOnly()</code> is
  386. * <code>false</code>, this value will typically remain 0,
  387. * indicating that random access is possible. Otherwise, it will
  388. * contain the value of the most recently accessed index, and
  389. * increase in a monotonic fashion.
  390. *
  391. * @return the minimum legal index for reading.
  392. */
  393. public int getMinIndex() {
  394. return minIndex;
  395. }
  396. // Localization
  397. /**
  398. * Returns an array of <code>Locale</code>s that may be used to
  399. * localize warning listeners and compression settings. A return
  400. * value of <code>null</code> indicates that localization is not
  401. * supported.
  402. *
  403. * <p> The default implementation returns a clone of the
  404. * <code>availableLocales</code> instance variable if it is
  405. * non-<code>null</code>, or else returns <code>null</code>.
  406. *
  407. * @return an array of <code>Locale</code>s that may be used as
  408. * arguments to <code>setLocale</code>, or <code>null</code>.
  409. */
  410. public Locale[] getAvailableLocales() {
  411. if (availableLocales == null) {
  412. return null;
  413. } else {
  414. return (Locale[])availableLocales.clone();
  415. }
  416. }
  417. /**
  418. * Sets the current <code>Locale</code> of this
  419. * <code>ImageReader</code> to the given value. A value of
  420. * <code>null</code> removes any previous setting, and indicates
  421. * that the reader should localize as it sees fit.
  422. *
  423. * @param locale the desired <code>Locale</code>, or
  424. * <code>null</code>.
  425. *
  426. * @exception IllegalArgumentException if <code>locale</code> is
  427. * non-<code>null</code> but is not one of the values returned by
  428. * <code>getAvailableLocales</code>.
  429. *
  430. * @see #getLocale
  431. */
  432. public void setLocale(Locale locale) {
  433. if (locale != null) {
  434. Locale[] locales = getAvailableLocales();
  435. boolean found = false;
  436. if (locales != null) {
  437. for (int i = 0; i < locales.length; i++) {
  438. if (locale.equals(locales[i])) {
  439. found = true;
  440. break;
  441. }
  442. }
  443. }
  444. if (!found) {
  445. throw new IllegalArgumentException("Invalid locale!");
  446. }
  447. }
  448. this.locale = locale;
  449. }
  450. /**
  451. * Returns the currently set <code>Locale</code>, or
  452. * <code>null</code> if none has been set.
  453. *
  454. * @return the current <code>Locale</code>, or <code>null</code>.
  455. *
  456. * @see #setLocale
  457. */
  458. public Locale getLocale() {
  459. return locale;
  460. }
  461. // Image queries
  462. /**
  463. * Returns the number of images, not including thumbnails, available
  464. * from the current input source.
  465. *
  466. * <p> Note that some image formats (such as animated GIF) do not
  467. * specify how many images are present in the stream. Thus
  468. * determining the number of images will require the entire stream
  469. * to be scanned and may require memory for buffering. If images
  470. * are to be processed in order, it may be more efficient to
  471. * simply call <code>read</code> with increasing indices until an
  472. * <code>IndexOutOfBoundsException</code> is thrown to indicate
  473. * that no more images are available. The
  474. * <code>allowSearch</code> parameter may be set to
  475. * <code>false</code> to indicate that an exhaustive search is not
  476. * desired; the return value will be <code>-1</code> to indicate
  477. * that a search is necessary. If the input has been specified
  478. * with <code>seekForwardOnly</code> set to <code>true</code>,
  479. * this method throws an <code>IllegalStateException</code> if
  480. * <code>allowSearch</code> is set to <code>true</code>.
  481. *
  482. * @param allowSearch if <code>true</code>, the true number of
  483. * images will be returned even if a search is required. If
  484. * <code>false</code>, the reader may return <code>-1</code>
  485. * without performing the search.
  486. *
  487. * @return the number of images, as an <code>int</code>, or
  488. * <code>-1</code> if <code>allowSearch</code> is
  489. * <code>false</code> and a search would be required.
  490. *
  491. * @exception IllegalStateException if the input source has not been set,
  492. * or if the input has been specified with <code>seekForwardOnly</code>
  493. * set to <code>true</code>.
  494. * @exception IOException if an error occurs reading the
  495. * information from the input source.
  496. *
  497. * @see #setInput
  498. */
  499. public abstract int getNumImages(boolean allowSearch) throws IOException;
  500. /**
  501. * Returns the width in pixels of the given image within the input
  502. * source.
  503. *
  504. * <p> If the image can be rendered to a user-specified size, then
  505. * this method returns the default width.
  506. *
  507. * @param imageIndex the index of the image to be queried.
  508. *
  509. * @return the width of the image, as an <code>int</code>.
  510. *
  511. * @exception IllegalStateException if the input source has not been set.
  512. * @exception IndexOutOfBoundsException if the supplied index is
  513. * out of bounds.
  514. * @exception IOException if an error occurs reading the width
  515. * information from the input source.
  516. */
  517. public abstract int getWidth(int imageIndex) throws IOException;
  518. /**
  519. * Returns the height in pixels of the given image within the
  520. * input source.
  521. *
  522. * <p> If the image can be rendered to a user-specified size, then
  523. * this method returns the default height.
  524. *
  525. * @param imageIndex the index of the image to be queried.
  526. *
  527. * @return the height of the image, as an <code>int</code>.
  528. *
  529. * @exception IllegalStateException if the input source has not been set.
  530. * @exception IndexOutOfBoundsException if the supplied index is
  531. * out of bounds.
  532. * @exception IOException if an error occurs reading the height
  533. * information from the input source.
  534. */
  535. public abstract int getHeight(int imageIndex) throws IOException;
  536. /**
  537. * Returns <code>true</code> if the storage format of the given
  538. * image places no inherent impediment on random access to pixels.
  539. * For most compressed formats, such as JPEG, this method should
  540. * return <code>false</code>, as a large section of the image in
  541. * addition to the region of interest may need to be decoded.
  542. *
  543. * <p> This is merely a hint for programs that wish to be
  544. * efficient; all readers must be able to read arbitrary regions
  545. * as specified in an <code>ImageReadParam</code>.
  546. *
  547. * <p> Note that formats that return <code>false</code> from
  548. * this method may nonetheless allow tiling (<i>e.g.</i> Restart
  549. * Markers in JPEG), and random access will likely be reasonably
  550. * efficient on tiles. See {@link #isImageTiled
  551. * <code>isImageTiled</code>}.
  552. *
  553. * <p> A reader for which all images are guaranteed to support
  554. * easy random access, or are guaranteed not to support easy
  555. * random access, may return <code>true</code> or
  556. * <code>false</code> respectively without accessing any image
  557. * data. In such cases, it is not necessary to throw an exception
  558. * even if no input source has been set or the image index is out
  559. * of bounds.
  560. *
  561. * <p> The default implementation returns <code>false</code>.
  562. *
  563. * @param imageIndex the index of the image to be queried.
  564. *
  565. * @return <code>true</code> if reading a region of interest of
  566. * the given image is likely to be efficient.
  567. *
  568. * @exception IllegalStateException if an input source is required
  569. * to determine the return value, but none has been set.
  570. * @exception IndexOutOfBoundsException if an image must be
  571. * accessed to determine the return value, but the supplied index
  572. * is out of bounds.
  573. * @exception IOException if an error occurs during reading.
  574. */
  575. public boolean isRandomAccessEasy(int imageIndex) throws IOException {
  576. return false;
  577. }
  578. /**
  579. * Returns the aspect ratio of the given image (that is, its width
  580. * divided by its height) as a <code>float</code>. For images
  581. * that are inherently resizable, this method provides a way to
  582. * determine the appropriate width given a deired height, or vice
  583. * versa. For non-resizable images, the true width and height
  584. * are used.
  585. *
  586. * <p> The default implementation simply returns
  587. * <code>(float)getWidth(imageIndex)/getHeight(imageIndex)</code>.
  588. *
  589. * @param imageIndex the index of the image to be queried.
  590. *
  591. * @return a <code>float</code> indicating the aspect ratio of the
  592. * given image.
  593. *
  594. * @exception IllegalStateException if the input source has not been set.
  595. * @exception IndexOutOfBoundsException if the supplied index is
  596. * out of bounds.
  597. * @exception IOException if an error occurs during reading.
  598. */
  599. public float getAspectRatio(int imageIndex) throws IOException {
  600. return (float)getWidth(imageIndex)/getHeight(imageIndex);
  601. }
  602. /**
  603. * Returns an <code>ImageTypeSpecifier</code> indicating the
  604. * <code>SampleModel</code> and <code>ColorModel</code> which most
  605. * closely represents the "raw" internal format of the image. For
  606. * example, for a JPEG image the raw type might have a YCbCr color
  607. * space even though the image would conventionally be transformed
  608. * into an RGB color space prior to display. The returned value
  609. * should also be included in the list of values returned by
  610. * <code>getImageTypes</code>.
  611. *
  612. * <p> The default implementation simply returns the first entry
  613. * from the list provided by <code>getImageType</code>.
  614. *
  615. * @param imageIndex the index of the image to be queried.
  616. *
  617. * @return an <code>ImageTypeSpecifier</code>.
  618. *
  619. * @exception IllegalStateException if the input source has not been set.
  620. * @exception IndexOutOfBoundsException if the supplied index is
  621. * out of bounds.
  622. * @exception IOException if an error occurs reading the format
  623. * information from the input source.
  624. */
  625. public ImageTypeSpecifier getRawImageType(int imageIndex)
  626. throws IOException {
  627. return (ImageTypeSpecifier)getImageTypes(imageIndex).next();
  628. }
  629. /**
  630. * Returns an <code>Iterator</code> containing possible image
  631. * types to which the given image may be decoded, in the form of
  632. * <code>ImageTypeSpecifiers</code>s. At least one legal image
  633. * type will be returned.
  634. *
  635. * <p> The first element of the iterator should be the most
  636. * "natural" type for decoding the image with as little loss as
  637. * possible. For example, for a JPEG image the first entry should
  638. * be an RGB image, even though the image data is stored
  639. * internally in a YCbCr color space.
  640. *
  641. * @param imageIndex the index of the image to be
  642. * <code>retrieved</code>.
  643. *
  644. * @return an <code>Iterator</code> containing at least one
  645. * <code>ImageTypeSpecifier</code> representing suggested image
  646. * types for decoding the current given image.
  647. *
  648. * @exception IllegalStateException if the input source has not been set.
  649. * @exception IndexOutOfBoundsException if the supplied index is
  650. * out of bounds.
  651. * @exception IOException if an error occurs reading the format
  652. * information from the input source.
  653. *
  654. * @see ImageReadParam#setDestination(BufferedImage)
  655. * @see ImageReadParam#setDestinationType(ImageTypeSpecifier)
  656. */
  657. public abstract Iterator getImageTypes(int imageIndex) throws IOException;
  658. /**
  659. * Returns a default <code>ImageReadParam</code> object
  660. * appropriate for this format. All subclasses should define a
  661. * set of default values for all parameters and return them with
  662. * this call. This method may be called before the input source
  663. * is set.
  664. *
  665. * <p> The default implementation constructs and returns a new
  666. * <code>ImageReadParam</code> object that does not allow source
  667. * scaling (<i>i.e.</i>, it returns <code>new
  668. * ImageReadParam()</code>.
  669. *
  670. * @return an <code>ImageReadParam</code> object which may be used
  671. * to control the decoding process using a set of default settings.
  672. */
  673. public ImageReadParam getDefaultReadParam() {
  674. return new ImageReadParam();
  675. }
  676. /**
  677. * Returns an <code>IIOMetadata</code> object representing the
  678. * metadata associated with the input source as a whole (i.e., not
  679. * associated with any particular image), or <code>null</code> if
  680. * the reader does not support reading metadata, is set to ignore
  681. * metadata, or if no metadata is available.
  682. *
  683. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  684. *
  685. * @exception IOException if an error occurs during reading.
  686. */
  687. public abstract IIOMetadata getStreamMetadata() throws IOException;
  688. /**
  689. * Returns an <code>IIOMetadata</code> object representing the
  690. * metadata associated with the input source as a whole (i.e.,
  691. * not associated with any particular image). If no such data
  692. * exists, <code>null</code> is returned.
  693. *
  694. * <p> The resuting metadata object is only responsible for
  695. * returning documents in the format named by
  696. * <code>formatName</code>. Within any documents that are
  697. * returned, only nodes whose names are members of
  698. * <code>nodeNames</code> are required to be returned. In this
  699. * way, the amount of metadata processing done by the reader may
  700. * be kept to a minimum, based on what information is actually
  701. * needed.
  702. *
  703. * <p> If <code>formatName</code> is not the name of a supported
  704. * metadata format, <code>null</code> is returned.
  705. *
  706. * <p> In all cases, it is legal to return a more capable metadata
  707. * object than strictly necessary. The format name and node names
  708. * are merely hints that may be used to reduce the reader's
  709. * workload.
  710. *
  711. * <p> The default implementation simply returns the result of
  712. * calling <code>getStreamMetadata()</code>, after checking that
  713. * the format name is supported. If it is not,
  714. * <code>null</code> is returned.
  715. *
  716. * @param formatName a metadata format name that may be used to retrieve
  717. * a document from the returned <code>IIOMetadata</code> object.
  718. * @param nodeNames a <code>Set</code> containing the names of
  719. * nodes that may be contained in a retrieved document.
  720. *
  721. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  722. *
  723. * @exception IllegalArgumentException if <code>formatName</code>
  724. * is <code>null</code>.
  725. * @exception IllegalArgumentException if <code>nodeNames</code>
  726. * is <code>null</code>.
  727. * @exception IOException if an error occurs during reading.
  728. */
  729. public IIOMetadata getStreamMetadata(String formatName, Set nodeNames)
  730. throws IOException {
  731. return getMetadata(formatName, nodeNames, true, 0);
  732. }
  733. private IIOMetadata getMetadata(String formatName,
  734. Set nodeNames,
  735. boolean wantStream,
  736. int imageIndex) throws IOException {
  737. if (formatName == null) {
  738. throw new IllegalArgumentException("formatName == null!");
  739. }
  740. if (nodeNames == null) {
  741. throw new IllegalArgumentException("nodeNames == null!");
  742. }
  743. IIOMetadata metadata =
  744. wantStream
  745. ? getStreamMetadata()
  746. : getImageMetadata(imageIndex);
  747. if (metadata != null) {
  748. if (metadata.isStandardMetadataFormatSupported() &&
  749. formatName.equals
  750. (IIOMetadataFormatImpl.standardMetadataFormatName)) {
  751. return metadata;
  752. }
  753. String nativeName = metadata.getNativeMetadataFormatName();
  754. if (nativeName != null && formatName.equals(nativeName)) {
  755. return metadata;
  756. }
  757. String[] extraNames = metadata.getExtraMetadataFormatNames();
  758. if (extraNames != null) {
  759. for (int i = 0; i < extraNames.length; i++) {
  760. if (formatName.equals(extraNames[i])) {
  761. return metadata;
  762. }
  763. }
  764. }
  765. }
  766. return null;
  767. }
  768. /**
  769. * Returns an <code>IIOMetadata</code> object containing metadata
  770. * associated with the given image, or <code>null</code> if the
  771. * reader does not support reading metadata, is set to ignore
  772. * metadata, or if no metadata is available.
  773. *
  774. * @param imageIndex the index of the image whose metadata is to
  775. * be retrieved.
  776. *
  777. * @return an <code>IIOMetadata</code> object, or
  778. * <code>null</code>.
  779. *
  780. * @exception IllegalStateException if the input source has not been
  781. * set.
  782. * @exception IndexOutOfBoundsException if the supplied index is
  783. * out of bounds.
  784. * @exception IOException if an error occurs during reading.
  785. */
  786. public abstract IIOMetadata getImageMetadata(int imageIndex)
  787. throws IOException;
  788. /**
  789. * Returns an <code>IIOMetadata</code> object representing the
  790. * metadata associated with the given image, or <code>null</code>
  791. * if the reader does not support reading metadata or none
  792. * is available.
  793. *
  794. * <p> The resuting metadata object is only responsible for
  795. * returning documents in the format named by
  796. * <code>formatName</code>. Within any documents that are
  797. * returned, only nodes whose names are members of
  798. * <code>nodeNames</code> are required to be returned. In this
  799. * way, the amount of metadata processing done by the reader may
  800. * be kept to a minimum, based on what information is actually
  801. * needed.
  802. *
  803. * <p> If <code>formatName</code> is not the name of a supported
  804. * metadata format, <code>null</code> may be returned.
  805. *
  806. * <p> In all cases, it is legal to return a more capable metadata
  807. * object than strictly necessary. The format name and node names
  808. * are merely hints that may be used to reduce the reader's
  809. * workload.
  810. *
  811. * <p> The default implementation simply returns the result of
  812. * calling <code>getImageMetadata(imageIndex)</code>, after
  813. * checking that the format name is supported. If it is not,
  814. * <code>null</code> is returned.
  815. *
  816. * @param imageIndex the index of the image whose metadata is to
  817. * be retrieved.
  818. * @param formatName a metadata format name that may be used to retrieve
  819. * a document from the returned <code>IIOMetadata</code> object.
  820. * @param nodeNames a <code>Set</code> containing the names of
  821. * nodes that may be contained in a retrieved document.
  822. *
  823. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  824. *
  825. * @exception IllegalStateException if the input source has not been
  826. * set.
  827. * @exception IndexOutOfBoundsException if the supplied index is
  828. * out of bounds.
  829. * @exception IllegalArgumentException if <code>formatName</code>
  830. * is <code>null</code>.
  831. * @exception IllegalArgumentException if <code>nodeNames</code>
  832. * is <code>null</code>.
  833. * @exception IOException if an error occurs during reading.
  834. */
  835. public IIOMetadata getImageMetadata(int imageIndex,
  836. String formatName, Set nodeNames)
  837. throws IOException {
  838. return getMetadata(formatName, nodeNames, false, imageIndex);
  839. }
  840. /**
  841. * Reads the image indexed by <code>imageIndex</code> and returns
  842. * it as a complete <code>BufferedImage</code>, using a default
  843. * <code>ImageReadParam</code>. This is a convenience method
  844. * that calls <code>read(imageIndex, null)</code>.
  845. *
  846. * <p> The image returned will be formatted according to the first
  847. * <code>ImageTypeSpecifier</code> returned from
  848. * <code>getImageTypes</code>.
  849. *
  850. * <p> Any registered <code>IIOReadProgressListener</code> objects
  851. * will be notified by calling their <code>imageStarted</code>
  852. * method, followed by calls to their <code>imageProgress</code>
  853. * method as the read progresses. Finally their
  854. * <code>imageComplete</code> method will be called.
  855. * <code>IIOReadUpdateListener</code> objects may be updated at
  856. * other times during the read as pixels are decoded. Finally,
  857. * <code>IIOReadWarningListener</code> objects will receive
  858. * notification of any non-fatal warnings that occur during
  859. * decoding.
  860. *
  861. * @param imageIndex the index of the image to be retrieved.
  862. *
  863. * @return the desired portion of the image as a
  864. * <code>BufferedImage</code>.
  865. *
  866. * @exception IllegalStateException if the input source has not been
  867. * set.
  868. * @exception IndexOutOfBoundsException if the supplied index is
  869. * out of bounds.
  870. * @exception IOException if an error occurs during reading.
  871. */
  872. public BufferedImage read(int imageIndex) throws IOException {
  873. return read(imageIndex, null);
  874. }
  875. /**
  876. * Reads the image indexed by <code>imageIndex</code> and returns
  877. * it as a complete <code>BufferedImage</code>, using a supplied
  878. * <code>ImageReadParam</code>.
  879. *
  880. * <p> The actual <code>BufferedImage</code> returned will be
  881. * chosen using the algorithm defined by the
  882. * <code>getDestination</code> method.
  883. *
  884. * <p> Any registered <code>IIOReadProgressListener</code> objects
  885. * will be notified by calling their <code>imageStarted</code>
  886. * method, followed by calls to their <code>imageProgress</code>
  887. * method as the read progresses. Finally their
  888. * <code>imageComplete</code> method will be called.
  889. * <code>IIOReadUpdateListener</code> objects may be updated at
  890. * other times during the read as pixels are decoded. Finally,
  891. * <code>IIOReadWarningListener</code> objects will receive
  892. * notification of any non-fatal warnings that occur during
  893. * decoding.
  894. *
  895. * <p> The set of source bands to be read and destination bands to
  896. * be written is determined by calling <code>getSourceBands</code>
  897. * and <code>getDestinationBands</code> on the supplied
  898. * <code>ImageReadParam</code>. If the lengths of the arrays
  899. * returned by these methods differ, the set of source bands
  900. * contains an index larger that the largest available source
  901. * index, or the set of destination bands contains an index larger
  902. * than the largest legal destination index, an
  903. * <code>IllegalArgumentException</code> is thrown.
  904. *
  905. * <p> If the supplied <code>ImageReadParam</code> contains
  906. * optional setting values not supported by this reader, they will
  907. * be ignored.
  908. *
  909. * @param imageIndex the index of the image to be retrieved.
  910. * @param param an <code>ImageReadParam</code> used to control
  911. * the reading process, or <code>null</code>.
  912. *
  913. * @return the desired portion of the image as a
  914. * <code>BufferedImage</code>.
  915. *
  916. * @exception IllegalStateException if the input source has not been
  917. * set.
  918. * @exception IndexOutOfBoundsException if the supplied index is
  919. * out of bounds.
  920. * @exception IllegalArgumentException if the set of source and
  921. * destination bands specified by
  922. * <code>param.getSourceBands</code> and
  923. * <code>param.getDestinationBands</code> differ in length or
  924. * include indices that are out of bounds.
  925. * @exception IllegalArgumentException if the resulting image would
  926. * have a width or height less than 1.
  927. * @exception IOException if an error occurs during reading.
  928. */
  929. public abstract BufferedImage read(int imageIndex, ImageReadParam param)
  930. throws IOException;
  931. /**
  932. * Reads the image indexed by <code>imageIndex</code> and returns
  933. * an <code>IIOImage</code> containing the image, thumbnails, and
  934. * associated image metadata, using a supplied
  935. * <code>ImageReadParam</code>.
  936. *
  937. * <p> The actual <code>BufferedImage</code> referenced by the
  938. * returned <code>IIOImage</code> will be chosen using the
  939. * algorithm defined by the <code>getDestination</code> method.
  940. *
  941. * <p> Any registered <code>IIOReadProgressListener</code> objects
  942. * will be notified by calling their <code>imageStarted</code>
  943. * method, followed by calls to their <code>imageProgress</code>
  944. * method as the read progresses. Finally their
  945. * <code>imageComplete</code> method will be called.
  946. * <code>IIOReadUpdateListener</code> objects may be updated at
  947. * other times during the read as pixels are decoded. Finally,
  948. * <code>IIOReadWarningListener</code> objects will receive
  949. * notification of any non-fatal warnings that occur during
  950. * decoding.
  951. *
  952. * <p> The set of source bands to be read and destination bands to
  953. * be written is determined by calling <code>getSourceBands</code>
  954. * and <code>getDestinationBands</code> on the supplied
  955. * <code>ImageReadParam</code>. If the lengths of the arrays
  956. * returned by these methods differ, the set of source bands
  957. * contains an index larger that the largest available source
  958. * index, or the set of destination bands contains an index larger
  959. * than the largest legal destination index, an
  960. * <code>IllegalArgumentException</code> is thrown.
  961. *
  962. * <p> Thumbnails will be returned in their entirety regardless of
  963. * the region settings.
  964. *
  965. * <p> If the supplied <code>ImageReadParam</code> contains
  966. * optional setting values not supported by this reader, those
  967. * values will be ignored.
  968. *
  969. * @param imageIndex the index of the image to be retrieved.
  970. * @param param an <code>ImageReadParam</code> used to control
  971. * the reading process, or <code>null</code>.
  972. *
  973. * @return an <code>IIOImage</code> containing the desired portion
  974. * of the image, a set of thumbnails, and associated image
  975. * metadata.
  976. *
  977. * @exception IllegalStateException if the input source has not been
  978. * set.
  979. * @exception IndexOutOfBoundsException if the supplied index is
  980. * out of bounds.
  981. * @exception IllegalArgumentException if the set of source and
  982. * destination bands specified by
  983. * <code>param.getSourceBands</code> and
  984. * <code>param.getDestinationBands</code> differ in length or
  985. * include indices that are out of bounds.
  986. * @exception IllegalArgumentException if the resulting image
  987. * would have a width or height less than 1.
  988. * @exception IOException if an error occurs during reading.
  989. */
  990. public IIOImage readAll(int imageIndex, ImageReadParam param)
  991. throws IOException {
  992. if (imageIndex < getMinIndex()) {
  993. throw new IndexOutOfBoundsException("imageIndex < getMinIndex()!");
  994. }
  995. BufferedImage im = read(imageIndex, param);
  996. List thumbnails = null;
  997. int numThumbnails = getNumThumbnails(imageIndex);
  998. for (int j = 0; j < numThumbnails; j++) {
  999. thumbnails.add(readThumbnail(imageIndex, j));
  1000. }
  1001. IIOMetadata metadata = getImageMetadata(imageIndex);
  1002. return new IIOImage(im, thumbnails, metadata);
  1003. }
  1004. /**
  1005. * Returns an <code>Iterator</code> containing all the images,
  1006. * thumbnails, and metadata, starting at the index given by
  1007. * <code>getMinIndex</code>, from the input source in the form of
  1008. * <code>IIOImage</code> objects. An <code>Iterator</code>
  1009. * containing <code>ImageReadParam</code> objects is supplied; one
  1010. * element is consumed for each image read from the input source
  1011. * until no more images are available. If the read param
  1012. * <code>Iterator</code> runs out of elements, but there are still
  1013. * more images available from the input source, default read
  1014. * params are used for the remaining images.
  1015. *
  1016. * <p> If <code>params</code> is <code>null</code>, a default read
  1017. * param will be used for all images.
  1018. *
  1019. * <p> The actual <code>BufferedImage</code> referenced by the
  1020. * returned <code>IIOImage</code> will be chosen using the
  1021. * algorithm defined by the <code>getDestination</code> method.
  1022. *
  1023. * <p> Any registered <code>IIOReadProgressListener</code> objects
  1024. * will be notified by calling their <code>sequenceStarted</code>
  1025. * method once. Then, for each image decoded, there will be a
  1026. * call to <code>imageStarted</code>, followed by calls to
  1027. * <code>imageProgress</code> as the read progresses, and finally
  1028. * to <code>imageComplete</code>. The
  1029. * <code>sequenceComplete</code> method will be called after the
  1030. * last image has been decoded.
  1031. * <code>IIOReadUpdateListener</code> objects may be updated at
  1032. * other times during the read as pixels are decoded. Finally,
  1033. * <code>IIOReadWarningListener</code> objects will receive
  1034. * notification of any non-fatal warnings that occur during
  1035. * decoding.
  1036. *
  1037. * <p> The set of source bands to be read and destination bands to
  1038. * be written is determined by calling <code>getSourceBands</code>
  1039. * and <code>getDestinationBands</code> on the supplied
  1040. * <code>ImageReadParam</code>. If the lengths of the arrays
  1041. * returned by these methods differ, the set of source bands
  1042. * contains an index larger that the largest available source
  1043. * index, or the set of destination bands contains an index larger
  1044. * than the largest legal destination index, an
  1045. * <code>IllegalArgumentException</code> is thrown.
  1046. *
  1047. * <p> Thumbnails will be returned in their entirety regardless of the
  1048. * region settings.
  1049. *
  1050. * <p> If any of the supplied <code>ImageReadParam</code>s contain
  1051. * optional setting values not supported by this reader, they will
  1052. * be ignored.
  1053. *
  1054. * @param params an <code>Iterator</code> containing
  1055. * <code>ImageReadParam</code> objects.
  1056. *
  1057. * @return an <code>Iterator</code> representing the
  1058. * contents of the input source as <code>IIOImage</code>s.
  1059. *
  1060. * @exception IllegalStateException if the input source has not been
  1061. * set.
  1062. * @exception IllegalArgumentException if any
  1063. * non-<code>null</code> element of <code>params</code> is not an
  1064. * <code>ImageReadParam</code>.
  1065. * @exception IllegalArgumentException if the set of source and
  1066. * destination bands specified by
  1067. * <code>param.getSourceBands</code> and
  1068. * <code>param.getDestinationBands</code> differ in length or
  1069. * include indices that are out of bounds.
  1070. * @exception IllegalArgumentException if a resulting image would
  1071. * have a width or height less than 1.
  1072. * @exception IOException if an error occurs during reading.
  1073. *
  1074. * @see ImageReadParam
  1075. * @see IIOImage
  1076. */
  1077. public Iterator readAll(Iterator params) throws IOException {
  1078. List output = new ArrayList();
  1079. int imageIndex = getMinIndex();
  1080. // Inform IIOReadProgressListeners we're starting a sequence
  1081. processSequenceStarted(imageIndex);
  1082. while (true) {
  1083. // Inform IIOReadProgressListeners and IIOReadUpdateListeners
  1084. // that we're starting a new image
  1085. ImageReadParam param = null;
  1086. if (params != null && params.hasNext()) {
  1087. Object o = params.next();
  1088. if (o != null) {
  1089. if (o instanceof ImageReadParam) {
  1090. param = (ImageReadParam)o;
  1091. } else {
  1092. throw new IllegalArgumentException
  1093. ("Non-ImageReadParam supplied as part of params!");
  1094. }
  1095. }
  1096. }
  1097. BufferedImage bi = null;
  1098. try {
  1099. bi = read(imageIndex, param);
  1100. } catch (IndexOutOfBoundsException e) {
  1101. break;
  1102. }
  1103. List thumbnails = null;
  1104. int numThumbnails = getNumThumbnails(imageIndex);
  1105. for (int j = 0; j < numThumbnails; j++) {
  1106. thumbnails.add(readThumbnail(imageIndex, j));
  1107. }
  1108. IIOMetadata metadata = getImageMetadata(imageIndex);
  1109. IIOImage im = new IIOImage(bi, thumbnails, metadata);
  1110. output.add(im);
  1111. ++imageIndex;
  1112. }
  1113. // Inform IIOReadProgressListeners we're ending a sequence
  1114. processSequenceComplete();
  1115. return output.iterator();
  1116. }
  1117. /**
  1118. * Returns <code>true</code> if this plug-in supports reading
  1119. * just a {@link java.awt.image.Raster <code>Raster</code>} of pixel data.
  1120. * If this method returns <code>false</code>, calls to
  1121. * {@link #readRaster <code>readRaster</code>} or {@link #readTileRaster
  1122. * <code>readTileRaster</code>} will throw an
  1123. * <code>UnsupportedOperationException</code>.
  1124. *
  1125. * <p> The default implementation returns <code>false</code>.
  1126. *
  1127. * @return <code>true</code> if this plug-in supports reading raw
  1128. * <code>Raster</code>s.
  1129. *
  1130. * @see #readRaster
  1131. * @see #readTileRaster
  1132. */
  1133. public boolean canReadRaster() {
  1134. return false;
  1135. }
  1136. /**
  1137. * Returns a new <code>Raster</code> object containing the raw pixel data
  1138. * from the image stream, without any color conversion applied. The
  1139. * application must determine how to interpret the pixel data by other
  1140. * means. Any destination or image-type parameters in the supplied
  1141. * <code>ImageReadParam</code> object are ignored, but all other
  1142. * parameters are used exactly as in the {@link #read <code>read</code>}
  1143. * method, except that any destination offset is used as a logical rather
  1144. * than a physical offset. The size of the returned <code>Raster</code>
  1145. * will always be that of the source region clipped to the actual image.
  1146. * Logical offsets in the stream itself are ignored.
  1147. *
  1148. * <p> This method allows formats that normally apply a color
  1149. * conversion, such as JPEG, and formats that do not normally have an
  1150. * associated colorspace, such as remote sensing or medical imaging data,
  1151. * to provide access to raw pixel data.
  1152. *
  1153. * <p> Any registered <code>readUpdateListener</code>s are ignored, as
  1154. * there is no <code>BufferedImage</code>, but all other listeners are
  1155. * called exactly as they are for the {@link #read <code>read</code>}
  1156. * method.
  1157. *
  1158. * <p> If {@link #canReadRaster <code>canReadRaster()</code>} returns
  1159. * <code>false</code>, this method throws an
  1160. * <code>UnsupportedOperationException</code>.
  1161. *
  1162. * <p> If the supplied <code>ImageReadParam</code> contains
  1163. * optional setting values not supported by this reader, they will
  1164. * be ignored.
  1165. *
  1166. * <p> The default implementation throws an
  1167. * <code>UnsupportedOperationException</code>.
  1168. *
  1169. * @param imageIndex the index of the image to be read.
  1170. * @param param an <code>ImageReadParam</code> used to control
  1171. * the reading process, or <code>null</code>.
  1172. *
  1173. * @return the desired portion of the image as a
  1174. * <code>Raster</code>.
  1175. *
  1176. * @exception UnsupportedOperationException if this plug-in does not
  1177. * support reading raw <code>Raster</code>s.
  1178. * @exception IllegalStateException if the input source has not been
  1179. * set.
  1180. * @exception IndexOutOfBoundsException if the supplied index is
  1181. * out of bounds.
  1182. * @exception IOException if an error occurs during reading.
  1183. *
  1184. * @see #canReadRaster
  1185. * @see #read
  1186. * @see java.awt.image.Raster
  1187. */
  1188. public Raster readRaster(int imageIndex, ImageReadParam param)
  1189. throws IOException {
  1190. throw new UnsupportedOperationException("readRaster not supported!");
  1191. }
  1192. /**
  1193. * Returns <code>true</code> if the image is organized into
  1194. * <i>tiles</i>, that is, equal-sized non-overlapping rectangles.
  1195. *
  1196. * <p> A reader plug-in may choose whether or not to expose tiling
  1197. * that is present in the image as it is stored. It may even
  1198. * choose to advertise tiling when none is explicitly present. In
  1199. * general, tiling should only be advertised if there is some
  1200. * advantage (in speed or space) to accessing individual tiles.
  1201. * Regardless of whether the reader advertises tiling, it must be
  1202. * capable of reading an arbitrary rectangular region specified in
  1203. * an <code>ImageReadParam</code>.
  1204. *
  1205. * <p> A reader for which all images are guaranteed to be tiled,
  1206. * or are guaranteed not to be tiled, may return <code>true</code>
  1207. * or <code>false</code> respectively without accessing any image
  1208. * data. In such cases, it is not necessary to throw an exception
  1209. * even if no input source has been set or the image index is out
  1210. * of bounds.
  1211. *
  1212. * <p> The default implementation just returns <code>false</code>.
  1213. *
  1214. * @param imageIndex the index of the image to be queried.
  1215. *
  1216. * @return <code>true</code> if the image is tiled.
  1217. *
  1218. * @exception IllegalStateException if an input source is required
  1219. * to determine the return value, but none has been set.
  1220. * @exception IndexOutOfBoundsException if an image must be
  1221. * accessed to determine the return value, but the supplied index
  1222. * is out of bounds.
  1223. * @exception IOException if an error occurs during reading.
  1224. */
  1225. public boolean isImageTiled(int imageIndex) throws IOException {
  1226. return false;
  1227. }
  1228. /**
  1229. * Returns the width of a tile in the given image.
  1230. *
  1231. * <p> The default implementation simply returns
  1232. * <code>getWidth(imageIndex)</code>, which is correct for
  1233. * non-tiled images. Readers that support tiling should override
  1234. * this method.
  1235. *
  1236. * @return the width of a tile.
  1237. *
  1238. * @param imageIndex the index of the image to be queried.
  1239. *
  1240. * @exception IllegalStateException if the input source has not been set.
  1241. * @exception IndexOutOfBoundsException if the supplied index is
  1242. * out of bounds.
  1243. * @exception IOException if an error occurs during reading.
  1244. */
  1245. public int getTileWidth(int imageIndex) throws IOException {
  1246. return getWidth(imageIndex);
  1247. }
  1248. /**
  1249. * Returns the height of a tile in the given image.
  1250. *
  1251. * <p> The default implementation simply returns
  1252. * <code>getHeight(imageIndex)</code>, which is correct for
  1253. * non-tiled images. Readers that support tiling should override
  1254. * this method.
  1255. *
  1256. * @return the height of a tile.
  1257. *
  1258. * @param imageIndex the index of the image to be queried.
  1259. *
  1260. * @exception IllegalStateException if the input source has not been set.
  1261. * @exception IndexOutOfBoundsException if the supplied index is
  1262. * out of bounds.
  1263. * @exception IOException if an error occurs during reading.
  1264. */
  1265. public int getTileHeight(int imageIndex) throws IOException {
  1266. return getHeight(imageIndex);
  1267. }
  1268. /**
  1269. * Returns the X coordinate of the upper-left corner of tile (0,
  1270. * 0) in the given image.
  1271. *
  1272. * <p> A reader for which the tile grid X offset always has the
  1273. * same value (usually 0), may return the value without accessing
  1274. * any image data. In such cases, it is not necessary to throw an
  1275. * exception even if no input source has been set or the image
  1276. * index is out of bounds.
  1277. *
  1278. * <p> The default implementation simply returns 0, which is
  1279. * correct for non-tiled images and tiled images in most formats.
  1280. * Readers that support tiling with non-(0, 0) offsets should
  1281. * override this method.
  1282. *
  1283. * @return the X offset of the tile grid.
  1284. *
  1285. * @param imageIndex the index of the image to be queried.
  1286. *
  1287. * @exception IllegalStateException if an input source is required
  1288. * to determine the return value, but none has been set.
  1289. * @exception IndexOutOfBoundsException if an image must be
  1290. * accessed to determine the return value, but the supplied index
  1291. * is out of bounds.
  1292. * @exception IOException if an error occurs during reading.
  1293. */
  1294. public int getTileGridXOffset(int imageIndex) throws IOException {
  1295. return 0;
  1296. }
  1297. /**
  1298. * Returns the Y coordinate of the upper-left corner of tile (0,
  1299. * 0) in the given image.
  1300. *
  1301. * <p> A reader for which the tile grid Y offset always has the
  1302. * same value (usually 0), may return the value without accessing
  1303. * any image data. In such cases, it is not necessary to throw an
  1304. * exception even if no input source has been set or the image
  1305. * index is out of bounds.
  1306. *
  1307. * <p> The default implementation simply returns 0, which is
  1308. * correct for non-tiled images and tiled images in most formats.
  1309. * Readers that support tiling with non-(0, 0) offsets should
  1310. * override this method.
  1311. *
  1312. * @return the Y offset of the tile grid.
  1313. *
  1314. * @param imageIndex the index of the image to be queried.
  1315. *
  1316. * @exception IllegalStateException if an input source is required
  1317. * to determine the return value, but none has been set.
  1318. * @exception IndexOutOfBoundsException if an image must be
  1319. * accessed to determine the return value, but the supplied index
  1320. * is out of bounds.
  1321. * @exception IOException if an error occurs during reading.
  1322. */
  1323. public int getTileGridYOffset(int imageIndex) throws IOException {
  1324. return 0;
  1325. }
  1326. /**
  1327. * Reads the tile indicated by the <code>tileX</code> and
  1328. * <code>tileY</code> arguments, returning it as a
  1329. * <code>BufferedImage</code>. If the arguments are out of range,
  1330. * an <code>IllegalArgumentException</code> is thrown. If the
  1331. * image is not tiled, the values 0, 0 will return the entire
  1332. * image; any other values will cause an
  1333. * <code>IllegalArgumentException</code> to be thrown.
  1334. *
  1335. * <p> This method is merely a convenience equivalent to calling
  1336. * <code>read(int, ImageReadParam)</code> with a read param
  1337. * specifiying a source region having offsets of
  1338. * <code>tileX*getTileWidth(imageIndex)</code>,
  1339. * <code>tileY*getTileHeight(imageIndex)</code> and width and
  1340. * height of <code>getTileWidth(imageIndex)</code>,
  1341. * <code>getTileHeight(imageIndex)</code> and subsampling
  1342. * factors of 1 and offsets of 0. To subsample a tile, call
  1343. * <code>read</code> with a read param specifying this region
  1344. * and different subsampling parameters.
  1345. *
  1346. * <p> The default implementation returns the entire image if
  1347. * <code>tileX</code> and <code>tileY</code> are 0, or throws
  1348. * an <code>IllegalArgumentException</code> otherwise.
  1349. *
  1350. * @param imageIndex the index of the image to be retrieved.
  1351. * @param tileX the column index (starting with 0) of the tile
  1352. * to be retrieved.
  1353. * @param tileY the row index (starting with 0) of the tile
  1354. * to be retrieved.
  1355. *
  1356. * @return the tile as a <code>BufferedImage</code>.
  1357. *
  1358. * @exception IllegalStateException if the input source has not been
  1359. * set.
  1360. * @exception IndexOutOfBoundsException if <code>imageIndex</code>
  1361. * is out of bounds.
  1362. * @exception IllegalArgumentException if the tile indices are
  1363. * out of bounds.
  1364. * @exception IOException if an error occurs during reading.
  1365. */
  1366. public BufferedImage readTile(int imageIndex,
  1367. int tileX, int tileY) throws IOException {
  1368. if ((tileX != 0) || (tileY != 0)) {
  1369. throw new IllegalArgumentException("Invalid tile indices");
  1370. }
  1371. return read(imageIndex);
  1372. }
  1373. /**
  1374. * Returns a new <code>Raster</code> object containing the raw
  1375. * pixel data from the tile, without any color conversion applied.
  1376. * The application must determine how to interpret the pixel data by other
  1377. * means.
  1378. *
  1379. * <p> If {@link #canReadRaster <code>canReadRaster()</code>} returns
  1380. * <code>false</code>, this method throws an
  1381. * <code>UnsupportedOperationException</code>.
  1382. *
  1383. * <p> The default implementation checks if reading
  1384. * <code>Raster</code>s is supported, and if so calls {@link
  1385. * #readRaster <code>readRaster(imageIndex, null)</code>} if
  1386. * <code>tileX</code> and <code>tileY</code> are 0, or throws an
  1387. * <code>IllegalArgumentException</code> otherwise.
  1388. *
  1389. * @param imageIndex the index of the image to be retrieved.
  1390. * @param tileX the column index (starting with 0) of the tile
  1391. * to be retrieved.
  1392. * @param tileY the row index (starting with 0) of the tile
  1393. * to be retrieved.
  1394. *
  1395. * @return the tile as a <code>Raster</code>.
  1396. *
  1397. * @exception UnsupportedOperationException if this plug-in does not
  1398. * support reading raw <code>Raster</code>s.
  1399. * @exception IllegalArgumentException if the tile indices are
  1400. * out of bounds.
  1401. * @exception IllegalStateException if the input source has not been
  1402. * set.
  1403. * @exception IndexOutOfBoundsException if <code>imageIndex</code>
  1404. * is out of bounds.
  1405. * @exception IOException if an error occurs during reading.
  1406. *
  1407. * @see #readTile
  1408. * @see #readRaster
  1409. * @see java.awt.image.Raster
  1410. */
  1411. public Raster readTileRaster(int imageIndex,
  1412. int tileX, int tileY) throws IOException {
  1413. if (!canReadRaster()) {
  1414. throw new UnsupportedOperationException
  1415. ("readTileRaster not supported!");
  1416. }
  1417. if ((tileX != 0) || (tileY != 0)) {
  1418. throw new IllegalArgumentException("Invalid tile indices");
  1419. }
  1420. return readRaster(imageIndex, null);
  1421. }
  1422. // RenderedImages
  1423. /**
  1424. * Returns a <code>RenderedImage</code> object that contains the
  1425. * contents of the image indexed by <code>imageIndex</code>. By
  1426. * default, the returned image is simply the
  1427. * <code>BufferedImage</code> returned by <code>read(imageIndex,
  1428. * param)</code>.
  1429. *
  1430. * <p> The semantics of this method may differ from those of the
  1431. * other <code>read</code> methods in several ways. First, any
  1432. * destination image and/or image type set in the
  1433. * <code>ImageReadParam</code> may be ignored. Second, the usual
  1434. * listener calls are not guaranteed to be made, or to be
  1435. * meaningful if they are. This is because the returned image may
  1436. * not be fully populated with pixel data at the time it is
  1437. * returned, or indeed at any time.
  1438. *
  1439. * <p> If the supplied <code>ImageReadParam</code> contains
  1440. * optional setting values not supported by this reader, they will
  1441. * be ignored.
  1442. *
  1443. * <p> The default implementation just calls {@link #read
  1444. * <code>read(imageIndex, param)</code>}.
  1445. *
  1446. * @param imageIndex the index of the image to be retrieved.
  1447. * @param param an <code>ImageReadParam</code> used to control
  1448. * the reading process, or <code>null</code>.
  1449. *
  1450. * @return a <code>RenderedImage</code> object providing a view of
  1451. * the image.
  1452. *
  1453. * @exception IllegalStateException if the input source has not been
  1454. * set.
  1455. * @exception IndexOutOfBoundsException if the supplied index is
  1456. * out of bounds.
  1457. * @exception IllegalArgumentException if the set of source and
  1458. * destination bands specified by
  1459. * <code>param.getSourceBands</code> and
  1460. * <code>param.getDestinationBands</code> differ in length or
  1461. * include indices that are out of bounds.
  1462. * @exception IllegalArgumentException if the resulting image
  1463. * would have a width or height less than 1.
  1464. * @exception IOException if an error occurs during reading.
  1465. */
  1466. public RenderedImage readAsRenderedImage(int imageIndex,
  1467. ImageReadParam param)
  1468. throws IOException {
  1469. return read(imageIndex, param);
  1470. }
  1471. // Thumbnails
  1472. /**
  1473. * Returns <code>true</code> if the image format understood by
  1474. * this reader supports thumbnail preview images associated with
  1475. * it. The default implementation returns <code>false</code>.
  1476. *
  1477. * <p> If this method returns <code>false</code>,
  1478. * <code>hasThumbnails</code> and <code>getNumThumbnails</code>
  1479. * will return <code>false</code> and <code>0</code>,
  1480. * respectively, and <code>readThumbnail</code> will throw an
  1481. * <code>UnsupportedOperationException</code>, regardless of their
  1482. * arguments.
  1483. *
  1484. * <p> A reader that does not support thumbnails need not
  1485. * implement any of the thumbnail-related methods.
  1486. *
  1487. * @return <code>true</code> if thumbnails are supported.
  1488. */
  1489. public boolean readerSupportsThumbnails() {
  1490. return false;
  1491. }
  1492. /**
  1493. * Returns <code>true</code> if the given image has thumbnail
  1494. * preview images associated with it. If the format does not
  1495. * support thumbnails (<code>readerSupportsThumbnails</code>
  1496. * returns <code>false</code>), <code>false</code> will be
  1497. * returned regardless of whether an input source has been set or
  1498. * whether <code>imageIndex</code> is in bounds.
  1499. *
  1500. * <p> The default implementation returns <code>true</code> if
  1501. * <code>getNumThumbnails</code> returns a value greater than 0.
  1502. *
  1503. * @param imageIndex the index of the image being queried.
  1504. *
  1505. * @return <code>true</code> if the given image has thumbnails.
  1506. *
  1507. * @exception IllegalStateException if the reader supports
  1508. * thumbnails but the input source has not been set.
  1509. * @exception IndexOutOfBoundsException if the reader supports
  1510. * thumbnails but <code>imageIndex</code> is out of bounds.
  1511. * @exception IOException if an error occurs during reading.
  1512. */
  1513. public boolean hasThumbnails(int imageIndex) throws IOException {
  1514. return getNumThumbnails(imageIndex) > 0;
  1515. }
  1516. /**
  1517. * Returns the number of thumbnail preview images associated with
  1518. * the given image. If the format does not support thumbnails,
  1519. * (<code>readerSupportsThumbnails</code> returns
  1520. * <code>false</code>), <code>0</code> will be returned regardless
  1521. * of whether an input source has been set or whether
  1522. * <code>imageIndex</code> is in bounds.
  1523. *
  1524. * <p> The default implementation returns 0 without checking its
  1525. * argument.
  1526. *
  1527. * @param imageIndex the index of the image being queried.
  1528. *
  1529. * @return the number of thumbnails associated with the given
  1530. * image.
  1531. *
  1532. * @exception IllegalStateException if the reader supports
  1533. * thumbnails but the input source has not been set.
  1534. * @exception IndexOutOfBoundsException if the reader supports
  1535. * thumbnails but <code>imageIndex</code> is out of bounds.
  1536. * @exception IOException if an error occurs during reading.
  1537. */
  1538. public int getNumThumbnails(int imageIndex)
  1539. throws IOException {
  1540. return 0;
  1541. }
  1542. /**
  1543. * Returns the width of the thumbnail preview image indexed by
  1544. * <code>thumbnailIndex</code>, associated with the image indexed
  1545. * by <code>ImageIndex</code>.
  1546. *
  1547. * <p> If the reader does not support thumbnails,
  1548. * (<code>readerSupportsThumbnails</code> returns
  1549. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1550. * will be thrown.
  1551. *
  1552. * <p> The default implementation simply returns
  1553. * <code>readThumbnail(imageindex,
  1554. * thumbnailIndex).getWidth()</code>. Subclasses should therefore
  1555. * override this method if possible in order to avoid forcing the
  1556. * thumbnail to be read.
  1557. *
  1558. * @param imageIndex the index of the image to be retrieved.
  1559. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1560. *
  1561. * @return the width of the desired thumbnail as an <code>int</code>.
  1562. *
  1563. * @exception UnsupportedOperationException if thumbnails are not
  1564. * supported.
  1565. * @exception IllegalStateException if the input source has not been set.
  1566. * @exception IndexOutOfBoundsException if either of the supplied
  1567. * indices are out of bounds.
  1568. * @exception IOException if an error occurs during reading.
  1569. */
  1570. public int getThumbnailWidth(int imageIndex, int thumbnailIndex)
  1571. throws IOException {
  1572. return readThumbnail(imageIndex, thumbnailIndex).getWidth();
  1573. }
  1574. /**
  1575. * Returns the height of the thumbnail preview image indexed by
  1576. * <code>thumbnailIndex</code>, associated with the image indexed
  1577. * by <code>ImageIndex</code>.
  1578. *
  1579. * <p> If the reader does not support thumbnails,
  1580. * (<code>readerSupportsThumbnails</code> returns
  1581. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1582. * will be thrown.
  1583. *
  1584. * <p> The default implementation simply returns
  1585. * <code>readThumbnail(imageindex,
  1586. * thumbnailIndex).getHeight()</code>. Subclasses should
  1587. * therefore override this method if possible in order to avoid
  1588. * forcing the thumbnail to be read.
  1589. *
  1590. * @param imageIndex the index of the image to be retrieved.
  1591. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1592. *
  1593. * @return the height of the desired thumbnail as an <code>int</code>.
  1594. *
  1595. * @exception UnsupportedOperationException if thumbnails are not
  1596. * supported.
  1597. * @exception IllegalStateException if the input source has not been set.
  1598. * @exception IndexOutOfBoundsException if either of the supplied
  1599. * indices are out of bounds.
  1600. * @exception IOException if an error occurs during reading.
  1601. */
  1602. public int getThumbnailHeight(int imageIndex, int thumbnailIndex)
  1603. throws IOException {
  1604. return readThumbnail(imageIndex, thumbnailIndex).getHeight();
  1605. }
  1606. /**
  1607. * Returns the thumbnail preview image indexed by
  1608. * <code>thumbnailIndex</code>, associated with the image indexed
  1609. * by <code>ImageIndex</code> as a <code>BufferedImage</code>.
  1610. *
  1611. * <p> Any registered <code>IIOReadProgressListener</code> objects
  1612. * will be notified by calling their
  1613. * <code>thumbnailStarted</code>, <code>thumbnailProgress</code>,
  1614. * and <code>thumbnailComplete</code> methods.
  1615. *
  1616. * <p> If the reader does not support thumbnails,
  1617. * (<code>readerSupportsThumbnails</code> returns
  1618. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1619. * will be thrown regardless of whether an input source has been
  1620. * set or whether the indices are in bounds.
  1621. *
  1622. * <p> The default implementation throws an
  1623. * <code>UnsupportedOperationException</code>.
  1624. *
  1625. * @param imageIndex the index of the image to be retrieved.
  1626. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1627. *
  1628. * @return the desired thumbnail as a <code>BufferedImage</code>.
  1629. *
  1630. * @exception UnsupportedOperationException if thumbnails are not
  1631. * supported.
  1632. * @exception IllegalStateException if the input source has not been set.
  1633. * @exception IndexOutOfBoundsException if either of the supplied
  1634. * indices are out of bounds.
  1635. * @exception IOException if an error occurs during reading.
  1636. */
  1637. public BufferedImage readThumbnail(int imageIndex,
  1638. int thumbnailIndex)
  1639. throws IOException {
  1640. throw new UnsupportedOperationException("Thumbnails not supported!");
  1641. }
  1642. // Abort
  1643. /**
  1644. * Requests that any current read operation be aborted. The
  1645. * contents of the image following the abort will be undefined.
  1646. *
  1647. * <p> Readers should call <code>clearAbortRequest</code> at the
  1648. * beginning of each read operation, and poll the value of
  1649. * <code>abortRequested</code> regularly during the read.
  1650. */
  1651. public synchronized void abort() {
  1652. this.abortFlag = true;
  1653. }
  1654. /**
  1655. * Returns <code>true</code> if a request to abort the current
  1656. * read operation has been made since the reader was instantiated or
  1657. * <code>clearAbortRequest</code> was called.
  1658. *
  1659. * @return <code>true</code> if the current read operation should
  1660. * be aborted.
  1661. *
  1662. * @see #abort
  1663. * @see #clearAbortRequest
  1664. */
  1665. protected synchronized boolean abortRequested() {
  1666. return this.abortFlag;
  1667. }
  1668. /**
  1669. * Clears any previous abort request. After this method has been
  1670. * called, <code>abortRequested</code> will return
  1671. * <code>false</code>.
  1672. *
  1673. * @see #abort
  1674. * @see #abortRequested
  1675. */
  1676. protected synchronized void clearAbortRequest() {
  1677. this.abortFlag = false;
  1678. }
  1679. // Listeners
  1680. // Add an element to a list, creating a new list if the
  1681. // existing list is null, and return the list.
  1682. static List addToList(List l, Object elt) {
  1683. if (l == null) {
  1684. l = new ArrayList();
  1685. }
  1686. l.add(elt);
  1687. return l;
  1688. }
  1689. // Remove an element from a list, discarding the list if the
  1690. // resulting list is empty, and return the list or null.
  1691. static List removeFromList(List l, Object elt) {
  1692. if (l == null) {
  1693. return l;
  1694. }
  1695. l.remove(elt);
  1696. if (l.size() == 0) {
  1697. l = null;
  1698. }
  1699. return l;
  1700. }
  1701. /**
  1702. * Adds an <code>IIOReadWarningListener</code> to the list of
  1703. * registered warning listeners. If <code>listener</code> is
  1704. * <code>null</code>, no exception will be thrown and no action
  1705. * will be taken. Messages sent to the given listener will be
  1706. * localized, if possible, to match the current
  1707. * <code>Locale</code>. If no <code>Locale</code> has been set,
  1708. * warning messages may be localized as the reader sees fit.
  1709. *
  1710. * @param listener an <code>IIOReadWarningListener</code> to be registered.
  1711. *
  1712. * @see #removeIIOReadWarningListener
  1713. */
  1714. public void addIIOReadWarningListener(IIOReadWarningListener listener) {
  1715. if (listener == null) {
  1716. return;
  1717. }
  1718. warningListeners = addToList(warningListeners, listener);
  1719. warningLocales = addToList(warningLocales, getLocale());
  1720. }
  1721. /**
  1722. * Removes an <code>IIOReadWarningListener</code> from the list of
  1723. * registered error listeners. If the listener was not previously
  1724. * registered, or if <code>listener</code> is <code>null</code>,
  1725. * no exception will be thrown and no action will be taken.
  1726. *
  1727. * @param listener an IIOReadWarningListener to be unregistered.
  1728. *
  1729. * @see #addIIOReadWarningListener
  1730. */
  1731. public void removeIIOReadWarningListener(IIOReadWarningListener listener) {
  1732. if (listener == null || warningListeners == null) {
  1733. return;
  1734. }
  1735. int index = warningListeners.indexOf(listener);
  1736. if (index != -1) {
  1737. warningListeners.remove(index);
  1738. warningLocales.remove(index);
  1739. if (warningListeners.size() == 0) {
  1740. warningListeners = warningLocales = null;
  1741. }
  1742. }
  1743. }
  1744. /**
  1745. * Removes all currently registered
  1746. * <code>IIOReadWarningListener</code> objects.
  1747. *
  1748. * <p> The default implementation sets the
  1749. * <code>warningListeners</code> and <code>warningLocales</code>
  1750. * instance variables to <code>null</code>.
  1751. */
  1752. public void removeAllIIOReadWarningListeners() {
  1753. warningListeners = null;
  1754. warningLocales = null;
  1755. }
  1756. /**
  1757. * Adds an <code>IIOReadProgressListener</code> to the list of
  1758. * registered progress listeners. If <code>listener</code> is
  1759. * <code>null</code>, no exception will be thrown and no action
  1760. * will be taken.
  1761. *
  1762. * @param listener an IIOReadProgressListener to be registered.
  1763. *
  1764. * @see #removeIIOReadProgressListener
  1765. */
  1766. public void addIIOReadProgressListener(IIOReadProgressListener listener) {
  1767. if (listener == null) {
  1768. return;
  1769. }
  1770. progressListeners = addToList(progressListeners, listener);
  1771. }
  1772. /**
  1773. * Removes an <code>IIOReadProgressListener</code> from the list
  1774. * of registered progress listeners. If the listener was not
  1775. * previously registered, or if <code>listener</code> is
  1776. * <code>null</code>, no exception will be thrown and no action
  1777. * will be taken.
  1778. *
  1779. * @param listener an IIOReadProgressListener to be unregistered.
  1780. *
  1781. * @see #addIIOReadProgressListener
  1782. */
  1783. public void
  1784. removeIIOReadProgressListener (IIOReadProgressListener listener) {
  1785. if (listener == null || progressListeners == null) {
  1786. return;
  1787. }
  1788. progressListeners = removeFromList(progressListeners, listener);
  1789. }
  1790. /**
  1791. * Removes all currently registered
  1792. * <code>IIOReadProgressListener</code> objects.
  1793. *
  1794. * <p> The default implementation sets the
  1795. * <code>progressListeners</code> instance variable to
  1796. * <code>null</code>.
  1797. */
  1798. public void removeAllIIOReadProgressListeners() {
  1799. progressListeners = null;
  1800. }
  1801. /**
  1802. * Adds an <code>IIOReadUpdateListener</code> to the list of
  1803. * registered update listeners. If <code>listener</code> is
  1804. * <code>null</code>, no exception will be thrown and no action
  1805. * will be taken. The listener will receive notification of pixel
  1806. * updates as images and thumbnails are decoded, including the
  1807. * starts and ends of progressive passes.
  1808. *
  1809. * <p> If no update listeners are present, the reader may choose
  1810. * to perform fewer updates to the pixels of the destination
  1811. * images and/or thumbnails, which may result in more efficient
  1812. * decoding.
  1813. *
  1814. * <p> For example, in progressive JPEG decoding each pass
  1815. * contains updates to a set of coefficients, which would have to
  1816. * be transformed into pixel values and converted to an RGB color
  1817. * space for each pass if listeners are present. If no listeners
  1818. * are present, the coefficients may simply be accumulated and the
  1819. * final results transformed and color converted one time only.
  1820. *
  1821. * <p> The final results of decoding will be the same whether or
  1822. * not intermediate updates are performed. Thus if only the final
  1823. * image is desired it may be perferable not to register any
  1824. * <code>IIOReadUpdateListener</code>s. In general, progressive
  1825. * updating is most effective when fetching images over a network
  1826. * connection that is very slow compared to local CPU processing;
  1827. * over a fast connection, progressive updates may actually slow
  1828. * down the presentation of the image.
  1829. *
  1830. * @param listener an IIOReadUpdateListener to be registered.
  1831. *
  1832. * @see #removeIIOReadUpdateListener
  1833. */
  1834. public void
  1835. addIIOReadUpdateListener(IIOReadUpdateListener listener) {
  1836. if (listener == null) {
  1837. return;
  1838. }
  1839. updateListeners = addToList(updateListeners, listener);
  1840. }
  1841. /**
  1842. * Removes an <code>IIOReadUpdateListener</code> from the list of
  1843. * registered update listeners. If the listener was not
  1844. * previously registered, or if <code>listener</code> is
  1845. * <code>null</code>, no exception will be thrown and no action
  1846. * will be taken.
  1847. *
  1848. * @param listener an IIOReadUpdateListener to be unregistered.
  1849. *
  1850. * @see #addIIOReadUpdateListener
  1851. */
  1852. public void removeIIOReadUpdateListener(IIOReadUpdateListener listener) {
  1853. if (listener == null || updateListeners == null) {
  1854. return;
  1855. }
  1856. updateListeners = removeFromList(updateListeners, listener);
  1857. }
  1858. /**
  1859. * Removes all currently registered
  1860. * <code>IIOReadUpdateListener</code> objects.
  1861. *
  1862. * <p> The default implementation sets the
  1863. * <code>updateListeners</code> instance variable to
  1864. * <code>null</code>.
  1865. */
  1866. public void removeAllIIOReadUpdateListeners() {
  1867. updateListeners = null;
  1868. }
  1869. /**
  1870. * Broadcasts the start of an sequence of image reads to all
  1871. * registered <code>IIOReadProgressListener</code>s by calling
  1872. * their <code>sequenceStarted</code> method. Subclasses may use
  1873. * this method as a convenience.
  1874. *
  1875. * @param minIndex the lowest index being read.
  1876. */
  1877. protected void processSequenceStarted(int minIndex) {
  1878. if (progressListeners == null) {
  1879. return;
  1880. }
  1881. int numListeners = progressListeners.size();
  1882. for (int i = 0; i < numListeners; i++) {
  1883. IIOReadProgressListener listener =
  1884. (IIOReadProgressListener)progressListeners.get(i);
  1885. listener.sequenceStarted(this, minIndex);
  1886. }
  1887. }
  1888. /**
  1889. * Broadcasts the completion of an sequence of image reads to all
  1890. * registered <code>IIOReadProgressListener</code>s by calling
  1891. * their <code>sequenceComplete</code> method. Subclasses may use
  1892. * this method as a convenience.
  1893. */
  1894. protected void processSequenceComplete() {
  1895. if (progressListeners == null) {
  1896. return;
  1897. }
  1898. int numListeners = progressListeners.size();
  1899. for (int i = 0; i < numListeners; i++) {
  1900. IIOReadProgressListener listener =
  1901. (IIOReadProgressListener)progressListeners.get(i);
  1902. listener.sequenceComplete(this);
  1903. }
  1904. }
  1905. /**
  1906. * Broadcasts the start of an image read to all registered
  1907. * <code>IIOReadProgressListener</code>s by calling their
  1908. * <code>imageStarted</code> method. Subclasses may use this
  1909. * method as a convenience.
  1910. *
  1911. * @param imageIndex the index of the image about to be read.
  1912. */
  1913. protected void processImageStarted(int imageIndex) {
  1914. if (progressListeners == null) {
  1915. return;
  1916. }
  1917. int numListeners = progressListeners.size();
  1918. for (int i = 0; i < numListeners; i++) {
  1919. IIOReadProgressListener listener =
  1920. (IIOReadProgressListener)progressListeners.get(i);
  1921. listener.imageStarted(this, imageIndex);
  1922. }
  1923. }
  1924. /**
  1925. * Broadcasts the current percentage of image completion to all
  1926. * registered <code>IIOReadProgressListener</code>s by calling
  1927. * their <code>imageProgress</code> method. Subclasses may use
  1928. * this method as a convenience.
  1929. *
  1930. * @param percentageDone the current percentage of completion,
  1931. * as a <code>float</code>.
  1932. */
  1933. protected void processImageProgress(float percentageDone) {
  1934. if (progressListeners == null) {
  1935. return;
  1936. }
  1937. int numListeners = progressListeners.size();
  1938. for (int i = 0; i < numListeners; i++) {
  1939. IIOReadProgressListener listener =
  1940. (IIOReadProgressListener)progressListeners.get(i);
  1941. listener.imageProgress(this, percentageDone);
  1942. }
  1943. }
  1944. /**
  1945. * Broadcasts the completion of an image read to all registered
  1946. * <code>IIOReadProgressListener</code>s by calling their
  1947. * <code>imageComplete</code> method. Subclasses may use this
  1948. * method as a convenience.
  1949. */
  1950. protected void processImageComplete() {
  1951. if (progressListeners == null) {
  1952. return;
  1953. }
  1954. int numListeners = progressListeners.size();
  1955. for (int i = 0; i < numListeners; i++) {
  1956. IIOReadProgressListener listener =
  1957. (IIOReadProgressListener)progressListeners.get(i);
  1958. listener.imageComplete(this);
  1959. }
  1960. }
  1961. /**
  1962. * Broadcasts the start of a thumbnail read to all registered
  1963. * <code>IIOReadProgressListener</code>s by calling their
  1964. * <code>thumbnailStarted</code> method. Subclasses may use this
  1965. * method as a convenience.
  1966. *
  1967. * @param imageIndex the index of the image associated with the
  1968. * thumbnail.
  1969. * @param thumbnailIndex the index of the thumbnail.
  1970. */
  1971. protected void processThumbnailStarted(int imageIndex,
  1972. int thumbnailIndex) {
  1973. if (progressListeners == null) {
  1974. return;
  1975. }
  1976. int numListeners = progressListeners.size();
  1977. for (int i = 0; i < numListeners; i++) {
  1978. IIOReadProgressListener listener =
  1979. (IIOReadProgressListener)progressListeners.get(i);
  1980. listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
  1981. }
  1982. }
  1983. /**
  1984. * Broadcasts the current percentage of thumbnail completion to
  1985. * all registered <code>IIOReadProgressListener</code>s by calling
  1986. * their <code>thumbnailProgress</code> method. Subclasses may
  1987. * use this method as a convenience.
  1988. *
  1989. * @param percentageDone the current percentage of completion,
  1990. * as a <code>float</code>.
  1991. */
  1992. protected void processThumbnailProgress(float percentageDone) {
  1993. if (progressListeners == null) {
  1994. return;
  1995. }
  1996. int numListeners = progressListeners.size();
  1997. for (int i = 0; i < numListeners; i++) {
  1998. IIOReadProgressListener listener =
  1999. (IIOReadProgressListener)progressListeners.get(i);
  2000. listener.thumbnailProgress(this, percentageDone);
  2001. }
  2002. }
  2003. /**
  2004. * Broadcasts the completion of a thumbnail read to all registered
  2005. * <code>IIOReadProgressListener</code>s by calling their
  2006. * <code>thumbnailComplete</code> method. Subclasses may use this
  2007. * method as a convenience.
  2008. */
  2009. protected void processThumbnailComplete() {
  2010. if (progressListeners == null) {
  2011. return;
  2012. }
  2013. int numListeners = progressListeners.size();
  2014. for (int i = 0; i < numListeners; i++) {
  2015. IIOReadProgressListener listener =
  2016. (IIOReadProgressListener)progressListeners.get(i);
  2017. listener.thumbnailComplete(this);
  2018. }
  2019. }
  2020. /**
  2021. * Broadcasts that the read has been aborted to all registered
  2022. * <code>IIOReadProgressListener</code>s by calling their
  2023. * <code>readAborted</code> method. Subclasses may use this
  2024. * method as a convenience.
  2025. */
  2026. protected void processReadAborted() {
  2027. if (progressListeners == null) {
  2028. return;
  2029. }
  2030. int numListeners = progressListeners.size();
  2031. for (int i = 0; i < numListeners; i++) {
  2032. IIOReadProgressListener listener =
  2033. (IIOReadProgressListener)progressListeners.get(i);
  2034. listener.readAborted(this);
  2035. }
  2036. }
  2037. /**
  2038. * Broadcasts the beginning of a progressive pass to all
  2039. * registered <code>IIOReadUpdateListener</code>s by calling their
  2040. * <code>passStarted</code> method. Subclasses may use this
  2041. * method as a convenience.
  2042. *
  2043. * @param theImage the <code>BufferedImage</code> being updated.
  2044. * @param pass the index of the current pass, starting with 0.
  2045. * @param minPass the index of the first pass that will be decoded.
  2046. * @param maxPass the index of the last pass that will be decoded.
  2047. * @param minX the X coordinate of the upper-left pixel included
  2048. * in the pass.
  2049. * @param minY the X coordinate of the upper-left pixel included
  2050. * in the pass.
  2051. * @param periodX the horizontal separation between pixels.
  2052. * @param periodY the vertical separation between pixels.
  2053. * @param bands an array of <code>int</code>s indicating the
  2054. * set of affected bands of the destination.
  2055. */
  2056. protected void processPassStarted(BufferedImage theImage,
  2057. int pass,
  2058. int minPass, int maxPass,
  2059. int minX, int minY,
  2060. int periodX, int periodY,
  2061. int[] bands) {
  2062. if (updateListeners == null) {
  2063. return;
  2064. }
  2065. int numListeners = updateListeners.size();
  2066. for (int i = 0; i < numListeners; i++) {
  2067. IIOReadUpdateListener listener =
  2068. (IIOReadUpdateListener)updateListeners.get(i);
  2069. listener.passStarted(this, theImage, pass,
  2070. minPass,
  2071. maxPass,
  2072. minX, minY,
  2073. periodX, periodY,
  2074. bands);
  2075. }
  2076. }
  2077. /**
  2078. * Broadcasts the update of a set of samples to all registered
  2079. * <code>IIOReadUpdateListener</code>s by calling their
  2080. * <code>imageUpdate</code> method. Subclasses may use this
  2081. * method as a convenience.
  2082. *
  2083. * @param theImage the <code>BufferedImage</code> being updated.
  2084. * @param minX the X coordinate of the upper-left pixel included
  2085. * in the pass.
  2086. * @param minY the X coordinate of the upper-left pixel included
  2087. * in the pass.
  2088. * @param width the total width of the area being updated, including
  2089. * pixels being skipped if <code>periodX > 1</code>.
  2090. * @param height the total height of the area being updated,
  2091. * including pixels being skipped if <code>periodY > 1</code>.
  2092. * @param periodX the horizontal separation between pixels.
  2093. * @param periodY the vertical separation between pixels.
  2094. * @param bands an array of <code>int</code>s indicating the
  2095. * set of affected bands of the destination.
  2096. */
  2097. protected void processImageUpdate(BufferedImage theImage,
  2098. int minX, int minY,
  2099. int width, int height,
  2100. int periodX, int periodY,
  2101. int[] bands) {
  2102. if (updateListeners == null) {
  2103. return;
  2104. }
  2105. int numListeners = updateListeners.size();
  2106. for (int i = 0; i < numListeners; i++) {
  2107. IIOReadUpdateListener listener =
  2108. (IIOReadUpdateListener)updateListeners.get(i);
  2109. listener.imageUpdate(this,
  2110. theImage,
  2111. minX, minY,
  2112. width, height,
  2113. periodX, periodY,
  2114. bands);
  2115. }
  2116. }
  2117. /**
  2118. * Broadcasts the end of a progressive pass to all
  2119. * registered <code>IIOReadUpdateListener</code>s by calling their
  2120. * <code>passComplete</code> method. Subclasses may use this
  2121. * method as a convenience.
  2122. *
  2123. * @param theImage the <code>BufferedImage</code> being updated.
  2124. */
  2125. protected void processPassComplete(BufferedImage theImage) {
  2126. if (updateListeners == null) {
  2127. return;
  2128. }
  2129. int numListeners = updateListeners.size();
  2130. for (int i = 0; i < numListeners; i++) {
  2131. IIOReadUpdateListener listener =
  2132. (IIOReadUpdateListener)updateListeners.get(i);
  2133. listener.passComplete(this, theImage);
  2134. }
  2135. }
  2136. /**
  2137. * Broadcasts the beginning of a thumbnail progressive pass to all
  2138. * registered <code>IIOReadUpdateListener</code>s by calling their
  2139. * <code>thumbnailPassStarted</code> method. Subclasses may use this
  2140. * method as a convenience.
  2141. *
  2142. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2143. * being updated.
  2144. * @param pass the index of the current pass, starting with 0.
  2145. * @param minPass the index of the first pass that will be decoded.
  2146. * @param maxPass the index of the last pass that will be decoded.
  2147. * @param minX the X coordinate of the upper-left pixel included
  2148. * in the pass.
  2149. * @param minY the X coordinate of the upper-left pixel included
  2150. * in the pass.
  2151. * @param periodX the horizontal separation between pixels.
  2152. * @param periodY the vertical separation between pixels.
  2153. * @param bands an array of <code>int</code>s indicating the
  2154. * set of affected bands of the destination.
  2155. */
  2156. protected void processThumbnailPassStarted(BufferedImage theThumbnail,
  2157. int pass,
  2158. int minPass, int maxPass,
  2159. int minX, int minY,
  2160. int periodX, int periodY,
  2161. int[] bands) {
  2162. if (updateListeners == null) {
  2163. return;
  2164. }
  2165. int numListeners = updateListeners.size();
  2166. for (int i = 0; i < numListeners; i++) {
  2167. IIOReadUpdateListener listener =
  2168. (IIOReadUpdateListener)updateListeners.get(i);
  2169. listener.thumbnailPassStarted(this, theThumbnail, pass,
  2170. minPass,
  2171. maxPass,
  2172. minX, minY,
  2173. periodX, periodY,
  2174. bands);
  2175. }
  2176. }
  2177. /**
  2178. * Broadcasts the update of a set of samples in a thumbnail image
  2179. * to all registered <code>IIOReadUpdateListener</code>s by
  2180. * calling their <code>thumbnailUpdate</code> method. Subclasses may
  2181. * use this method as a convenience.
  2182. *
  2183. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2184. * being updated.
  2185. * @param minX the X coordinate of the upper-left pixel included
  2186. * in the pass.
  2187. * @param minY the X coordinate of the upper-left pixel included
  2188. * in the pass.
  2189. * @param width the total width of the area being updated, including
  2190. * pixels being skipped if <code>periodX > 1</code>.
  2191. * @param height the total height of the area being updated,
  2192. * including pixels being skipped if <code>periodY > 1</code>.
  2193. * @param periodX the horizontal separation between pixels.
  2194. * @param periodY the vertical separation between pixels.
  2195. * @param bands an array of <code>int</code>s indicating the
  2196. * set of affected bands of the destination.
  2197. */
  2198. protected void processThumbnailUpdate(BufferedImage theThumbnail,
  2199. int minX, int minY,
  2200. int width, int height,
  2201. int periodX, int periodY,
  2202. int[] bands) {
  2203. if (updateListeners == null) {
  2204. return;
  2205. }
  2206. int numListeners = updateListeners.size();
  2207. for (int i = 0; i < numListeners; i++) {
  2208. IIOReadUpdateListener listener =
  2209. (IIOReadUpdateListener)updateListeners.get(i);
  2210. listener.thumbnailUpdate(this,
  2211. theThumbnail,
  2212. minX, minY,
  2213. width, height,
  2214. periodX, periodY,
  2215. bands);
  2216. }
  2217. }
  2218. /**
  2219. * Broadcasts the end of a thumbnail progressive pass to all
  2220. * registered <code>IIOReadUpdateListener</code>s by calling their
  2221. * <code>thumbnailPassComplete</code> method. Subclasses may use this
  2222. * method as a convenience.
  2223. *
  2224. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2225. * being updated.
  2226. */
  2227. protected void processThumbnailPassComplete(BufferedImage theThumbnail) {
  2228. if (updateListeners == null) {
  2229. return;
  2230. }
  2231. int numListeners = updateListeners.size();
  2232. for (int i = 0; i < numListeners; i++) {
  2233. IIOReadUpdateListener listener =
  2234. (IIOReadUpdateListener)updateListeners.get(i);
  2235. listener.thumbnailPassComplete(this, theThumbnail);
  2236. }
  2237. }
  2238. /**
  2239. * Broadcasts a warning message to all registered
  2240. * <code>IIOReadWarningListener</code>s by calling their
  2241. * <code>warningOccurred</code> method. Subclasses may use this
  2242. * method as a convenience.
  2243. *
  2244. * @param warning the warning message to send.
  2245. *
  2246. * @exception IllegalArgumentException if <code>warning</code>
  2247. * is <code>null</code>.
  2248. */
  2249. protected void processWarningOccurred(String warning) {
  2250. if (warningListeners == null) {
  2251. return;
  2252. }
  2253. if (warning == null) {
  2254. throw new IllegalArgumentException("warning == null!");
  2255. }
  2256. int numListeners = warningListeners.size();
  2257. for (int i = 0; i < numListeners; i++) {
  2258. IIOReadWarningListener listener =
  2259. (IIOReadWarningListener)warningListeners.get(i);
  2260. listener.warningOccurred(this, warning);
  2261. }
  2262. }
  2263. /**
  2264. * Broadcasts a localized warning message to all registered
  2265. * <code>IIOReadWarningListener</code>s by calling their
  2266. * <code>warningOccurred</code> method with a string taken
  2267. * from a <code>ResourceBundle</code>. Subclasses may use this
  2268. * method as a convenience.
  2269. *
  2270. * @param baseName the base name of a set of
  2271. * <code>ResourceBundle</code>s containing localized warning
  2272. * messages.
  2273. * @param keyword the keyword used to index the warning message
  2274. * within the set of <code>ResourceBundle</code>s.
  2275. *
  2276. * @exception IllegalArgumentException if <code>baseName</code>
  2277. * is <code>null</code>.
  2278. * @exception IllegalArgumentException if <code>keyword</code>
  2279. * is <code>null</code>.
  2280. * @exception IllegalArgumentException if no appropriate
  2281. * <code>ResourceBundle</code> may be located.
  2282. * @exception IllegalArgumentException if the named resource is
  2283. * not found in the located <code>ResourceBundle</code>.
  2284. * @exception IllegalArgumentException if the object retrieved
  2285. * from the <code>ResourceBundle</code> is not a
  2286. * <code>String</code>.
  2287. */
  2288. protected void processWarningOccurred(String baseName,
  2289. String keyword) {
  2290. if (warningListeners == null) {
  2291. return;
  2292. }
  2293. if (baseName == null) {
  2294. throw new IllegalArgumentException("baseName == null!");
  2295. }
  2296. if (keyword == null) {
  2297. throw new IllegalArgumentException("keyword == null!");
  2298. }
  2299. int numListeners = warningListeners.size();
  2300. for (int i = 0; i < numListeners; i++) {
  2301. IIOReadWarningListener listener =
  2302. (IIOReadWarningListener)warningListeners.get(i);
  2303. Locale locale = (Locale)warningLocales.get(i);
  2304. ResourceBundle bundle = null;
  2305. try {
  2306. bundle = (locale == null)
  2307. ? ResourceBundle.getBundle(baseName)
  2308. : ResourceBundle.getBundle(baseName, locale);
  2309. } catch (MissingResourceException mre) {
  2310. throw new IllegalArgumentException("Bundle not found!");
  2311. }
  2312. String warning = null;
  2313. try {
  2314. warning = bundle.getString(keyword);
  2315. } catch (ClassCastException cce) {
  2316. throw new IllegalArgumentException("Resource is not a String!");
  2317. } catch (MissingResourceException mre) {
  2318. throw new IllegalArgumentException("Resource is missing!");
  2319. }
  2320. listener.warningOccurred(this, warning);
  2321. }
  2322. }
  2323. // State management
  2324. /**
  2325. * Restores the <code>ImageReader</code> to its initial state.
  2326. *
  2327. * <p> The default implementation calls <code>setInput(null,
  2328. * false)</code>, <code>setLocale(null)</code>,
  2329. * <code>removeAllIIOReadUpdateListeners()</code>,
  2330. * <code>removeAllIIOReadWarningListeners()</code>,
  2331. * <code>removeAllIIOReadProgressListeners()</code>, and
  2332. * <code>clearAbortRequest</code>.
  2333. */
  2334. public void reset() {
  2335. setInput(null, false, false);
  2336. setLocale(null);
  2337. removeAllIIOReadUpdateListeners();
  2338. removeAllIIOReadProgressListeners();
  2339. removeAllIIOReadWarningListeners();
  2340. clearAbortRequest();
  2341. }
  2342. /**
  2343. * Allows any resources held by this object to be released. The
  2344. * result of calling any other method (other than
  2345. * <code>finalize</code>) subsequent to a call to this method
  2346. * is undefined.
  2347. *
  2348. * <p>It is important for applications to call this method when they
  2349. * know they will no longer be using this <code>ImageReader</code>.
  2350. * Otherwise, the reader may continue to hold on to resources
  2351. * indefinitely.
  2352. *
  2353. * <p>The default implementation of this method in the superclass does
  2354. * nothing. Subclass implementations should ensure that all resources,
  2355. * especially native resources, are released.
  2356. */
  2357. public void dispose() {
  2358. }
  2359. // Utility methods
  2360. /**
  2361. * A utility method that may be used by readers to compute the
  2362. * region of the source image that should be read, taking into
  2363. * account any source region and subsampling offset settings in
  2364. * the supplied <code>ImageReadParam</code>. The actual
  2365. * subsampling factors, destination size, and destination offset
  2366. * are <em>not</em> taken into consideration, thus further
  2367. * clipping must take place. The {@link #computeRegions
  2368. * <code>computeRegions</code>} method performs all necessary
  2369. * clipping.
  2370. *
  2371. * @param param the <code>ImageReadParam</code> being used, or
  2372. * <code>null</code>.
  2373. * @param srcWidth the width of the source image.
  2374. * @param srcHeight the height of the source image.
  2375. *
  2376. * @return the source region as a <code>Rectangle</code>.
  2377. */
  2378. protected static Rectangle getSourceRegion(ImageReadParam param,
  2379. int srcWidth,
  2380. int srcHeight) {
  2381. Rectangle sourceRegion = new Rectangle(0, 0, srcWidth, srcHeight);
  2382. if (param != null) {
  2383. Rectangle region = param.getSourceRegion();
  2384. if (region != null) {
  2385. sourceRegion = sourceRegion.intersection(region);
  2386. }
  2387. int subsampleXOffset = param.getSubsamplingXOffset();
  2388. int subsampleYOffset = param.getSubsamplingYOffset();
  2389. sourceRegion.x += subsampleXOffset;
  2390. sourceRegion.y += subsampleYOffset;
  2391. sourceRegion.width -= subsampleXOffset;
  2392. sourceRegion.height -= subsampleYOffset;
  2393. }
  2394. return sourceRegion;
  2395. }
  2396. /**
  2397. * Computes the source region of interest and the destination
  2398. * region of interest, taking the width and height of the source
  2399. * image, an optional destination image, and an optional
  2400. * <code>ImageReadParam</code> into account. The source region
  2401. * begins with the entire source image. Then that is clipped to
  2402. * the source region specified in the <code>ImageReadParam</code>,
  2403. * if one is specified.
  2404. *
  2405. * <p> If either of the destination offsets are negative, the
  2406. * source region is clipped so that its top left will coincide
  2407. * with the top left of the destination image, taking subsampling
  2408. * into account. Then the result is clipped to the destination
  2409. * image on the right and bottom, if one is specified, taking
  2410. * subsampling and destination offsets into account.
  2411. *
  2412. * <p> Similarly, the destination region begins with the source
  2413. * image, is translated to the destination offset given in the
  2414. * <code>ImageReadParam</code> if there is one, and finally is
  2415. * clipped to the destination image, if there is one.
  2416. *
  2417. * <p> If either the source or destination regions end up having a
  2418. * width or height of 0, an <code>IllegalArgumentException</code>
  2419. * is thrown.
  2420. *
  2421. * <p> The {@link #getSourceRegion <code>getSourceRegion</code>}
  2422. * method may be used if only source clipping is desired.
  2423. *
  2424. * @param param an <code>ImageReadParam</code>, or <code>null</code>.
  2425. * @param srcWidth the width of the source image.
  2426. * @param srcHeight the height of the source image.
  2427. * @param image a <code>BufferedImage</code> that will be the
  2428. * destination image, or <code>null</code>.
  2429. * @param srcRegion a <code>Rectangle</code> that will be filled with
  2430. * the source region of interest.
  2431. * @param destRegion a <code>Rectangle</code> that will be filled with
  2432. * the destination region of interest.
  2433. * @exception IllegalArgumentException if <code>srcRegion</code>
  2434. * is <code>null</code>.
  2435. * @exception IllegalArgumentException if <code>dstRegion</code>
  2436. * is <code>null</code>.
  2437. * @exception IllegalArgumentException if the resulting source or
  2438. * destination region is empty.
  2439. */
  2440. protected static void computeRegions(ImageReadParam param,
  2441. int srcWidth,
  2442. int srcHeight,
  2443. BufferedImage image,
  2444. Rectangle srcRegion,
  2445. Rectangle destRegion) {
  2446. if (srcRegion == null) {
  2447. throw new IllegalArgumentException("srcRegion == null!");
  2448. }
  2449. if (destRegion == null) {
  2450. throw new IllegalArgumentException("destRegion == null!");
  2451. }
  2452. // Start with the entire source image
  2453. srcRegion.setBounds(0, 0, srcWidth, srcHeight);
  2454. // Destination also starts with source image, as that is the
  2455. // maximum extent if there is no subsampling
  2456. destRegion.setBounds(0, 0, srcWidth, srcHeight);
  2457. // Clip that to the param region, if there is one
  2458. int periodX = 1;
  2459. int periodY = 1;
  2460. int gridX = 0;
  2461. int gridY = 0;
  2462. if (param != null) {
  2463. Rectangle paramSrcRegion = param.getSourceRegion();
  2464. if (paramSrcRegion != null) {
  2465. srcRegion.setBounds(srcRegion.intersection(paramSrcRegion));
  2466. }
  2467. periodX = param.getSourceXSubsampling();
  2468. periodY = param.getSourceYSubsampling();
  2469. gridX = param.getSubsamplingXOffset();
  2470. gridY = param.getSubsamplingYOffset();
  2471. srcRegion.translate(gridX, gridY);
  2472. srcRegion.width -= gridX;
  2473. srcRegion.height -= gridY;
  2474. destRegion.setLocation(param.getDestinationOffset());
  2475. }
  2476. // Now clip any negative destination offsets, i.e. clip
  2477. // to the top and left of the destination image
  2478. if (destRegion.x < 0) {
  2479. int delta = -destRegion.x*periodX;
  2480. srcRegion.x += delta;
  2481. srcRegion.width -= delta;
  2482. destRegion.x = 0;
  2483. }
  2484. if (destRegion.y < 0) {
  2485. int delta = -destRegion.y*periodY;
  2486. srcRegion.y += delta;
  2487. srcRegion.height -= delta;
  2488. destRegion.y = 0;
  2489. }
  2490. // Now clip the destination Region to the subsampled width and height
  2491. int subsampledWidth = (srcRegion.width + periodX - 1)/periodX;
  2492. int subsampledHeight = (srcRegion.height + periodY - 1)/periodY;
  2493. destRegion.width = subsampledWidth;
  2494. destRegion.height = subsampledHeight;
  2495. // Now clip that to right and bottom of the destination image,
  2496. // if there is one, taking subsampling into account
  2497. if (image != null) {
  2498. Rectangle destImageRect = new Rectangle(0, 0,
  2499. image.getWidth(),
  2500. image.getHeight());
  2501. destRegion.setBounds(destRegion.intersection(destImageRect));
  2502. if (destRegion.isEmpty()) {
  2503. throw new IllegalArgumentException
  2504. ("Empty destination region!");
  2505. }
  2506. int deltaX = destRegion.x + subsampledWidth - image.getWidth();
  2507. if (deltaX > 0) {
  2508. srcRegion.width -= deltaX*periodX;
  2509. }
  2510. int deltaY = destRegion.y + subsampledHeight - image.getHeight();
  2511. if (deltaY > 0) {
  2512. srcRegion.height -= deltaY*periodY;
  2513. }
  2514. }
  2515. if (srcRegion.isEmpty() || destRegion.isEmpty()) {
  2516. throw new IllegalArgumentException("Empty region!");
  2517. }
  2518. }
  2519. /**
  2520. * A utility method that may be used by readers to test the
  2521. * validity of the source and destination band settings of an
  2522. * <code>ImageReadParam</code>. This method may be called as soon
  2523. * as the reader knows both the number of bands of the source
  2524. * image as it exists in the input stream, and the number of bands
  2525. * of the destination image that being written.
  2526. *
  2527. * <p> The method retrieves the source and destination band
  2528. * setting arrays from param using the <code>getSourceBands</code>
  2529. * and <code>getDestinationBands</code>methods (or considers them
  2530. * to be <code>null</code> if <code>param</code> is
  2531. * <code>null</code>). If the source band setting array is
  2532. * <code>null</code>, it is considered to be equal to the array
  2533. * <code>{ 0, 1, ..., numSrcBands - 1 }</code>, and similarly for
  2534. * the destination band setting array.
  2535. *
  2536. * <p> The method then tests that both arrays are equal in length,
  2537. * and that neither array contains a value larger than the largest
  2538. * available band index.
  2539. *
  2540. * <p> Any failure results in an
  2541. * <code>IllegalArgumentException</code> being thrown; success
  2542. * results in the method returning silently.
  2543. *
  2544. * @param param the <code>ImageReadParam</code> being used to read
  2545. * the image.
  2546. * @param numSrcBands the number of bands of the image as it exists
  2547. * int the input source.
  2548. * @param numDstBands the number of bands in the destination image
  2549. * being written.
  2550. *
  2551. * @exception IllegalArgumentException if <code>param</code>
  2552. * contains an invalid specification of a source and/or
  2553. * destination band subset.
  2554. */
  2555. protected static void checkReadParamBandSettings(ImageReadParam param,
  2556. int numSrcBands,
  2557. int numDstBands) {
  2558. // A null param is equivalent to srcBands == dstBands == null.
  2559. int[] srcBands = null;
  2560. int[] dstBands = null;
  2561. if (param != null) {
  2562. srcBands = param.getSourceBands();
  2563. dstBands = param.getDestinationBands();
  2564. }
  2565. int paramSrcBandLength =
  2566. (srcBands == null) ? numSrcBands : srcBands.length;
  2567. int paramDstBandLength =
  2568. (dstBands == null) ? numDstBands : dstBands.length;
  2569. if (paramSrcBandLength != paramDstBandLength) {
  2570. throw new IllegalArgumentException("ImageReadParam num source & dest bands differ!");
  2571. }
  2572. if (srcBands != null) {
  2573. for (int i = 0; i < srcBands.length; i++) {
  2574. if (srcBands[i] >= numSrcBands) {
  2575. throw new IllegalArgumentException("ImageReadParam source bands contains a value >= the number of source bands!");
  2576. }
  2577. }
  2578. }
  2579. if (dstBands != null) {
  2580. for (int i = 0; i < dstBands.length; i++) {
  2581. if (dstBands[i] >= numDstBands) {
  2582. throw new IllegalArgumentException("ImageReadParam dest bands contains a value >= the number of dest bands!");
  2583. }
  2584. }
  2585. }
  2586. }
  2587. /**
  2588. * Returns the <code>BufferedImage</code> to which decoded pixel
  2589. * data should be written. The image is determined by inspecting
  2590. * the supplied <code>ImageReadParam</code> if it is
  2591. * non-<code>null</code> if its <code>getDestination</code>
  2592. * method returns a non-<code>null</code> value, that image is
  2593. * simply returned. Otherwise,
  2594. * <code>param.getDestinationType</code> method is called to
  2595. * determine if a particular image type has been specified. If
  2596. * so, the returned <code>ImageTypeSpecifier</code> is used after
  2597. * checking that it is equal to one of those included in
  2598. * <code>imageTypes</code>.
  2599. *
  2600. * <p> If <code>param</code> is <code>null</code> or the above
  2601. * steps have not yielded an image or an
  2602. * <code>ImageTypeSpecifier</code>, the first value obtained from
  2603. * the <code>imageTypes</code> parameter is used. Typically, the
  2604. * caller will set <code>imageTypes</code> to the value of
  2605. * <code>getImageTypes(imageIndex)</code>.
  2606. *
  2607. * <p> Next, the dimensions of the image are determined by a call
  2608. * to <code>computeRegions</code>. The actual width and height of
  2609. * the image being decoded are passed in as the <code>width</code>
  2610. * and <code>height</code> parameters.
  2611. *
  2612. * @param param an <code>ImageReadParam</code> to be used to get
  2613. * the destination image or image type, or <code>null</code>.
  2614. * @param imageTypes an <code>Iterator</code> of
  2615. * <code>ImageTypeSpecifier</code>s indicating the legal image
  2616. * types, with the default first.
  2617. * @param width the true width of the image or tile begin decoded.
  2618. * @param height the true width of the image or tile being decoded.
  2619. *
  2620. * @return the <code>BufferedImage</code> to which decoded pixel
  2621. * data should be written.
  2622. *
  2623. * @exception IIOException if the <code>ImageTypeSpecifier</code>
  2624. * specified by <code>param</code> does not match any of the legal
  2625. * ones from <code>imageTypes</code>.
  2626. * @exception IllegalArgumentException if <code>imageTypes</code>
  2627. * is <code>null</code> or empty, or if an object not of type
  2628. * <code>ImageTypeSpecifier</code> is retrieved from it.
  2629. * @exception IllegalArgumentException if the resulting image would
  2630. * have a width or height less than 1.
  2631. * @exception IllegalArgumentException if the product of
  2632. * <code>width</code> and <code>height</code> is greater than
  2633. * <code>Integer.MAX_VALUE</code>.
  2634. */
  2635. protected static BufferedImage getDestination(ImageReadParam param,
  2636. Iterator imageTypes,
  2637. int width,
  2638. int height)
  2639. throws IIOException {
  2640. if (imageTypes == null || !imageTypes.hasNext()) {
  2641. throw new IllegalArgumentException("imageTypes null or empty!");
  2642. }
  2643. if ((long)width*height > Integer.MAX_VALUE) {
  2644. throw new IllegalArgumentException
  2645. ("width*height > Integer.MAX_VALUE!");
  2646. }
  2647. BufferedImage dest = null;
  2648. ImageTypeSpecifier imageType = null;
  2649. // If param is non-null, use it
  2650. if (param != null) {
  2651. // Try to get the image itself
  2652. dest = param.getDestination();
  2653. if (dest != null) {
  2654. return dest;
  2655. }
  2656. // No image, get the image type
  2657. imageType = param.getDestinationType();
  2658. }
  2659. // No info from param, use fallback image type
  2660. if (imageType == null) {
  2661. Object o = imageTypes.next();
  2662. if (!(o instanceof ImageTypeSpecifier)) {
  2663. throw new IllegalArgumentException
  2664. ("Non-ImageTypeSpecifier retrieved from imageTypes!");
  2665. }
  2666. imageType = (ImageTypeSpecifier)o;
  2667. } else {
  2668. boolean foundIt = false;
  2669. while (imageTypes.hasNext()) {
  2670. ImageTypeSpecifier type =
  2671. (ImageTypeSpecifier)imageTypes.next();
  2672. if (type.equals(imageType)) {
  2673. foundIt = true;
  2674. break;
  2675. }
  2676. }
  2677. if (!foundIt) {
  2678. throw new IIOException
  2679. ("Destination type from ImageReadParam does not match!");
  2680. }
  2681. }
  2682. Rectangle srcRegion = new Rectangle(0,0,0,0);
  2683. Rectangle destRegion = new Rectangle(0,0,0,0);
  2684. computeRegions(param,
  2685. width,
  2686. height,
  2687. null,
  2688. srcRegion,
  2689. destRegion);
  2690. int destWidth = destRegion.x + destRegion.width;
  2691. int destHeight = destRegion.y + destRegion.height;
  2692. // Create a new image based on the type specifier
  2693. return imageType.createBufferedImage(destWidth, destHeight);
  2694. }
  2695. }