KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > ImageReadParam


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
8 package javax.imageio;
9
10 import java.awt.Dimension JavaDoc;
11 import java.awt.image.BufferedImage JavaDoc;
12
13 /**
14  * A class describing how a stream is to be decoded. Instances of
15  * this class or its subclasses are used to supply prescriptive
16  * "how-to" information to instances of <code>ImageReader</code>.
17  *
18  * <p> An image encoded as part of a file or stream may be thought of
19  * extending out in multiple dimensions: the spatial dimensions of
20  * width and height, a number of bands, and a number of progressive
21  * decoding passes. This class allows a contiguous (hyper)rectangular
22  * subarea of the image in all of these dimensions to be selected for
23  * decoding. Additionally, the spatial dimensions may be subsampled
24  * discontinuously. Finally, color and format conversions may be
25  * specified by controlling the <code>ColorModel</code> and
26  * <code>SampleModel</code> of the destination image, either by
27  * providing a <code>BufferedImage</code> or by using an
28  * <code>ImageTypeSpecifier</code>.
29  *
30  * <p> An <code>ImageReadParam</code> object is used to specify how an
31  * image, or a set of images, will be converted on input from
32  * a stream in the context of the Java Image I/O framework. A plug-in for a
33  * specific image format will return instances of
34  * <code>ImageReadParam</code> from the
35  * <code>getDefaultReadParam</code> method of its
36  * <code>ImageReader</code> implementation.
37  *
38  * <p> The state maintained by an instance of
39  * <code>ImageReadParam</code> is independent of any particular image
40  * being decoded. When actual decoding takes place, the values set in
41  * the read param are combined with the actual properties of the image
42  * being decoded from the stream and the destination
43  * <code>BufferedImage</code> that will receive the decoded pixel
44  * data. For example, the source region set using
45  * <code>setSourceRegion</code> will first be intersected with the
46  * actual valid source area. The result will be translated by the
47  * value returned by <code>getDestinationOffset</code>, and the
48  * resulting rectangle intersected with the actual valid destination
49  * area to yield the destination area that will be written.
50  *
51  * <p> The parameters specified by an <code>ImageReadParam</code> are
52  * applied to an image as follows. First, if a rendering size has
53  * been set by <code>setSourceRenderSize</code>, the entire decoded
54  * image is rendered at the size given by
55  * <code>getSourceRenderSize</code>. Otherwise, the image has its
56  * natural size given by <code>ImageReader.getWidth</code> and
57  * <code>ImageReader.getHeight</code>.
58  *
59  * <p> Next, the image is clipped against the source region
60  * specified by <code>getSourceXOffset</code>, <code>getSourceYOffset</code>,
61  * <code>getSourceWidth</code>, and <code>getSourceHeight</code>.
62  *
63  * <p> The resulting region is then subsampled according to the
64  * factors given in {@link IIOParam#setSourceSubsampling
65  * <code>IIOParam.setSourceSubsampling</code>}. The first pixel,
66  * the number of pixels per row, and the number of rows all depend
67  * on the subsampling settings.
68  * Call the minimum X and Y coordinates of the resulting rectangle
69  * (<code>minX</code>, <code>minY</code>), its width <code>w</code>
70  * and its height <code>h</code>.
71  *
72  * <p> This rectangle is offset by
73  * (<code>getDestinationOffset().x</code>,
74  * <code>getDestinationOffset().y</code>) and clipped against the
75  * destination bounds. If no destination image has been set, the
76  * destination is defined to have a width of
77  * <code>getDestinationOffset().x</code> + <code>w</code>, and a
78  * height of <code>getDestinationOffset().y</code> + <code>h</code> so
79  * that all pixels of the source region may be written to the
80  * destination.
81  *
82  * <p> Pixels that land, after subsampling, within the destination
83  * image, and that are written in one of the progressive passes
84  * specified by <code>getSourceMinProgressivePass</code> and
85  * <code>getSourceNumProgressivePasses</code> are passed along to the
86  * next step.
87  *
88  * <p> Finally, the source samples of each pixel are mapped into
89  * destination bands according to the algorithm described in the
90  * comment for <code>setDestinationBands</code>.
91  *
92  * <p> Plug-in writers may extend the functionality of
93  * <code>ImageReadParam</code> by providing a subclass that implements
94  * additional, plug-in specific interfaces. It is up to the plug-in
95  * to document what interfaces are available and how they are to be
96  * used. Readers will silently ignore any extended features of an
97  * <code>ImageReadParam</code> subclass of which they are not aware.
98  * Also, they may ignore any optional features that they normally
99  * disable when creating their own <code>ImageReadParam</code>
100  * instances via <code>getDefaultReadParam</code>.
101  *
102  * <p> Note that unless a query method exists for a capability, it must
103  * be supported by all <code>ImageReader</code> implementations
104  * (<i>e.g.</i> source render size is optional, but subsampling must be
105  * supported).
106  *
107  * @version 0.5
108  *
109  * @see ImageReader
110  * @see ImageWriter
111  * @see ImageWriteParam
112  */

