Skip to main content

javax.imageio.ImageReader.readRaster(..) returns raster with no color info.

13 replies [Last post]
demonduck
Offline
Joined: 2008-03-14

javax.imageio.ImageReader.readRaster(0,null) will read a Raster about
10-20% faster than javax.imageio.ImageReader.read(0) will read a
BufferedImage. Not much unless one intends to do it on the order
of 100 times -- O(2). Which I do intend to do in a tiling applet.

But readRaster() returns a Raster with no ColorModel or ColorSpace
information. The docs say:

/**
* Returns a new Raster object containing the raw pixel data
* from the image stream, without any color conversion applied. The
* application must determine how to interpret the pixel data by other
* means. Any destination or image-type parameters in the supplied
* ImageReadParam object are ignored, but all other
* parameters are used exactly as in the read(}
.
* This method allows formats that normally apply a color
* conversion, such as JPEG, and formats that do not normally have an
* associated colorspace, such as remote sensing or medical imaging data,
* to provide access to raw pixel data.

So that would be ok for my use. I could apply a ColorConvertOp to get the pixel
data into the color space that I wanted.

NOPE!

The only ColorConvertOp that works on Rasters needs a src ColorSpace and a dest
ColorSpace.

public ColorConvertOp(ColorSpace srcCspace,
ColorSpace dstCspace,
RenderingHints hints)

The problem is you can't know what the src ColorSpace is just from inspecting
the Raster returned by readRaster().

So the question:

How do I find out what the ColorSpace is of the Raster returned by readRaster()?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
demonduck
Offline
Joined: 2008-03-14

NO WAIT! No, that wouldn't work. The ColorConvertOp needs the ColorSpace of the src -- which is the Raster. And we don't know the ColorSpace of the Raster...

jxc
Offline
Joined: 2005-02-24

How about this?

http://java.sun.com/javase/6/docs/api/javax/imageio/ImageReader.html#getRawImageType(int)

Nidel, Mike

can you get the RenderedImage first using readAsRenderedImage(), get the
ColorModel/ColorSpace from there just 1 time, then do the rest of your
tiling from that point?

> -----Original Message-----
> From: Discussion list for Java 2D API
> [mailto:JAVA2D-INTEREST@JAVA.SUN.COM] On Behalf Of
> java2d@JAVADESKTOP.ORG
> Sent: Tuesday, June 17, 2008 3:38 PM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Subject: [JAVA2D] javax.imageio.ImageReader.readRaster(..)
> returns raster with no color info.
>
> javax.imageio.ImageReader.readRaster(0,null) will read a
> Raster about 10-20% faster than
> javax.imageio.ImageReader.read(0) will read a BufferedImage.
> Not much unless one intends to do it on the order of 100
> times -- O(2). Which I do intend to do in a tiling applet.
>
> But readRaster() returns a Raster with no ColorModel or
> ColorSpace information. The docs say:
>
> /**
> * Returns a new Raster object containing the raw pixel data
> * from the image stream, without any color conversion
> applied. The
> * application must determine how to interpret the pixel
> data by other
> * means. Any destination or image-type parameters in
> the supplied
> * ImageReadParam object are ignored, but all other
> * parameters are used exactly as in the read(} .
> * This method allows formats that normally apply a color
> * conversion, such as JPEG, and formats that do not
> normally have an
> * associated colorspace, such as remote sensing or
> medical imaging data,
> * to provide access to raw pixel data.
>
> So that would be ok for my use. I could apply a
> ColorConvertOp to get the pixel data into the color space
> that I wanted.
>
> NOPE!
>
> The only ColorConvertOp that works on Rasters needs a src
> ColorSpace and a dest ColorSpace.
>
> public ColorConvertOp(ColorSpace srcCspace,
> ColorSpace dstCspace,
> RenderingHints hints)
>
> The problem is you can't know what the src ColorSpace is just
> from inspecting the Raster returned by readRaster().
>
> So the question:
>
> How do I find out what the ColorSpace is of the Raster
> returned by readRaster()?
> [Message sent by forum member 'demonduck' (demonduck)]
>
> http://forums.java.net/jive/thread.jspa?messageID=280814
>
> ==============================================================
> =============
> To unsubscribe, send email to listserv@java.sun.com and
> include in the body of the message "signoff JAVA2D-INTEREST".
> For general help, send email to listserv@java.sun.com and
> include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

demonduck
Offline
Joined: 2008-03-14

> can you get the RenderedImage first using
> readAsRenderedImage(), get the
> ColorModel/ColorSpace from there just 1 time, then do
> the rest of your
> tiling from that point?
>

ummm yes, could do that... That would work ok.

But it would be a good thing to know how to find out what the color space of the Raster is though. Might be useful later -- or now.

Nidel, Mike

well a Raster just doesn't have a color space. that's an interpretation
thing...

another thing you could do is read the first tile as a buffered image
and
get the color space, then read the rest as Rasters and reuse the color
space.
but i don't like that solution much...

> -----Original Message-----
> From: Discussion list for Java 2D API
> [mailto:JAVA2D-INTEREST@JAVA.SUN.COM] On Behalf Of
> java2d@JAVADESKTOP.ORG
> Sent: Tuesday, June 17, 2008 4:07 PM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Subject: Re: [JAVA2D]
> javax.imageio.ImageReader.readRaster(..) returns raster wi
>
> > can you get the RenderedImage first using
> readAsRenderedImage(), get
> > the ColorModel/ColorSpace from there just 1 time, then do
> the rest of
> > your tiling from that point?
> >
>
> ummm yes, could do that... That would work ok.
>
> But it would be a good thing to know how to find out what the
> color space of the Raster is though. Might be useful later -- or now.
> [Message sent by forum member 'demonduck' (demonduck)]
>
> http://forums.java.net/jive/thread.jspa?messageID=280825
>
> ==============================================================
> =============
> To unsubscribe, send email to listserv@java.sun.com and
> include in the body of the message "signoff JAVA2D-INTEREST".
> For general help, send email to listserv@java.sun.com and
> include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

demonduck
Offline
Joined: 2008-03-14

The Raster has bytes that represent the colors of the pixels. But the bytes are not in the RGB or sRGB space. They have to be converted from whatever they are to the RGB space.

So how do you do that if you don't know what colorspace the Raster bytes are in?

Do you shift left; right; up; down; subtract; add -- what do you do? That's the problem.

The conversion happens when a BufferedImage is read. So the information about the colorspace in the image is known. But that information is not available to a programmer writing an applet or app.

I can get the colorspace that the Raster *SHOULD* be converted to by using the getRawImageType to get an ImageTypeSpecifier but that only gives me a colorspace for the dest in the ColorConvertOp. I need a colorspace for the src -- the Raster.

jxc
Offline
Joined: 2005-02-24

It seems to me that getRawImageType was supposed to give the "raw" colorspace
(say YCbCr) for the src but for some reason it couldn't so it gave an RGB colorspace,
which makes it unreliable.

The following javadoc seems to be somewhat confusing:
http://java.sun.com/javase/6/docs/api/javax/imageio/ImageReader.html#getRawImageType(int)
http://java.sun.com/javase/6/docs/api/javax/imageio/ImageReader.html#getImageTypes(int)

The default implementation of getRawImageType doesn't seem to work for you.
There are some comments in JPEGImageReader.java saying that YCbCr colorspace
is not available so RGB has to be returned.

demonduck
Offline
Joined: 2008-03-14

> It seems to me that getRawImageType was supposed to
> give the "raw" colorspace
> (say YCbCr) for the src but for some reason it
> couldn't so it gave an RGB colorspace,
> which makes it unreliable.
>
> The following javadoc seems to be somewhat
> confusing:
> http://java.sun.com/javase/6/docs/api/javax/imageio/Im
> ageReader.html#getRawImageType(int)
> http://java.sun.com/javase/6/docs/api/javax/imageio/Im
> ageReader.html#getImageTypes(int)
>
> The default implementation of getRawImageType doesn't
> seem to work for you.
> There are some comments in JPEGImageReader.java
> saying that YCbCr colorspace
> is not available so RGB has to be returned.

I think that you are exactly right. That's what I think also.

The root of the problem is that readRaster(int, ImageReadParam) ignores the ImageTypeSpecifier's color and/or destination in the ImageReadParam.

If I could specify a ColorSpace or ColorModel in the ImageReadParam and that ColorSpace or ColorModel was used in the reading, then there would be no problem.

jxc
Offline
Joined: 2005-02-24

> The root of the problem is that readRaster(int,
> ImageReadParam) ignores the ImageTypeSpecifier's
> color and/or destination in the ImageReadParam.

I don't agree with you here. The javadoc of this method
clearly says that it returns raw pixel data without color
conversion applied, and it ignores image-type parameters.

> If I could specify a ColorSpace or ColorModel in the
> ImageReadParam and that ColorSpace or ColorModel was
> used in the reading, then there would be no problem.

Then you need read(int, ImageReadParam).

At beginning, you said that you'd like to use readRaster(0, null)
instead of read(0) because the former is 10 to 20% faster.
I guess it's faster because it skips the step of color conversion.
If you do color conversion with ColorConvertOp after
readRaster(0, null), the overall performance might not be
better than read(0) .

demonduck
Offline
Joined: 2008-03-14

> > The root of the problem is that readRaster(int,
> > ImageReadParam) ignores the ImageTypeSpecifier's
> > color and/or destination in the ImageReadParam.
>
> I don't agree with you here. The javadoc of this
> method
> clearly says that it returns raw pixel data without
> color
> conversion applied, and it ignores image-type
> parameters.
>
> > If I could specify a ColorSpace or ColorModel in
> the
> > ImageReadParam and that ColorSpace or ColorModel
> was
> > used in the reading, then there would be no
> problem.
>
> Then you need read(int, ImageReadParam).
>
> At beginning, you said that you'd like to use
> readRaster(0, null)
> instead of read(0) because the former is 10 to 20%
> faster.
> I guess it's faster because it skips the step of
> color conversion.
> If you do color conversion with ColorConvertOp after
> readRaster(0, null), the overall performance might
> not be
> better than read(0) .

I know what the docs say. That's how I know readRaster() ignores the ImageTypeSpecifier.

But if readRaster() did not ignore the ImageTypeSpecifier, then there would be no problem.

You might be right about the performance hit if color conversion takes place -- but we will never know for sure.

All we know for sure is that the only way to get the Raster is to make a BufferedImage using read(0) which guarantees a performance hit.

jxc
Offline
Joined: 2005-02-24

> All we know for sure is that the only way to get the
> Raster is to make a BufferedImage using read(0) which
> guarantees a performance hit.

I am not that sure. You seem to be asking for something
more than the Raster. :-) As Robert suggested at
http://forums.java.net/jive/thread.jspa?messageID=280813
you might be able to get the raw color space info from
image metadata.

demonduck
Offline
Joined: 2008-03-14

> > All we know for sure is that the only way to get
> the
> > Raster is to make a BufferedImage using read(0)
> which
> > guarantees a performance hit.
>
> I am not that sure. You seem to be asking for
> something
> more than the Raster. :-) As Robert suggested at
> http://forums.java.net/jive/thread.jspa?messageID=2808
> 13
> you might be able to get the raw color space info
> from
> image metadata.

Actually, I want less than the Raster. All I really want is the data in the dataBuffer. But I want it as an array of int's.

As for getting, "...the raw color space info from image metadata..." I would really appreciate some example code. I'm too stupid to figure out that one myself.

So if Robert is reading this, perhaps you could post a simple example of how to read the image metadata.

jxc
Offline
Joined: 2005-02-24