Skip to main content

[JAI-IMAGEIO] Question about ImageReadParam.setSourceBands/setDestinationBands/setDestination methods and how to handle them

2 replies [Last post]
Anonymous

Hi list,
I'm extending our ImageIO-Ext project and the actual step is supporting
setSourceBands settings in the ImageReadParam used as argument of the
ImageReader.read(...) method.
Looking at ImageReader/IIOParam/ImageReadParam javadocs I have collected the
following information.

- 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
one.

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
"convert/subset" sampleModels.
Any help would be greatly appreciated. Thank you very much

Best Regards,
Daniele Romagnoli

--
-------------------------------------------------------
Eng. Daniele Romagnoli
Software Engineer

GeoSolutions S.A.S.
Via Carignoni 51
55041 Camaiore (LU)
Italy

phone: +39 0584983027
fax: +39 0584983027
mob: +39 328 0559267

http://www.geo-solutions.it

-------------------------------------------------------
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

Hi Daniele,

On Tue, 22 Jan 2008, Daniele Romagnoli wrote:

> I'm extending our ImageIO-Ext project and the actual step is supporting
> setSourceBands settings in the ImageReadParam used as argument of the
> ImageReader.read(...) method.
> Looking at ImageReader/IIOParam/ImageReadParam javadocs I have collected the
> following information.
>
> - 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.

This does indeed all seem rather confusing ...

> At this point, my silly question is:
> "what is the right way to manage all of these parameters?"

I don't think that there is any particular correct sequence per se of checks
to be performed to determine whether the supplied settings of the IIOParam are
correct.

> 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.

The specification states that an IllegalArgumentException and not an Error
would be thrown. If the number of destination bands is not explicitly
specified it defaults to the number of bands in the destination image which
will be created.

> 2) in case the numSpecifiedSourceBands==numSpecifiedDestinationBands check
> for a destination image.

You might need to check for a destination image in order to be able to do this
check as the number of destination bands defaults to the number of bands in
the destination if not given via the param.

> 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?)

There is no original image: you are loading data from a stream. The closest
thing to "original image" would be an image created from the
ImageTypeSpecifier returned by ImageReader.getRawImageType().

> 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?

The ImageTypeSpecifier check is mandated by the specification.

> 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
> one.

As various plugins were implemented by various people then different
approaches might have been used but if they adhere to the specification they
can all be legal.

> 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
> "convert/subset" sampleModels.
> Any help would be greatly appreciated. Thank you very much

Perhaps some more specific examples would be more enlightening. Do you have
any?

Brian

>^..^< >^..^<

Brian Burkhalter
Java Imaging and Video
Sun Microsystems, Inc.

This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

Daniele Romagnoli

On Jan 23, 2008 2:46 AM, Brian Burkhalter wrote:

> Hi Daniele,
>
> On Tue, 22 Jan 2008, Daniele Romagnoli wrote:
>
> > I'm extending our ImageIO-Ext project and the actual step is supporting
> > setSourceBands settings in the ImageReadParam used as argument of the
> > ImageReader.read(...) method.
> > Looking at ImageReader/IIOParam/ImageReadParam javadocs I have collected
> the
> > following information.
> >
> > - 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.
>
> This does indeed all seem rather confusing ...
>
> > At this point, my silly question is:
> > "what is the right way to manage all of these parameters?"
>
> I don't think that there is any particular correct sequence per se of
> checks
> to be performed to determine whether the supplied settings of the IIOParam
> are
> correct.
>
> > 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.
>
> The specification states that an IllegalArgumentException and not an Error
> would be thrown.

Oh sure. I mean IllegalArgumentException. I know this is not the same
thing... sorry

> If the number of destination bands is not explicitly
> specified it defaults to the number of bands in the destination image
> which
> will be created.
>
> > 2) in case the numSpecifiedSourceBands==numSpecifiedDestinationBands
> check
> > for a destination image.
>
> You might need to check for a destination image in order to be able to do
> this
> check as the number of destination bands defaults to the number of bands
> in
> the destination if not given via the param.

Ok. Thanks for this correction. PS: In the meantime I have added support for
the setDestinationType method which should override the setting performed by
the setDestination method... right?
For this reason I guess I need to check for a destinationType and if not
defined, for a destinationImage. Finally if the destinationImage has not
been defined, is the (3a) rule correct? (See also my answer about the wrong
term "original 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?)
>
> There is no original image: you are loading data from a stream. The
> closest
> thing to "original image" would be an image created from the
> ImageTypeSpecifier returned by ImageReader.getRawImageType().

You are right. I have said "original image" since my ImageReader plugins are
built on top of GDAL which allows to know in advance the datatype, the
number of bands, the data sizes (width,height), the colorInterpretation and
several other basic properties of a datasource (represented as a dataset).
For this reason I have wrongly called "original image" an image with the
same properties of a Gdal Dataset opened on the underlying datasource.

>
> > 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?
>
> The ImageTypeSpecifier check is mandated by the specification.
>
> > 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
> > one.
>
> As various plugins were implemented by various people then different
> approaches might have been used but if they adhere to the specification
> they
> can all be legal.
>
> > 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
> > "convert/subset" sampleModels.
> > Any help would be greatly appreciated. Thank you very much
>
> Perhaps some more specific examples would be more enlightening. Do you
> have
> any?

Basically, the ImageIO-Ext framework is composed of a main core, called
GDALFramework which is basically composed of abstracts
GDALImageReader(+SPI)/GDALImageWriter(+SPI) extending ImageIO classes
(ImageReader/ImageWriter), IIOMetadata subclasses and other auxiliary
classes. Defining a format specific plugin (such as, as an instance, JPEG,
ECW, JP2K,...) simply requires to customize a little a specific SPI
subclass. The "big" data access work is performed by the
GDALImageReader/GDALImageWriter classes which are very general purpose in
order to grant access to "any" data available from an underlying datasource
(leveraging on the GDAL data access capabilities). As an instance, I can
work on a datasource containing a dataset with 10 Bands of type Byte. An
user could need, as an instance, an image containing a single band as well
as all 10 (to be written in another format) as well as 3 of them to obtain
an RGB image. Due to the wide number of different cases, we have defined
only an imageTypeSpecifier compliant to what I have previously called the
"Original Image". Should I need to define any possible imageTypeSpecifier?
(As an instance, for the datasource of the example, defining a "1band"
imageTypeSpecifier, a "2band" imageTypeSpecifier, and so on, ...)

Thanks for your great help.
Best Regards,
Daniele

>
>
> Brian
>
> >^..^< >^..^<
>
> Brian Burkhalter
> Java Imaging and Video
> Sun Microsystems, Inc.
>
> This email message is for the sole use of the intended recipient(s)
> and may contain confidential and privileged information. Any
> unauthorized review, use, disclosure or distribution is prohibited.
> If you are not the intended recipient, please contact the sender by
> reply email and destroy all copies of the original message.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

--
-------------------------------------------------------
Eng. Daniele Romagnoli
Software Engineer

GeoSolutions S.A.S.
Via Carignoni 51
55041 Camaiore (LU)
Italy

phone: +39 0584983027
fax: +39 0584983027
mob: +39 328 0559267

http://www.geo-solutions.it

-------------------------------------------------------
[att1.html]