[JAI-IMAGEIO] Question about ImageReadParam.setSourceBands/setDestinationBands/setDestination methods and how to handle them
I'm extending our ImageIO-Ext project and the actual step is supporting
setSourceBands settings in the ImageReadParam used as argument of the
Looking at ImageReader/IIOParam/ImageReadParam javadocs I have collected the
- setSourceBands allows to set the indices of the source bands to be used.
Moreover, quoting from javadoc: "*At the time of reading, an
IllegalArgumentException will be thrown by the reader [...] if the number of
source bands and destination bands to be used differ*". For this reason, I
guess that setting sourceBands also requires setting destinationBands.
- setDestinationBands allows to set the indices of the destination bands
where data will be placed. As well as for the setSourceBands settings, the
same checks on destination bands selections is performed at the time of
reading. The javadoc also states that a destination band subset will not
affect the number of bands in the output image of a read if no destination
image is specified. If I need a different number of bands in the destination
image, I need to specify the destination using the setDestination method.
- setDestination allows to specify a BufferedImage which will be used as the
destination for pixel data.
Quoting from Javadoc: "*At the time of reading, the image is checked to
verify that its ColorModel and SampleModel correspond to one of the
ImageTypeSpecifiers returned from the ImageReader's getImageTypes method. If
it does not, the reader will throw an IIOException*".
actually, my ColorModel and SampleModel are set from the underlying
datasource. Basically, I get the number of bands, the image sizes and (if
available) a color interpretation and I setup a proper colorModel and
sampleModel which are used to build up a single ImageTypeSpecifier.
At this point, my silly question is:
"what is the right way to manage all of these parameters?"
Is the following set of rules correct? -->
1) check for sourceBands; if defined, check for destinationBands. In case
the number of bands is not the same, throws an error.
2) in case the numSpecifiedSourceBands==numSpecifiedDestinationBands check
for a destination image.
3a) if destination is not provided, build a new BufferedImage having the
same sampleModel and colorModel of the original image and just "Fill
somehow" the destination bands with the data coming from the read operation
(using srcBands/destBands settings). Wrong uses of the newly generated
destination image may throw errors (As an instance, in case someone attempts
to get data from Bands which are never used as destination... Right?)
3b) if destination has been defined, check if its sampleModel and colorModel
are supported in the imageTypeSpecifier list.
NOTE: My GenericImageReader should support a wide range of dataTypes,
sampleModels, colorModels. Could I avoid the imageTypeSpecifier check? In
such a case, should I directly use the provided destination image?
I have taken a look on the jai-imageio-core plugins source code and I have
noticed that some formats use an approach while some others use a different
Sorry about my confusion but I have some doubts on "when I need to perform
coherency checks instead of assuming a 'default behaviour' " and how to
Any help would be greatly appreciated. Thank you very much
Eng. Daniele Romagnoli
Via Carignoni 51
55041 Camaiore (LU)
phone: +39 0584983027
fax: +39 0584983027
mob: +39 328 0559267