1. /*
  2. * @(#)ImageReadParam.java 1.58 03/12/19
  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.Dimension;
  9. import java.awt.image.BufferedImage;
  10. /**
  11. * A class describing how a stream is to be decoded. Instances of
  12. * this class or its subclasses are used to supply prescriptive
  13. * "how-to" information to instances of <code>ImageReader</code>.
  14. *
  15. * <p> An image encoded as part of a file or stream may be thought of
  16. * extending out in multiple dimensions: the spatial dimensions of
  17. * width and height, a number of bands, and a number of progressive
  18. * decoding passes. This class allows a contiguous (hyper)rectangular
  19. * subarea of the image in all of these dimensions to be selected for
  20. * decoding. Additionally, the spatial dimensions may be subsampled
  21. * discontinuously. Finally, color and format conversions may be
  22. * specified by controlling the <code>ColorModel</code> and
  23. * <code>SampleModel</code> of the destination image, either by
  24. * providing a <code>BufferedImage</code> or by using an
  25. * <code>ImageTypeSpecifier</code>.
  26. *
  27. * <p> An <code>ImageReadParam</code> object is used to specify how an
  28. * image, or a set of images, will be converted on input from
  29. * a stream in the context of the Java Image I/O framework. A plug-in for a
  30. * specific image format will return instances of
  31. * <code>ImageReadParam</code> from the
  32. * <code>getDefaultReadParam</code> method of its
  33. * <code>ImageReader</code> implementation.
  34. *
  35. * <p> The state maintained by an instance of
  36. * <code>ImageReadParam</code> is independent of any particular image
  37. * being decoded. When actual decoding takes place, the values set in
  38. * the read param are combined with the actual properties of the image
  39. * being decoded from the stream and the destination
  40. * <code>BufferedImage</code> that will receive the decoded pixel
  41. * data. For example, the source region set using
  42. * <code>setSourceRegion</code> will first be intersected with the
  43. * actual valid source area. The result will be translated by the
  44. * value returned by <code>getDestinationOffset</code>, and the
  45. * resulting rectangle intersected with the actual valid destination
  46. * area to yield the destination area that will be written.
  47. *
  48. * <p> The parameters specified by an <code>ImageReadParam</code> are
  49. * applied to an image as follows. First, if a rendering size has
  50. * been set by <code>setSourceRenderSize</code>, the entire decoded
  51. * image is rendered at the size given by
  52. * <code>getSourceRenderSize</code>. Otherwise, the image has its
  53. * natural size given by <code>ImageReader.getWidth</code> and
  54. * <code>ImageReader.getHeight</code>.
  55. *
  56. * <p> Next, the image is clipped against the source region
  57. * specified by <code>getSourceXOffset</code>, <code>getSourceYOffset</code>,
  58. * <code>getSourceWidth</code>, and <code>getSourceHeight</code>.
  59. *
  60. * <p> The resulting region is then subsampled according to the
  61. * factors given in {@link IIOParam#setSourceSubsampling
  62. * <code>IIOParam.setSourceSubsampling</code>}. The first pixel,
  63. * the number of pixels per row, and the number of rows all depend
  64. * on the subsampling settings.
  65. * Call the minimum X and Y coordinates of the resulting rectangle
  66. * (<code>minX</code>, <code>minY</code>), its width <code>w</code>
  67. * and its height <code>h</code>.
  68. *
  69. * <p> This rectangle is offset by
  70. * (<code>getDestinationOffset().x</code>,
  71. * <code>getDestinationOffset().y</code>) and clipped against the
  72. * destination bounds. If no destination image has been set, the
  73. * destination is defined to have a width of
  74. * <code>getDestinationOffset().x</code> + <code>w</code>, and a
  75. * height of <code>getDestinationOffset().y</code> + <code>h</code> so
  76. * that all pixels of the source region may be written to the
  77. * destination.
  78. *
  79. * <p> Pixels that land, after subsampling, within the destination
  80. * image, and that are written in one of the progressive passes
  81. * specified by <code>getSourceMinProgressivePass</code> and
  82. * <code>getSourceNumProgressivePasses</code> are passed along to the
  83. * next step.
  84. *
  85. * <p> Finally, the source samples of each pixel are mapped into
  86. * destination bands according to the algorithm described in the
  87. * comment for <code>setDestinationBands</code>.
  88. *
  89. * <p> Plug-in writers may extend the functionality of
  90. * <code>ImageReadParam</code> by providing a subclass that implements
  91. * additional, plug-in specific interfaces. It is up to the plug-in
  92. * to document what interfaces are available and how they are to be
  93. * used. Readers will silently ignore any extended features of an
  94. * <code>ImageReadParam</code> subclass of which they are not aware.
  95. * Also, they may ignore any optional features that they normally
  96. * disable when creating their own <code>ImageReadParam</code>
  97. * instances via <code>getDefaultReadParam</code>.
  98. *
  99. * <p> Note that unless a query method exists for a capability, it must
  100. * be supported by all <code>ImageReader</code> implementations
  101. * (<i>e.g.</i> source render size is optional, but subsampling must be
  102. * supported).
  103. *
  104. * @version 0.5
  105. *
  106. * @see ImageReader
  107. * @see ImageWriter
  108. * @see ImageWriteParam
  109. */
  110. public class ImageReadParam extends IIOParam {
  111. /**
  112. * <code>true</code> if this <code>ImageReadParam</code> allows
  113. * the source rendering dimensions to be set. By default, the
  114. * value is <code>false</code>. Subclasses must set this value
  115. * manually.
  116. *
  117. * <p> <code>ImageReader</code>s that do not support setting of
  118. * the source render size should set this value to
  119. * <code>false</code>.
  120. */
  121. protected boolean canSetSourceRenderSize = false;
  122. /**
  123. * The desired rendering width and height of the source, if
  124. * <code>canSetSourceRenderSize</code> is <code>true</code>, or
  125. * <code>null</code>.
  126. *
  127. * <p> <code>ImageReader</code>s that do not support setting of
  128. * the source render size may ignore this value.
  129. */
  130. protected Dimension sourceRenderSize = null;
  131. /**
  132. * The current destination <code>BufferedImage</code>, or
  133. * <code>null</code> if none has been set. By default, the value
  134. * is <code>null</code>.
  135. */
  136. protected BufferedImage destination = null;
  137. /**
  138. * The set of destination bands to be used, as an array of
  139. * <code>int</code>s. By default, the value is <code>null</code>,
  140. * indicating all destination bands should be written in order.
  141. */
  142. protected int[] destinationBands = null;
  143. /**
  144. * The minimum index of a progressive pass to read from the
  145. * source. By default, the value is set to 0, which indicates
  146. * that passes starting with the first available pass should be
  147. * decoded.
  148. *
  149. * <p> Subclasses should ensure that this value is
  150. * non-negative.
  151. */
  152. protected int minProgressivePass = 0;
  153. /**
  154. * The maximum number of progressive passes to read from the
  155. * source. By default, the value is set to
  156. * <code>Integer.MAX_VALUE</code>, which indicates that passes up
  157. * to and including the last available pass should be decoded.
  158. *
  159. * <p> Subclasses should ensure that this value is positive.
  160. * Additionally, if the value is not
  161. * <code>Integer.MAX_VALUE</code>, then <code>minProgressivePass +
  162. * numProgressivePasses - 1</code> should not exceed
  163. * <code>Integer.MAX_VALUE</code>.
  164. */
  165. protected int numProgressivePasses = Integer.MAX_VALUE;
  166. /**
  167. * Constructs an <code>ImageReadParam</code>.
  168. */
  169. public ImageReadParam() {}
  170. // Comment inherited
  171. public void setDestinationType(ImageTypeSpecifier destinationType) {
  172. super.setDestinationType(destinationType);
  173. setDestination(null);
  174. }
  175. /**
  176. * Supplies a <code>BufferedImage</code> to be used as the
  177. * destination for decoded pixel data. The currently set image
  178. * will be written to by the <code>read</code>,
  179. * <code>readAll</code>, and <code>readRaster</code> methods, and
  180. * a reference to it will be returned by those methods.
  181. *
  182. * <p> Pixel data from the aforementioned methods will be written
  183. * starting at the offset specified by
  184. * <code>getDestinationOffset</code>.
  185. *
  186. * <p> If <code>destination</code> is <code>null</code>, a
  187. * newly-created <code>BufferedImage</code> will be returned by
  188. * those methods.
  189. *
  190. * <p> At the time of reading, the image is checked to verify that
  191. * its <code>ColorModel</code> and <code>SampleModel</code>
  192. * correspond to one of the <code>ImageTypeSpecifier</code>s
  193. * returned from the <code>ImageReader</code>'s
  194. * <code>getImageTypes</code> method. If it does not, the reader
  195. * will throw an <code>IIOException</code>.
  196. *
  197. * @param destination the BufferedImage to be written to, or
  198. * <code>null</code>.
  199. *
  200. * @see #getDestination
  201. */
  202. public void setDestination(BufferedImage destination) {
  203. this.destination = destination;
  204. }
  205. /**
  206. * Returns the <code>BufferedImage</code> currently set by the
  207. * <code>setDestination</code> method, or <code>null</code>
  208. * if none is set.
  209. *
  210. * @return the BufferedImage to be written to.
  211. *
  212. * @see #setDestination
  213. */
  214. public BufferedImage getDestination() {
  215. return destination;
  216. }
  217. /**
  218. * Sets the indices of the destination bands where data
  219. * will be placed. Duplicate indices are not allowed.
  220. *
  221. * <p> A <code>null</code> value indicates that all destination
  222. * bands will be used.
  223. *
  224. * <p> Choosing a destination band subset will not affect the
  225. * number of bands in the output image of a read if no destination
  226. * image is specified; the created destination image will still
  227. * have the same number of bands as if this method had never been
  228. * called. If a different number of bands in the destination
  229. * image is desired, an image must be supplied using the
  230. * <code>ImageReadParam.setDestination</code> method.
  231. *
  232. * <p> At the time of reading or writing, an
  233. * <code>IllegalArgumentException</code> will be thrown by the
  234. * reader or writer if a value larger than the largest destination
  235. * band index has been specified, or if the number of source bands
  236. * and destination bands to be used differ. The
  237. * <code>ImageReader.checkReadParamBandSettings</code> method may
  238. * be used to automate this test.
  239. *
  240. * @param destinationBands an array of integer band indices to be
  241. * used.
  242. *
  243. * @exception IllegalArgumentException if <code>destinationBands</code>
  244. * contains a negative or duplicate value.
  245. *
  246. * @see #getDestinationBands
  247. * @see #getSourceBands
  248. * @see ImageReader#checkReadParamBandSettings
  249. */
  250. public void setDestinationBands(int[] destinationBands) {
  251. if (destinationBands == null) {
  252. this.destinationBands = null;
  253. } else {
  254. int numBands = destinationBands.length;
  255. for (int i = 0; i < numBands; i++) {
  256. int band = destinationBands[i];
  257. if (band < 0) {
  258. throw new IllegalArgumentException("Band value < 0!");
  259. }
  260. for (int j = i + 1; j < numBands; j++) {
  261. if (band == destinationBands[j]) {
  262. throw new IllegalArgumentException("Duplicate band value!");
  263. }
  264. }
  265. }
  266. this.destinationBands = (int[])destinationBands.clone();
  267. }
  268. }
  269. /**
  270. * Returns the set of band indices where data will be placed.
  271. * If no value has been set, <code>null</code> is returned to
  272. * indicate that all destination bands will be used.
  273. *
  274. * @return the indices of the destination bands to be used,
  275. * or <code>null</code>.
  276. *
  277. * @see #setDestinationBands
  278. */
  279. public int[] getDestinationBands() {
  280. if (destinationBands == null) {
  281. return null;
  282. } else {
  283. return (int[])(destinationBands.clone());
  284. }
  285. }
  286. /**
  287. * Returns <code>true</code> if this reader allows the source
  288. * image to be rendered at an arbitrary size as part of the
  289. * decoding process, by means of the
  290. * <code>setSourceRenderSize</code> method. If this method
  291. * returns <code>false</code>, calls to
  292. * <code>setSourceRenderSize</code> will throw an
  293. * <code>UnsupportedOperationException</code>.
  294. *
  295. * @return <code>true</code> if setting source rendering size is
  296. * supported.
  297. *
  298. * @see #setSourceRenderSize
  299. */
  300. public boolean canSetSourceRenderSize() {
  301. return canSetSourceRenderSize;
  302. }
  303. /**
  304. * If the image is able to be rendered at an arbitrary size, sets
  305. * the source width and height to the supplied values. Note that
  306. * the values returned from the <code>getWidth</code> and
  307. * <code>getHeight</code> methods on <code>ImageReader</code> are
  308. * not affected by this method; they will continue to return the
  309. * default size for the image. Similarly, if the image is also
  310. * tiled the tile width and height are given in terms of the default
  311. * size.
  312. *
  313. * <p> Typically, the width and height should be chosen such that
  314. * the ratio of width to height closely approximates the aspect
  315. * ratio of the image, as returned from
  316. * <code>ImageReader.getAspectRatio</code>.
  317. *
  318. * <p> If this plug-in does not allow the rendering size to be
  319. * set, an <code>UnsupportedOperationException</code> will be
  320. * thrown.
  321. *
  322. * <p> To remove the render size setting, pass in a value of
  323. * <code>null</code> for <code>size</code>.
  324. *
  325. * @param size a <code>Dimension</code> indicating the desired
  326. * width and height.
  327. *
  328. * @exception IllegalArgumentException if either the width or the
  329. * height is negative or 0.
  330. * @exception UnsupportedOperationException if image resizing
  331. * is not supported by this plug-in.
  332. *
  333. * @see #getSourceRenderSize
  334. * @see ImageReader#getWidth
  335. * @see ImageReader#getHeight
  336. * @see ImageReader#getAspectRatio
  337. */
  338. public void setSourceRenderSize(Dimension size)
  339. throws UnsupportedOperationException {
  340. if (!canSetSourceRenderSize()) {
  341. throw new UnsupportedOperationException
  342. ("Can't set source render size!");
  343. }
  344. if (size == null) {
  345. this.sourceRenderSize = null;
  346. } else {
  347. if (size.width <= 0 || size.height <= 0) {
  348. throw new IllegalArgumentException("width or height <= 0!");
  349. }
  350. this.sourceRenderSize = (Dimension)size.clone();
  351. }
  352. }
  353. /**
  354. * Returns the width and height of the source image as it
  355. * will be rendered during decoding, if they have been set via the
  356. * <code>setSourceRenderSize</code> method. A
  357. * <code>null</code>value indicates that no setting has been made.
  358. *
  359. * @return the rendered width and height of the source image
  360. * as a <code>Dimension</code>.
  361. *
  362. * @see #setSourceRenderSize
  363. */
  364. public Dimension getSourceRenderSize() {
  365. return (sourceRenderSize == null) ?
  366. null : (Dimension)sourceRenderSize.clone();
  367. }
  368. /**
  369. * Sets the range of progressive passes that will be decoded.
  370. * Passes outside of this range will be ignored.
  371. *
  372. * <p> A progressive pass is a re-encoding of the entire image,
  373. * generally at progressively higher effective resolutions, but
  374. * requiring greater transmission bandwidth. The most common use
  375. * of progressive encoding is found in the JPEG format, where
  376. * successive passes include more detailed representations of the
  377. * high-frequency image content.
  378. *
  379. * <p> The actual number of passes to be decoded is determined
  380. * during decoding, based on the number of actual passes available
  381. * in the stream. Thus if <code>minPass + numPasses - 1</code> is
  382. * larger than the index of the last available passes, decoding
  383. * will end with that pass.
  384. *
  385. * <p> A value of <code>numPasses</code> of
  386. * <code>Integer.MAX_VALUE</code> indicates that all passes from
  387. * <code>minPass</code> forward should be read. Otherwise, the
  388. * index of the last pass (<i>i.e.</i>, <code>minPass + numPasses
  389. * - 1</code>) must not exceed <code>Integer.MAX_VALUE</code>.
  390. *
  391. * <p> There is no <code>unsetSourceProgressivePasses</code>
  392. * method; the same effect may be obtained by calling
  393. * <code>setSourceProgressivePasses(0, Integer.MAX_VALUE)</code>.
  394. *
  395. * @param minPass the index of the first pass to be decoded.
  396. * @param numPasses the maximum number of passes to be decoded.
  397. *
  398. * @exception IllegalArgumentException if <code>minPass</code> is
  399. * negative, <code>numPasses</code> is negative or 0, or
  400. * <code>numPasses</code> is smaller than
  401. * <code>Integer.MAX_VALUE</code> but <code>minPass +
  402. * numPasses - 1</code> is greater than
  403. * <code>INTEGER.MAX_VALUE</code>.
  404. *
  405. * @see #getSourceMinProgressivePass
  406. * @see #getSourceMaxProgressivePass
  407. */
  408. public void setSourceProgressivePasses(int minPass, int numPasses) {
  409. if (minPass < 0) {
  410. throw new IllegalArgumentException("minPass < 0!");
  411. }
  412. if (numPasses <= 0) {
  413. throw new IllegalArgumentException("numPasses <= 0!");
  414. }
  415. if ((numPasses != Integer.MAX_VALUE) &&
  416. (((minPass + numPasses - 1) & 0x80000000) != 0)) {
  417. throw new IllegalArgumentException
  418. ("minPass + numPasses - 1 > INTEGER.MAX_VALUE!");
  419. }
  420. this.minProgressivePass = minPass;
  421. this.numProgressivePasses = numPasses;
  422. }
  423. /**
  424. * Returns the index of the first progressive pass that will be
  425. * decoded. If no value has been set, 0 will be returned (which is
  426. * the correct value).
  427. *
  428. * @return the index of the first pass that will be decoded.
  429. *
  430. * @see #setSourceProgressivePasses
  431. * @see #getSourceNumProgressivePasses
  432. */
  433. public int getSourceMinProgressivePass() {
  434. return minProgressivePass;
  435. }
  436. /**
  437. * If <code>getSourceNumProgressivePasses</code> is equal to
  438. * <code>Integer.MAX_VALUE</code>, returns
  439. * <code>Integer.MAX_VALUE</code>. Otherwise, returns
  440. * <code>getSourceMinProgressivePass() +
  441. * getSourceNumProgressivePasses() - 1</code>.
  442. *
  443. * @return the index of the last pass to be read, or
  444. * <code>Integer.MAX_VALUE</code>.
  445. */
  446. public int getSourceMaxProgressivePass() {
  447. if (numProgressivePasses == Integer.MAX_VALUE) {
  448. return Integer.MAX_VALUE;
  449. } else {
  450. return minProgressivePass + numProgressivePasses - 1;
  451. }
  452. }
  453. /**
  454. * Returns the number of the progressive passes that will be
  455. * decoded. If no value has been set,
  456. * <code>Integer.MAX_VALUE</code> will be returned (which is the
  457. * correct value).
  458. *
  459. * @return the number of the passes that will be decoded.
  460. *
  461. * @see #setSourceProgressivePasses
  462. * @see #getSourceMinProgressivePass
  463. */
  464. public int getSourceNumProgressivePasses() {
  465. return numProgressivePasses;
  466. }
  467. }