1. /*
  2. * @(#)ImageReader.java 1.140 03/08/27
  3. *
  4. * Copyright 2004 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<IIOReadWarningListener> 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<Locale> 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<IIOReadProgressListener> 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<IIOReadUpdateListener> 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<ImageTypeSpecifier>
  658. getImageTypes(int imageIndex) throws IOException;
  659. /**
  660. * Returns a default <code>ImageReadParam</code> object
  661. * appropriate for this format. All subclasses should define a
  662. * set of default values for all parameters and return them with
  663. * this call. This method may be called before the input source
  664. * is set.
  665. *
  666. * <p> The default implementation constructs and returns a new
  667. * <code>ImageReadParam</code> object that does not allow source
  668. * scaling (<i>i.e.</i>, it returns <code>new
  669. * ImageReadParam()</code>.
  670. *
  671. * @return an <code>ImageReadParam</code> object which may be used
  672. * to control the decoding process using a set of default settings.
  673. */
  674. public ImageReadParam getDefaultReadParam() {
  675. return new ImageReadParam();
  676. }
  677. /**
  678. * Returns an <code>IIOMetadata</code> object representing the
  679. * metadata associated with the input source as a whole (i.e., not
  680. * associated with any particular image), or <code>null</code> if
  681. * the reader does not support reading metadata, is set to ignore
  682. * metadata, or if no metadata is available.
  683. *
  684. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  685. *
  686. * @exception IOException if an error occurs during reading.
  687. */
  688. public abstract IIOMetadata getStreamMetadata() throws IOException;
  689. /**
  690. * Returns an <code>IIOMetadata</code> object representing the
  691. * metadata associated with the input source as a whole (i.e.,
  692. * not associated with any particular image). If no such data
  693. * exists, <code>null</code> is returned.
  694. *
  695. * <p> The resuting metadata object is only responsible for
  696. * returning documents in the format named by
  697. * <code>formatName</code>. Within any documents that are
  698. * returned, only nodes whose names are members of
  699. * <code>nodeNames</code> are required to be returned. In this
  700. * way, the amount of metadata processing done by the reader may
  701. * be kept to a minimum, based on what information is actually
  702. * needed.
  703. *
  704. * <p> If <code>formatName</code> is not the name of a supported
  705. * metadata format, <code>null</code> is returned.
  706. *
  707. * <p> In all cases, it is legal to return a more capable metadata
  708. * object than strictly necessary. The format name and node names
  709. * are merely hints that may be used to reduce the reader's
  710. * workload.
  711. *
  712. * <p> The default implementation simply returns the result of
  713. * calling <code>getStreamMetadata()</code>, after checking that
  714. * the format name is supported. If it is not,
  715. * <code>null</code> is returned.
  716. *
  717. * @param formatName a metadata format name that may be used to retrieve
  718. * a document from the returned <code>IIOMetadata</code> object.
  719. * @param nodeNames a <code>Set</code> containing the names of
  720. * nodes that may be contained in a retrieved document.
  721. *
  722. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  723. *
  724. * @exception IllegalArgumentException if <code>formatName</code>
  725. * is <code>null</code>.
  726. * @exception IllegalArgumentException if <code>nodeNames</code>
  727. * is <code>null</code>.
  728. * @exception IOException if an error occurs during reading.
  729. */
  730. public IIOMetadata getStreamMetadata(String formatName,
  731. Set<String> nodeNames)
  732. throws IOException
  733. {
  734. return getMetadata(formatName, nodeNames, true, 0);
  735. }
  736. private IIOMetadata getMetadata(String formatName,
  737. Set nodeNames,
  738. boolean wantStream,
  739. int imageIndex) throws IOException {
  740. if (formatName == null) {
  741. throw new IllegalArgumentException("formatName == null!");
  742. }
  743. if (nodeNames == null) {
  744. throw new IllegalArgumentException("nodeNames == null!");
  745. }
  746. IIOMetadata metadata =
  747. wantStream
  748. ? getStreamMetadata()
  749. : getImageMetadata(imageIndex);
  750. if (metadata != null) {
  751. if (metadata.isStandardMetadataFormatSupported() &&
  752. formatName.equals
  753. (IIOMetadataFormatImpl.standardMetadataFormatName)) {
  754. return metadata;
  755. }
  756. String nativeName = metadata.getNativeMetadataFormatName();
  757. if (nativeName != null && formatName.equals(nativeName)) {
  758. return metadata;
  759. }
  760. String[] extraNames = metadata.getExtraMetadataFormatNames();
  761. if (extraNames != null) {
  762. for (int i = 0; i < extraNames.length; i++) {
  763. if (formatName.equals(extraNames[i])) {
  764. return metadata;
  765. }
  766. }
  767. }
  768. }
  769. return null;
  770. }
  771. /**
  772. * Returns an <code>IIOMetadata</code> object containing metadata
  773. * associated with the given image, or <code>null</code> if the
  774. * reader does not support reading metadata, is set to ignore
  775. * metadata, or if no metadata is available.
  776. *
  777. * @param imageIndex the index of the image whose metadata is to
  778. * be retrieved.
  779. *
  780. * @return an <code>IIOMetadata</code> object, or
  781. * <code>null</code>.
  782. *
  783. * @exception IllegalStateException if the input source has not been
  784. * set.
  785. * @exception IndexOutOfBoundsException if the supplied index is
  786. * out of bounds.
  787. * @exception IOException if an error occurs during reading.
  788. */
  789. public abstract IIOMetadata getImageMetadata(int imageIndex)
  790. throws IOException;
  791. /**
  792. * Returns an <code>IIOMetadata</code> object representing the
  793. * metadata associated with the given image, or <code>null</code>
  794. * if the reader does not support reading metadata or none
  795. * is available.
  796. *
  797. * <p> The resuting metadata object is only responsible for
  798. * returning documents in the format named by
  799. * <code>formatName</code>. Within any documents that are
  800. * returned, only nodes whose names are members of
  801. * <code>nodeNames</code> are required to be returned. In this
  802. * way, the amount of metadata processing done by the reader may
  803. * be kept to a minimum, based on what information is actually
  804. * needed.
  805. *
  806. * <p> If <code>formatName</code> is not the name of a supported
  807. * metadata format, <code>null</code> may be returned.
  808. *
  809. * <p> In all cases, it is legal to return a more capable metadata
  810. * object than strictly necessary. The format name and node names
  811. * are merely hints that may be used to reduce the reader's
  812. * workload.
  813. *
  814. * <p> The default implementation simply returns the result of
  815. * calling <code>getImageMetadata(imageIndex)</code>, after
  816. * checking that the format name is supported. If it is not,
  817. * <code>null</code> is returned.
  818. *
  819. * @param imageIndex the index of the image whose metadata is to
  820. * be retrieved.
  821. * @param formatName a metadata format name that may be used to retrieve
  822. * a document from the returned <code>IIOMetadata</code> object.
  823. * @param nodeNames a <code>Set</code> containing the names of
  824. * nodes that may be contained in a retrieved document.
  825. *
  826. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  827. *
  828. * @exception IllegalStateException if the input source has not been
  829. * set.
  830. * @exception IndexOutOfBoundsException if the supplied index is
  831. * out of bounds.
  832. * @exception IllegalArgumentException if <code>formatName</code>
  833. * is <code>null</code>.
  834. * @exception IllegalArgumentException if <code>nodeNames</code>
  835. * is <code>null</code>.
  836. * @exception IOException if an error occurs during reading.
  837. */
  838. public IIOMetadata getImageMetadata(int imageIndex,
  839. String formatName,
  840. Set<String> nodeNames)
  841. throws IOException {
  842. return getMetadata(formatName, nodeNames, false, imageIndex);
  843. }
  844. /**
  845. * Reads the image indexed by <code>imageIndex</code> and returns
  846. * it as a complete <code>BufferedImage</code>, using a default
  847. * <code>ImageReadParam</code>. This is a convenience method
  848. * that calls <code>read(imageIndex, null)</code>.
  849. *
  850. * <p> The image returned will be formatted according to the first
  851. * <code>ImageTypeSpecifier</code> returned from
  852. * <code>getImageTypes</code>.
  853. *
  854. * <p> Any registered <code>IIOReadProgressListener</code> objects
  855. * will be notified by calling their <code>imageStarted</code>
  856. * method, followed by calls to their <code>imageProgress</code>
  857. * method as the read progresses. Finally their
  858. * <code>imageComplete</code> method will be called.
  859. * <code>IIOReadUpdateListener</code> objects may be updated at
  860. * other times during the read as pixels are decoded. Finally,
  861. * <code>IIOReadWarningListener</code> objects will receive
  862. * notification of any non-fatal warnings that occur during
  863. * decoding.
  864. *
  865. * @param imageIndex the index of the image to be retrieved.
  866. *
  867. * @return the desired portion of the image as a
  868. * <code>BufferedImage</code>.
  869. *
  870. * @exception IllegalStateException if the input source has not been
  871. * set.
  872. * @exception IndexOutOfBoundsException if the supplied index is
  873. * out of bounds.
  874. * @exception IOException if an error occurs during reading.
  875. */
  876. public BufferedImage read(int imageIndex) throws IOException {
  877. return read(imageIndex, null);
  878. }
  879. /**
  880. * Reads the image indexed by <code>imageIndex</code> and returns
  881. * it as a complete <code>BufferedImage</code>, using a supplied
  882. * <code>ImageReadParam</code>.
  883. *
  884. * <p> The actual <code>BufferedImage</code> returned will be
  885. * chosen using the algorithm defined by the
  886. * <code>getDestination</code> method.
  887. *
  888. * <p> Any registered <code>IIOReadProgressListener</code> objects
  889. * will be notified by calling their <code>imageStarted</code>
  890. * method, followed by calls to their <code>imageProgress</code>
  891. * method as the read progresses. Finally their
  892. * <code>imageComplete</code> method will be called.
  893. * <code>IIOReadUpdateListener</code> objects may be updated at
  894. * other times during the read as pixels are decoded. Finally,
  895. * <code>IIOReadWarningListener</code> objects will receive
  896. * notification of any non-fatal warnings that occur during
  897. * decoding.
  898. *
  899. * <p> The set of source bands to be read and destination bands to
  900. * be written is determined by calling <code>getSourceBands</code>
  901. * and <code>getDestinationBands</code> on the supplied
  902. * <code>ImageReadParam</code>. If the lengths of the arrays
  903. * returned by these methods differ, the set of source bands
  904. * contains an index larger that the largest available source
  905. * index, or the set of destination bands contains an index larger
  906. * than the largest legal destination index, an
  907. * <code>IllegalArgumentException</code> is thrown.
  908. *
  909. * <p> If the supplied <code>ImageReadParam</code> contains
  910. * optional setting values not supported by this reader (<i>e.g.</i>
  911. * source render size or any format-specific settings), they will
  912. * be ignored.
  913. *
  914. * @param imageIndex the index of the image to be retrieved.
  915. * @param param an <code>ImageReadParam</code> used to control
  916. * the reading process, or <code>null</code>.
  917. *
  918. * @return the desired portion of the image as a
  919. * <code>BufferedImage</code>.
  920. *
  921. * @exception IllegalStateException if the input source has not been
  922. * set.
  923. * @exception IndexOutOfBoundsException if the supplied index is
  924. * out of bounds.
  925. * @exception IllegalArgumentException if the set of source and
  926. * destination bands specified by
  927. * <code>param.getSourceBands</code> and
  928. * <code>param.getDestinationBands</code> differ in length or
  929. * include indices that are out of bounds.
  930. * @exception IllegalArgumentException if the resulting image would
  931. * have a width or height less than 1.
  932. * @exception IOException if an error occurs during reading.
  933. */
  934. public abstract BufferedImage read(int imageIndex, ImageReadParam param)
  935. throws IOException;
  936. /**
  937. * Reads the image indexed by <code>imageIndex</code> and returns
  938. * an <code>IIOImage</code> containing the image, thumbnails, and
  939. * associated image metadata, using a supplied
  940. * <code>ImageReadParam</code>.
  941. *
  942. * <p> The actual <code>BufferedImage</code> referenced by the
  943. * returned <code>IIOImage</code> will be chosen using the
  944. * algorithm defined by the <code>getDestination</code> method.
  945. *
  946. * <p> Any registered <code>IIOReadProgressListener</code> objects
  947. * will be notified by calling their <code>imageStarted</code>
  948. * method, followed by calls to their <code>imageProgress</code>
  949. * method as the read progresses. Finally their
  950. * <code>imageComplete</code> method will be called.
  951. * <code>IIOReadUpdateListener</code> objects may be updated at
  952. * other times during the read as pixels are decoded. Finally,
  953. * <code>IIOReadWarningListener</code> objects will receive
  954. * notification of any non-fatal warnings that occur during
  955. * decoding.
  956. *
  957. * <p> The set of source bands to be read and destination bands to
  958. * be written is determined by calling <code>getSourceBands</code>
  959. * and <code>getDestinationBands</code> on the supplied
  960. * <code>ImageReadParam</code>. If the lengths of the arrays
  961. * returned by these methods differ, the set of source bands
  962. * contains an index larger that the largest available source
  963. * index, or the set of destination bands contains an index larger
  964. * than the largest legal destination index, an
  965. * <code>IllegalArgumentException</code> is thrown.
  966. *
  967. * <p> Thumbnails will be returned in their entirety regardless of
  968. * the region settings.
  969. *
  970. * <p> If the supplied <code>ImageReadParam</code> contains
  971. * optional setting values not supported by this reader (<i>e.g.</i>
  972. * source render size or any format-specific settings), those
  973. * values will be ignored.
  974. *
  975. * @param imageIndex the index of the image to be retrieved.
  976. * @param param an <code>ImageReadParam</code> used to control
  977. * the reading process, or <code>null</code>.
  978. *
  979. * @return an <code>IIOImage</code> containing the desired portion
  980. * of the image, a set of thumbnails, and associated image
  981. * metadata.
  982. *
  983. * @exception IllegalStateException if the input source has not been
  984. * set.
  985. * @exception IndexOutOfBoundsException if the supplied index is
  986. * out of bounds.
  987. * @exception IllegalArgumentException if the set of source and
  988. * destination bands specified by
  989. * <code>param.getSourceBands</code> and
  990. * <code>param.getDestinationBands</code> differ in length or
  991. * include indices that are out of bounds.
  992. * @exception IllegalArgumentException if the resulting image
  993. * would have a width or height less than 1.
  994. * @exception IOException if an error occurs during reading.
  995. */
  996. public IIOImage readAll(int imageIndex, ImageReadParam param)
  997. throws IOException {
  998. if (imageIndex < getMinIndex()) {
  999. throw new IndexOutOfBoundsException("imageIndex < getMinIndex()!");
  1000. }
  1001. BufferedImage im = read(imageIndex, param);
  1002. ArrayList thumbnails = null;
  1003. int numThumbnails = getNumThumbnails(imageIndex);
  1004. if (numThumbnails > 0) {
  1005. thumbnails = new ArrayList();
  1006. for (int j = 0; j < numThumbnails; j++) {
  1007. thumbnails.add(readThumbnail(imageIndex, j));
  1008. }
  1009. }
  1010. IIOMetadata metadata = getImageMetadata(imageIndex);
  1011. return new IIOImage(im, thumbnails, metadata);
  1012. }
  1013. /**
  1014. * Returns an <code>Iterator</code> containing all the images,
  1015. * thumbnails, and metadata, starting at the index given by
  1016. * <code>getMinIndex</code>, from the input source in the form of
  1017. * <code>IIOImage</code> objects. An <code>Iterator</code>
  1018. * containing <code>ImageReadParam</code> objects is supplied; one
  1019. * element is consumed for each image read from the input source
  1020. * until no more images are available. If the read param
  1021. * <code>Iterator</code> runs out of elements, but there are still
  1022. * more images available from the input source, default read
  1023. * params are used for the remaining images.
  1024. *
  1025. * <p> If <code>params</code> is <code>null</code>, a default read
  1026. * param will be used for all images.
  1027. *
  1028. * <p> The actual <code>BufferedImage</code> referenced by the
  1029. * returned <code>IIOImage</code> will be chosen using the
  1030. * algorithm defined by the <code>getDestination</code> method.
  1031. *
  1032. * <p> Any registered <code>IIOReadProgressListener</code> objects
  1033. * will be notified by calling their <code>sequenceStarted</code>
  1034. * method once. Then, for each image decoded, there will be a
  1035. * call to <code>imageStarted</code>, followed by calls to
  1036. * <code>imageProgress</code> as the read progresses, and finally
  1037. * to <code>imageComplete</code>. The
  1038. * <code>sequenceComplete</code> method will be called after the
  1039. * last image has been decoded.
  1040. * <code>IIOReadUpdateListener</code> objects may be updated at
  1041. * other times during the read as pixels are decoded. Finally,
  1042. * <code>IIOReadWarningListener</code> objects will receive
  1043. * notification of any non-fatal warnings that occur during
  1044. * decoding.
  1045. *
  1046. * <p> The set of source bands to be read and destination bands to
  1047. * be written is determined by calling <code>getSourceBands</code>
  1048. * and <code>getDestinationBands</code> on the supplied
  1049. * <code>ImageReadParam</code>. If the lengths of the arrays
  1050. * returned by these methods differ, the set of source bands
  1051. * contains an index larger that the largest available source
  1052. * index, or the set of destination bands contains an index larger
  1053. * than the largest legal destination index, an
  1054. * <code>IllegalArgumentException</code> is thrown.
  1055. *
  1056. * <p> Thumbnails will be returned in their entirety regardless of the
  1057. * region settings.
  1058. *
  1059. * <p> If any of the supplied <code>ImageReadParam</code>s contain
  1060. * optional setting values not supported by this reader (<i>e.g.</i>
  1061. * source render size or any format-specific settings), they will
  1062. * be ignored.
  1063. *
  1064. * @param params an <code>Iterator</code> containing
  1065. * <code>ImageReadParam</code> objects.
  1066. *
  1067. * @return an <code>Iterator</code> representing the
  1068. * contents of the input source as <code>IIOImage</code>s.
  1069. *
  1070. * @exception IllegalStateException if the input source has not been
  1071. * set.
  1072. * @exception IllegalArgumentException if any
  1073. * non-<code>null</code> element of <code>params</code> is not an
  1074. * <code>ImageReadParam</code>.
  1075. * @exception IllegalArgumentException if the set of source and
  1076. * destination bands specified by
  1077. * <code>param.getSourceBands</code> and
  1078. * <code>param.getDestinationBands</code> differ in length or
  1079. * include indices that are out of bounds.
  1080. * @exception IllegalArgumentException if a resulting image would
  1081. * have a width or height less than 1.
  1082. * @exception IOException if an error occurs during reading.
  1083. *
  1084. * @see ImageReadParam
  1085. * @see IIOImage
  1086. */
  1087. public Iterator<IIOImage>
  1088. readAll(Iterator<? extends ImageReadParam> params)
  1089. throws IOException
  1090. {
  1091. List output = new ArrayList();
  1092. int imageIndex = getMinIndex();
  1093. // Inform IIOReadProgressListeners we're starting a sequence
  1094. processSequenceStarted(imageIndex);
  1095. while (true) {
  1096. // Inform IIOReadProgressListeners and IIOReadUpdateListeners
  1097. // that we're starting a new image
  1098. ImageReadParam param = null;
  1099. if (params != null && params.hasNext()) {
  1100. Object o = params.next();
  1101. if (o != null) {
  1102. if (o instanceof ImageReadParam) {
  1103. param = (ImageReadParam)o;
  1104. } else {
  1105. throw new IllegalArgumentException
  1106. ("Non-ImageReadParam supplied as part of params!");
  1107. }
  1108. }
  1109. }
  1110. BufferedImage bi = null;
  1111. try {
  1112. bi = read(imageIndex, param);
  1113. } catch (IndexOutOfBoundsException e) {
  1114. break;
  1115. }
  1116. ArrayList thumbnails = null;
  1117. int numThumbnails = getNumThumbnails(imageIndex);
  1118. if (numThumbnails > 0) {
  1119. thumbnails = new ArrayList();
  1120. for (int j = 0; j < numThumbnails; j++) {
  1121. thumbnails.add(readThumbnail(imageIndex, j));
  1122. }
  1123. }
  1124. IIOMetadata metadata = getImageMetadata(imageIndex);
  1125. IIOImage im = new IIOImage(bi, thumbnails, metadata);
  1126. output.add(im);
  1127. ++imageIndex;
  1128. }
  1129. // Inform IIOReadProgressListeners we're ending a sequence
  1130. processSequenceComplete();
  1131. return output.iterator();
  1132. }
  1133. /**
  1134. * Returns <code>true</code> if this plug-in supports reading
  1135. * just a {@link java.awt.image.Raster <code>Raster</code>} of pixel data.
  1136. * If this method returns <code>false</code>, calls to
  1137. * {@link #readRaster <code>readRaster</code>} or {@link #readTileRaster
  1138. * <code>readTileRaster</code>} will throw an
  1139. * <code>UnsupportedOperationException</code>.
  1140. *
  1141. * <p> The default implementation returns <code>false</code>.
  1142. *
  1143. * @return <code>true</code> if this plug-in supports reading raw
  1144. * <code>Raster</code>s.
  1145. *
  1146. * @see #readRaster
  1147. * @see #readTileRaster
  1148. */
  1149. public boolean canReadRaster() {
  1150. return false;
  1151. }
  1152. /**
  1153. * Returns a new <code>Raster</code> object containing the raw pixel data
  1154. * from the image stream, without any color conversion applied. The
  1155. * application must determine how to interpret the pixel data by other
  1156. * means. Any destination or image-type parameters in the supplied
  1157. * <code>ImageReadParam</code> object are ignored, but all other
  1158. * parameters are used exactly as in the {@link #read <code>read</code>}
  1159. * method, except that any destination offset is used as a logical rather
  1160. * than a physical offset. The size of the returned <code>Raster</code>
  1161. * will always be that of the source region clipped to the actual image.
  1162. * Logical offsets in the stream itself are ignored.
  1163. *
  1164. * <p> This method allows formats that normally apply a color
  1165. * conversion, such as JPEG, and formats that do not normally have an
  1166. * associated colorspace, such as remote sensing or medical imaging data,
  1167. * to provide access to raw pixel data.
  1168. *
  1169. * <p> Any registered <code>readUpdateListener</code>s are ignored, as
  1170. * there is no <code>BufferedImage</code>, but all other listeners are
  1171. * called exactly as they are for the {@link #read <code>read</code>}
  1172. * method.
  1173. *
  1174. * <p> If {@link #canReadRaster <code>canReadRaster()</code>} returns
  1175. * <code>false</code>, this method throws an
  1176. * <code>UnsupportedOperationException</code>.
  1177. *
  1178. * <p> If the supplied <code>ImageReadParam</code> contains
  1179. * optional setting values not supported by this reader (<i>e.g.</i>
  1180. * source render size or any format-specific settings), they will
  1181. * be ignored.
  1182. *
  1183. * <p> The default implementation throws an
  1184. * <code>UnsupportedOperationException</code>.
  1185. *
  1186. * @param imageIndex the index of the image to be read.
  1187. * @param param an <code>ImageReadParam</code> used to control
  1188. * the reading process, or <code>null</code>.
  1189. *
  1190. * @return the desired portion of the image as a
  1191. * <code>Raster</code>.
  1192. *
  1193. * @exception UnsupportedOperationException if this plug-in does not
  1194. * support reading raw <code>Raster</code>s.
  1195. * @exception IllegalStateException if the input source has not been
  1196. * set.
  1197. * @exception IndexOutOfBoundsException if the supplied index is
  1198. * out of bounds.
  1199. * @exception IOException if an error occurs during reading.
  1200. *
  1201. * @see #canReadRaster
  1202. * @see #read
  1203. * @see java.awt.image.Raster
  1204. */
  1205. public Raster readRaster(int imageIndex, ImageReadParam param)
  1206. throws IOException {
  1207. throw new UnsupportedOperationException("readRaster not supported!");
  1208. }
  1209. /**
  1210. * Returns <code>true</code> if the image is organized into
  1211. * <i>tiles</i>, that is, equal-sized non-overlapping rectangles.
  1212. *
  1213. * <p> A reader plug-in may choose whether or not to expose tiling
  1214. * that is present in the image as it is stored. It may even
  1215. * choose to advertise tiling when none is explicitly present. In
  1216. * general, tiling should only be advertised if there is some
  1217. * advantage (in speed or space) to accessing individual tiles.
  1218. * Regardless of whether the reader advertises tiling, it must be
  1219. * capable of reading an arbitrary rectangular region specified in
  1220. * an <code>ImageReadParam</code>.
  1221. *
  1222. * <p> A reader for which all images are guaranteed to be tiled,
  1223. * or are guaranteed not to be tiled, may return <code>true</code>
  1224. * or <code>false</code> respectively without accessing any image
  1225. * data. In such cases, it is not necessary to throw an exception
  1226. * even if no input source has been set or the image index is out
  1227. * of bounds.
  1228. *
  1229. * <p> The default implementation just returns <code>false</code>.
  1230. *
  1231. * @param imageIndex the index of the image to be queried.
  1232. *
  1233. * @return <code>true</code> if the image is tiled.
  1234. *
  1235. * @exception IllegalStateException if an input source is required
  1236. * to determine the return value, but none has been set.
  1237. * @exception IndexOutOfBoundsException if an image must be
  1238. * accessed to determine the return value, but the supplied index
  1239. * is out of bounds.
  1240. * @exception IOException if an error occurs during reading.
  1241. */
  1242. public boolean isImageTiled(int imageIndex) throws IOException {
  1243. return false;
  1244. }
  1245. /**
  1246. * Returns the width of a tile in the given image.
  1247. *
  1248. * <p> The default implementation simply returns
  1249. * <code>getWidth(imageIndex)</code>, which is correct for
  1250. * non-tiled images. Readers that support tiling should override
  1251. * this method.
  1252. *
  1253. * @return the width of a tile.
  1254. *
  1255. * @param imageIndex the index of the image to be queried.
  1256. *
  1257. * @exception IllegalStateException if the input source has not been set.
  1258. * @exception IndexOutOfBoundsException if the supplied index is
  1259. * out of bounds.
  1260. * @exception IOException if an error occurs during reading.
  1261. */
  1262. public int getTileWidth(int imageIndex) throws IOException {
  1263. return getWidth(imageIndex);
  1264. }
  1265. /**
  1266. * Returns the height of a tile in the given image.
  1267. *
  1268. * <p> The default implementation simply returns
  1269. * <code>getHeight(imageIndex)</code>, which is correct for
  1270. * non-tiled images. Readers that support tiling should override
  1271. * this method.
  1272. *
  1273. * @return the height of a tile.
  1274. *
  1275. * @param imageIndex the index of the image to be queried.
  1276. *
  1277. * @exception IllegalStateException if the input source has not been set.
  1278. * @exception IndexOutOfBoundsException if the supplied index is
  1279. * out of bounds.
  1280. * @exception IOException if an error occurs during reading.
  1281. */
  1282. public int getTileHeight(int imageIndex) throws IOException {
  1283. return getHeight(imageIndex);
  1284. }
  1285. /**
  1286. * Returns the X coordinate of the upper-left corner of tile (0,
  1287. * 0) in the given image.
  1288. *
  1289. * <p> A reader for which the tile grid X offset always has the
  1290. * same value (usually 0), may return the value without accessing
  1291. * any image data. In such cases, it is not necessary to throw an
  1292. * exception even if no input source has been set or the image
  1293. * index is out of bounds.
  1294. *
  1295. * <p> The default implementation simply returns 0, which is
  1296. * correct for non-tiled images and tiled images in most formats.
  1297. * Readers that support tiling with non-(0, 0) offsets should
  1298. * override this method.
  1299. *
  1300. * @return the X offset of the tile grid.
  1301. *
  1302. * @param imageIndex the index of the image to be queried.
  1303. *
  1304. * @exception IllegalStateException if an input source is required
  1305. * to determine the return value, but none has been set.
  1306. * @exception IndexOutOfBoundsException if an image must be
  1307. * accessed to determine the return value, but the supplied index
  1308. * is out of bounds.
  1309. * @exception IOException if an error occurs during reading.
  1310. */
  1311. public int getTileGridXOffset(int imageIndex) throws IOException {
  1312. return 0;
  1313. }
  1314. /**
  1315. * Returns the Y coordinate of the upper-left corner of tile (0,
  1316. * 0) in the given image.
  1317. *
  1318. * <p> A reader for which the tile grid Y offset always has the
  1319. * same value (usually 0), may return the value without accessing
  1320. * any image data. In such cases, it is not necessary to throw an
  1321. * exception even if no input source has been set or the image
  1322. * index is out of bounds.
  1323. *
  1324. * <p> The default implementation simply returns 0, which is
  1325. * correct for non-tiled images and tiled images in most formats.
  1326. * Readers that support tiling with non-(0, 0) offsets should
  1327. * override this method.
  1328. *
  1329. * @return the Y offset of the tile grid.
  1330. *
  1331. * @param imageIndex the index of the image to be queried.
  1332. *
  1333. * @exception IllegalStateException if an input source is required
  1334. * to determine the return value, but none has been set.
  1335. * @exception IndexOutOfBoundsException if an image must be
  1336. * accessed to determine the return value, but the supplied index
  1337. * is out of bounds.
  1338. * @exception IOException if an error occurs during reading.
  1339. */
  1340. public int getTileGridYOffset(int imageIndex) throws IOException {
  1341. return 0;
  1342. }
  1343. /**
  1344. * Reads the tile indicated by the <code>tileX</code> and
  1345. * <code>tileY</code> arguments, returning it as a
  1346. * <code>BufferedImage</code>. If the arguments are out of range,
  1347. * an <code>IllegalArgumentException</code> is thrown. If the
  1348. * image is not tiled, the values 0, 0 will return the entire
  1349. * image; any other values will cause an
  1350. * <code>IllegalArgumentException</code> to be thrown.
  1351. *
  1352. * <p> This method is merely a convenience equivalent to calling
  1353. * <code>read(int, ImageReadParam)</code> with a read param
  1354. * specifiying a source region having offsets of
  1355. * <code>tileX*getTileWidth(imageIndex)</code>,
  1356. * <code>tileY*getTileHeight(imageIndex)</code> and width and
  1357. * height of <code>getTileWidth(imageIndex)</code>,
  1358. * <code>getTileHeight(imageIndex)</code> and subsampling
  1359. * factors of 1 and offsets of 0. To subsample a tile, call
  1360. * <code>read</code> with a read param specifying this region
  1361. * and different subsampling parameters.
  1362. *
  1363. * <p> The default implementation returns the entire image if
  1364. * <code>tileX</code> and <code>tileY</code> are 0, or throws
  1365. * an <code>IllegalArgumentException</code> otherwise.
  1366. *
  1367. * @param imageIndex the index of the image to be retrieved.
  1368. * @param tileX the column index (starting with 0) of the tile
  1369. * to be retrieved.
  1370. * @param tileY the row index (starting with 0) of the tile
  1371. * to be retrieved.
  1372. *
  1373. * @return the tile as a <code>BufferedImage</code>.
  1374. *
  1375. * @exception IllegalStateException if the input source has not been
  1376. * set.
  1377. * @exception IndexOutOfBoundsException if <code>imageIndex</code>
  1378. * is out of bounds.
  1379. * @exception IllegalArgumentException if the tile indices are
  1380. * out of bounds.
  1381. * @exception IOException if an error occurs during reading.
  1382. */
  1383. public BufferedImage readTile(int imageIndex,
  1384. int tileX, int tileY) throws IOException {
  1385. if ((tileX != 0) || (tileY != 0)) {
  1386. throw new IllegalArgumentException("Invalid tile indices");
  1387. }
  1388. return read(imageIndex);
  1389. }
  1390. /**
  1391. * Returns a new <code>Raster</code> object containing the raw
  1392. * pixel data from the tile, without any color conversion applied.
  1393. * The application must determine how to interpret the pixel data by other
  1394. * means.
  1395. *
  1396. * <p> If {@link #canReadRaster <code>canReadRaster()</code>} returns
  1397. * <code>false</code>, this method throws an
  1398. * <code>UnsupportedOperationException</code>.
  1399. *
  1400. * <p> The default implementation checks if reading
  1401. * <code>Raster</code>s is supported, and if so calls {@link
  1402. * #readRaster <code>readRaster(imageIndex, null)</code>} if
  1403. * <code>tileX</code> and <code>tileY</code> are 0, or throws an
  1404. * <code>IllegalArgumentException</code> otherwise.
  1405. *
  1406. * @param imageIndex the index of the image to be retrieved.
  1407. * @param tileX the column index (starting with 0) of the tile
  1408. * to be retrieved.
  1409. * @param tileY the row index (starting with 0) of the tile
  1410. * to be retrieved.
  1411. *
  1412. * @return the tile as a <code>Raster</code>.
  1413. *
  1414. * @exception UnsupportedOperationException if this plug-in does not
  1415. * support reading raw <code>Raster</code>s.
  1416. * @exception IllegalArgumentException if the tile indices are
  1417. * out of bounds.
  1418. * @exception IllegalStateException if the input source has not been
  1419. * set.
  1420. * @exception IndexOutOfBoundsException if <code>imageIndex</code>
  1421. * is out of bounds.
  1422. * @exception IOException if an error occurs during reading.
  1423. *
  1424. * @see #readTile
  1425. * @see #readRaster
  1426. * @see java.awt.image.Raster
  1427. */
  1428. public Raster readTileRaster(int imageIndex,
  1429. int tileX, int tileY) throws IOException {
  1430. if (!canReadRaster()) {
  1431. throw new UnsupportedOperationException
  1432. ("readTileRaster not supported!");
  1433. }
  1434. if ((tileX != 0) || (tileY != 0)) {
  1435. throw new IllegalArgumentException("Invalid tile indices");
  1436. }
  1437. return readRaster(imageIndex, null);
  1438. }
  1439. // RenderedImages
  1440. /**
  1441. * Returns a <code>RenderedImage</code> object that contains the
  1442. * contents of the image indexed by <code>imageIndex</code>. By
  1443. * default, the returned image is simply the
  1444. * <code>BufferedImage</code> returned by <code>read(imageIndex,
  1445. * param)</code>.
  1446. *
  1447. * <p> The semantics of this method may differ from those of the
  1448. * other <code>read</code> methods in several ways. First, any
  1449. * destination image and/or image type set in the
  1450. * <code>ImageReadParam</code> may be ignored. Second, the usual
  1451. * listener calls are not guaranteed to be made, or to be
  1452. * meaningful if they are. This is because the returned image may
  1453. * not be fully populated with pixel data at the time it is
  1454. * returned, or indeed at any time.
  1455. *
  1456. * <p> If the supplied <code>ImageReadParam</code> contains
  1457. * optional setting values not supported by this reader (<i>e.g.</i>
  1458. * source render size or any format-specific settings), they will
  1459. * be ignored.
  1460. *
  1461. * <p> The default implementation just calls {@link #read
  1462. * <code>read(imageIndex, param)</code>}.
  1463. *
  1464. * @param imageIndex the index of the image to be retrieved.
  1465. * @param param an <code>ImageReadParam</code> used to control
  1466. * the reading process, or <code>null</code>.
  1467. *
  1468. * @return a <code>RenderedImage</code> object providing a view of
  1469. * the image.
  1470. *
  1471. * @exception IllegalStateException if the input source has not been
  1472. * set.
  1473. * @exception IndexOutOfBoundsException if the supplied index is
  1474. * out of bounds.
  1475. * @exception IllegalArgumentException if the set of source and
  1476. * destination bands specified by
  1477. * <code>param.getSourceBands</code> and
  1478. * <code>param.getDestinationBands</code> differ in length or
  1479. * include indices that are out of bounds.
  1480. * @exception IllegalArgumentException if the resulting image
  1481. * would have a width or height less than 1.
  1482. * @exception IOException if an error occurs during reading.
  1483. */
  1484. public RenderedImage readAsRenderedImage(int imageIndex,
  1485. ImageReadParam param)
  1486. throws IOException {
  1487. return read(imageIndex, param);
  1488. }
  1489. // Thumbnails
  1490. /**
  1491. * Returns <code>true</code> if the image format understood by
  1492. * this reader supports thumbnail preview images associated with
  1493. * it. The default implementation returns <code>false</code>.
  1494. *
  1495. * <p> If this method returns <code>false</code>,
  1496. * <code>hasThumbnails</code> and <code>getNumThumbnails</code>
  1497. * will return <code>false</code> and <code>0</code>,
  1498. * respectively, and <code>readThumbnail</code> will throw an
  1499. * <code>UnsupportedOperationException</code>, regardless of their
  1500. * arguments.
  1501. *
  1502. * <p> A reader that does not support thumbnails need not
  1503. * implement any of the thumbnail-related methods.
  1504. *
  1505. * @return <code>true</code> if thumbnails are supported.
  1506. */
  1507. public boolean readerSupportsThumbnails() {
  1508. return false;
  1509. }
  1510. /**
  1511. * Returns <code>true</code> if the given image has thumbnail
  1512. * preview images associated with it. If the format does not
  1513. * support thumbnails (<code>readerSupportsThumbnails</code>
  1514. * returns <code>false</code>), <code>false</code> will be
  1515. * returned regardless of whether an input source has been set or
  1516. * whether <code>imageIndex</code> is in bounds.
  1517. *
  1518. * <p> The default implementation returns <code>true</code> if
  1519. * <code>getNumThumbnails</code> returns a value greater than 0.
  1520. *
  1521. * @param imageIndex the index of the image being queried.
  1522. *
  1523. * @return <code>true</code> if the given image has thumbnails.
  1524. *
  1525. * @exception IllegalStateException if the reader supports
  1526. * thumbnails but the input source has not been set.
  1527. * @exception IndexOutOfBoundsException if the reader supports
  1528. * thumbnails but <code>imageIndex</code> is out of bounds.
  1529. * @exception IOException if an error occurs during reading.
  1530. */
  1531. public boolean hasThumbnails(int imageIndex) throws IOException {
  1532. return getNumThumbnails(imageIndex) > 0;
  1533. }
  1534. /**
  1535. * Returns the number of thumbnail preview images associated with
  1536. * the given image. If the format does not support thumbnails,
  1537. * (<code>readerSupportsThumbnails</code> returns
  1538. * <code>false</code>), <code>0</code> will be returned regardless
  1539. * of whether an input source has been set or whether
  1540. * <code>imageIndex</code> is in bounds.
  1541. *
  1542. * <p> The default implementation returns 0 without checking its
  1543. * argument.
  1544. *
  1545. * @param imageIndex the index of the image being queried.
  1546. *
  1547. * @return the number of thumbnails associated with the given
  1548. * image.
  1549. *
  1550. * @exception IllegalStateException if the reader supports
  1551. * thumbnails but the input source has not been set.
  1552. * @exception IndexOutOfBoundsException if the reader supports
  1553. * thumbnails but <code>imageIndex</code> is out of bounds.
  1554. * @exception IOException if an error occurs during reading.
  1555. */
  1556. public int getNumThumbnails(int imageIndex)
  1557. throws IOException {
  1558. return 0;
  1559. }
  1560. /**
  1561. * Returns the width of the thumbnail preview image indexed by
  1562. * <code>thumbnailIndex</code>, associated with the image indexed
  1563. * by <code>ImageIndex</code>.
  1564. *
  1565. * <p> If the reader does not support thumbnails,
  1566. * (<code>readerSupportsThumbnails</code> returns
  1567. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1568. * will be thrown.
  1569. *
  1570. * <p> The default implementation simply returns
  1571. * <code>readThumbnail(imageindex,
  1572. * thumbnailIndex).getWidth()</code>. Subclasses should therefore
  1573. * override this method if possible in order to avoid forcing the
  1574. * thumbnail to be read.
  1575. *
  1576. * @param imageIndex the index of the image to be retrieved.
  1577. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1578. *
  1579. * @return the width of the desired thumbnail as an <code>int</code>.
  1580. *
  1581. * @exception UnsupportedOperationException if thumbnails are not
  1582. * supported.
  1583. * @exception IllegalStateException if the input source has not been set.
  1584. * @exception IndexOutOfBoundsException if either of the supplied
  1585. * indices are out of bounds.
  1586. * @exception IOException if an error occurs during reading.
  1587. */
  1588. public int getThumbnailWidth(int imageIndex, int thumbnailIndex)
  1589. throws IOException {
  1590. return readThumbnail(imageIndex, thumbnailIndex).getWidth();
  1591. }
  1592. /**
  1593. * Returns the height of the thumbnail preview image indexed by
  1594. * <code>thumbnailIndex</code>, associated with the image indexed
  1595. * by <code>ImageIndex</code>.
  1596. *
  1597. * <p> If the reader does not support thumbnails,
  1598. * (<code>readerSupportsThumbnails</code> returns
  1599. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1600. * will be thrown.
  1601. *
  1602. * <p> The default implementation simply returns
  1603. * <code>readThumbnail(imageindex,
  1604. * thumbnailIndex).getHeight()</code>. Subclasses should
  1605. * therefore override this method if possible in order to avoid
  1606. * forcing the thumbnail to be read.
  1607. *
  1608. * @param imageIndex the index of the image to be retrieved.
  1609. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1610. *
  1611. * @return the height of the desired thumbnail as an <code>int</code>.
  1612. *
  1613. * @exception UnsupportedOperationException if thumbnails are not
  1614. * supported.
  1615. * @exception IllegalStateException if the input source has not been set.
  1616. * @exception IndexOutOfBoundsException if either of the supplied
  1617. * indices are out of bounds.
  1618. * @exception IOException if an error occurs during reading.
  1619. */
  1620. public int getThumbnailHeight(int imageIndex, int thumbnailIndex)
  1621. throws IOException {
  1622. return readThumbnail(imageIndex, thumbnailIndex).getHeight();
  1623. }
  1624. /**
  1625. * Returns the thumbnail preview image indexed by
  1626. * <code>thumbnailIndex</code>, associated with the image indexed
  1627. * by <code>ImageIndex</code> as a <code>BufferedImage</code>.
  1628. *
  1629. * <p> Any registered <code>IIOReadProgressListener</code> objects
  1630. * will be notified by calling their
  1631. * <code>thumbnailStarted</code>, <code>thumbnailProgress</code>,
  1632. * and <code>thumbnailComplete</code> methods.
  1633. *
  1634. * <p> If the reader does not support thumbnails,
  1635. * (<code>readerSupportsThumbnails</code> returns
  1636. * <code>false</code>), an <code>UnsupportedOperationException</code>
  1637. * will be thrown regardless of whether an input source has been
  1638. * set or whether the indices are in bounds.
  1639. *
  1640. * <p> The default implementation throws an
  1641. * <code>UnsupportedOperationException</code>.
  1642. *
  1643. * @param imageIndex the index of the image to be retrieved.
  1644. * @param thumbnailIndex the index of the thumbnail to be retrieved.
  1645. *
  1646. * @return the desired thumbnail as a <code>BufferedImage</code>.
  1647. *
  1648. * @exception UnsupportedOperationException if thumbnails are not
  1649. * supported.
  1650. * @exception IllegalStateException if the input source has not been set.
  1651. * @exception IndexOutOfBoundsException if either of the supplied
  1652. * indices are out of bounds.
  1653. * @exception IOException if an error occurs during reading.
  1654. */
  1655. public BufferedImage readThumbnail(int imageIndex,
  1656. int thumbnailIndex)
  1657. throws IOException {
  1658. throw new UnsupportedOperationException("Thumbnails not supported!");
  1659. }
  1660. // Abort
  1661. /**
  1662. * Requests that any current read operation be aborted. The
  1663. * contents of the image following the abort will be undefined.
  1664. *
  1665. * <p> Readers should call <code>clearAbortRequest</code> at the
  1666. * beginning of each read operation, and poll the value of
  1667. * <code>abortRequested</code> regularly during the read.
  1668. */
  1669. public synchronized void abort() {
  1670. this.abortFlag = true;
  1671. }
  1672. /**
  1673. * Returns <code>true</code> if a request to abort the current
  1674. * read operation has been made since the reader was instantiated or
  1675. * <code>clearAbortRequest</code> was called.
  1676. *
  1677. * @return <code>true</code> if the current read operation should
  1678. * be aborted.
  1679. *
  1680. * @see #abort
  1681. * @see #clearAbortRequest
  1682. */
  1683. protected synchronized boolean abortRequested() {
  1684. return this.abortFlag;
  1685. }
  1686. /**
  1687. * Clears any previous abort request. After this method has been
  1688. * called, <code>abortRequested</code> will return
  1689. * <code>false</code>.
  1690. *
  1691. * @see #abort
  1692. * @see #abortRequested
  1693. */
  1694. protected synchronized void clearAbortRequest() {
  1695. this.abortFlag = false;
  1696. }
  1697. // Listeners
  1698. // Add an element to a list, creating a new list if the
  1699. // existing list is null, and return the list.
  1700. static List addToList(List l, Object elt) {
  1701. if (l == null) {
  1702. l = new ArrayList();
  1703. }
  1704. l.add(elt);
  1705. return l;
  1706. }
  1707. // Remove an element from a list, discarding the list if the
  1708. // resulting list is empty, and return the list or null.
  1709. static List removeFromList(List l, Object elt) {
  1710. if (l == null) {
  1711. return l;
  1712. }
  1713. l.remove(elt);
  1714. if (l.size() == 0) {
  1715. l = null;
  1716. }
  1717. return l;
  1718. }
  1719. /**
  1720. * Adds an <code>IIOReadWarningListener</code> to the list of
  1721. * registered warning listeners. If <code>listener</code> is
  1722. * <code>null</code>, no exception will be thrown and no action
  1723. * will be taken. Messages sent to the given listener will be
  1724. * localized, if possible, to match the current
  1725. * <code>Locale</code>. If no <code>Locale</code> has been set,
  1726. * warning messages may be localized as the reader sees fit.
  1727. *
  1728. * @param listener an <code>IIOReadWarningListener</code> to be registered.
  1729. *
  1730. * @see #removeIIOReadWarningListener
  1731. */
  1732. public void addIIOReadWarningListener(IIOReadWarningListener listener) {
  1733. if (listener == null) {
  1734. return;
  1735. }
  1736. warningListeners = addToList(warningListeners, listener);
  1737. warningLocales = addToList(warningLocales, getLocale());
  1738. }
  1739. /**
  1740. * Removes an <code>IIOReadWarningListener</code> from the list of
  1741. * registered error listeners. If the listener was not previously
  1742. * registered, or if <code>listener</code> is <code>null</code>,
  1743. * no exception will be thrown and no action will be taken.
  1744. *
  1745. * @param listener an IIOReadWarningListener to be unregistered.
  1746. *
  1747. * @see #addIIOReadWarningListener
  1748. */
  1749. public void removeIIOReadWarningListener(IIOReadWarningListener listener) {
  1750. if (listener == null || warningListeners == null) {
  1751. return;
  1752. }
  1753. int index = warningListeners.indexOf(listener);
  1754. if (index != -1) {
  1755. warningListeners.remove(index);
  1756. warningLocales.remove(index);
  1757. if (warningListeners.size() == 0) {
  1758. warningListeners = null;
  1759. warningLocales = null;
  1760. }
  1761. }
  1762. }
  1763. /**
  1764. * Removes all currently registered
  1765. * <code>IIOReadWarningListener</code> objects.
  1766. *
  1767. * <p> The default implementation sets the
  1768. * <code>warningListeners</code> and <code>warningLocales</code>
  1769. * instance variables to <code>null</code>.
  1770. */
  1771. public void removeAllIIOReadWarningListeners() {
  1772. warningListeners = null;
  1773. warningLocales = null;
  1774. }
  1775. /**
  1776. * Adds an <code>IIOReadProgressListener</code> to the list of
  1777. * registered progress listeners. If <code>listener</code> is
  1778. * <code>null</code>, no exception will be thrown and no action
  1779. * will be taken.
  1780. *
  1781. * @param listener an IIOReadProgressListener to be registered.
  1782. *
  1783. * @see #removeIIOReadProgressListener
  1784. */
  1785. public void addIIOReadProgressListener(IIOReadProgressListener listener) {
  1786. if (listener == null) {
  1787. return;
  1788. }
  1789. progressListeners = addToList(progressListeners, listener);
  1790. }
  1791. /**
  1792. * Removes an <code>IIOReadProgressListener</code> from the list
  1793. * of registered progress listeners. If the listener was not
  1794. * previously registered, or if <code>listener</code> is
  1795. * <code>null</code>, no exception will be thrown and no action
  1796. * will be taken.
  1797. *
  1798. * @param listener an IIOReadProgressListener to be unregistered.
  1799. *
  1800. * @see #addIIOReadProgressListener
  1801. */
  1802. public void
  1803. removeIIOReadProgressListener (IIOReadProgressListener listener) {
  1804. if (listener == null || progressListeners == null) {
  1805. return;
  1806. }
  1807. progressListeners = removeFromList(progressListeners, listener);
  1808. }
  1809. /**
  1810. * Removes all currently registered
  1811. * <code>IIOReadProgressListener</code> objects.
  1812. *
  1813. * <p> The default implementation sets the
  1814. * <code>progressListeners</code> instance variable to
  1815. * <code>null</code>.
  1816. */
  1817. public void removeAllIIOReadProgressListeners() {
  1818. progressListeners = null;
  1819. }
  1820. /**
  1821. * Adds an <code>IIOReadUpdateListener</code> to the list of
  1822. * registered update listeners. If <code>listener</code> is
  1823. * <code>null</code>, no exception will be thrown and no action
  1824. * will be taken. The listener will receive notification of pixel
  1825. * updates as images and thumbnails are decoded, including the
  1826. * starts and ends of progressive passes.
  1827. *
  1828. * <p> If no update listeners are present, the reader may choose
  1829. * to perform fewer updates to the pixels of the destination
  1830. * images and/or thumbnails, which may result in more efficient
  1831. * decoding.
  1832. *
  1833. * <p> For example, in progressive JPEG decoding each pass
  1834. * contains updates to a set of coefficients, which would have to
  1835. * be transformed into pixel values and converted to an RGB color
  1836. * space for each pass if listeners are present. If no listeners
  1837. * are present, the coefficients may simply be accumulated and the
  1838. * final results transformed and color converted one time only.
  1839. *
  1840. * <p> The final results of decoding will be the same whether or
  1841. * not intermediate updates are performed. Thus if only the final
  1842. * image is desired it may be perferable not to register any
  1843. * <code>IIOReadUpdateListener</code>s. In general, progressive
  1844. * updating is most effective when fetching images over a network
  1845. * connection that is very slow compared to local CPU processing;
  1846. * over a fast connection, progressive updates may actually slow
  1847. * down the presentation of the image.
  1848. *
  1849. * @param listener an IIOReadUpdateListener to be registered.
  1850. *
  1851. * @see #removeIIOReadUpdateListener
  1852. */
  1853. public void
  1854. addIIOReadUpdateListener(IIOReadUpdateListener listener) {
  1855. if (listener == null) {
  1856. return;
  1857. }
  1858. updateListeners = addToList(updateListeners, listener);
  1859. }
  1860. /**
  1861. * Removes an <code>IIOReadUpdateListener</code> from the list of
  1862. * registered update listeners. If the listener was not
  1863. * previously registered, or if <code>listener</code> is
  1864. * <code>null</code>, no exception will be thrown and no action
  1865. * will be taken.
  1866. *
  1867. * @param listener an IIOReadUpdateListener to be unregistered.
  1868. *
  1869. * @see #addIIOReadUpdateListener
  1870. */
  1871. public void removeIIOReadUpdateListener(IIOReadUpdateListener listener) {
  1872. if (listener == null || updateListeners == null) {
  1873. return;
  1874. }
  1875. updateListeners = removeFromList(updateListeners, listener);
  1876. }
  1877. /**
  1878. * Removes all currently registered
  1879. * <code>IIOReadUpdateListener</code> objects.
  1880. *
  1881. * <p> The default implementation sets the
  1882. * <code>updateListeners</code> instance variable to
  1883. * <code>null</code>.
  1884. */
  1885. public void removeAllIIOReadUpdateListeners() {
  1886. updateListeners = null;
  1887. }
  1888. /**
  1889. * Broadcasts the start of an sequence of image reads to all
  1890. * registered <code>IIOReadProgressListener</code>s by calling
  1891. * their <code>sequenceStarted</code> method. Subclasses may use
  1892. * this method as a convenience.
  1893. *
  1894. * @param minIndex the lowest index being read.
  1895. */
  1896. protected void processSequenceStarted(int minIndex) {
  1897. if (progressListeners == null) {
  1898. return;
  1899. }
  1900. int numListeners = progressListeners.size();
  1901. for (int i = 0; i < numListeners; i++) {
  1902. IIOReadProgressListener listener =
  1903. (IIOReadProgressListener)progressListeners.get(i);
  1904. listener.sequenceStarted(this, minIndex);
  1905. }
  1906. }
  1907. /**
  1908. * Broadcasts the completion of an sequence of image reads to all
  1909. * registered <code>IIOReadProgressListener</code>s by calling
  1910. * their <code>sequenceComplete</code> method. Subclasses may use
  1911. * this method as a convenience.
  1912. */
  1913. protected void processSequenceComplete() {
  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.sequenceComplete(this);
  1922. }
  1923. }
  1924. /**
  1925. * Broadcasts the start of an image read to all registered
  1926. * <code>IIOReadProgressListener</code>s by calling their
  1927. * <code>imageStarted</code> method. Subclasses may use this
  1928. * method as a convenience.
  1929. *
  1930. * @param imageIndex the index of the image about to be read.
  1931. */
  1932. protected void processImageStarted(int imageIndex) {
  1933. if (progressListeners == null) {
  1934. return;
  1935. }
  1936. int numListeners = progressListeners.size();
  1937. for (int i = 0; i < numListeners; i++) {
  1938. IIOReadProgressListener listener =
  1939. (IIOReadProgressListener)progressListeners.get(i);
  1940. listener.imageStarted(this, imageIndex);
  1941. }
  1942. }
  1943. /**
  1944. * Broadcasts the current percentage of image completion to all
  1945. * registered <code>IIOReadProgressListener</code>s by calling
  1946. * their <code>imageProgress</code> method. Subclasses may use
  1947. * this method as a convenience.
  1948. *
  1949. * @param percentageDone the current percentage of completion,
  1950. * as a <code>float</code>.
  1951. */
  1952. protected void processImageProgress(float percentageDone) {
  1953. if (progressListeners == null) {
  1954. return;
  1955. }
  1956. int numListeners = progressListeners.size();
  1957. for (int i = 0; i < numListeners; i++) {
  1958. IIOReadProgressListener listener =
  1959. (IIOReadProgressListener)progressListeners.get(i);
  1960. listener.imageProgress(this, percentageDone);
  1961. }
  1962. }
  1963. /**
  1964. * Broadcasts the completion of an image read to all registered
  1965. * <code>IIOReadProgressListener</code>s by calling their
  1966. * <code>imageComplete</code> method. Subclasses may use this
  1967. * method as a convenience.
  1968. */
  1969. protected void processImageComplete() {
  1970. if (progressListeners == null) {
  1971. return;
  1972. }
  1973. int numListeners = progressListeners.size();
  1974. for (int i = 0; i < numListeners; i++) {
  1975. IIOReadProgressListener listener =
  1976. (IIOReadProgressListener)progressListeners.get(i);
  1977. listener.imageComplete(this);
  1978. }
  1979. }
  1980. /**
  1981. * Broadcasts the start of a thumbnail read to all registered
  1982. * <code>IIOReadProgressListener</code>s by calling their
  1983. * <code>thumbnailStarted</code> method. Subclasses may use this
  1984. * method as a convenience.
  1985. *
  1986. * @param imageIndex the index of the image associated with the
  1987. * thumbnail.
  1988. * @param thumbnailIndex the index of the thumbnail.
  1989. */
  1990. protected void processThumbnailStarted(int imageIndex,
  1991. int thumbnailIndex) {
  1992. if (progressListeners == null) {
  1993. return;
  1994. }
  1995. int numListeners = progressListeners.size();
  1996. for (int i = 0; i < numListeners; i++) {
  1997. IIOReadProgressListener listener =
  1998. (IIOReadProgressListener)progressListeners.get(i);
  1999. listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
  2000. }
  2001. }
  2002. /**
  2003. * Broadcasts the current percentage of thumbnail completion to
  2004. * all registered <code>IIOReadProgressListener</code>s by calling
  2005. * their <code>thumbnailProgress</code> method. Subclasses may
  2006. * use this method as a convenience.
  2007. *
  2008. * @param percentageDone the current percentage of completion,
  2009. * as a <code>float</code>.
  2010. */
  2011. protected void processThumbnailProgress(float percentageDone) {
  2012. if (progressListeners == null) {
  2013. return;
  2014. }
  2015. int numListeners = progressListeners.size();
  2016. for (int i = 0; i < numListeners; i++) {
  2017. IIOReadProgressListener listener =
  2018. (IIOReadProgressListener)progressListeners.get(i);
  2019. listener.thumbnailProgress(this, percentageDone);
  2020. }
  2021. }
  2022. /**
  2023. * Broadcasts the completion of a thumbnail read to all registered
  2024. * <code>IIOReadProgressListener</code>s by calling their
  2025. * <code>thumbnailComplete</code> method. Subclasses may use this
  2026. * method as a convenience.
  2027. */
  2028. protected void processThumbnailComplete() {
  2029. if (progressListeners == null) {
  2030. return;
  2031. }
  2032. int numListeners = progressListeners.size();
  2033. for (int i = 0; i < numListeners; i++) {
  2034. IIOReadProgressListener listener =
  2035. (IIOReadProgressListener)progressListeners.get(i);
  2036. listener.thumbnailComplete(this);
  2037. }
  2038. }
  2039. /**
  2040. * Broadcasts that the read has been aborted to all registered
  2041. * <code>IIOReadProgressListener</code>s by calling their
  2042. * <code>readAborted</code> method. Subclasses may use this
  2043. * method as a convenience.
  2044. */
  2045. protected void processReadAborted() {
  2046. if (progressListeners == null) {
  2047. return;
  2048. }
  2049. int numListeners = progressListeners.size();
  2050. for (int i = 0; i < numListeners; i++) {
  2051. IIOReadProgressListener listener =
  2052. (IIOReadProgressListener)progressListeners.get(i);
  2053. listener.readAborted(this);
  2054. }
  2055. }
  2056. /**
  2057. * Broadcasts the beginning of a progressive pass to all
  2058. * registered <code>IIOReadUpdateListener</code>s by calling their
  2059. * <code>passStarted</code> method. Subclasses may use this
  2060. * method as a convenience.
  2061. *
  2062. * @param theImage the <code>BufferedImage</code> being updated.
  2063. * @param pass the index of the current pass, starting with 0.
  2064. * @param minPass the index of the first pass that will be decoded.
  2065. * @param maxPass the index of the last pass that will be decoded.
  2066. * @param minX the X coordinate of the upper-left pixel included
  2067. * in the pass.
  2068. * @param minY the X coordinate of the upper-left pixel included
  2069. * in the pass.
  2070. * @param periodX the horizontal separation between pixels.
  2071. * @param periodY the vertical separation between pixels.
  2072. * @param bands an array of <code>int</code>s indicating the
  2073. * set of affected bands of the destination.
  2074. */
  2075. protected void processPassStarted(BufferedImage theImage,
  2076. int pass,
  2077. int minPass, int maxPass,
  2078. int minX, int minY,
  2079. int periodX, int periodY,
  2080. int[] bands) {
  2081. if (updateListeners == null) {
  2082. return;
  2083. }
  2084. int numListeners = updateListeners.size();
  2085. for (int i = 0; i < numListeners; i++) {
  2086. IIOReadUpdateListener listener =
  2087. (IIOReadUpdateListener)updateListeners.get(i);
  2088. listener.passStarted(this, theImage, pass,
  2089. minPass,
  2090. maxPass,
  2091. minX, minY,
  2092. periodX, periodY,
  2093. bands);
  2094. }
  2095. }
  2096. /**
  2097. * Broadcasts the update of a set of samples to all registered
  2098. * <code>IIOReadUpdateListener</code>s by calling their
  2099. * <code>imageUpdate</code> method. Subclasses may use this
  2100. * method as a convenience.
  2101. *
  2102. * @param theImage the <code>BufferedImage</code> being updated.
  2103. * @param minX the X coordinate of the upper-left pixel included
  2104. * in the pass.
  2105. * @param minY the X coordinate of the upper-left pixel included
  2106. * in the pass.
  2107. * @param width the total width of the area being updated, including
  2108. * pixels being skipped if <code>periodX > 1</code>.
  2109. * @param height the total height of the area being updated,
  2110. * including pixels being skipped if <code>periodY > 1</code>.
  2111. * @param periodX the horizontal separation between pixels.
  2112. * @param periodY the vertical separation between pixels.
  2113. * @param bands an array of <code>int</code>s indicating the
  2114. * set of affected bands of the destination.
  2115. */
  2116. protected void processImageUpdate(BufferedImage theImage,
  2117. int minX, int minY,
  2118. int width, int height,
  2119. int periodX, int periodY,
  2120. int[] bands) {
  2121. if (updateListeners == null) {
  2122. return;
  2123. }
  2124. int numListeners = updateListeners.size();
  2125. for (int i = 0; i < numListeners; i++) {
  2126. IIOReadUpdateListener listener =
  2127. (IIOReadUpdateListener)updateListeners.get(i);
  2128. listener.imageUpdate(this,
  2129. theImage,
  2130. minX, minY,
  2131. width, height,
  2132. periodX, periodY,
  2133. bands);
  2134. }
  2135. }
  2136. /**
  2137. * Broadcasts the end of a progressive pass to all
  2138. * registered <code>IIOReadUpdateListener</code>s by calling their
  2139. * <code>passComplete</code> method. Subclasses may use this
  2140. * method as a convenience.
  2141. *
  2142. * @param theImage the <code>BufferedImage</code> being updated.
  2143. */
  2144. protected void processPassComplete(BufferedImage theImage) {
  2145. if (updateListeners == null) {
  2146. return;
  2147. }
  2148. int numListeners = updateListeners.size();
  2149. for (int i = 0; i < numListeners; i++) {
  2150. IIOReadUpdateListener listener =
  2151. (IIOReadUpdateListener)updateListeners.get(i);
  2152. listener.passComplete(this, theImage);
  2153. }
  2154. }
  2155. /**
  2156. * Broadcasts the beginning of a thumbnail progressive pass to all
  2157. * registered <code>IIOReadUpdateListener</code>s by calling their
  2158. * <code>thumbnailPassStarted</code> method. Subclasses may use this
  2159. * method as a convenience.
  2160. *
  2161. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2162. * being updated.
  2163. * @param pass the index of the current pass, starting with 0.
  2164. * @param minPass the index of the first pass that will be decoded.
  2165. * @param maxPass the index of the last pass that will be decoded.
  2166. * @param minX the X coordinate of the upper-left pixel included
  2167. * in the pass.
  2168. * @param minY the X coordinate of the upper-left pixel included
  2169. * in the pass.
  2170. * @param periodX the horizontal separation between pixels.
  2171. * @param periodY the vertical separation between pixels.
  2172. * @param bands an array of <code>int</code>s indicating the
  2173. * set of affected bands of the destination.
  2174. */
  2175. protected void processThumbnailPassStarted(BufferedImage theThumbnail,
  2176. int pass,
  2177. int minPass, int maxPass,
  2178. int minX, int minY,
  2179. int periodX, int periodY,
  2180. int[] bands) {
  2181. if (updateListeners == null) {
  2182. return;
  2183. }
  2184. int numListeners = updateListeners.size();
  2185. for (int i = 0; i < numListeners; i++) {
  2186. IIOReadUpdateListener listener =
  2187. (IIOReadUpdateListener)updateListeners.get(i);
  2188. listener.thumbnailPassStarted(this, theThumbnail, pass,
  2189. minPass,
  2190. maxPass,
  2191. minX, minY,
  2192. periodX, periodY,
  2193. bands);
  2194. }
  2195. }
  2196. /**
  2197. * Broadcasts the update of a set of samples in a thumbnail image
  2198. * to all registered <code>IIOReadUpdateListener</code>s by
  2199. * calling their <code>thumbnailUpdate</code> method. Subclasses may
  2200. * use this method as a convenience.
  2201. *
  2202. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2203. * being updated.
  2204. * @param minX the X coordinate of the upper-left pixel included
  2205. * in the pass.
  2206. * @param minY the X coordinate of the upper-left pixel included
  2207. * in the pass.
  2208. * @param width the total width of the area being updated, including
  2209. * pixels being skipped if <code>periodX > 1</code>.
  2210. * @param height the total height of the area being updated,
  2211. * including pixels being skipped if <code>periodY > 1</code>.
  2212. * @param periodX the horizontal separation between pixels.
  2213. * @param periodY the vertical separation between pixels.
  2214. * @param bands an array of <code>int</code>s indicating the
  2215. * set of affected bands of the destination.
  2216. */
  2217. protected void processThumbnailUpdate(BufferedImage theThumbnail,
  2218. int minX, int minY,
  2219. int width, int height,
  2220. int periodX, int periodY,
  2221. int[] bands) {
  2222. if (updateListeners == null) {
  2223. return;
  2224. }
  2225. int numListeners = updateListeners.size();
  2226. for (int i = 0; i < numListeners; i++) {
  2227. IIOReadUpdateListener listener =
  2228. (IIOReadUpdateListener)updateListeners.get(i);
  2229. listener.thumbnailUpdate(this,
  2230. theThumbnail,
  2231. minX, minY,
  2232. width, height,
  2233. periodX, periodY,
  2234. bands);
  2235. }
  2236. }
  2237. /**
  2238. * Broadcasts the end of a thumbnail progressive pass to all
  2239. * registered <code>IIOReadUpdateListener</code>s by calling their
  2240. * <code>thumbnailPassComplete</code> method. Subclasses may use this
  2241. * method as a convenience.
  2242. *
  2243. * @param theThumbnail the <code>BufferedImage</code> thumbnail
  2244. * being updated.
  2245. */
  2246. protected void processThumbnailPassComplete(BufferedImage theThumbnail) {
  2247. if (updateListeners == null) {
  2248. return;
  2249. }
  2250. int numListeners = updateListeners.size();
  2251. for (int i = 0; i < numListeners; i++) {
  2252. IIOReadUpdateListener listener =
  2253. (IIOReadUpdateListener)updateListeners.get(i);
  2254. listener.thumbnailPassComplete(this, theThumbnail);
  2255. }
  2256. }
  2257. /**
  2258. * Broadcasts a warning message to all registered
  2259. * <code>IIOReadWarningListener</code>s by calling their
  2260. * <code>warningOccurred</code> method. Subclasses may use this
  2261. * method as a convenience.
  2262. *
  2263. * @param warning the warning message to send.
  2264. *
  2265. * @exception IllegalArgumentException if <code>warning</code>
  2266. * is <code>null</code>.
  2267. */
  2268. protected void processWarningOccurred(String warning) {
  2269. if (warningListeners == null) {
  2270. return;
  2271. }
  2272. if (warning == null) {
  2273. throw new IllegalArgumentException("warning == null!");
  2274. }
  2275. int numListeners = warningListeners.size();
  2276. for (int i = 0; i < numListeners; i++) {
  2277. IIOReadWarningListener listener =
  2278. (IIOReadWarningListener)warningListeners.get(i);
  2279. listener.warningOccurred(this, warning);
  2280. }
  2281. }
  2282. /**
  2283. * Broadcasts a localized warning message to all registered
  2284. * <code>IIOReadWarningListener</code>s by calling their
  2285. * <code>warningOccurred</code> method with a string taken
  2286. * from a <code>ResourceBundle</code>. Subclasses may use this
  2287. * method as a convenience.
  2288. *
  2289. * @param baseName the base name of a set of
  2290. * <code>ResourceBundle</code>s containing localized warning
  2291. * messages.
  2292. * @param keyword the keyword used to index the warning message
  2293. * within the set of <code>ResourceBundle</code>s.
  2294. *
  2295. * @exception IllegalArgumentException if <code>baseName</code>
  2296. * is <code>null</code>.
  2297. * @exception IllegalArgumentException if <code>keyword</code>
  2298. * is <code>null</code>.
  2299. * @exception IllegalArgumentException if no appropriate
  2300. * <code>ResourceBundle</code> may be located.
  2301. * @exception IllegalArgumentException if the named resource is
  2302. * not found in the located <code>ResourceBundle</code>.
  2303. * @exception IllegalArgumentException if the object retrieved
  2304. * from the <code>ResourceBundle</code> is not a
  2305. * <code>String</code>.
  2306. */
  2307. protected void processWarningOccurred(String baseName,
  2308. String keyword) {
  2309. if (warningListeners == null) {
  2310. return;
  2311. }
  2312. if (baseName == null) {
  2313. throw new IllegalArgumentException("baseName == null!");
  2314. }
  2315. if (keyword == null) {
  2316. throw new IllegalArgumentException("keyword == null!");
  2317. }
  2318. int numListeners = warningListeners.size();
  2319. for (int i = 0; i < numListeners; i++) {
  2320. IIOReadWarningListener listener =
  2321. (IIOReadWarningListener)warningListeners.get(i);
  2322. Locale locale = (Locale)warningLocales.get(i);
  2323. if (locale == null) {
  2324. locale = Locale.getDefault();
  2325. }
  2326. /**
  2327. * If an applet supplies an implementation of ImageReader and
  2328. * resource bundles, then the resource bundle will need to be
  2329. * accessed via the applet class loader. So first try the context
  2330. * class loader to locate the resource bundle.
  2331. * If that throws MissingResourceException, then try the
  2332. * system class loader.
  2333. */
  2334. ClassLoader loader = (ClassLoader)
  2335. java.security.AccessController.doPrivileged(
  2336. new java.security.PrivilegedAction() {
  2337. public Object run() {
  2338. return Thread.currentThread().getContextClassLoader();
  2339. }
  2340. });
  2341. ResourceBundle bundle = null;
  2342. try {
  2343. bundle = ResourceBundle.getBundle(baseName, locale, loader);
  2344. } catch (MissingResourceException mre) {
  2345. try {
  2346. bundle = ResourceBundle.getBundle(baseName, locale);
  2347. } catch (MissingResourceException mre1) {
  2348. throw new IllegalArgumentException("Bundle not found!");
  2349. }
  2350. }
  2351. String warning = null;
  2352. try {
  2353. warning = bundle.getString(keyword);
  2354. } catch (ClassCastException cce) {
  2355. throw new IllegalArgumentException("Resource is not a String!");
  2356. } catch (MissingResourceException mre) {
  2357. throw new IllegalArgumentException("Resource is missing!");
  2358. }
  2359. listener.warningOccurred(this, warning);
  2360. }
  2361. }
  2362. // State management
  2363. /**
  2364. * Restores the <code>ImageReader</code> to its initial state.
  2365. *
  2366. * <p> The default implementation calls <code>setInput(null,
  2367. * false)</code>, <code>setLocale(null)</code>,
  2368. * <code>removeAllIIOReadUpdateListeners()</code>,
  2369. * <code>removeAllIIOReadWarningListeners()</code>,
  2370. * <code>removeAllIIOReadProgressListeners()</code>, and
  2371. * <code>clearAbortRequest</code>.
  2372. */
  2373. public void reset() {
  2374. setInput(null, false, false);
  2375. setLocale(null);
  2376. removeAllIIOReadUpdateListeners();
  2377. removeAllIIOReadProgressListeners();
  2378. removeAllIIOReadWarningListeners();
  2379. clearAbortRequest();
  2380. }
  2381. /**
  2382. * Allows any resources held by this object to be released. The
  2383. * result of calling any other method (other than
  2384. * <code>finalize</code>) subsequent to a call to this method
  2385. * is undefined.
  2386. *
  2387. * <p>It is important for applications to call this method when they
  2388. * know they will no longer be using this <code>ImageReader</code>.
  2389. * Otherwise, the reader may continue to hold on to resources
  2390. * indefinitely.
  2391. *
  2392. * <p>The default implementation of this method in the superclass does
  2393. * nothing. Subclass implementations should ensure that all resources,
  2394. * especially native resources, are released.
  2395. */
  2396. public void dispose() {
  2397. }
  2398. // Utility methods
  2399. /**
  2400. * A utility method that may be used by readers to compute the
  2401. * region of the source image that should be read, taking into
  2402. * account any source region and subsampling offset settings in
  2403. * the supplied <code>ImageReadParam</code>. The actual
  2404. * subsampling factors, destination size, and destination offset
  2405. * are <em>not</em> taken into consideration, thus further
  2406. * clipping must take place. The {@link #computeRegions
  2407. * <code>computeRegions</code>} method performs all necessary
  2408. * clipping.
  2409. *
  2410. * @param param the <code>ImageReadParam</code> being used, or
  2411. * <code>null</code>.
  2412. * @param srcWidth the width of the source image.
  2413. * @param srcHeight the height of the source image.
  2414. *
  2415. * @return the source region as a <code>Rectangle</code>.
  2416. */
  2417. protected static Rectangle getSourceRegion(ImageReadParam param,
  2418. int srcWidth,
  2419. int srcHeight) {
  2420. Rectangle sourceRegion = new Rectangle(0, 0, srcWidth, srcHeight);
  2421. if (param != null) {
  2422. Rectangle region = param.getSourceRegion();
  2423. if (region != null) {
  2424. sourceRegion = sourceRegion.intersection(region);
  2425. }
  2426. int subsampleXOffset = param.getSubsamplingXOffset();
  2427. int subsampleYOffset = param.getSubsamplingYOffset();
  2428. sourceRegion.x += subsampleXOffset;
  2429. sourceRegion.y += subsampleYOffset;
  2430. sourceRegion.width -= subsampleXOffset;
  2431. sourceRegion.height -= subsampleYOffset;
  2432. }
  2433. return sourceRegion;
  2434. }
  2435. /**
  2436. * Computes the source region of interest and the destination
  2437. * region of interest, taking the width and height of the source
  2438. * image, an optional destination image, and an optional
  2439. * <code>ImageReadParam</code> into account. The source region
  2440. * begins with the entire source image. Then that is clipped to
  2441. * the source region specified in the <code>ImageReadParam</code>,
  2442. * if one is specified.
  2443. *
  2444. * <p> If either of the destination offsets are negative, the
  2445. * source region is clipped so that its top left will coincide
  2446. * with the top left of the destination image, taking subsampling
  2447. * into account. Then the result is clipped to the destination
  2448. * image on the right and bottom, if one is specified, taking
  2449. * subsampling and destination offsets into account.
  2450. *
  2451. * <p> Similarly, the destination region begins with the source
  2452. * image, is translated to the destination offset given in the
  2453. * <code>ImageReadParam</code> if there is one, and finally is
  2454. * clipped to the destination image, if there is one.
  2455. *
  2456. * <p> If either the source or destination regions end up having a
  2457. * width or height of 0, an <code>IllegalArgumentException</code>
  2458. * is thrown.
  2459. *
  2460. * <p> The {@link #getSourceRegion <code>getSourceRegion</code>}
  2461. * method may be used if only source clipping is desired.
  2462. *
  2463. * @param param an <code>ImageReadParam</code>, or <code>null</code>.
  2464. * @param srcWidth the width of the source image.
  2465. * @param srcHeight the height of the source image.
  2466. * @param image a <code>BufferedImage</code> that will be the
  2467. * destination image, or <code>null</code>.
  2468. * @param srcRegion a <code>Rectangle</code> that will be filled with
  2469. * the source region of interest.
  2470. * @param destRegion a <code>Rectangle</code> that will be filled with
  2471. * the destination region of interest.
  2472. * @exception IllegalArgumentException if <code>srcRegion</code>
  2473. * is <code>null</code>.
  2474. * @exception IllegalArgumentException if <code>dstRegion</code>
  2475. * is <code>null</code>.
  2476. * @exception IllegalArgumentException if the resulting source or
  2477. * destination region is empty.
  2478. */
  2479. protected static void computeRegions(ImageReadParam param,
  2480. int srcWidth,
  2481. int srcHeight,
  2482. BufferedImage image,
  2483. Rectangle srcRegion,
  2484. Rectangle destRegion) {
  2485. if (srcRegion == null) {
  2486. throw new IllegalArgumentException("srcRegion == null!");
  2487. }
  2488. if (destRegion == null) {
  2489. throw new IllegalArgumentException("destRegion == null!");
  2490. }
  2491. // Start with the entire source image
  2492. srcRegion.setBounds(0, 0, srcWidth, srcHeight);
  2493. // Destination also starts with source image, as that is the
  2494. // maximum extent if there is no subsampling
  2495. destRegion.setBounds(0, 0, srcWidth, srcHeight);
  2496. // Clip that to the param region, if there is one
  2497. int periodX = 1;
  2498. int periodY = 1;
  2499. int gridX = 0;
  2500. int gridY = 0;
  2501. if (param != null) {
  2502. Rectangle paramSrcRegion = param.getSourceRegion();
  2503. if (paramSrcRegion != null) {
  2504. srcRegion.setBounds(srcRegion.intersection(paramSrcRegion));
  2505. }
  2506. periodX = param.getSourceXSubsampling();
  2507. periodY = param.getSourceYSubsampling();
  2508. gridX = param.getSubsamplingXOffset();
  2509. gridY = param.getSubsamplingYOffset();
  2510. srcRegion.translate(gridX, gridY);
  2511. srcRegion.width -= gridX;
  2512. srcRegion.height -= gridY;
  2513. destRegion.setLocation(param.getDestinationOffset());
  2514. }
  2515. // Now clip any negative destination offsets, i.e. clip
  2516. // to the top and left of the destination image
  2517. if (destRegion.x < 0) {
  2518. int delta = -destRegion.x*periodX;
  2519. srcRegion.x += delta;
  2520. srcRegion.width -= delta;
  2521. destRegion.x = 0;
  2522. }
  2523. if (destRegion.y < 0) {
  2524. int delta = -destRegion.y*periodY;
  2525. srcRegion.y += delta;
  2526. srcRegion.height -= delta;
  2527. destRegion.y = 0;
  2528. }
  2529. // Now clip the destination Region to the subsampled width and height
  2530. int subsampledWidth = (srcRegion.width + periodX - 1)/periodX;
  2531. int subsampledHeight = (srcRegion.height + periodY - 1)/periodY;
  2532. destRegion.width = subsampledWidth;
  2533. destRegion.height = subsampledHeight;
  2534. // Now clip that to right and bottom of the destination image,
  2535. // if there is one, taking subsampling into account
  2536. if (image != null) {
  2537. Rectangle destImageRect = new Rectangle(0, 0,
  2538. image.getWidth(),
  2539. image.getHeight());
  2540. destRegion.setBounds(destRegion.intersection(destImageRect));
  2541. if (destRegion.isEmpty()) {
  2542. throw new IllegalArgumentException
  2543. ("Empty destination region!");
  2544. }
  2545. int deltaX = destRegion.x + subsampledWidth - image.getWidth();
  2546. if (deltaX > 0) {
  2547. srcRegion.width -= deltaX*periodX;
  2548. }
  2549. int deltaY = destRegion.y + subsampledHeight - image.getHeight();
  2550. if (deltaY > 0) {
  2551. srcRegion.height -= deltaY*periodY;
  2552. }
  2553. }
  2554. if (srcRegion.isEmpty() || destRegion.isEmpty()) {
  2555. throw new IllegalArgumentException("Empty region!");
  2556. }
  2557. }
  2558. /**
  2559. * A utility method that may be used by readers to test the
  2560. * validity of the source and destination band settings of an
  2561. * <code>ImageReadParam</code>. This method may be called as soon
  2562. * as the reader knows both the number of bands of the source
  2563. * image as it exists in the input stream, and the number of bands
  2564. * of the destination image that being written.
  2565. *
  2566. * <p> The method retrieves the source and destination band
  2567. * setting arrays from param using the <code>getSourceBands</code>
  2568. * and <code>getDestinationBands</code>methods (or considers them
  2569. * to be <code>null</code> if <code>param</code> is
  2570. * <code>null</code>). If the source band setting array is
  2571. * <code>null</code>, it is considered to be equal to the array
  2572. * <code>{ 0, 1, ..., numSrcBands - 1 }</code>, and similarly for
  2573. * the destination band setting array.
  2574. *
  2575. * <p> The method then tests that both arrays are equal in length,
  2576. * and that neither array contains a value larger than the largest
  2577. * available band index.
  2578. *
  2579. * <p> Any failure results in an
  2580. * <code>IllegalArgumentException</code> being thrown; success
  2581. * results in the method returning silently.
  2582. *
  2583. * @param param the <code>ImageReadParam</code> being used to read
  2584. * the image.
  2585. * @param numSrcBands the number of bands of the image as it exists
  2586. * int the input source.
  2587. * @param numDstBands the number of bands in the destination image
  2588. * being written.
  2589. *
  2590. * @exception IllegalArgumentException if <code>param</code>
  2591. * contains an invalid specification of a source and/or
  2592. * destination band subset.
  2593. */
  2594. protected static void checkReadParamBandSettings(ImageReadParam param,
  2595. int numSrcBands,
  2596. int numDstBands) {
  2597. // A null param is equivalent to srcBands == dstBands == null.
  2598. int[] srcBands = null;
  2599. int[] dstBands = null;
  2600. if (param != null) {
  2601. srcBands = param.getSourceBands();
  2602. dstBands = param.getDestinationBands();
  2603. }
  2604. int paramSrcBandLength =
  2605. (srcBands == null) ? numSrcBands : srcBands.length;
  2606. int paramDstBandLength =
  2607. (dstBands == null) ? numDstBands : dstBands.length;
  2608. if (paramSrcBandLength != paramDstBandLength) {
  2609. throw new IllegalArgumentException("ImageReadParam num source & dest bands differ!");
  2610. }
  2611. if (srcBands != null) {
  2612. for (int i = 0; i < srcBands.length; i++) {
  2613. if (srcBands[i] >= numSrcBands) {
  2614. throw new IllegalArgumentException("ImageReadParam source bands contains a value >= the number of source bands!");
  2615. }
  2616. }
  2617. }
  2618. if (dstBands != null) {
  2619. for (int i = 0; i < dstBands.length; i++) {
  2620. if (dstBands[i] >= numDstBands) {
  2621. throw new IllegalArgumentException("ImageReadParam dest bands contains a value >= the number of dest bands!");
  2622. }
  2623. }
  2624. }
  2625. }
  2626. /**
  2627. * Returns the <code>BufferedImage</code> to which decoded pixel
  2628. * data should be written. The image is determined by inspecting
  2629. * the supplied <code>ImageReadParam</code> if it is
  2630. * non-<code>null</code> if its <code>getDestination</code>
  2631. * method returns a non-<code>null</code> value, that image is
  2632. * simply returned. Otherwise,
  2633. * <code>param.getDestinationType</code> method is called to
  2634. * determine if a particular image type has been specified. If
  2635. * so, the returned <code>ImageTypeSpecifier</code> is used after
  2636. * checking that it is equal to one of those included in
  2637. * <code>imageTypes</code>.
  2638. *
  2639. * <p> If <code>param</code> is <code>null</code> or the above
  2640. * steps have not yielded an image or an
  2641. * <code>ImageTypeSpecifier</code>, the first value obtained from
  2642. * the <code>imageTypes</code> parameter is used. Typically, the
  2643. * caller will set <code>imageTypes</code> to the value of
  2644. * <code>getImageTypes(imageIndex)</code>.
  2645. *
  2646. * <p> Next, the dimensions of the image are determined by a call
  2647. * to <code>computeRegions</code>. The actual width and height of
  2648. * the image being decoded are passed in as the <code>width</code>
  2649. * and <code>height</code> parameters.
  2650. *
  2651. * @param param an <code>ImageReadParam</code> to be used to get
  2652. * the destination image or image type, or <code>null</code>.
  2653. * @param imageTypes an <code>Iterator</code> of
  2654. * <code>ImageTypeSpecifier</code>s indicating the legal image
  2655. * types, with the default first.
  2656. * @param width the true width of the image or tile begin decoded.
  2657. * @param height the true width of the image or tile being decoded.
  2658. *
  2659. * @return the <code>BufferedImage</code> to which decoded pixel
  2660. * data should be written.
  2661. *
  2662. * @exception IIOException if the <code>ImageTypeSpecifier</code>
  2663. * specified by <code>param</code> does not match any of the legal
  2664. * ones from <code>imageTypes</code>.
  2665. * @exception IllegalArgumentException if <code>imageTypes</code>
  2666. * is <code>null</code> or empty, or if an object not of type
  2667. * <code>ImageTypeSpecifier</code> is retrieved from it.
  2668. * @exception IllegalArgumentException if the resulting image would
  2669. * have a width or height less than 1.
  2670. * @exception IllegalArgumentException if the product of
  2671. * <code>width</code> and <code>height</code> is greater than
  2672. * <code>Integer.MAX_VALUE</code>.
  2673. */
  2674. protected static BufferedImage
  2675. getDestination(ImageReadParam param,
  2676. Iterator<ImageTypeSpecifier> imageTypes,
  2677. int width, int height)
  2678. throws IIOException {
  2679. if (imageTypes == null || !imageTypes.hasNext()) {
  2680. throw new IllegalArgumentException("imageTypes null or empty!");
  2681. }
  2682. if ((long)width*height > Integer.MAX_VALUE) {
  2683. throw new IllegalArgumentException
  2684. ("width*height > Integer.MAX_VALUE!");
  2685. }
  2686. BufferedImage dest = null;
  2687. ImageTypeSpecifier imageType = null;
  2688. // If param is non-null, use it
  2689. if (param != null) {
  2690. // Try to get the image itself
  2691. dest = param.getDestination();
  2692. if (dest != null) {
  2693. return dest;
  2694. }
  2695. // No image, get the image type
  2696. imageType = param.getDestinationType();
  2697. }
  2698. // No info from param, use fallback image type
  2699. if (imageType == null) {
  2700. Object o = imageTypes.next();
  2701. if (!(o instanceof ImageTypeSpecifier)) {
  2702. throw new IllegalArgumentException
  2703. ("Non-ImageTypeSpecifier retrieved from imageTypes!");
  2704. }
  2705. imageType = (ImageTypeSpecifier)o;
  2706. } else {
  2707. boolean foundIt = false;
  2708. while (imageTypes.hasNext()) {
  2709. ImageTypeSpecifier type =
  2710. (ImageTypeSpecifier)imageTypes.next();
  2711. if (type.equals(imageType)) {
  2712. foundIt = true;
  2713. break;
  2714. }
  2715. }
  2716. if (!foundIt) {
  2717. throw new IIOException
  2718. ("Destination type from ImageReadParam does not match!");
  2719. }
  2720. }
  2721. Rectangle srcRegion = new Rectangle(0,0,0,0);
  2722. Rectangle destRegion = new Rectangle(0,0,0,0);
  2723. computeRegions(param,
  2724. width,
  2725. height,
  2726. null,
  2727. srcRegion,
  2728. destRegion);
  2729. int destWidth = destRegion.x + destRegion.width;
  2730. int destHeight = destRegion.y + destRegion.height;
  2731. // Create a new image based on the type specifier
  2732. return imageType.createBufferedImage(destWidth, destHeight);
  2733. }
  2734. }