113 public class ImageReadParam extends IIOParam JavaDoc {
114
115     /**
116      * <code>true</code> if this <code>ImageReadParam</code> allows
117      * the source rendering dimensions to be set. By default, the
118      * value is <code>false</code>. Subclasses must set this value
119      * manually.
120      *
121      * <p> <code>ImageReader</code>s that do not support setting of
122      * the source render size should set this value to
123      * <code>false</code>.
124      */

125     protected boolean canSetSourceRenderSize = false;
126
127     /**
128      * The desired rendering width and height of the source, if
129      * <code>canSetSourceRenderSize</code> is <code>true</code>, or
130      * <code>null</code>.
131      *
132      * <p> <code>ImageReader</code>s that do not support setting of
133      * the source render size may ignore this value.
134      */

135     protected Dimension JavaDoc sourceRenderSize = null;
136
137     /**
138      * The current destination <code>BufferedImage</code>, or
139      * <code>null</code> if none has been set. By default, the value
140      * is <code>null</code>.
141      */

142     protected BufferedImage JavaDoc destination = null;
143
144     /**
145      * The set of destination bands to be used, as an array of
146      * <code>int</code>s. By default, the value is <code>null</code>,
147      * indicating all destination bands should be written in order.
148      */

149     protected int[] destinationBands = null;
150
151     /**
152      * The minimum index of a progressive pass to read from the
153      * source. By default, the value is set to 0, which indicates
154      * that passes starting with the first available pass should be
155      * decoded.
156      *
157      * <p> Subclasses should ensure that this value is
158      * non-negative.
159      */

160     protected int minProgressivePass = 0;
161
162     /**
163      * The maximum number of progressive passes to read from the
164      * source. By default, the value is set to
165      * <code>Integer.MAX_VALUE</code>, which indicates that passes up
166      * to and including the last available pass should be decoded.
167      *
168      * <p> Subclasses should ensure that this value is positive.
169      * Additionally, if the value is not
170      * <code>Integer.MAX_VALUE</code>, then <code>minProgressivePass +
171      * numProgressivePasses - 1</code> should not exceed
172      * <code>Integer.MAX_VALUE</code>.
173      */

174     protected int numProgressivePasses = Integer.MAX_VALUE;
175
176     /**
177      * Constructs an <code>ImageReadParam</code>.
178      */

179     public ImageReadParam() {}
180
181     // Comment inherited
182
public void setDestinationType(ImageTypeSpecifier JavaDoc destinationType) {
183         super.setDestinationType(destinationType);
184         setDestination(null);
185     }
186
187     /**
188      * Supplies a <code>BufferedImage</code> to be used as the
189      * destination for decoded pixel data. The currently set image
190      * will be written to by the <code>read</code>,
191      * <code>readAll</code>, and <code>readRaster</code> methods, and
192      * a reference to it will be returned by those methods.
193      *
194      * <p> Pixel data from the aforementioned methods will be written
195      * starting at the offset specified by
196      * <code>getDestinationOffset</code>.
197      *
198      * <p> If <code>destination</code> is <code>null</code>, a
199      * newly-created <code>BufferedImage</code> will be returned by
200      * those methods.
201      *
202      * <p> At the time of reading, the image is checked to verify that
203      * its <code>ColorModel</code> and <code>SampleModel</code>
204      * correspond to one of the <code>ImageTypeSpecifier</code>s
205      * returned from the <code>ImageReader</code>'s
206      * <code>getImageTypes</code> method. If it does not, the reader
207      * will throw an <code>IIOException</code>.
208      *
209      * @param destination the BufferedImage to be written to, or
210      * <code>null</code>.
211      *
212      * @see #getDestination
213      */

214     public void setDestination(BufferedImage JavaDoc destination) {
215         this.destination = destination;
216     }
217
218     /**
219      * Returns the <code>BufferedImage</code> currently set by the
220      * <code>setDestination</code> method, or <code>null</code>
221      * if none is set.
222      *
223      * @return the BufferedImage to be written to.
224      *
225      * @see #setDestination
226      */

227     public BufferedImage JavaDoc getDestination() {
228         return destination;
229     }
230
231     /**
232      * Sets the indices of the destination bands where data
233      * will be placed. Duplicate indices are not allowed.
234      *
235      * <p> A <code>null</code> value indicates that all destination
236      * bands will be used.
237      *
238      * <p> Choosing a destination band subset will not affect the
239      * number of bands in the output image of a read if no destination
240      * image is specified; the created destination image will still
241      * have the same number of bands as if this method had never been
242      * called. If a different number of bands in the destination
243      * image is desired, an image must be supplied using the
244      * <code>ImageReadParam.setDestination</code> method.
245      *
246      * <p> At the time of reading or writing, an
247      * <code>IllegalArgumentException</code> will be thrown by the
248      * reader or writer if a value larger than the largest destination
249      * band index has been specified, or if the number of source bands
250      * and destination bands to be used differ. The
251      * <code>ImageReader.checkReadParamBandSettings</code> method may
252      * be used to automate this test.
253      *
254      * @param destinationBands an array of integer band indices to be
255      * used.
256      *
257      * @exception IllegalArgumentException if <code>destinationBands</code>
258      * contains a negative or duplicate value.
259      *
260      * @see #getDestinationBands
261      * @see #getSourceBands
262      * @see ImageReader#checkReadParamBandSettings
263      */

264     public void setDestinationBands(int[] destinationBands) {
265         if (destinationBands == null) {
266             this.destinationBands = null;
267         } else {
268             int numBands = destinationBands.length;
269             for (int i = 0; i < numBands; i++) {
270                 int band = destinationBands[i];
271                 if (band < 0) {
272                     throw new IllegalArgumentException JavaDoc("Band value < 0!");
273                 }
274                 for (int j = i + 1; j < numBands; j++) {
275                     if (band == destinationBands[j]) {
276                         throw new IllegalArgumentException JavaDoc("Duplicate band value!");
277                     }
278                 }
279             }
280             this.destinationBands = (int[])destinationBands.clone();
281         }
282     }
283
284     /**
285      * Returns the set of band indices where data will be placed.
286      * If no value has been set, <code>null</code> is returned to
287      * indicate that all destination bands will be used.
288      *
289      * @return the indices of the destination bands to be used,
290      * or <code>null</code>.
291      *
292      * @see #setDestinationBands
293      */

294     public int[] getDestinationBands() {
295         if (destinationBands == null) {
296             return null;
297         } else {
298             return (int[])(destinationBands.clone());
299         }
300     }
301
302     /**
303      * Returns <code>true</code> if this reader allows the source
304      * image to be rendered at an arbitrary size as part of the
305      * decoding process, by means of the
306      * <code>setSourceRenderSize</code> method. If this method
307      * returns <code>false</code>, calls to
308      * <code>setSourceRenderSize</code> will throw an
309      * <code>UnsupportedOperationException</code>.
310      *
311      * @return <code>true</code> if setting source rendering size is
312      * supported.
313      *
314      * @see #setSourceRenderSize
315      */

316     public boolean canSetSourceRenderSize() {
317         return canSetSourceRenderSize;
318     }
319
320     /**
321      * If the image is able to be rendered at an arbitrary size, sets
322      * the source width and height to the supplied values. Note that
323      * the values returned from the <code>getWidth</code> and
324      * <code>getHeight</code> methods on <code>ImageReader</code> are
325      * not affected by this method; they will continue to return the
326      * default size for the image. Similarly, if the image is also
327      * tiled the tile width and height are given in terms of the default
328      * size.
329      *
330      * <p> Typically, the width and height should be chosen such that
331      * the ratio of width to height closely approximates the aspect
332      * ratio of the image, as returned from
333      * <code>ImageReader.getAspectRatio</code>.
334      *
335      * <p> If this plug-in does not allow the rendering size to be
336      * set, an <code>UnsupportedOperationException</code> will be
337      * thrown.
338      *
339      * <p> To remove the render size setting, pass in a value of
340      * <code>null</code> for <code>size</code>.
341      *
342      * @param size a <code>Dimension</code> indicating the desired
343      * width and height.
344      *
345      * @exception IllegalArgumentException if either the width or the
346      * height is negative or 0.
347      * @exception UnsupportedOperationException if image resizing
348      * is not supported by this plug-in.
349      *
350      * @see #getSourceRenderSize
351      * @see ImageReader#getWidth
352      * @see ImageReader#getHeight
353      * @see ImageReader#getAspectRatio
354      */

355     public void setSourceRenderSize(Dimension JavaDoc size)
356         throws UnsupportedOperationException JavaDoc {
357         if (!canSetSourceRenderSize()) {
358             throw new UnsupportedOperationException JavaDoc
359                 ("Can't set source render size!");
360         }
361         
362         if (size == null) {
363             this.sourceRenderSize = null;
364         } else {
365             if (size.width <= 0 || size.height <= 0) {
366                 throw new IllegalArgumentException JavaDoc("width or height <= 0!");
367             }
368             this.sourceRenderSize = (Dimension JavaDoc)size.clone();
369         }
370     }
371
372     /**
373      * Returns the width and height of the source image as it
374      * will be rendered during decoding, if they have been set via the
375      * <code>setSourceRenderSize</code> method. A
376      * <code>null</code>value indicates that no setting has been made.
377      *
378      * @return the rendered width and height of the source image
379      * as a <code>Dimension</code>.
380      *
381      * @see #setSourceRenderSize
382      */

383     public Dimension JavaDoc getSourceRenderSize() {
384         return (sourceRenderSize == null) ?
385             null : (Dimension JavaDoc)sourceRenderSize.clone();
386     }
387
388     /**
389      * Sets the range of progressive passes that will be decoded.
390      * Passes outside of this range will be ignored.
391      *
392      * <p> A progressive pass is a re-encoding of the entire image,
393      * generally at progressively higher effective resolutions, but
394      * requiring greater transmission bandwidth. The most common use
395      * of progressive encoding is found in the JPEG format, where
396      * successive passes include more detailed representations of the
397      * high-frequency image content.
398      *
399      * <p> The actual number of passes to be decoded is determined
400      * during decoding, based on the number of actual passes available
401      * in the stream. Thus if <code>minPass + numPasses - 1</code> is
402      * larger than the index of the last available passes, decoding
403      * will end with that pass.
404      *
405      * <p> A value of <code>numPasses</code> of
406      * <code>Integer.MAX_VALUE</code> indicates that all passes from
407      * <code>minPass</code> forward should be read. Otherwise, the
408      * index of the last pass (<i>i.e.</i>, <code>minPass + numPasses
409      * - 1</code>) must not exceed <code>Integer.MAX_VALUE</code>.
410      *
411      * <p> There is no <code>unsetSourceProgressivePasses</code>
412      * method; the same effect may be obtained by calling
413      * <code>setSourceProgressivePasses(0, Integer.MAX_VALUE)</code>.
414      *
415      * @param minPass the index of the first pass to be decoded.
416      * @param numPasses the maximum number of passes to be decoded.
417      *
418      * @exception IllegalArgumentException if <code>minPass</code> is
419      * negative, <code>numPasses</code> is negative or 0, or
420      * <code>numPasses</code> is smaller than
421      * <code>Integer.MAX_VALUE</code> but <code>minPass +
422      * numPasses - 1</code> is greater than
423      * <code>INTEGER.MAX_VALUE</code>.
424      *
425      * @see #getSourceMinProgressivePass
426      * @see #getSourceMaxProgressivePass
427      */

428     public void setSourceProgressivePasses(int minPass, int numPasses) {
429         if (minPass < 0) {
430             throw new IllegalArgumentException JavaDoc("minPass < 0!");
431         }
432         if (numPasses <= 0) {
433             throw new IllegalArgumentException JavaDoc("numPasses <= 0!");
434         }
435         if ((numPasses != Integer.MAX_VALUE) &&
436             (((minPass + numPasses - 1) & 0x80000000) != 0)) {
437             throw new IllegalArgumentException JavaDoc
438                 ("minPass + numPasses - 1 > INTEGER.MAX_VALUE!");
439         }
440
441         this.minProgressivePass = minPass;
442         this.numProgressivePasses = numPasses;
443     }
444
445     /**
446      * Returns the index of the first progressive pass that will be
447      * decoded. If no value has been set, 0 will be returned (which is
448      * the correct value).
449      *
450      * @return the index of the first pass that will be decoded.
451      *
452      * @see #setSourceProgressivePasses
453      * @see #getSourceNumProgressivePasses
454      */

455     public int getSourceMinProgressivePass() {
456         return minProgressivePass;
457     }
458
459     /**
460      * If <code>getSourceNumProgressivePasses</code> is equal to
461      * <code>Integer.MAX_VALUE</code>, returns
462      * <code>Integer.MAX_VALUE</code>. Otherwise, returns
463      * <code>getSourceMinProgressivePass() +
464      * getSourceNumProgressivePasses() - 1</code>.
465      *
466      * @return the index of the last pass to be read, or
467      * <code>Integer.MAX_VALUE</code>.
468      */

469     public int getSourceMaxProgressivePass() {
470         if (numProgressivePasses == Integer.MAX_VALUE) {
471             return Integer.MAX_VALUE;
472         } else {
473             return minProgressivePass + numProgressivePasses - 1;
474         }
475     }
476
477     /**
478      * Returns the number of the progressive passes that will be
479      * decoded. If no value has been set,
480      * <code>Integer.MAX_VALUE</code> will be returned (which is the
481      * correct value).
482      *
483      * @return the number of the passes that will be decoded.
484      *
485      * @see #setSourceProgressivePasses
486      * @see #getSourceMinProgressivePass
487      */

488     public int getSourceNumProgressivePasses() {
489         return numProgressivePasses;
490     }
491 }
492
Popular Tags