1. /*
  2. * @(#)ImageReadParam.java 1.56 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.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. * @version 0.5
  100. *
  101. * @see ImageReader
  102. * @see ImageWriter
  103. * @see ImageWriteParam
  104. */
  105. public class ImageReadParam extends IIOParam {
  106. /**
  107. * <code>true</code> if this <code>ImageReadParam</code> allows
  108. * the source rendering dimensions to be set. By default, the
  109. * value is <code>false</code>. Subclasses must set this value
  110. * manually.
  111. *
  112. * <p> <code>ImageReader</code>s that do not support setting of
  113. * the source render size should set this value to
  114. * <code>false</code>.
  115. */
  116. protected boolean canSetSourceRenderSize = false;
  117. /**
  118. * The desired rendering width and height of the source, if
  119. * <code>canSetSourceRenderSize</code> is <code>true</code>, or
  120. * <code>null</code>.
  121. *
  122. * <p> <code>ImageReader</code>s that do not support setting of
  123. * the source render size may ignore this value.
  124. */
  125. protected Dimension sourceRenderSize = null;
  126. /**
  127. * The current destination <code>BufferedImage</code>, or
  128. * <code>null</code> if none has been set. By default, the value
  129. * is <code>null</code>.
  130. */
  131. protected BufferedImage destination = null;
  132. /**
  133. * The set of destination bands to be used, as an array of
  134. * <code>int</code>s. By default, the value is <code>null</code>,
  135. * indicating all destination bands should be written in order.
  136. */
  137. protected int[] destinationBands = null;
  138. /**
  139. * The minimum index of a progressive pass to read from the
  140. * source. By default, the value is set to 0, which indicates
  141. * that passes starting with the first available pass should be
  142. * decoded.
  143. *
  144. * <p> Subclasses should ensure that this value is
  145. * non-negative.
  146. */
  147. protected int minProgressivePass = 0;
  148. /**
  149. * The maximum number of progressive passes to read from the
  150. * source. By default, the value is set to
  151. * <code>Integer.MAX_VALUE</code>, which indicates that passes up
  152. * to and including the last available pass should be decoded.
  153. *
  154. * <p> Subclasses should ensure that this value is positive.
  155. * Additionally, if the value is not
  156. * <code>Integer.MAX_VALUE</code>, then <code>minProgressivePass +
  157. * numProgressivePasses - 1</code> should not exceed
  158. * <code>Integer.MAX_VALUE</code>.
  159. */
  160. protected int numProgressivePasses = Integer.MAX_VALUE;
  161. /**
  162. * Constructs an <code>ImageReadParam</code>.
  163. */
  164. public ImageReadParam() {}
  165. // Comment inherited
  166. public void setDestinationType(ImageTypeSpecifier destinationType) {
  167. super.setDestinationType(destinationType);
  168. setDestination(null);
  169. }
  170. /**
  171. * Supplies a <code>BufferedImage</code> to be used as the
  172. * destination for decoded pixel data. The currently set image
  173. * will be written to by the <code>read</code>,
  174. * <code>readAll</code>, and <code>readRaster</code> methods, and
  175. * a reference to it will be returned by those methods.
  176. *
  177. * <p> Pixel data from the aforementioned methods will be written
  178. * starting at the offset specified by
  179. * <code>getDestinationOffset</code>.
  180. *
  181. * <p> If <code>destination</code> is <code>null</code>, a
  182. * newly-created <code>BufferedImage</code> will be returned by
  183. * those methods.
  184. *
  185. * <p> At the time of reading, the image is checked to verify that
  186. * its <code>ColorModel</code> and <code>SampleModel</code>
  187. * correspond to one of the <code>ImageTypeSpecifier</code>s
  188. * returned from the <code>ImageReader</code>'s
  189. * <code>getImageTypes</code> method. If it does not, the reader
  190. * will throw an <code>IIOException</code>.
  191. *
  192. * @param destination the BufferedImage to be written to, or
  193. * <code>null</code>.
  194. *
  195. * @see #getDestination
  196. */
  197. public void setDestination(BufferedImage destination) {
  198. this.destination = destination;
  199. }
  200. /**
  201. * Returns the <code>BufferedImage</code> currently set by the
  202. * <code>setDestination</code> method, or <code>null</code>
  203. * if none is set.
  204. *
  205. * @return the BufferedImage to be written to.
  206. *
  207. * @see #setDestination
  208. */
  209. public BufferedImage getDestination() {
  210. return destination;
  211. }
  212. /**
  213. * Sets the indices of the destination bands where data
  214. * will be placed. Duplicate indices are not allowed.
  215. *
  216. * <p> A <code>null</code> value indicates that all destination
  217. * bands will be used.
  218. *
  219. * <p> Choosing a destination band subset will not affect the
  220. * number of bands in the output image of a read if no destination
  221. * image is specified; the created destination image will still
  222. * have the same number of bands as if this method had never been
  223. * called. If a different number of bands in the destination
  224. * image is desired, an image must be supplied using the
  225. * <code>ImageReadParam.setDestination</code> method.
  226. *
  227. * <p> At the time of reading or writing, an
  228. * <code>IllegalArgumentException</code> will be thrown by the
  229. * reader or writer if a value larger than the largest destination
  230. * band index has been specified, or if the number of source bands
  231. * and destination bands to be used differ. The
  232. * <code>ImageReader.checkReadParamBandSettings</code> method may
  233. * be used to automate this test.
  234. *
  235. * @param destinationBands an array of integer band indices to be
  236. * used.
  237. *
  238. * @exception IllegalArgumentException if <code>destinationBands</code>
  239. * contains a negative or duplicate value.
  240. *
  241. * @see #getDestinationBands
  242. * @see #getSourceBands
  243. * @see ImageReader#checkReadParamBandSettings
  244. */
  245. public void setDestinationBands(int[] destinationBands) {
  246. if (destinationBands == null) {
  247. this.destinationBands = null;
  248. } else {
  249. int numBands = destinationBands.length;
  250. for (int i = 0; i < numBands; i++) {
  251. int band = destinationBands[i];
  252. if (band < 0) {
  253. throw new IllegalArgumentException("Band value < 0!");
  254. }
  255. for (int j = i + 1; j < numBands; j++) {
  256. if (band == destinationBands[j]) {
  257. throw new IllegalArgumentException("Duplicate band value!");
  258. }
  259. }
  260. }
  261. this.destinationBands = (int[])destinationBands.clone();
  262. }
  263. }
  264. /**
  265. * Returns the set of band indices where data will be placed.
  266. * If no value has been set, <code>null</code> is returned to
  267. * indicate that all destination bands will be used.
  268. *
  269. * @return the indices of the destination bands to be used,
  270. * or <code>null</code>.
  271. *
  272. * @see #setDestinationBands
  273. */
  274. public int[] getDestinationBands() {
  275. if (destinationBands == null) {
  276. return null;
  277. } else {
  278. return (int[])(destinationBands.clone());
  279. }
  280. }
  281. /**
  282. * Returns <code>true</code> if this reader allows the source
  283. * image to be rendered at an arbitrary size as part of the
  284. * decoding process, by means of the
  285. * <code>setSourceRenderSize</code> method. If this method
  286. * returns <code>false</code>, calls to
  287. * <code>setSourceRenderSize</code> will throw an
  288. * <code>UnsupportedOperationException</code>.
  289. *
  290. * @return <code>true</code> if setting source rendering size is
  291. * supported.
  292. *
  293. * @see #setSourceRenderSize
  294. */
  295. public boolean canSetSourceRenderSize() {
  296. return canSetSourceRenderSize;
  297. }
  298. /**
  299. * If the image is able to be rendered at an arbitrary size, sets
  300. * the source width and height to the supplied values. Note that
  301. * the values returned from the <code>getWidth</code> and
  302. * <code>getHeight</code> methods on <code>ImageReader</code> are
  303. * not affected by this method; they will continue to return the
  304. * default size for the image. Similarly, if the image is also
  305. * tiled the tile width and height are given in terms of the default
  306. * size.
  307. *
  308. * <p> Typically, the width and height should be chosen such that
  309. * the ratio of width to height closely approximates the aspect
  310. * ratio of the image, as returned from
  311. * <code>ImageReader.getAspectRatio</code>.
  312. *
  313. * <p> If this plug-in does not allow the rendering size to be
  314. * set, an <code>UnsupportedOperationException</code> will be
  315. * thrown.
  316. *
  317. * <p> To remove the render size setting, pass in a value of
  318. * <code>null</code> for <code>size</code>.
  319. *
  320. * @param size a <code>Dimension</code> indicating the desired
  321. * width and height.
  322. *
  323. * @exception IllegalArgumentException if either the width or the
  324. * height is negative or 0.
  325. * @exception UnsupportedOperationException if image resizing
  326. * is not supported by this plug-in.
  327. *
  328. * @see #getSourceRenderSize
  329. * @see ImageReader#getWidth
  330. * @see ImageReader#getHeight
  331. * @see ImageReader#getAspectRatio
  332. */
  333. public void setSourceRenderSize(Dimension size)
  334. throws UnsupportedOperationException {
  335. if (!canSetSourceRenderSize()) {
  336. throw new UnsupportedOperationException
  337. ("Can't set source render size!");
  338. }
  339. if (size == null) {
  340. this.sourceRenderSize = null;
  341. } else {
  342. if (size.width <= 0 || size.height <= 0) {
  343. throw new IllegalArgumentException("width or height <= 0!");
  344. }
  345. this.sourceRenderSize = (Dimension)size.clone();
  346. }
  347. }
  348. /**
  349. * Returns the width and height of the source image as it
  350. * will be rendered during decoding, if they have been set via the
  351. * <code>setSourceRenderSize</code> method. A
  352. * <code>null</code>value indicates that no setting has been made.
  353. *
  354. * @return the rendered width and height of the source image
  355. * as a <code>Dimension</code>.
  356. *
  357. * @see #setSourceRenderSize
  358. */
  359. public Dimension getSourceRenderSize() {
  360. return (sourceRenderSize == null) ?
  361. null : (Dimension)sourceRenderSize.clone();
  362. }
  363. /**
  364. * Sets the range of progressive passes that will be decoded.
  365. * Passes outside of this range will be ignored.
  366. *
  367. * <p> A progressive pass is a re-encoding of the entire image,
  368. * generally at progressively higher effective resolutions, but
  369. * requiring greater transmission bandwidth. The most common use
  370. * of progressive encoding is found in the JPEG format, where
  371. * successive passes include more detailed representations of the
  372. * high-frequency image content.
  373. *
  374. * <p> The actual number of passes to be decoded is determined
  375. * during decoding, based on the number of actual passes available
  376. * in the stream. Thus if <code>minPass + numPasses - 1</code> is
  377. * larger than the index of the last available passes, decoding
  378. * will end with that pass.
  379. *
  380. * <p> A value of <code>numPasses</code> of
  381. * <code>Integer.MAX_VALUE</code> indicates that all passes from
  382. * <code>minPass</code> forward should be read. Otherwise, the
  383. * index of the last pass (<i>i.e.</i>, <code>minPass + numPasses
  384. * - 1</code>) must not exceed <code>Integer.MAX_VALUE</code>.
  385. *
  386. * <p> There is no <code>unsetSourceProgressivePasses</code>
  387. * method; the same effect may be obtained by calling
  388. * <code>setSourceProgressivePasses(0, Integer.MAX_VALUE)</code>.
  389. *
  390. * @param minPass the index of the first pass to be decoded.
  391. * @param numPasses the maximum number of passes to be decoded.
  392. *
  393. * @exception IllegalArgumentException if <code>minPass</code> is
  394. * negative, <code>numPasses</code> is negative or 0, or
  395. * <code>numPasses</code> is smaller than
  396. * <code>Integer.MAX_VALUE</code> but <code>minPass +
  397. * numPasses - 1</code> is greater than
  398. * <code>INTEGER.MAX_VALUE</code>.
  399. *
  400. * @see #getSourceMinProgressivePass
  401. * @see #getSourceMaxProgressivePass
  402. */
  403. public void setSourceProgressivePasses(int minPass, int numPasses) {
  404. if (minPass < 0) {
  405. throw new IllegalArgumentException("minPass < 0!");
  406. }
  407. if (numPasses <= 0) {
  408. throw new IllegalArgumentException("numPasses <= 0!");
  409. }
  410. if ((numPasses != Integer.MAX_VALUE) &&
  411. (((minPass + numPasses - 1) & 0x80000000) != 0)) {
  412. throw new IllegalArgumentException
  413. ("minPass + numPasses - 1 > INTEGER.MAX_VALUE!");
  414. }
  415. this.minProgressivePass = minPass;
  416. this.numProgressivePasses = numPasses;
  417. }
  418. /**
  419. * Returns the index of the first progressive pass that will be
  420. * decoded. If no value has been set, 0 will be returned (which is
  421. * the correct value).
  422. *
  423. * @return the index of the first pass that will be decoded.
  424. *
  425. * @see #setSourceProgressivePasses
  426. * @see #getSourceNumProgressivePasses
  427. */
  428. public int getSourceMinProgressivePass() {
  429. return minProgressivePass;
  430. }
  431. /**
  432. * If <code>getSourceNumProgressivePasses</code> is equal to
  433. * <code>Integer.MAX_VALUE</code>, returns
  434. * <code>Integer.MAX_VALUE</code>. Otherwise, returns
  435. * <code>getSourceMinProgressivePass() +
  436. * getSourceNumProgressivePasses() - 1</code>.
  437. *
  438. * @return the index of the last pass to be read, or
  439. * <code>Integer.MAX_VALUE</code>.
  440. */
  441. public int getSourceMaxProgressivePass() {
  442. if (numProgressivePasses == Integer.MAX_VALUE) {
  443. return Integer.MAX_VALUE;
  444. } else {
  445. return minProgressivePass + numProgressivePasses - 1;
  446. }
  447. }
  448. /**
  449. * Returns the number of the progressive passes that will be
  450. * decoded. If no value has been set,
  451. * <code>Integer.MAX_VALUE</code> will be returned (which is the
  452. * correct value).
  453. *
  454. * @return the number of the passes that will be decoded.
  455. *
  456. * @see #setSourceProgressivePasses
  457. * @see #getSourceMinProgressivePass
  458. */
  459. public int getSourceNumProgressivePasses() {
  460. return numProgressivePasses;
  461. }
  462. }