1. /*
  2. * @(#)ImageReader.java 1.138 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio;
  8. import java.awt.Point;
  9. import java.awt.Rectangle;
  10. import java.awt.image.BufferedImage;
  11. import java.awt.image.Raster;
  12. import java.awt.image.RenderedImage;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Iterator;
  16. import java.util.List;
  17. import java.util.Locale;
  18. import java.util.MissingResourceException;
  19. import java.util.ResourceBundle;
  20. import java.util.Set;
  21. import javax.imageio.spi.ImageReaderSpi;
  22. import javax.imageio.event.IIOReadWarningListener;
  23. import javax.imageio.event.IIOReadProgressListener;
  24. import javax.imageio.event.IIOReadUpdateListener;
  25. import javax.imageio.metadata.IIOMetadata;
  26. import javax.imageio.metadata.IIOMetadataFormatImpl;
  27. import javax.imageio.stream.ImageInputStream;
  28. /**
  29. * An abstract superclass for parsing and decoding of images. This
  30. * class must be subclassed by classes that read in images in the
  31. * context of the Java Image I/O framework.
  32. *
  33. * <p> <code>ImageReader</code> objects are normally instantiated by
  34. * the service provider interface (SPI) class for the specific format.
  35. * Service provider classes (e.g., instances of
  36. * <code>ImageReaderSpi</code>) are registered with the
  37. * <code>IIORegistry</code>, which uses them for format recognition
  38. * and presentation of available format readers and writers.
  39. *
  40. * <p> When an input source is set (using the <code>setInput</code>
  41. * method), it may be marked as "seek forward only". This setting
  42. * means that images contained within the input source will only be
  43. * read in order, possibly allowing the reader to avoid caching
  44. * portions of the input containing data associated with images that
  45. * have been read previously.
  46. *
  47. * @see ImageWriter
  48. * @see javax.imageio.spi.IIORegistry
  49. * @see javax.imageio.spi.ImageReaderSpi
  50. *
  51. * @version 0.5
  52. */
  53. public abstract class ImageReader {
  54. /**
  55. * The <code>ImageReaderSpi</code> that instantiated this object,
  56. * or <code>null</code> if its identity is not known or none
  57. * exists. By default it is initialized to <code>null</code>.
  58. */
  59. protected ImageReaderSpi originatingProvider;
  60. /**
  61. * The <code>ImageInputStream</code> or other
  62. * <code>Object</code> by <code>setInput</code> and retrieved
  63. * by <code>getInput</code>. By default it is initialized to
  64. * <code>null</code>.
  65. */
  66. protected Object input = null;
  67. /**
  68. * <code>true</code> if the current input source has been marked
  69. * as allowing only forward seeking by <code>setInput</code>. By
  70. * default, the value is <code>false</code>.
  71. *
  72. * @see #minIndex
  73. * @see #setInput
  74. */
  75. protected boolean seekForwardOnly = false;
  76. /**
  77. * <code>true</code> if the current input source has been marked
  78. * as allowing metadata to be ignored by <code>setInput</code>.
  79. * By default, the value is <code>false</code>.
  80. *
  81. * @see #setInput
  82. */
  83. protected boolean ignoreMetadata = false;
  84. /**
  85. * The smallest valid index for reading, initially 0. When
  86. * <code>seekForwardOnly</code> is <code>true</code>, various methods
  87. * may throw an <code>IndexOutOfBoundsException</code> on an
  88. * attempt to access data associate with an image having a lower
  89. * index.
  90. *
  91. * @see #seekForwardOnly
  92. * @see #setInput
  93. */
  94. protected int minIndex = 0;
  95. /**
  96. * An array of <code>Locale</code>s which may be used to localize
  97. * warning messages, or <code>null</code> if localization is not
  98. * supported.
  99. */
  100. protected Locale[] availableLocales = null;
  101. /**
  102. * The current <code>Locale</code> to be used for localization, or
  103. * <code>null</code> if none has been set.
  104. */
  105. protected Locale locale = null;
  106. /**
  107. * A <code>List</code> of currently registered
  108. * <code>IIOReadWarningListener</code>s, initialized by default to
  109. * <code>null</code>, which is synonymous with an empty
  110. * <code>List</code>.
  111. */
  112. protected List warningListeners = null;
  113. /**
  114. * A <code>List</code> of the <code>Locale</code>s associated with
  115. * each currently registered <code>IIOReadWarningListener</code>,
  116. * initialized by default to <code>null</code>, which is
  117. * synonymous with an empty <code>List</code>.
  118. */
  119. protected List warningLocales = null;
  120. /**
  121. * A <code>List</code> of currently registered
  122. * <code>IIOReadProgressListener</code>s, initialized by default
  123. * to <code>null</code>, which is synonymous with an empty
  124. * <code>List</code>.
  125. */
  126. protected List progressListeners = null;
  127. /**
  128. * A <code>List</code> of currently registered
  129. * <code>IIOReadUpdateListener</code>s, initialized by default to
  130. * <code>null</code>, which is synonymous with an empty
  131. * <code>List</code>.
  132. */
  133. protected List updateListeners = null;
  134. /**
  135. * If <code>true</code>, the current read operation should be
  136. * aborted.
  137. */
  138. private boolean abortFlag = false;
  139. /**
  140. * Constructs an <code>ImageReader</code> and sets its
  141. * <code>originatingProvider</code> field to the supplied value.
  142. *
  143. * <p> Subclasses that make use of extensions should provide a
  144. * constructor with signature <code>(ImageReaderSpi,
  145. * Object)</code> in order to retrieve the extension object. If
  146. * the extension object is unsuitable, an
  147. * <code>IllegalArgumentException</code> should be thrown.
  148. *
  149. * @param originatingProvider the <code>ImageReaderSpi</code> that is
  150. * invoking this constructor, or <code>null</code>.
  151. */
  152. protected ImageReader(ImageReaderSpi originatingProvider) {
  153. this.originatingProvider = originatingProvider;
  154. }
  155. /**
  156. * Returns a <code>String</code> identifying the format of the
  157. * input source.
  158. *
  159. * <p> The default implementation returns
  160. * <code>originatingProvider.getFormatNames()[0]</code>.
  161. * Implementations that may not have an originating service
  162. * provider, or which desire a different naming policy should
  163. * override this method.
  164. *
  165. * @exception IOException if an error occurs reading the
  166. * information from the input source.
  167. *
  168. * @return the format name, as a <code>String</code>.
  169. */
  170. public String getFormatName() throws IOException {
  171. return originatingProvider.getFormatNames()[0];
  172. }
  173. /**
  174. * Returns the <code>ImageReaderSpi</code> that was passed in on
  175. * the constructor. Note that this value may be <code>null</code>.
  176. *
  177. * @return an <code>ImageReaderSpi</code>, or <code>null</code>.
  178. *
  179. * @see ImageReaderSpi
  180. */
  181. public ImageReaderSpi getOriginatingProvider() {
  182. return originatingProvider;
  183. }
  184. /**
  185. * Sets the input source to use to the given
  186. * <code>ImageInputStream</code> or other <code>Object</code>.
  187. * The input source must be set before any of the query or read
  188. * methods are used. If <code>input</code> is <code>null</code>,
  189. * any currently set input source will be removed. In any case,
  190. * the value of <code>minIndex</code> will be initialized to 0.
  191. *
  192. * <p> The <code>seekForwardOnly</code> parameter controls whether
  193. * the value returned by <code>getMinIndex</code> will be
  194. * increased as each image (or thumbnail, or image metadata) is
  195. * read. If <code>seekForwardOnly</code> is true, then a call to
  196. * <code>read(index)</code> will throw an
  197. * <code>IndexOutOfBoundsException</code> if <code>index <
  198. * this.minIndex</code> otherwise, the value of
  199. * <code>minIndex</code> will be set to <code>index</code>. If
  200. * <code>seekForwardOnly</code> is <code>false</code>, the value of
  201. * <code>minIndex</code> will remain 0 regardless of any read
  202. * operations.
  203. *
  204. * <p> The <code>ignoreMetadata</code> parameter, if set to
  205. * <code>true</code>, allows the reader to disregard any metadata
  206. * encountered during the read. Subsequent calls to the
  207. * <code>getStreamMetadata</code> and
  208. * <code>getImageMetadata</code> methods may return
  209. * <code>null</code>, and an <code>IIOImage</code> returned from
  210. * <code>readAll</code> may return <code>null</code> from their
  211. * <code>getMetadata</code> method. Setting this parameter may
  212. * allow the reader to work more efficiently. The reader may
  213. * choose to disregard this setting and return metadata normally.
  214. *
  215. * <p> Subclasses should take care to remove any cached
  216. * information based on the previous stream, such as header
  217. * information or partially decoded image data.
  218. *
  219. * <p> Use of a general <code>Object</code> other than an
  220. * <code>ImageInputStream</code> is intended for readers that
  221. * interact directly with a capture device or imaging protocol.
  222. * The set of legal classes is advertised by the reader's service
  223. * provider's <code>getInputTypes</code> method; most readers
  224. * will return a single-element array containing only
  225. * <code>ImageInputStream.class</code> to indicate that they
  226. * accept only an <code>ImageInputStream</code>.
  227. *
  228. * <p> The default implementation checks the <code>input</code>
  229. * argument against the list returned by
  230. * <code>originatingProvider.getInputTypes()</code> and fails
  231. * if the argument is not an instance of one of the classes
  232. * in the list. If the originating provider is set to
  233. * <code>null</code>, the input is accepted only if it is an
  234. * <code>ImageInputStream</code>.
  235. *
  236. * @param input the <code>ImageInputStream</code> or other
  237. * <code>Object</code> to use for future decoding.
  238. * @param seekForwardOnly if <code>true</code>, images and metadata
  239. * may only be read in ascending order from this input source.
  240. * @param ignoreMetadata if <code>true</code>, metadata
  241. * may be ignored during reads.
  242. *
  243. * @exception IllegalArgumentException if <code>input</code> is
  244. * not an instance of one of the classes returned by the
  245. * originating service provider's <code>getInputTypes</code>
  246. * method, or is not an <code>ImageInputStream</code>.
  247. *
  248. * @see ImageInputStream
  249. * @see #getInput
  250. * @see javax.imageio.spi.ImageReaderSpi#getInputTypes
  251. */
  252. public void setInput(Object input,
  253. boolean seekForwardOnly,
  254. boolean ignoreMetadata) {
  255. if (input != null) {
  256. boolean found = false;
  257. if (originatingProvider != null) {
  258. Class[] classes = originatingProvider.getInputTypes();
  259. for (int i = 0; i < classes.length; i++) {
  260. if (classes[i].isInstance(input)) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. } else {
  266. if (input instanceof ImageInputStream) {
  267. found = true;
  268. }
  269. }
  270. if (!found) {
  271. throw new IllegalArgumentException("Incorrect input type!");
  272. }
  273. this.seekForwardOnly = seekForwardOnly;
  274. this.ignoreMetadata = ignoreMetadata;
  275. this.minIndex = 0;
  276. }
  277. this.input = input;
  278. }
  279. /**
  280. * Sets the input source to use to the given
  281. * <code>ImageInputStream</code> or other <code>Object</code>.
  282. * The input source must be set before any of the query or read
  283. * methods are used. If <code>input</code> is <code>null</code>,
  284. * any currently set input source will be removed. In any case,
  285. * the value of <code>minIndex</code> will be initialized to 0.
  286. *
  287. * <p> The <code>seekForwardOnly</code> parameter controls whether
  288. * the value returned by <code>getMinIndex</code> will be
  289. * increased as each image (or thumbnail, or image metadata) is
  290. * read. If <code>seekForwardOnly</code> is true, then a call to
  291. * <code>read(index)</code> will throw an
  292. * <code>IndexOutOfBoundsException</code> if <code>index <
  293. * this.minIndex</code> otherwise, the value of
  294. * <code>minIndex</code> will be set to <code>index</code>. If
  295. * <code>seekForwardOnly</code> is <code>false</code>, the value of
  296. * <code>minIndex</code> will remain 0 regardless of any read
  297. * operations.
  298. *
  299. * <p> This method is equivalent to <code>setInput(input,
  300. * seekForwardOnly, false)</code>.
  301. *
  302. * @param input the <code>ImageInputStream</code> or other
  303. * <code>Object</code> to use for future decoding.
  304. * @param seekForwardOnly if <code>true</code>, images and metadata
  305. * may only be read in ascending order from this input source.
  306. *
  307. * @exception IllegalArgumentException if <code>input</code> is
  308. * not an instance of one of the classes returned by the
  309. * originating service provider's <code>getInputTypes</code>
  310. * method, or is not an <code>ImageInputStream</code>.
  311. *
  312. * @see #getInput
  313. */
  314. public void setInput(Object input,
  315. boolean seekForwardOnly) {
  316. setInput(input, seekForwardOnly, false);
  317. }
  318. /**
  319. * Sets the input source to use to the given
  320. * <code>ImageInputStream</code> or other <code>Object</code>.
  321. * The input source must be set before any of the query or read
  322. * methods are used. If <code>input</code> is <code>null</code>,
  323. * any currently set input source will be removed. In any case,
  324. * the value of <code>minIndex</code> will be initialized to 0.
  325. *
  326. * <p> This method is equivalent to <code>setInput(input, false,
  327. * false)</code>.
  328. *
  329. * @param input the <code>ImageInputStream</code> or other
  330. * <code>Object</code> to use for future decoding.
  331. *
  332. * @exception IllegalArgumentException if <code>input</code> is
  333. * not an instance of one of the classes returned by the
  334. * originating service provider's <code>getInputTypes</code>
  335. * method, or is not an <code>ImageInputStream</code>.
  336. *
  337. * @see #getInput
  338. */
  339. public void setInput(Object input) {
  340. setInput(input, false, false);
  341. }
  342. /**
  343. * Returns the <code>ImageInputStream</code> or other
  344. * <code>Object</code> previously set as the input source. If the
  345. * input source has not been set, <code>null</code> is returned.
  346. *
  347. * @return the <code>Object</code> that will be used for future
  348. * decoding, or <code>null</code>.
  349. *
  350. * @see ImageInputStream
  351. * @see #setInput
  352. */
  353. public Object getInput() {
  354. return input;
  355. }
  356. /**
  357. * Returns <code>true</code> if the current input source has been
  358. * marked as seek forward only by passing <code>true</code> as the
  359. * <code>seekForwardOnly</code> argument to the
  360. * <code>setInput</code> method.
  361. *
  362. * @return <code>true</code> if the input source is seek forward
  363. * only.
  364. *
  365. * @see #setInput
  366. */
  367. public boolean isSeekForwardOnly() {
  368. return seekForwardOnly;
  369. }
  370. /**
  371. * Returns <code>true</code> if the current input source has been
  372. * marked as allowing metadata to be ignored by passing
  373. * <code>true</code> as the <code>ignoreMetadata</code> argument
  374. * to the <code>setInput</code> method.
  375. *
  376. * @return <code>true</code> if the metadata may be ignored.
  377. *
  378. * @see #setInput
  379. */
  380. public boolean isIgnoringMetadata() {
  381. return ignoreMetadata;
  382. }
  383. /**
  384. * Returns the lowest valid index for reading an image, thumbnail,
  385. * or image metadata. If <code>seekForwardOnly()</code> is
  386. * <code>false</code>, this value will typically remain 0,
  387. * indicating that random access is possible. Otherwise, it will
  388. * contain the value of the most recently accessed index, and
  389. * increase in a monotonic fashion.
  390. *
  391. * @return the minimum legal index for reading.
  392. */
  393. public int getMinIndex() {
  394. return minIndex;
  395. }
  396. // Localization
  397. /**
  398. * Returns an array of <code>Locale</code>s that may be used to
  399. * localize warning listeners and compression settings. A return
  400. * value of <code>null</code> indicates that localization is not
  401. * supported.
  402. *
  403. * <p> The default implementation returns a clone of the
  404. * <code>availableLocales</code> instance variable if it is
  405. * non-<code>null</code>, or else returns <code>null</code>.
  406. *
  407. * @return an array of <code>Locale</code>s that may be used as
  408. * arguments to <code>setLocale</code>, or <code>null</code>.
  409. */
  410. public Locale[] getAvailableLocales() {
  411. if (availableLocales == null) {
  412. return null;
  413. } else {
  414. return (Locale[])availableLocales.clone();
  415. }
  416. }
  417. /**
  418. * Sets the current <code>Locale</code> of this
  419. * <code>ImageReader</code> to the given value. A value of
  420. * <code>null</code> removes any previous setting, and indicates
  421. * that the reader should localize as it sees fit.
  422. *
  423. * @param locale the desired <code>Locale</code>, or
  424. * <code>null</code>.
  425. *
  426. * @exception IllegalArgumentException if <code>locale</code> is
  427. * non-<code>null</code> but is not one of the values returned by
  428. * <code>getAvailableLocales</code>.
  429. *
  430. * @see #getLocale
  431. */
  432. public void setLocale(Locale locale) {
  433. if (locale != null) {
  434. Locale[] locales = getAvailableLocales();
  435. boolean found = false;
  436. if (locales != null) {
  437. for (int i = 0; i < locales.length; i++) {
  438. if (locale.equals(locales[i])) {
  439. found = true;
  440. break;
  441. }
  442. }
  443. }
  444. if (!found) {
  445. throw new IllegalArgumentException("Invalid locale!");
  446. }
  447. }
  448. this.locale = locale;
  449. }
  450. /**
  451. * Returns the currently set <code>Locale</code>, or
  452. * <code>null</code> if none has been set.
  453. *
  454. * @return the current <code>Locale</code>, or <code>null</code>.
  455. *
  456. * @see #setLocale
  457. */
  458. public Locale getLocale() {
  459. return locale;
  460. }
  461. // Image queries
  462. /**
  463. * Returns the number of images, not including thumbnails, available
  464. * from the current input source.
  465. *
  466. * <p> Note that some image formats (such as animated GIF) do not
  467. * specify how many images are present in the stream. Thus
  468. * determining the number of images will require the entire stream
  469. * to be scanned and may require memory for buffering. If images
  470. * are to be processed in order, it may be more efficient to
  471. * simply call <code>read</code> with increasing indices until an
  472. * <code>IndexOutOfBoundsException</code> is thrown to indicate
  473. * that no more images are available. The
  474. * <code>allowSearch</code> parameter may be set to
  475. * <code>false</code> to indicate that an exhaustive search is not
  476. * desired; the return value will be <code>-1</code> to indicate
  477. * that a search is necessary. If the input has been specified
  478. * with <code>seekForwardOnly</code> set to <code>true</code>,
  479. * this method throws an <code>IllegalStateException</code> if
  480. * <code>allowSearch</code> is set to <code>true</code>.
  481. *
  482. * @param allowSearch if <code>true</code>, the true number of
  483. * images will be returned even if a search is required. If
  484. * <code>false</code>, the reader may return <code>-1</code>
  485. * without performing the search.
  486. *
  487. * @return the number of images, as an <code>int</code>, or
  488. * <code>-1</code> if <code>allowSearch</code> is
  489. * <code>false</code> and a search would be required.
  490. *
  491. * @exception IllegalStateException if the input source has not been set,
  492. * or if the input has been specified with <code>seekForwardOnly</code>
  493. * set to <code>true</code>.
  494. * @exception IOException if an error occurs reading the
  495. * information from the input source.
  496. *
  497. * @see #setInput
  498. */
  499. public abstract int getNumImages(boolean allowSearch) throws IOException;
  500. /**
  501. * Returns the width in pixels of the given image within the input
  502. * source.
  503. *
  504. * <p> If the image can be rendered to a user-specified size, then
  505. * this method returns the default width.
  506. *
  507. * @param imageIndex the index of the image to be queried.
  508. *
  509. * @return the width of the image, as an <code>int</code>.
  510. *
  511. * @exception IllegalStateException if the input source has not been set.
  512. * @exception IndexOutOfBoundsException if the supplied index is
  513. * out of bounds.
  514. * @exception IOException if an error occurs reading the width
  515. * information from the input source.
  516. */
  517. public abstract int getWidth(int imageIndex) throws IOException;
  518. /**
  519. * Returns the height in pixels of the given image within the
  520. * input source.
  521. *
  522. * <p> If the image can be rendered to a user-specified size, then
  523. * this method returns the default height.
  524. *
  525. * @param imageIndex the index of the image to be queried.
  526. *
  527. * @return the height of the image, as an <code>int</code>.
  528. *
  529. * @exception IllegalStateException if the input source has not been set.
  530. * @exception IndexOutOfBoundsException if the supplied index is
  531. * out of bounds.
  532. * @exception IOException if an error occurs reading the height
  533. * information from the input source.
  534. */
  535. public abstract int getHeight(int imageIndex) throws IOException;
  536. /**
  537. * Returns <code>true</code> if the storage format of the given
  538. * image places no inherent impediment on random access to pixels.
  539. * For most compressed formats, such as JPEG, this method should
  540. * return <code>false</code>, as a large section of the image in
  541. * addition to the region of interest may need to be decoded.
  542. *
  543. * <p> This is merely a hint for programs that wish to be
  544. * efficient; all readers must be able to read arbitrary regions
  545. * as specified in an <code>ImageReadParam</code>.
  546. *
  547. * <p> Note that formats that return <code>false</code> from
  548. * this method may nonetheless allow tiling (<i>e.g.</i> Restart
  549. * Markers in JPEG), and random access will likely be reasonably
  550. * efficient on tiles. See {@link #isImageTiled
  551. * <code>isImageTiled</code>}.
  552. *
  553. * <p> A reader for which all images are guaranteed to support
  554. * easy random access, or are guaranteed not to support easy
  555. * random access, may return <code>true</code> or
  556. * <code>false</code> respectively without accessing any image
  557. * data. In such cases, it is not necessary to throw an exception
  558. * even if no input source has been set or the image index is out
  559. * of bounds.
  560. *
  561. * <p> The default implementation returns <code>false</code>.
  562. *
  563. * @param imageIndex the index of the image to be queried.
  564. *
  565. * @return <code>true</code> if reading a region of interest of
  566. * the given image is likely to be efficient.
  567. *
  568. * @exception IllegalStateException if an input source is required
  569. * to determine the return value, but none has been set.
  570. * @exception IndexOutOfBoundsException if an image must be
  571. * accessed to determine the return value, but the supplied index
  572. * is out of bounds.
  573. * @exception IOException if an error occurs during reading.
  574. */
  575. public boolean isRandomAccessEasy(int imageIndex) throws IOException {
  576. return false;
  577. }
  578. /**
  579. * Returns the aspect ratio of the given image (that is, its width
  580. * divided by its height) as a <code>float</code>. For images
  581. * that are inherently resizable, this method provides a way to
  582. * determine the appropriate width given a deired height, or vice
  583. * versa. For non-resizable images, the true width and height
  584. * are used.
  585. *
  586. * <p> The default implementation simply returns
  587. * <code>(float)getWidth(imageIndex)/getHeight(imageIndex)</code>.
  588. *
  589. * @param imageIndex the index of the image to be queried.
  590. *
  591. * @return a <code>float</code> indicating the aspect ratio of the
  592. * given image.
  593. *
  594. * @exception IllegalStateException if the input source has not been set.
  595. * @exception IndexOutOfBoundsException if the supplied index is
  596. * out of bounds.
  597. * @exception IOException if an error occurs during reading.
  598. */
  599. public float getAspectRatio(int imageIndex) throws IOException {
  600. return (float)getWidth(imageIndex)/getHeight(imageIndex);
  601. }
  602. /**
  603. * Returns an <code>ImageTypeSpecifier</code> indicating the
  604. * <code>SampleModel</code> and <code>ColorModel</code> which most
  605. * closely represents the "raw" internal format of the image. For
  606. * example, for a JPEG image the raw type might have a YCbCr color
  607. * space even though the image would conventionally be transformed
  608. * into an RGB color space prior to display. The returned value
  609. * should also be included in the list of values returned by
  610. * <code>getImageTypes</code>.
  611. *
  612. * <p> The default implementation simply returns the first entry
  613. * from the list provided by <code>getImageType</code>.
  614. *
  615. * @param imageIndex the index of the image to be queried.
  616. *
  617. * @return an <code>ImageTypeSpecifier</code>.
  618. *
  619. * @exception IllegalStateException if the input source has not been set.
  620. * @exception IndexOutOfBoundsException if the supplied index is
  621. * out of bounds.
  622. * @exception IOException if an error occurs reading the format
  623. * information from the input source.
  624. */
  625. public ImageTypeSpecifier getRawImageType(int imageIndex)
  626. throws IOException {
  627. return (ImageTypeSpecifier)getImageTypes(imageIndex).next();
  628. }
  629. /**
  630. * Returns an <code>Iterator</code> containing possible image
  631. * types to which the given image may be decoded, in the form of
  632. * <code>ImageTypeSpecifiers</code>s. At least one legal image
  633. * type will be returned.
  634. *
  635. * <p> The first element of the iterator should be the most
  636. * "natural" type for decoding the image with as little loss as
  637. * possible. For example, for a JPEG image the first entry should
  638. * be an RGB image, even though the image data is stored
  639. * internally in a YCbCr color space.
  640. *
  641. * @param imageIndex the index of the image to be
  642. * <code>retrieved</code>.
  643. *
  644. * @return an <code>Iterator</code> containing at least one
  645. * <code>ImageTypeSpecifier</code> representing suggested image
  646. * types for decoding the current given image.
  647. *
  648. * @exception IllegalStateException if the input source has not been set.
  649. * @exception IndexOutOfBoundsException if the supplied index is
  650. * out of bounds.
  651. * @exception IOException if an error occurs reading the format
  652. * information from the input source.
  653. *
  654. * @see ImageReadParam#setDestination(BufferedImage)
  655. * @see ImageReadParam#setDestinationType(ImageTypeSpecifier)
  656. */
  657. public abstract Iterator getImageTypes(int imageIndex) throws IOException;
  658. /**
  659. * Returns a default <code>ImageReadParam</code> object
  660. * appropriate for this format. All subclasses should define a
  661. * set of default values for all parameters and return them with
  662. * this call. This method may be called before the input source
  663. * is set.
  664. *
  665. * <p> The default implementation constructs and returns a new
  666. * <code>ImageReadParam</code> object that does not allow source
  667. * scaling (<i>i.e.</i>, it returns <code>new
  668. * ImageReadParam()</code>.
  669. *
  670. * @return an <code>ImageReadParam</code> object which may be used
  671. * to control the decoding process using a set of default settings.
  672. */
  673. public ImageReadParam getDefaultReadParam() {
  674. return new ImageReadParam();
  675. }
  676. /**
  677. * Returns an <code>IIOMetadata</code> object representing the
  678. * metadata associated with the input source as a whole (i.e., not
  679. * associated with any particular image), or <code>null</code> if
  680. * the reader does not support reading metadata, is set to ignore
  681. * metadata, or if no metadata is available.
  682. *
  683. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  684. *
  685. * @exception IOException if an error occurs during reading.
  686. */
  687. public abstract IIOMetadata getStreamMetadata() throws IOException;
  688. /**
  689. * Returns an <code>IIOMetadata</code> object representing the
  690. * metadata associated with the input source as a whole (i.e.,
  691. * not associated with any particular image). If no such data
  692. * exists, <code>null</code> is returned.
  693. *
  694. * <p> The resuting metadata object is only responsible for
  695. * returning documents in the format named by
  696. * <code>formatName</code>. Within any documents that are
  697. * returned, only nodes whose names are members of
  698. * <code>nodeNames</code> are required to be returned. In this
  699. * way, the amount of metadata processing done by the reader may
  700. * be kept to a minimum, based on what information is actually
  701. * needed.
  702. *
  703. * <p> If <code>formatName</code> is not the name of a supported
  704. * metadata format, <code>null</code> is returned.
  705. *
  706. * <p> In all cases, it is legal to return a more capable metadata
  707. * object than strictly necessary. The format name and node names
  708. * are merely hints that may be used to reduce the reader's
  709. * workload.
  710. *
  711. * <p> The default implementation simply returns the result of
  712. * calling <code>getStreamMetadata()</code>, after checking that
  713. * the format name is supported. If it is not,
  714. * <code>null</code> is returned.
  715. *
  716. * @param formatName a metadata format name that may be used to retrieve
  717. * a document from the returned <code>IIOMetadata</code> object.
  718. * @param nodeNames a <code>Set</code> containing the names of
  719. * nodes that may be contained in a retrieved document.
  720. *
  721. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  722. *
  723. * @exception IllegalArgumentException if <code>formatName</code>
  724. * is <code>null</code>.
  725. * @exception IllegalArgumentException if <code>nodeNames</code>
  726. * is <code>null</code>.
  727. * @exception IOException if an error occurs during reading.
  728. */
  729. public IIOMetadata getStreamMetadata(String formatName, Set nodeNames)
  730. throws IOException {
  731. return getMetadata(formatName, nodeNames, true, 0);
  732. }
  733. private IIOMetadata getMetadata(String formatName,
  734. Set nodeNames,
  735. boolean wantStream,
  736. int imageIndex) throws IOException {
  737. if (formatName == null) {
  738. throw new IllegalArgumentException("formatName == null!");
  739. }
  740. if (nodeNames == null) {
  741. throw new IllegalArgumentException("nodeNames == null!");
  742. }
  743. IIOMetadata metadata =
  744. wantStream
  745. ? getStreamMetadata()
  746. : getImageMetadata(imageIndex);
  747. if (metadata != null) {
  748. if (metadata.isStandardMetadataFormatSupported() &&
  749. formatName.equals
  750. (IIOMetadataFormatImpl.standardMetadataFormatName)) {
  751. return metadata;
  752. }
  753. String nativeName = metadata.getNativeMetadataFormatName();
  754. if (nativeName != null && formatName.equals(nativeName)) {
  755. return metadata;
  756. }
  757. String[] extraNames = metadata.getExtraMetadataFormatNames();
  758. if (extraNames != null) {
  759. for (int i = 0; i < extraNames.length; i++) {
  760. if (formatName.equals(extraNames[i])) {
  761. return metadata;
  762. }
  763. }
  764. }
  765. }
  766. return null;
  767. }
  768. /**
  769. * Returns an <code>IIOMetadata</code> object containing metadata
  770. * associated with the given image, or <code>null</code> if the
  771. * reader does not support reading metadata, is set to ignore
  772. * metadata, or if no metadata is available.
  773. *
  774. * @param imageIndex the index of the image whose metadata is to
  775. * be retrieved.
  776. *
  777. * @return an <code>IIOMetadata</code> object, or
  778. * <code>null</code>.
  779. *
  780. * @exception IllegalStateException if the input source has not been
  781. * set.
  782. * @exception IndexOutOfBoundsException if the supplied index is
  783. * out of bounds.
  784. * @exception IOException if an error occurs during reading.
  785. */
  786. public abstract IIOMetadata getImageMetadata(int imageIndex)
  787. throws IOException;
  788. /**
  789. * Returns an <code>IIOMetadata</code> object representing the
  790. * metadata associated with the given image, or <code>null</code>
  791. * if the reader does not support reading metadata or none
  792. * is available.
  793. *
  794. * <p> The resuting metadata object is only responsible for
  795. * returning documents in the format named by
  796. * <code>formatName</code>. Within any documents that are
  797. * returned, only nodes whose names are members of
  798. * <code>nodeNames</code> are required to be returned. In this
  799. * way, the amount of metadata processing done by the reader may
  800. * be kept to a minimum, based on what information is actually
  801. * needed.
  802. *
  803. * <p> If <code>formatName</code> is not the name of a supported
  804. * metadata format, <code>null</code> may be returned.
  805. *
  806. * <p> In all cases, it is legal to return a more capable metadata
  807. * object than strictly necessary. The format name and node names
  808. * are merely hints that may be used to reduce the reader's
  809. * workload.
  810. *
  811. * <p> The default implementation simply returns the result of
  812. * calling <code>getImageMetadata(imageIndex)</code>, after
  813. * checking that the format name is supported. If it is not,
  814. * <code>null</code> is returned.
  815. *
  816. * @param imageIndex the index of the image whose metadata is to
  817. * be retrieved.
  818. * @param formatName a metadata format name that may be used to retrieve
  819. * a document from the returned <code>IIOMetadata</code> object.
  820. * @param nodeNames a <code>Set</code> containing the names of
  821. * nodes that may be contained in a retrieved document.
  822. *
  823. * @return an <code>IIOMetadata</code> object, or <code>null</code>.
  824. *
  825. * @exception IllegalStateException if the input source has not been
  826. * set.
  827. * @exception IndexOutOfBoundsException if the supplied index is
  828. * out of bounds.
  829. * @exception IllegalArgumentException if <code>formatName</code>
  830. * is <code>null</code>.
  831. * @exception IllegalArgumentException if <code>nodeNames</code>
  832. * is <code>null</code>.
  833. * @exception IOException if an error occurs during reading.
  834. */
  835. public IIOMetadata getImageMetadata(int imageIndex,
  836. String formatName, Set nodeNames)
  837. throws IOException {
  838. return getMetadata(formatName, nodeNames, false, imageIndex);
  839. }
  840. /**
  841. * Reads the image indexed by <code>imageIndex</code> and returns
  842. * it as a complete <code>BufferedImage</code>, using a default
  843. * <code>ImageReadParam</code>. This is a convenience method
  844. * that calls <code>read(imageIndex, null)</code>.
  845. *
  846. * <p> The image returned will be formatted according to the first
  847. * <code>ImageTypeSpecifier</code> returned from
  848. * <code>getImageTypes</code>.
  849. *
  850. * <p> Any registered <code>IIOReadProgressListener</code> objects
  851. * will be notified by calling their <code>imageStarted</code>
  852. * method, followed by calls to their <code>imageProgress</code>
  853. * method as the read progresses. Finally their
  854. * <code>imageComplete</code> method will be called.
  855. * <code>IIOReadUpdateListener</code> objects may be updated at
  856. * other times during the read as pixels are decoded. Finally,
  857. * <code>IIOReadWarningListener</code> objects will receive
  858. * notification of any non-fatal warnings that occur during
  859. * decoding.
  860. *
  861. * @param imageIndex the index of the image to be retrieved.
  862. *
  863. * @return the desired portion of the image as a
  864. * <code>BufferedImage</code>.
  865. *
  866. * @exception IllegalStateException if the input source has not been
  867. * set.
  868. * @exception IndexOutOfBoundsException if the supplied index is
  869. * out of bounds.
  870. * @exception IOException if an error occurs during reading.
  871. */
  872. public BufferedImage read(int imageIndex) throws IOException {
  873. return read(imageIndex, null);
  874. }
  875. /**
  876. * Reads the image indexed by <code>imageIndex</code> and returns
  877. * it as a complete <code>BufferedImage</code>, using a supplied
  878. * <code>ImageReadParam</code>.
  879. *
  880. * <p> The actual <code>BufferedImage</code> returned will be
  881. * chosen using the algorithm defined by the
  882. * <code>getDestination</code> method.
  883. *
  884. * <p> Any registered <code>IIOReadProgressListener</code> objects
  885. * will be notified by calling their <code>imageStarted</code>
  886. * method, followed by calls to their <code>imageProgress</code>
  887. * method as the read progresses. Finally their
  888. * <code>imageComplete</code> method will be called.
  889. * <code>IIOReadUpdateListener</code> objects may be updated at
  890. * other times during the read as pixels are decoded. Finally,
  891. * <code>IIOReadWarningListener</code> objects will receive
  892. * notification of any non-fatal warnings that occur during
  893. * decoding.
  894. *
  895. * <p> The set of source bands to be read and destination bands to
  896. * be written is determined by calling <code>getSourceBands</code>
  897. * and <code>getDestinationBands</code> on the supplied
  898. * <code>ImageReadParam</code>. If the lengths of the arrays
  899. * returned by these methods differ, the set of source bands
  900. * contains an index larger that the largest available source
  901. * index, or the set of destination bands contains an index larger
  902. * than the largest legal destination index, an
  903. * <code>IllegalArgumentException</code> is thrown.
  904. *
  905. * <p> If the supplied <code>ImageReadParam</code> contains
  906. * optional setting values not supported by this reader, they will
  907. * be ignored.
  908. *
  909. * @param imageIndex the index of the image to be retrieved.
  910. * @param param an <code>ImageReadParam</code> used to control
  911. * the reading process, or <code>null</code>.
  912. *
  913. * @return the desired portion of the image as a
  914. * <code>BufferedImage</code>.
  915. *
  916. * @exception IllegalStateException if the input source has not been
  917. * set.
  918. * @exception IndexOutOfBoundsException if the supplied index is
  919. * out of bounds.
  920. * @exception IllegalArgumentException if the set of source and
  921. * destination bands specified by
  922. * <code>param.getSourceBands</code> and
  923. * <code>param.getDestinationBands</code> differ in length or
  924. * include indices that are out of bounds.
  925. * @exception IllegalArgumentException if the resulting image would
  926. * have a width or height less than 1.
  927. * @exception IOException if an error occurs during reading.
  928. */
  929. public abstract BufferedImage read(int imageIndex, ImageReadParam param)
  930. throws IOException;
  931. /**
  932. * Reads the image indexed by <code>imageIndex</code> and returns
  933. * an <code>IIOImage</code> containing the image, thumbnails, and
  934. * associated image metadata, using a supplied
  935. * <code>ImageReadParam</code>.
  936. *
  937. * <p> The actual <code>BufferedImage</code> referenced by the
  938. * returned <code>IIOImage</code> will be chosen using the
  939. * algorithm defined by the <code>getDestination</code> method.
  940. *
  941. * <p> Any registered <code>IIOReadProgressListener</code> objects
  942. * will be notified by calling their <code>imageStarted</code>
  943. * method, followed by calls to their <code>imageProgress</code>
  944. * method as the read progresses. Finally their
  945. * <code>imageComplete</code> method will be called.
  946. * <code>IIOReadUpdateListener</code> objects may be updated at
  947. * other times during the read as pixels are decoded. Finally,
  948. * <code>IIOReadWarningListener</code> objects will receive
  949. * notification of any non-fatal warnings that occur during
  950. * decoding.
  951. *
  952. * <p> The set of source bands to be read and destination bands to
  953. * be written is determined by calling <code>getSourceBands</code>
  954. * and <code>getDestinationBands</code> on the supplied
  955. * <code>ImageReadParam</code>. If the lengths of the arrays
  956. * returned by these methods differ, the set of source bands
  957. * contains an index larger that the largest available source
  958. * index, or the set of destination bands contains an index larger
  959. * than the largest legal destination index, an
  960. * <code>IllegalArgumentException</code> is thrown.
  961. *
  962. * <p> Thumbnails will be returned in their entirety regardless of
  963. * the region settings.
  964. *
  965. * <p> If the supplied <code>ImageReadParam</code> contains
  966. * optional setting values not supported by this reader, those
  967. * values will be ignored.
  968. *
  969. * @param imageIndex the index of the image to be retrieved.
  970. * @param param an <code>ImageReadParam</code> used to control
  971. * the reading process, or <code>null</code>.
  972. *
  973. * @return an <code>IIOImage</code> containing the desired portion
  974. * of the image, a set of thumbnails, and associated image
  975. * metadata.
  976. *
  977. * @exception IllegalStateException if the input source has not been
  978. * set.
  979. * @exception IndexOutOfBoundsException if the supplied index is
  980. * out of bounds.
  981. * @exception IllegalArgumentException if the set of source and
  982. * destination bands specified by
  983. * <code>param.getSourceBands</code> and
  984. * <code>param.getDestinationBands</code> differ in length or
  985. * include indices that are out of bounds.
  986. * @exception IllegalArgumentException if the resulting image
  987. * would have a width or height less than 1.
  988. * @exception IOException if an error occurs during reading.
  989. */
  990. public IIOImage readAll(int imageIndex, ImageReadParam param)
  991. throws IOException {
  992. if (imageIndex < getMinIndex()) {
  993. throw new IndexOutOfBoundsException("imageIndex < getMinIndex()!");
  994. }
  995. BufferedImage im = read(imageIndex, param);
  996. List thumbnails = null;
  997. int numThumbnails = getNumThumbnails(imageIndex);
  998. for (int j = 0; j < numThumbnails; j++) {
  999. thumbnails.add(readThumbnail(imageIndex, j));
  1000. }
  1001. IIOMetadata metadata = getImageMetadata(imageIndex);
  1002. return new IIOImage(im, thumbnails, metadata);
  1003. }
  1004. /**
  1005. * Returns an <code>Iterator</code> containing all the images,
  1006. * thumbnails, and metadata, starting at the index given by
  1007. * <code>getMinIndex</code>, from the input source in the form of
  1008. * <code>IIOImage</code> objects. An <code>Iterator</code>
  1009. * containing <code>ImageReadParam</code> objects is supplied; one
  1010. * element is consumed for each image read from the input source
  1011. * until no more images are available. If the read param
  1012. * <code>Iterator</code> runs out of elements, but there are still
  1013. * more images available from the input source, default read
  1014. * params are used for the remaining images.
  1015. *
  1016. * <p> If <code>params</code> is <code>null</code>, a default read
  1017. * param will be used for all images.
  1018. *
  1019. * <p> The actual <code>BufferedImage</code> referenced by the
  1020. * returned <code>IIOImage</code> will be chosen using the
  1021. * algorithm defined by the <code>getDestination</code> method.
  1022. *
  1023. * <p> Any registered <code>IIOReadProgressListener</code> objects
  1024. * will be notified by calling their <code>sequenceStarted</code>
  1025. * method once. Then, for each image decoded, there will be a
  1026. * call to <code>imageStarted</code>, followed by calls to
  1027. * <code>imageProgress</code> as the read progresses, and finally
  1028. * to <code>imageComplete</code>. The
  1029. * <code>sequenceComplete</code> method will be called after the
  1030. * last image has been decoded.
  1031. * <code>IIOReadUpdateListener</code> objects may be updated at
  1032. * other times during the read as pixels are decoded. Finally,
  1033. * <code>IIOReadWarningListener</code> objects will receive
  1034. * notification of any non-fatal warnings that occur during
  1035. * decoding.
  1036. *
  1037. * <p> The set of source bands to be read and destination bands to
  1038. * be written is determined by calling <code>getSourceBands</code>
  1039. * and <code>getDestinationBands</code> on the supplied
  1040. * <code>ImageReadParam</code>. If the lengths of the arrays
  1041. * returned by these methods differ, the set of source bands
  1042. * contains an index larger that the largest available source
  1043. * index, or the set of destination bands contains an index larger
  1044. * than the largest legal destination index, an
  1045. * <code>IllegalArgumentException</code> is thrown.
  1046. *
  1047. * <p> Thumbnails will be returned in their entirety regardless of the
  1048. * region settings.
  1049. *
  1050. * <p> If any of the supplied <code>ImageReadParam</code>s contain
  1051. * optional setting values not supported by this reader, they will
  1052. * be ignored.
  1053. *
  1054. * @param params an <code>Iterator</code> containing
  1055. * <code>ImageReadParam</code> objects.
  1056. *
  1057. * @return an <code>Iterator</code> representing the
  1058. * contents of the input source as <code>IIOImage</code>s.
  1059. *
  1060. * @exception IllegalStateException if the input source has not been
  1061. * set.
  1062. * @exception IllegalArgumentException if any
  1063. * non-<code>null</code> element of <code>params</code> is not an
  1064. * <code>ImageReadParam</code>.
  1065. * @exception IllegalArgumentException if the set of source and
  1066. * destination bands specified by
  1067. * <code>param.getSourceBands</code> and
  1068. * <code>param.getDestinationBands</code> differ in length or
  1069. * include indices that are out of bounds.
  1070. * @exception IllegalArgumentException if a resulting image would
  1071. * have a width or height less than 1.
  1072. * @exception IOException if an error occurs during reading.
  1073. *
  1074. * @see ImageReadParam
  1075. * @see IIOImage
  1076. */
  1077. public Iterator readAll(Iterator params) throws IOException {
  1078. List output = new ArrayList();
  1079. int imageIndex = getMinIndex();
  1080. // Inform IIOReadProgressListeners we're starting a sequence
  1081. processSequenceStarted(imageIndex);
  1082. while (true) {
  1083. // Inform IIOReadProgressListeners and IIOReadUpdateListeners
  1084. // that we're starting a new image
  1085. ImageReadParam param = null;
  1086. if (params != null && params.hasNext()) {
  1087. Object o = params.next();
  1088. if (o != null) {
  1089. if (o instanceof ImageReadParam) {
  1090. param = (ImageReadParam)o;
  1091. } else {
  1092. throw new IllegalArgumentException
  1093. ("Non-ImageReadParam supplied as part of params!");
  1094. }
  1095. }
  1096. }
  1097. BufferedImage bi = null;
  1098. try {
  1099. bi = read(imageIndex, param);
  1100. } catch (IndexOutOfBoundsException e) {
  1101. break;
  1102. }
  1103. List thumbnails = null;
  1104. int numThumbnails = getNumThumbnails(imageIndex);
  1105. for (int j = 0; j < numThumbnails; j++) {
  1106. thumbnails.add(readThumbnail(imageIndex, j));
  1107. }
  1108. IIOMetadata metadata = getImageMetadata(imageIndex);
  1109. IIOImage im = new IIOImage(bi, thumbnails, metadata);
  1110. output.add(im);
  1111. ++imageIndex;
  1112. }
  1113. // Inform IIOReadProgressListeners we're ending a sequence
  1114. processSequenceComplete();
  1115. return output.iterator();
  1116. }
  1117. /**
  1118. * Returns <code>true</code> if this plug-in supports reading
  1119. * just a {@link java.awt.image.Raster <code>Raster</code>} of pixel data.
  1120. * If this method returns <code>false</code>, calls to
  1121. * {@link #readRaster <code>readRaster</code>} or {@link #readTileRaster
  1122. * <code>readTileRaster</code>} will throw an
  1123. * <code>UnsupportedOperationException</code>.
  1124. *
  1125. * <p> The default implementation returns <code>false</code>.
  1126. *
  1127. * @return <code>true</code> if this plug-in supports reading raw
  1128. * <code>Raster</code>s.
  1129. *
  1130. * @see #readRaster
  1131. * @see #readTileRaster
  1132. */
  1133. public boolean canReadRaster() {
  1134. return false;
  1135. }
  1136. /**
  1137. * Returns a new <code>Raster</code> object containing the raw pixel data
  1138. * from the image stream, without any color conversion applied. The
  1139. * application must determine how to interpret the pixel data by other
  1140. * means. Any destination or image-type parameters in the supplied
  1141. * <code>ImageReadParam</code> object are ignored, but all other
  1142. * parameters are used exactly as in the {@link #read <code>read</code>}
  1143. * method, except that any destination offset is used as a logical rather
  1144. * than a physical offset. The size of the returned <code>Raster</code>
  1145. * will always be that of the source region clipped to the actual image.
  1146. * Logical offsets in the stream itself are ignored.
  1147. *
  1148. * <p> This method allows formats that normally apply a color
  1149. * conversion, such as JPEG, and formats that do not normally have an
  1150. * associated colorspace, such as remote sensing or medical imaging data,
  1151. * to provide access to raw pixel data.
  1152. *
  1153. * <p> Any registered <code>readUpdateListener</code>s are ignored, as
  1154. * there is no <code>BufferedImage</code>, but all other listeners are
  1155. * called exactly as they are for the {@link #read <code>read</code>}
  1156. * method.
  1157. *
  1158. * <p> If {@link #canReadRaster <code>canReadRaster()</code>} returns
  1159. * <code>false</code>, this method throws an
  1160. * <code>UnsupportedOperationException</code>.
  1161. *
  1162. * <p> If the supplied <code>ImageReadParam</code> contains
  1163. * optional setting values not supported by this reader, they will
  1164. * be ignored.
  1165. *
  1166. * <p> The default implementation throws an
  1167. * <code>UnsupportedOperationException</code>.
  1168. *
  1169. * @param imageIndex the index of the image to be read.
  1170. * @param param an <code>ImageReadParam</code> used to control
  1171. * the reading process, or <code>null</code>.
  1172. *
  1173. * @return the desired portion of the image as a
  1174. * <code>Raster</code>.
  1175. *
  1176. * @exception UnsupportedOperationException if this plug-in does not
  1177. * support reading raw <code>Raster</code>s.
  1178. * @exception IllegalStateException if the input source has not been
  1179. * set.
  1180. * @exception IndexOutOfBoundsException if the supplied index is
  1181. * out of bounds.
  1182. * @exception IOException if an error occurs during reading.
  1183. *
  1184. * @see #canReadRaster
  1185. * @see #read
  1186. * @see java.awt.image.Raster
  1187. */
  1188. public Raster readRaster(int imageIndex, ImageReadParam param)
  1189. throws IOException {
  1190. throw new UnsupportedOperationException("readRaster not supported!");
  1191. }
  1192. /**
  1193. * Returns <code>true</code> if the image is organized into
  1194. * <i>tiles</i>, that is, equal-sized non-overlapping rectangles.
  1195. *
  1196. * <p> A reader plug-in may choose whether or not to expose tiling
  1197. * that is present in the image as it is stored. It may even
  1198. * choose to advertise tiling when none is explicitly present. In
  1199. * general, tiling should only be advertised if there is some
  1200. * advantage (in speed or space) to accessing individual tiles.
  1201. * Regardless of whether the reader advertises tiling, it must be
  1202. * capable of reading an arbitrary rectangular region specified in
  1203. * an <code>ImageReadParam</code>.
  1204. *
  1205. * <p> A reader for which all images are guaranteed to be tiled,
  1206. * or are guaranteed not to be tiled, may return <code>true</code>
  1207. * or <code>false</code> respectively without accessing any image
  1208. * data. In such cases, it is not necessary to throw an exception
  1209. * even if no input source has been set or the image index is out
  1210. * of bounds.
  1211. *
  1212. * <p> The default implementation just returns <code>false</code>.
  1213. *
  1214. * @param imageIndex the index of the image to be queried.
  1215. *
  1216. * @return <code>true</code> if the image is tiled.
  1217. *
  1218. * @exception IllegalStateException if an input source is required
  1219. * to determine the return value, but none has been set.
  1220. * @exception IndexOutOfBoundsException if an image must be
  1221. * accessed to determine the return value, but the supplied index
  1222. * is out of bounds.
  1223. * @exception IOException if an error occurs during reading.
  1224. */
  1225. public boolean isImageTiled(int imageIndex) throws IOException {
  1226. return false;
  1227. }
  1228. /**
  1229. * Returns the width of a tile in the given image.
  1230. *
  1231. * <p> The default implementation simply returns
  1232. * <code>getWidth(imageIndex)</code>, which is correct for
  1233. * non-tiled images. Readers that support tiling should override
  1234. * this method.
  1235. *
  1236. * @return the width of a tile.
  1237. *
  1238. * @param imageIndex the index of the image to be queried.
  1239. *
  1240. * @exception IllegalStateException if the input source has not been set.
  1241. * @exception IndexOutOfBoundsException if the supplied index is
  1242. * out of bounds.
  1243. * @exception IOException if an error occurs during reading.
  1244. */
  1245. public int getTileWidth(int imageIndex) throws IOException {
  1246. return getWidth(imageIndex);
  1247. }
  1248. /**
  1249. * Returns the height of a tile in the given image.
  1250. *
  1251. * <p> The default implementation simply returns
  1252. * <code>getHeight(imageIndex)</code>, which is correct for
  1253. * non-tiled images. Readers that support tiling should override
  1254. * this method.
  1255. *
  1256. * @return the height of a tile.
  1257. *
  1258. * @param imageIndex the index of the image to be queried.
  1259. *
  1260. * @exception IllegalStateException if the input source has not been set.
  1261. * @exception IndexOutOfBoundsException if the supplied index is
  1262. * out of bounds.
  1263. * @exception IOException if an error occurs during reading.
  1264. */
  1265. public int getTileHeight(int imageIndex) throws IOException {
  1266. return getHeight(imageIndex);
  1267. }
  1268. /**
  1269. * Returns the X coordinate of the upper-left corner of tile (0,
  1270. * 0) in the given image.
  1271. *
  1272. * <p> A reader for which the tile grid X offset always has the
  1273. * same value (usually 0), may return the value without accessing
  1274. * any image data. In such cases, it is not necessary to throw an
  1275. * exception even if no input source has been set or the image
  1276. * index is out of bounds.
  1277. *
  1278. * <p> The default implementation simply returns 0, which is
  1279. * correct for non-tiled images and tiled images in most formats.
  1280. * Readers that support tiling with non-(0, 0) offsets should
  1281. * override this method.
  1282. *
  1283. * @return the X offset of the tile grid.
  1284. *
  1285. * @param imageIndex the index of the image to be queried.
  1286. *
  1287. * @exception IllegalStateException if an input source is required
  1288. * to determine the return value, but none has been set.
  1289. * @exception IndexOutOfBoundsException if an image must be
  1290. * accessed to determine the return value, but the supplied index
  1291. * is out of bounds.
  1292. * @exception IOException if an error occurs during reading.
  1293. */
  1294. public int getTileGridXOffset(int imageIndex) throws IOException {
  1295. return 0;
  1296. }
  1297. /**
  1298. * Returns the Y coordinate of the upper-left corner of tile (0,
  1299. * 0) in the given image.
  1300. *
  1301. * <p> A reader for which the tile grid Y offset always has the
  1302. * same value (usually 0), may return the value without accessing
  1303. * any image data. In such cases, it is not necessary to throw an
  1304. * exception even if no input source has been set or the image
  1305. * index is out of bounds.
  1306. *
  1307. * <p> The default implementation simply returns 0, which is
  1308. * correct for non-tiled images and tiled images in most formats.
  1309. * Readers that support tiling with non-(0, 0) offsets should
  1310. * override this method.
  1311. *
  1312. * @return the Y offset of the tile grid.
  1313. *
  1314. * @param imageIndex the index of the image to be queried.
  1315. *
  1316. * @exception IllegalStateException if an input source is required
  1317. * to determine the return value, but none has been set.
  1318. * @exception IndexOutOfBoundsException if an image must be
  1319. * accessed to determine the return value, but the supplied index
  1320. * is out of bounds.
  1321. * @exception IOException if an error occurs during reading.
  1322. */
  1323. public int getTileGridYOffset(int imageIndex) throws IOException {
  1324. return 0;
  1325. }
  1326. /**
  1327. * Reads the tile indicated by the <code>tileX</code> and
  1328. * <code>tileY</code> arguments, returning it as a
  1329. * <code>BufferedImage</code>. If the arguments are out of range,
  1330. * an <code>IllegalArgumentException</code> is thrown. If the
  1331. * image is not tiled, the values 0, 0 will return the entire
  1332. * image; any other values will cause an
  1333. * <code>IllegalArgumentException</code> to be thrown.
  1334. *
  1335. * <p> This method is merely a convenience equivalent to calling
  1336. * <code>read(int, ImageReadParam)</code> with a read param
  1337. * specifiying a source region having offsets of
  1338. * <code>tileX*getTileWidth(imageIndex)</code>,
  1339. * <code>tileY*getTileHeight(imageIndex)</code> and width and
  1340. * height of <code>getTileWidth(imageIndex)</code>,
  1341. * <code>getTileHeight(imageIndex)</code> and subsampling
  1342. * factors of