Skip to main content

[JAI-IMAGEIO] "RAW" ImageReader and grayscale images

12 replies [Last post]
Anonymous

Hi to everybody.

In my JAI Image I/O - based project, I have some 16-bit grayscale images
stored as short[] arrays containing the raw pixel values. In order to build
PlanarImage's starting from those short[] arrays, I was previously using a
code looking like this:

==========================

// This array contains pixel values for a 16-bit grayscale image
short[] pixelArray = ...;

Point origin = new Point();
DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
(imgWidth *
imgHeight),
arrayOffset);
int[] bandOffets = {0};
ComponentSampleModel sampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
WritableRaster raster = Raster.createWritableRaster(sampleModel,
dataBuffer, origin);

BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_USHORT_GRAY);
buffImg.setData(raster);
retImage = PlanarImage.wrapRenderedImage(buffImg);

==========================

Now I would like to use the "raw" ImageReader instead, which seems like a
more elegant approach. Here is my current code:

==========================

// This array contains pixel values for a 16-bit grayscale image (the
same image as the previous example)
short[] pixelArray = ...;

// TEMP: convert the pixel array to a byte array (BIG ENDIAN)
byte[] bytePixelArray = new byte[pixelArray.length * 2];
for (int i = 0; i < pixelArray.length; i++)
{
bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
}

// Create support variables for reading the RAW image
long[] imgOffsets = {0L};
int[] bandOffets = {0};
Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};

// Create Color Space, Color Model and Sample Model
ColorSpace imgColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
ComponentSampleModel imgSampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier(imgColorModel,
imgSampleModel);

// Create the Input Streams
ByteArrayInputStream byteIS = new
ByteArrayInputStream(bytePixelArray);
ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
imgTypeSpec, imgOffsets, imgDims);

// Get the RAW Image Reader
Iterator iter = ImageIO.getImageReadersByFormatName("raw");
ImageReader rawReader = null;
if (iter.hasNext())
{
rawReader = (ImageReader) iter.next();
}

// Read the image through the ImageRead JAI operator
if (rawReader != null)
{
ParameterBlock pb = new ParameterBlock();
pb.add(rawImgIS);
pb.add(new Integer(0));
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(null);
pb.add(null);
pb.add(null);
pb.add(rawReader);

retImage = JAI.create("ImageRead", pb);
}

==========================

In both cases, the PlanarImage (retImage) gets correctly created. The
strange thing is that I obtain a slightly different image in the two cases.
In particular, the image created in the second way appears slightly
"lighter" (i.e., higher brightness) than the image created with the first
code sample. Please notice that I am applying JAI's "rescale" operator
before displaying, in order to adjust contrast, but with the same "rescale"
operator, the same image created in the two ways above appears different.

Can you guess why? Do you see something wrong in my second code sample?

Any help/suggestion would be greatly appreciated.

Regards,

Marco.

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

Reply viewing options

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

On Fri, 14 Jul 2006, robert engels wrote:

> Although not the PRIMARY cause, a contributer is in BufferedImage.java.
>
> It is TOO restrictive in creating the correct image type for non-RGB color
> models.

[...]

I concur.

> So the only way to create a TYPE_USHORT_GRAY image using a supplied raster is
> by using a ShortComponentRaster.
>
> The RAWImageReader ends up using this method to create the returned image.
>
> I assume what is happening is that further down the in the image rendering
> code, there are optimized paths for grayscale images. This is probably
> determined by looking at the image type when the image is a BufferedImage.
> Since the image type is CUSTOM it probably uses a path that does color
> conversion based on the color space.

I think that is probably true.

> Anyway, to make the RAWImageReader work correctly, create the following
> sample model instead of a ComponentSampleModel and the proper BufferedImage
> will be created.
>
> SampleModel sampleModel = new
> PixelInterleavedSampleModel(DataBuffer.TYPE_USHORT,
> IMAGE_WIDTH,
> IMAGE_HEIGHT,
> 1,
> IMAGE_WIDTH,
> bandOffsets
> );

This approach does in fact solve the problem and is superior to using the
setDestination() method on the ImageReadParam as in the latter case if
'rightImage' is displayed it will still appear too light although if the
BufferedImage supplied to setDestination() is displayed it will not.

Brian

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
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

robert engels

Attached is an updated version that matches the RAWImageReader. I
also removed the JAI dependencies (not needed).

As to why it is different, it is puzzling, but it has to do with the
color space and/or the image display in Java2D... I added some code
to compare the pixel values - they are identical between the images
(including the alpha component).

Hopefully someone from Sun can shed some light here.



On Jul 11, 2006, at 12:33 PM, Marco Sambin - NeoLogica wrote:

> Hi Robert,
>
> thanks again for your reply and suggestion.
>
> I've just tried avoiding to explicitly create the ColorModel, as
> you showed in your last message, but the result is still the same!
> The two images are still different!
>
> I believe the RAW image reader shall not normalize of manipulate
> the pixel values during the read, at least not in an
> "uncontrollable" way...
>
> Any comment on this is highly appreciated.
>
> Regards,
>
> Marco.
>
>
> From: robert engels [mailto:rengels@ix.netcom.com]
> Sent: martedì 11 luglio 2006 19.23
> To: interest@jai-imageio.dev.java.net
> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>
> Not completely certain :) I just took the code from the
> Buffredimage.java
>
> Another idea is that the RAW image reader is normalizing the pixel
> values during the read.
>
> You might try avoiding creating the ColorModel altogether... and
> use the following ctor for the image input steam:
>
> public RawImageInputStream(ImageInputStream source,
> SampleModel sampleModel,
> long[] imageOffsets,
> Dimension[] imageDimensions)
>
> And just create a 1-banded ComponentSampleModel. According to the
> JavaDoc it will automatically be GRAYSCALE.
>
>
>
>
> On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:
>
>> Hi Robert,
>>
>> thank you for your suggestion. I tested it, but the result is
>> unchanged: the image obtained through the RAW image reader always
>> looks a bit brighter than the one created through the BufferedImage.
>>
>> Just one question. You suggested to create the ColorModel in this
>> way:
>>
>> =================
>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
>> true,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_USHORT);
>> =================
>>
>> So, you are passing "hasAlpha=false" and
>> "isAlphaPremultiplied=true". What's the reason for this?
>>
>> Thanks again for your help.
>>
>> Marco.
>>
>>
>> From: robert engels [mailto:rengels@ix.netcom.com]
>> Sent: martedì 11 luglio 2006 18.44
>> To: interest@jai-imageio.dev.java.net
>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>
>> In locking at the BufferedImage code, I think you want to create
>> the ColorModel as follows:
>>
>> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>> int[] nBits = {16};
>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
>> true,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_USHORT);
>>
>>
>>
>> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>>
>>> Hi.
>>>
>>> Did anybody have the chance to run the provided code sample on
>>> the test
>>> image? Any comments?
>>> Thank you in advance.
>>>
>>> Regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>> Sent: venerdì 7 luglio 2006 17.45
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>
>>> Hi to everybody.
>>>
>>> As a follow-up to my recent post (reported below), I built a self-
>>> contained
>>> test code sample which demonstrates the problem I am experiencing
>>> with the
>>> RAW ImageReader and 16-bit grayscale images. The code sample and
>>> a test
>>> image file are included in the attached ZIP archive.
>>>
>>> I would really appreciate if someone could take a look at this
>>> code sample,
>>> and let me know if you find something wrong in it, or if it may
>>> be a bug of
>>> the RAW ImageReader.
>>>
>>> Best regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>> Sent: giovedì 22 giugno 2006 10.12
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>
>>> Hi to everybody.
>>>
>>> In my JAI Image I/O - based project, I have some 16-bit grayscale
>>> images
>>> stored as short[] arrays containing the raw pixel values. In
>>> order to build
>>> PlanarImage's starting from those short[] arrays, I was
>>> previously using a
>>> code looking like this:
>>>
>>> ==========================
>>>
>>> // This array contains pixel values for a 16-bit grayscale
>>> image
>>> short[] pixelArray = ...;
>>>
>>> Point origin = new Point();
>>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>>> (imgWidth *
>>> imgHeight),
>>> arrayOffset);
>>> int[] bandOffets = {0};
>>> ComponentSampleModel sampleModel = new
>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>> imgWidth,
>>> imgHeight,
>>> 1,
>>> imgWidth,
>>> bandOffets
>>> );
>>> WritableRaster raster = Raster.createWritableRaster(sampleModel,
>>> dataBuffer, origin);
>>>
>>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>>> BufferedImage.TYPE_USHORT_GRAY);
>>> buffImg.setData(raster);
>>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>>
>>> ==========================
>>>
>>> Now I would like to use the "raw" ImageReader instead, which
>>> seems like a
>>> more elegant approach. Here is my current code:
>>>
>>> ==========================
>>>
>>> // This array contains pixel values for a 16-bit grayscale
>>> image (the
>>> same image as the previous example)
>>> short[] pixelArray = ...;
>>>
>>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>>> for (int i = 0; i < pixelArray.length; i++)
>>> {
>>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) &
>>> 0x00FF);
>>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>>> }
>>>
>>> // Create support variables for reading the RAW image
>>> long[] imgOffsets = {0L};
>>> int[] bandOffets = {0};
>>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>>
>>> // Create Color Space, Color Model and Sample Model
>>> ColorSpace imgColorSpace = ColorSpace.getInstance
>>> (ColorSpace.CS_GRAY);
>>> ColorModel imgColorModel = new ComponentColorModel
>>> (imgColorSpace,
>>> false,
>>> false,
>>> Transparency.OPAQUE,
>>> DataBuffer.TYPE_USHORT);
>>> ComponentSampleModel imgSampleModel = new
>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>> imgWidth,
>>> imgHeight,
>>> 1,
>>> imgWidth,
>>> bandOffets
>>> );
>>> ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier
>>> (imgColorModel,
>>> imgSampleModel);
>>>
>>> // Create the Input Streams
>>> ByteArrayInputStream byteIS = new
>>> ByteArrayInputStream(bytePixelArray);
>>> ImageInputStream imgIS = ImageIO.createImageInputStream
>>> (byteIS);
>>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>>> imgTypeSpec, imgOffsets, imgDims);
>>>
>>> // Get the RAW Image Reader
>>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>>> ImageReader rawReader = null;
>>> if (iter.hasNext())
>>> {
>>> rawReader = (ImageReader) iter.next();
>>> }
>>>
>>> // Read the image through the ImageRead JAI operator
>>> if (rawReader != null)
>>> {
>>> ParameterBlock pb = new ParameterBlock();
>>> pb.add(rawImgIS);
>>> pb.add(new Integer(0));
>>> pb.add(Boolean.FALSE);
>>> pb.add(Boolean.FALSE);
>>> pb.add(Boolean.FALSE);
>>> pb.add(null);
>>> pb.add(null);
>>> pb.add(null);
>>> pb.add(rawReader);
>>>
>>> retImage = JAI.create("ImageRead", pb);
>>> }
>>>
>>> ==========================
>>>
>>> In both cases, the PlanarImage (retImage) gets correctly created.
>>> The
>>> strange thing is that I obtain a slightly different image in the
>>> two cases.
>>> In particular, the image created in the second way appears slightly
>>> "lighter" (i.e., higher brightness) than the image created with
>>> the first
>>> code sample. Please notice that I am applying JAI's "rescale"
>>> operator
>>> before displaying, in order to adjust contrast, but with the same
>>> "rescale"
>>> operator, the same image created in the two ways above appears
>>> different.
>>>
>>> Can you guess why? Do you see something wrong in my second code
>>> sample?
>>>
>>> Any help/suggestion would be greatly appreciated.
>>>
>>> Regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>> imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-
>>> imageio.dev.java.net
>>>
>>>
>>>
>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>> imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-
>>> imageio.dev.java.net
>>>
>>
>

[att1.html]
[RawReaderTester.java]
[att1.html]

Brian Burkhalter

Although I have not identified the proximate cause for this problem I think
the ultimate cause is this bug

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5051418

but in this case for USHORT images rather than BYTE. If this is of interest to
you please vote for it and add your comments.

The basis for my conclusion is that if I add these lines

// XXX BEGIN
BufferedImage tmpImage = new
BufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY);

tmpImage.setData(rightImage.getRaster());

rightImage = tmpImage;
// XXX END

after line 112 (BufferedImage rightImage = rawReader.read(0);) then both
images are displayed the same.

A workaround is to pass in a destination via the ImageReadParam for example as

ImageReadParam readParam = rawReader.getDefaultReadParam();
readParam.setDestination(new
BufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY));
BufferedImage rightImage = rawReader.read(0, readParam);

This assures that the display code will recognize the type of the
BufferedImage.

If you think that we should attempt to modify the raw reader plug-in such that
this does not happen even if the Java 2D bug 5051418 is not fixed then please
file an issue against jai-imageio-core.

Brian

On Tue, 11 Jul 2006, robert engels wrote:

> Attached is an updated version that matches the RAWImageReader. I also
> removed the JAI dependencies (not needed).
>
> As to why it is different, it is puzzling, but it has to do with the color
> space and/or the image display in Java2D... I added some code to compare the
> pixel values - they are identical between the images (including the alpha
> component).
>
> Hopefully someone from Sun can shed some light here.
>
>
>
>
> On Jul 11, 2006, at 12:33 PM, Marco Sambin - NeoLogica wrote:
>
>> Hi Robert,
>>
>> thanks again for your reply and suggestion.
>>
>> I've just tried avoiding to explicitly create the ColorModel, as you showed
>> in your last message, but the result is still the same! The two images are
>> still different!
>>
>> I believe the RAW image reader shall not normalize of manipulate the pixel
>> values during the read, at least not in an "uncontrollable" way...
>>
>> Any comment on this is highly appreciated.
>>
>> Regards,
>>
>> Marco.
>>
>>
>> From: robert engels [mailto:rengels@ix.netcom.com]
>> Sent: martedì 11 luglio 2006 19.23
>> To: interest@jai-imageio.dev.java.net
>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>
>> Not completely certain :) I just took the code from the Buffredimage.java
>>
>> Another idea is that the RAW image reader is normalizing the pixel values
>> during the read.
>>
>> You might try avoiding creating the ColorModel altogether... and use the
>> following ctor for the image input steam:
>>
>> public RawImageInputStream(ImageInputStream source,
>> SampleModel sampleModel,
>> long[] imageOffsets,
>> Dimension[] imageDimensions)
>>
>> And just create a 1-banded ComponentSampleModel. According to the JavaDoc
>> it will automatically be GRAYSCALE.
>>
>>
>>
>>
>> On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:
>>
>>> Hi Robert,
>>>
>>> thank you for your suggestion. I tested it, but the result is unchanged:
>>> the image obtained through the RAW image reader always looks a bit
>>> brighter than the one created through the BufferedImage.
>>>
>>> Just one question. You suggested to create the ColorModel in this way:
>>>
>>> =================
>>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
>>> Transparency.OPAQUE,
>>> DataBuffer.TYPE_USHORT);
>>> =================
>>>
>>> So, you are passing "hasAlpha=false" and "isAlphaPremultiplied=true".
>>> What's the reason for this?
>>>
>>> Thanks again for your help.
>>>
>>> Marco.
>>>
>>>
>>> From: robert engels [mailto:rengels@ix.netcom.com]
>>> Sent: martedì 11 luglio 2006 18.44
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>
>>> In locking at the BufferedImage code, I think you want to create the
>>> ColorModel as follows:
>>>
>>> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>>> int[] nBits = {16};
>>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
>>> Transparency.OPAQUE,
>>> DataBuffer.TYPE_USHORT);
>>>
>>>
>>>
>>> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>>>
>>>> Hi.
>>>>
>>>> Did anybody have the chance to run the provided code sample on the test
>>>> image? Any comments?
>>>> Thank you in advance.
>>>>
>>>> Regards,
>>>>
>>>> Marco.
>>>>
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>> Sent: venerdì 7 luglio 2006 17.45
>>>> To: interest@jai-imageio.dev.java.net
>>>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>
>>>> Hi to everybody.
>>>>
>>>> As a follow-up to my recent post (reported below), I built a
>>>> self-contained
>>>> test code sample which demonstrates the problem I am experiencing with
>>>> the
>>>> RAW ImageReader and 16-bit grayscale images. The code sample and a test
>>>> image file are included in the attached ZIP archive.
>>>>
>>>> I would really appreciate if someone could take a look at this code
>>>> sample,
>>>> and let me know if you find something wrong in it, or if it may be a bug
>>>> of
>>>> the RAW ImageReader.
>>>>
>>>> Best regards,
>>>>
>>>> Marco.
>>>>
>>>>
>>>>
>>>> -----Original Message-----
>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>> Sent: giovedì 22 giugno 2006 10.12
>>>> To: interest@jai-imageio.dev.java.net
>>>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>
>>>> Hi to everybody.
>>>>
>>>> In my JAI Image I/O - based project, I have some 16-bit grayscale images
>>>> stored as short[] arrays containing the raw pixel values. In order to
>>>> build
>>>> PlanarImage's starting from those short[] arrays, I was previously using
>>>> a
>>>> code looking like this:
>>>>
>>>> ==========================
>>>>
>>>> // This array contains pixel values for a 16-bit grayscale image
>>>> short[] pixelArray = ...;
>>>>
>>>> Point origin = new Point();
>>>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>>>> (imgWidth *
>>>> imgHeight),
>>>> arrayOffset);
>>>> int[] bandOffets = {0};
>>>> ComponentSampleModel sampleModel = new
>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>> imgWidth,
>>>> imgHeight,
>>>> 1,
>>>> imgWidth,
>>>> bandOffets
>>>> );
>>>> WritableRaster raster = Raster.createWritableRaster(sampleModel,
>>>> dataBuffer, origin);
>>>>
>>>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>>>> BufferedImage.TYPE_USHORT_GRAY);
>>>> buffImg.setData(raster);
>>>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>>>
>>>> ==========================
>>>>
>>>> Now I would like to use the "raw" ImageReader instead, which seems like a
>>>> more elegant approach. Here is my current code:
>>>>
>>>> ==========================
>>>>
>>>> // This array contains pixel values for a 16-bit grayscale image
>>>> (the
>>>> same image as the previous example)
>>>> short[] pixelArray = ...;
>>>>
>>>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>>>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>>>> for (int i = 0; i < pixelArray.length; i++)
>>>> {
>>>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
>>>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>>>> }
>>>>
>>>> // Create support variables for reading the RAW image
>>>> long[] imgOffsets = {0L};
>>>> int[] bandOffets = {0};
>>>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>>>
>>>> // Create Color Space, Color Model and Sample Model
>>>> ColorSpace imgColorSpace =
>>>> ColorSpace.getInstance(ColorSpace.CS_GRAY);
>>>> ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
>>>> false,
>>>> false,
>>>> Transparency.OPAQUE,
>>>> DataBuffer.TYPE_USHORT);
>>>> ComponentSampleModel imgSampleModel = new
>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>> imgWidth,
>>>> imgHeight,
>>>> 1,
>>>> imgWidth,
>>>> bandOffets
>>>> );
>>>> ImageTypeSpecifier imgTypeSpec = new
>>>> ImageTypeSpecifier(imgColorModel,
>>>> imgSampleModel);
>>>>
>>>> // Create the Input Streams
>>>> ByteArrayInputStream byteIS = new
>>>> ByteArrayInputStream(bytePixelArray);
>>>> ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
>>>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>>>> imgTypeSpec, imgOffsets, imgDims);
>>>>
>>>> // Get the RAW Image Reader
>>>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>>>> ImageReader rawReader = null;
>>>> if (iter.hasNext())
>>>> {
>>>> rawReader = (ImageReader) iter.next();
>>>> }
>>>>
>>>> // Read the image through the ImageRead JAI operator
>>>> if (rawReader != null)
>>>> {
>>>> ParameterBlock pb = new ParameterBlock();
>>>> pb.add(rawImgIS);
>>>> pb.add(new Integer(0));
>>>> pb.add(Boolean.FALSE);
>>>> pb.add(Boolean.FALSE);
>>>> pb.add(Boolean.FALSE);
>>>> pb.add(null);
>>>> pb.add(null);
>>>> pb.add(null);
>>>> pb.add(rawReader);
>>>>
>>>> retImage = JAI.create("ImageRead", pb);
>>>> }
>>>>
>>>> ==========================
>>>>
>>>> In both cases, the PlanarImage (retImage) gets correctly created. The
>>>> strange thing is that I obtain a slightly different image in the two
>>>> cases.
>>>> In particular, the image created in the second way appears slightly
>>>> "lighter" (i.e., higher brightness) than the image created with the first
>>>> code sample. Please notice that I am applying JAI's "rescale" operator
>>>> before displaying, in order to adjust contrast, but with the same
>>>> "rescale"
>>>> operator, the same image created in the two ways above appears different.
>>>>
>>>> Can you guess why? Do you see something wrong in my second code sample?
>>>>
>>>> Any help/suggestion would be greatly appreciated.
>>>>
>>>> Regards,
>>>>
>>>> Marco.
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>>
>>>
>>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
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

robert engels

Although not the PRIMARY cause, a contributer is in BufferedImage.java.

It is TOO restrictive in creating the correct image type for non-RGB
color models.

The code is

if (csType != ColorSpace.TYPE_RGB) {
if (csType == ColorSpace.TYPE_GRAY
&& cm instanceof ComponentColorModel) {
// Check if this might be a child raster (fix for
bug 4240596)
if (sm instanceof ComponentSampleModel &&
((ComponentSampleModel)sm).getPixelStride() !=
numBands) {
imageType = TYPE_CUSTOM;
} else if (raster instanceof ByteComponentRaster &&
raster.getNumBands() == 1 &&
cm.getComponentSize(0) == 8 &&
((ByteComponentRaster)raster).getPixelStride
() == 1) {
imageType = TYPE_BYTE_GRAY;
} else if (raster instanceof ShortComponentRaster &&
raster.getNumBands() == 1 &&
cm.getComponentSize(0) == 16 &&
((ShortComponentRaster)raster).getPixelStride
() == 1) {
imageType = TYPE_USHORT_GRAY;
}
} else {
imageType = TYPE_CUSTOM;
}
return;
}

So the only way to create a TYPE_USHORT_GRAY image using a supplied
raster is by using a ShortComponentRaster.

The RAWImageReader ends up using this method to create the returned
image.

I assume what is happening is that further down the in the image
rendering code, there are optimized paths for grayscale images. This
is probably determined by looking at the image type when the image is
a BufferedImage. Since the image type is CUSTOM it probably uses a
path that does color conversion based on the color space.

Anyway, to make the RAWImageReader work correctly, create the
following sample model instead of a ComponentSampleModel and the
proper BufferedImage will be created.

SampleModel sampleModel = new PixelInterleavedSampleModel
(DataBuffer.TYPE_USHORT,
IMAGE_WIDTH,
IMAGE_HEIGHT,
1,
IMAGE_WIDTH,
bandOffsets
);

Hope this helps,
Robert

On Jul 13, 2006, at 7:17 PM, Brian Burkhalter wrote:

> Although I have not identified the proximate cause for this problem
> I think the ultimate cause is this bug
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5051418
>
> but in this case for USHORT images rather than BYTE. If this is of
> interest to you please vote for it and add your comments.
>
> The basis for my conclusion is that if I add these lines
>
> // XXX BEGIN
> BufferedImage tmpImage = new BufferedImage
> (IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY);
>
> tmpImage.setData(rightImage.getRaster());
>
> rightImage = tmpImage;
> // XXX END
>
> after line 112 (BufferedImage rightImage = rawReader.read(0);) then
> both images are displayed the same.
>
> A workaround is to pass in a destination via the ImageReadParam for
> example as
>
> ImageReadParam readParam = rawReader.getDefaultReadParam();
> readParam.setDestination(new BufferedImage
> (IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY));
> BufferedImage rightImage = rawReader.read(0, readParam);
>
> This assures that the display code will recognize the type of the
> BufferedImage.
>
> If you think that we should attempt to modify the raw reader plug-
> in such that this does not happen even if the Java 2D bug 5051418
> is not fixed then please file an issue against jai-imageio-core.
>
> Brian
>
> On Tue, 11 Jul 2006, robert engels wrote:
>
>> Attached is an updated version that matches the RAWImageReader. I
>> also removed the JAI dependencies (not needed).
>>
>> As to why it is different, it is puzzling, but it has to do with
>> the color space and/or the image display in Java2D... I added some
>> code to compare the pixel values - they are identical between the
>> images (including the alpha component).
>>
>> Hopefully someone from Sun can shed some light here.
>>
>>
>>
>>
>> On Jul 11, 2006, at 12:33 PM, Marco Sambin - NeoLogica wrote:
>>
>>> Hi Robert,
>>> thanks again for your reply and suggestion.
>>> I've just tried avoiding to explicitly create the ColorModel, as
>>> you showed in your last message, but the result is still the
>>> same! The two images are still different!
>>> I believe the RAW image reader shall not normalize of manipulate
>>> the pixel values during the read, at least not in an
>>> "uncontrollable" way...
>>> Any comment on this is highly appreciated.
>>> Regards,
>>> Marco.
>>> From: robert engels [mailto:rengels@ix.netcom.com]
>>> Sent: martedì 11 luglio 2006 19.23
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>> Not completely certain :) I just took the code from the
>>> Buffredimage.java
>>> Another idea is that the RAW image reader is normalizing the
>>> pixel values during the read.
>>> You might try avoiding creating the ColorModel altogether... and
>>> use the following ctor for the image input steam:
>>> public RawImageInputStream(ImageInputStream source,
>>> SampleModel sampleModel,
>>> long[] imageOffsets,
>>> Dimension[] imageDimensions)
>>> And just create a 1-banded ComponentSampleModel. According to the
>>> JavaDoc it will automatically be GRAYSCALE.
>>> On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:
>>>> Hi Robert,
>>>> thank you for your suggestion. I tested it, but the result is
>>>> unchanged: the image obtained through the RAW image reader
>>>> always looks a bit brighter than the one created through the
>>>> BufferedImage.
>>>> Just one question. You suggested to create the ColorModel in
>>>> this way:
>>>> =================
>>>> ColorModel colorModel = new ComponentColorModel(cs, nBits,
>>>> false, true,
>>>> Transparency.OPAQUE,
>>>> DataBuffer.TYPE_USHORT);
>>>> =================
>>>> So, you are passing "hasAlpha=false" and
>>>> "isAlphaPremultiplied=true". What's the reason for this?
>>>> Thanks again for your help.
>>>> Marco.
>>>> From: robert engels [mailto:rengels@ix.netcom.com]
>>>> Sent: martedì 11 luglio 2006 18.44
>>>> To: interest@jai-imageio.dev.java.net
>>>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>> In locking at the BufferedImage code, I think you want to create
>>>> the ColorModel as follows:
>>>> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>>>> int[] nBits = {16};
>>>> ColorModel colorModel = new ComponentColorModel(cs, nBits,
>>>> false, true,
>>>> Transparency.OPAQUE,
>>>> DataBuffer.TYPE_USHORT);
>>>> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>>>>> Hi.
>>>>> Did anybody have the chance to run the provided code sample on
>>>>> the test
>>>>> image? Any comments?
>>>>> Thank you in advance.
>>>>> Regards,
>>>>> Marco.
>>>>> -----Original Message-----
>>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>>> Sent: venerdì 7 luglio 2006 17.45
>>>>> To: interest@jai-imageio.dev.java.net
>>>>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>> Hi to everybody.
>>>>> As a follow-up to my recent post (reported below), I built a
>>>>> self-contained
>>>>> test code sample which demonstrates the problem I am
>>>>> experiencing with the
>>>>> RAW ImageReader and 16-bit grayscale images. The code sample
>>>>> and a test
>>>>> image file are included in the attached ZIP archive.
>>>>> I would really appreciate if someone could take a look at this
>>>>> code sample,
>>>>> and let me know if you find something wrong in it, or if it may
>>>>> be a bug of
>>>>> the RAW ImageReader.
>>>>> Best regards,
>>>>> Marco.
>>>>> -----Original Message-----
>>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>>> Sent: giovedì 22 giugno 2006 10.12
>>>>> To: interest@jai-imageio.dev.java.net
>>>>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>> Hi to everybody.
>>>>> In my JAI Image I/O - based project, I have some 16-bit
>>>>> grayscale images
>>>>> stored as short[] arrays containing the raw pixel values. In
>>>>> order to build
>>>>> PlanarImage's starting from those short[] arrays, I was
>>>>> previously using a
>>>>> code looking like this:
>>>>> ==========================
>>>>>
>>>>> // This array contains pixel values for a 16-bit grayscale
>>>>> image
>>>>> short[] pixelArray = ...;
>>>>>
>>>>> Point origin = new Point();
>>>>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>>>>> (imgWidth *
>>>>> imgHeight),
>>>>> arrayOffset);
>>>>> int[] bandOffets = {0};
>>>>> ComponentSampleModel sampleModel = new
>>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>>> imgWidth,
>>>>> imgHeight,
>>>>> 1,
>>>>> imgWidth,
>>>>> bandOffets
>>>>> );
>>>>> WritableRaster raster = Raster.createWritableRaster
>>>>> (sampleModel,
>>>>> dataBuffer, origin);
>>>>>
>>>>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>>>>> BufferedImage.TYPE_USHORT_GRAY);
>>>>> buffImg.setData(raster);
>>>>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>>>> ==========================
>>>>> Now I would like to use the "raw" ImageReader instead, which
>>>>> seems like a
>>>>> more elegant approach. Here is my current code:
>>>>> ==========================
>>>>>
>>>>> // This array contains pixel values for a 16-bit grayscale
>>>>> image (the
>>>>> same image as the previous example)
>>>>> short[] pixelArray = ...;
>>>>>
>>>>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>>>>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>>>>> for (int i = 0; i < pixelArray.length; i++)
>>>>> {
>>>>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) &
>>>>> 0x00FF);
>>>>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>>>>> }
>>>>>
>>>>> // Create support variables for reading the RAW image
>>>>> long[] imgOffsets = {0L};
>>>>> int[] bandOffets = {0};
>>>>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>>>>
>>>>> // Create Color Space, Color Model and Sample Model
>>>>> ColorSpace imgColorSpace = ColorSpace.getInstance
>>>>> (ColorSpace.CS_GRAY);
>>>>> ColorModel imgColorModel = new ComponentColorModel
>>>>> (imgColorSpace,
>>>>> false,
>>>>> false,
>>>>> Transparency.OPAQUE,
>>>>> DataBuffer.TYPE_USHORT);
>>>>> ComponentSampleModel imgSampleModel = new
>>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>>> imgWidth,
>>>>> imgHeight,
>>>>> 1,
>>>>> imgWidth,
>>>>> bandOffets
>>>>> );
>>>>> ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier
>>>>> (imgColorModel,
>>>>> imgSampleModel);
>>>>>
>>>>> // Create the Input Streams
>>>>> ByteArrayInputStream byteIS = new
>>>>> ByteArrayInputStream(bytePixelArray);
>>>>> ImageInputStream imgIS = ImageIO.createImageInputStream
>>>>> (byteIS);
>>>>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>>>>> imgTypeSpec, imgOffsets, imgDims);
>>>>>
>>>>> // Get the RAW Image Reader
>>>>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>>>>> ImageReader rawReader = null;
>>>>> if (iter.hasNext())
>>>>> {
>>>>> rawReader = (ImageReader) iter.next();
>>>>> }
>>>>>
>>>>> // Read the image through the ImageRead JAI operator
>>>>> if (rawReader != null)
>>>>> {
>>>>> ParameterBlock pb = new ParameterBlock();
>>>>> pb.add(rawImgIS);
>>>>> pb.add(new Integer(0));
>>>>> pb.add(Boolean.FALSE);
>>>>> pb.add(Boolean.FALSE);
>>>>> pb.add(Boolean.FALSE);
>>>>> pb.add(null);
>>>>> pb.add(null);
>>>>> pb.add(null);
>>>>> pb.add(rawReader);
>>>>>
>>>>> retImage = JAI.create("ImageRead", pb);
>>>>> }
>>>>> ==========================
>>>>> In both cases, the PlanarImage (retImage) gets correctly
>>>>> created. The
>>>>> strange thing is that I obtain a slightly different image in
>>>>> the two cases.
>>>>> In particular, the image created in the second way appears
>>>>> slightly
>>>>> "lighter" (i.e., higher brightness) than the image created with
>>>>> the first
>>>>> code sample. Please notice that I am applying JAI's "rescale"
>>>>> operator
>>>>> before displaying, in order to adjust contrast, but with the
>>>>> same "rescale"
>>>>> operator, the same image created in the two ways above appears
>>>>> different.
>>>>> Can you guess why? Do you see something wrong in my second code
>>>>> sample?
>>>>> Any help/suggestion would be greatly appreciated.
>>>>> Regards,
>>>>> Marco.
>>>>> ------------------------------------------------------------------
>>>>> ---
>>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>>>> imageio.dev.java.net
>>>>> For additional commands, e-mail: interest-help@jai-
>>>>> imageio.dev.java.net
>>>>> ------------------------------------------------------------------
>>>>> ---
>>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>>>> imageio.dev.java.net
>>>>> For additional commands, e-mail: interest-help@jai-
>>>>> imageio.dev.java.net
>>
>
> ----------------
> Brian Burkhalter
> Java Media, Imaging, and Graphics
> 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

[att1.html]

Brian Burkhalter

Robert,

Thanks for your suggestion.

Brian

On Fri, 14 Jul 2006, robert engels wrote:

> Although not the PRIMARY cause, a contributer is in BufferedImage.java.
>
> It is TOO restrictive in creating the correct image type for non-RGB color
> models.
>
> The code is
>
> if (csType != ColorSpace.TYPE_RGB) {
> if (csType == ColorSpace.TYPE_GRAY
> && cm instanceof ComponentColorModel) {
> // Check if this might be a child raster (fix for bug 4240596)
> if (sm instanceof ComponentSampleModel &&
> ((ComponentSampleModel)sm).getPixelStride() != numBands) {
> imageType = TYPE_CUSTOM;
> } else if (raster instanceof ByteComponentRaster &&
> raster.getNumBands() == 1 &&
> cm.getComponentSize(0) == 8 &&
> ((ByteComponentRaster)raster).getPixelStride() == 1) {
> imageType = TYPE_BYTE_GRAY;
> } else if (raster instanceof ShortComponentRaster &&
> raster.getNumBands() == 1 &&
> cm.getComponentSize(0) == 16 &&
> ((ShortComponentRaster)raster).getPixelStride() == 1) {
> imageType = TYPE_USHORT_GRAY;
> }
> } else {
> imageType = TYPE_CUSTOM;
> }
> return;
> }
>
> So the only way to create a TYPE_USHORT_GRAY image using a supplied raster is
> by using a ShortComponentRaster.
>
> The RAWImageReader ends up using this method to create the returned image.
>
> I assume what is happening is that further down the in the image rendering
> code, there are optimized paths for grayscale images. This is probably
> determined by looking at the image type when the image is a BufferedImage.
> Since the image type is CUSTOM it probably uses a path that does color
> conversion based on the color space.
>
> Anyway, to make the RAWImageReader work correctly, create the following
> sample model instead of a ComponentSampleModel and the proper BufferedImage
> will be created.
>
> SampleModel sampleModel = new
> PixelInterleavedSampleModel(DataBuffer.TYPE_USHORT,
> IMAGE_WIDTH,
> IMAGE_HEIGHT,
> 1,
> IMAGE_WIDTH,
> bandOffsets
> );
>
> Hope this helps,
> Robert
>
> On Jul 13, 2006, at 7:17 PM, Brian Burkhalter wrote:
>
>> Although I have not identified the proximate cause for this problem I think
>> the ultimate cause is this bug
>>
>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5051418
>>
>> but in this case for USHORT images rather than BYTE. If this is of interest
>> to you please vote for it and add your comments.
>>
>> The basis for my conclusion is that if I add these lines
>>
>> // XXX BEGIN
>> BufferedImage tmpImage = new
>> BufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY);
>>
>> tmpImage.setData(rightImage.getRaster());
>>
>> rightImage = tmpImage;
>> // XXX END
>>
>> after line 112 (BufferedImage rightImage = rawReader.read(0);) then both
>> images are displayed the same.
>>
>> A workaround is to pass in a destination via the ImageReadParam for example
>> as
>>
>> ImageReadParam readParam = rawReader.getDefaultReadParam();
>> readParam.setDestination(new
>> BufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_USHORT_GRAY));
>> BufferedImage rightImage = rawReader.read(0, readParam);
>>
>> This assures that the display code will recognize the type of the
>> BufferedImage.
>>
>> If you think that we should attempt to modify the raw reader plug-in such
>> that this does not happen even if the Java 2D bug 5051418 is not fixed then
>> please file an issue against jai-imageio-core.
>>
>> Brian
>>
>> On Tue, 11 Jul 2006, robert engels wrote:
>>
>>> Attached is an updated version that matches the RAWImageReader. I also
>>> removed the JAI dependencies (not needed).
>>>
>>> As to why it is different, it is puzzling, but it has to do with the color
>>> space and/or the image display in Java2D... I added some code to compare
>>> the pixel values - they are identical between the images (including the
>>> alpha component).
>>>
>>> Hopefully someone from Sun can shed some light here.
>>>
>>>
>>>
>>>
>>> On Jul 11, 2006, at 12:33 PM, Marco Sambin - NeoLogica wrote:
>>>
>>>> Hi Robert,
>>>> thanks again for your reply and suggestion.
>>>> I've just tried avoiding to explicitly create the ColorModel, as you
>>>> showed in your last message, but the result is still the same! The two
>>>> images are still different!
>>>> I believe the RAW image reader shall not normalize of manipulate the
>>>> pixel values during the read, at least not in an "uncontrollable" way...
>>>> Any comment on this is highly appreciated.
>>>> Regards,
>>>> Marco.
>>>> From: robert engels [mailto:rengels@ix.netcom.com]
>>>> Sent: martedì 11 luglio 2006 19.23
>>>> To: interest@jai-imageio.dev.java.net
>>>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>> Not completely certain :) I just took the code from the Buffredimage.java
>>>> Another idea is that the RAW image reader is normalizing the pixel values
>>>> during the read.
>>>> You might try avoiding creating the ColorModel altogether... and use the
>>>> following ctor for the image input steam:
>>>> public RawImageInputStream(ImageInputStream source,
>>>> SampleModel sampleModel,
>>>> long[] imageOffsets,
>>>> Dimension[] imageDimensions)
>>>> And just create a 1-banded ComponentSampleModel. According to the JavaDoc
>>>> it will automatically be GRAYSCALE.
>>>> On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:
>>>>> Hi Robert,
>>>>> thank you for your suggestion. I tested it, but the result is unchanged:
>>>>> the image obtained through the RAW image reader always looks a bit
>>>>> brighter than the one created through the BufferedImage.
>>>>> Just one question. You suggested to create the ColorModel in this way:
>>>>> =================
>>>>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
>>>>> Transparency.OPAQUE,
>>>>> DataBuffer.TYPE_USHORT);
>>>>> =================
>>>>> So, you are passing "hasAlpha=false" and "isAlphaPremultiplied=true".
>>>>> What's the reason for this?
>>>>> Thanks again for your help.
>>>>> Marco.
>>>>> From: robert engels [mailto:rengels@ix.netcom.com]
>>>>> Sent: martedì 11 luglio 2006 18.44
>>>>> To: interest@jai-imageio.dev.java.net
>>>>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>> In locking at the BufferedImage code, I think you want to create the
>>>>> ColorModel as follows:
>>>>> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>>>>> int[] nBits = {16};
>>>>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
>>>>> Transparency.OPAQUE,
>>>>> DataBuffer.TYPE_USHORT);
>>>>> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>>>>>> Hi.
>>>>>> Did anybody have the chance to run the provided code sample on the test
>>>>>> image? Any comments?
>>>>>> Thank you in advance.
>>>>>> Regards,
>>>>>> Marco.
>>>>>> -----Original Message-----
>>>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>>>> Sent: venerdì 7 luglio 2006 17.45
>>>>>> To: interest@jai-imageio.dev.java.net
>>>>>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>>> Hi to everybody.
>>>>>> As a follow-up to my recent post (reported below), I built a
>>>>>> self-contained
>>>>>> test code sample which demonstrates the problem I am experiencing with
>>>>>> the
>>>>>> RAW ImageReader and 16-bit grayscale images. The code sample and a test
>>>>>> image file are included in the attached ZIP archive.
>>>>>> I would really appreciate if someone could take a look at this code
>>>>>> sample,
>>>>>> and let me know if you find something wrong in it, or if it may be a
>>>>>> bug of
>>>>>> the RAW ImageReader.
>>>>>> Best regards,
>>>>>> Marco.
>>>>>> -----Original Message-----
>>>>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>>>>> Sent: giovedì 22 giugno 2006 10.12
>>>>>> To: interest@jai-imageio.dev.java.net
>>>>>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>>>> Hi to everybody.
>>>>>> In my JAI Image I/O - based project, I have some 16-bit grayscale
>>>>>> images
>>>>>> stored as short[] arrays containing the raw pixel values. In order to
>>>>>> build
>>>>>> PlanarImage's starting from those short[] arrays, I was previously
>>>>>> using a
>>>>>> code looking like this:
>>>>>> ==========================
>>>>>>
>>>>>> // This array contains pixel values for a 16-bit grayscale image
>>>>>> short[] pixelArray = ...;
>>>>>>
>>>>>> Point origin = new Point();
>>>>>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>>>>>> (imgWidth *
>>>>>> imgHeight),
>>>>>> arrayOffset);
>>>>>> int[] bandOffets = {0};
>>>>>> ComponentSampleModel sampleModel = new
>>>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>>>> imgWidth,
>>>>>> imgHeight,
>>>>>> 1,
>>>>>> imgWidth,
>>>>>> bandOffets
>>>>>> );
>>>>>> WritableRaster raster = Raster.createWritableRaster(sampleModel,
>>>>>> dataBuffer, origin);
>>>>>>
>>>>>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>>>>>> BufferedImage.TYPE_USHORT_GRAY);
>>>>>> buffImg.setData(raster);
>>>>>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>>>>> ==========================
>>>>>> Now I would like to use the "raw" ImageReader instead, which seems like
>>>>>> a
>>>>>> more elegant approach. Here is my current code:
>>>>>> ==========================
>>>>>>
>>>>>> // This array contains pixel values for a 16-bit grayscale image
>>>>>> (the
>>>>>> same image as the previous example)
>>>>>> short[] pixelArray = ...;
>>>>>>
>>>>>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>>>>>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>>>>>> for (int i = 0; i < pixelArray.length; i++)
>>>>>> {
>>>>>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
>>>>>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>>>>>> }
>>>>>>
>>>>>> // Create support variables for reading the RAW image
>>>>>> long[] imgOffsets = {0L};
>>>>>> int[] bandOffets = {0};
>>>>>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>>>>>
>>>>>> // Create Color Space, Color Model and Sample Model
>>>>>> ColorSpace imgColorSpace =
>>>>>> ColorSpace.getInstance(ColorSpace.CS_GRAY);
>>>>>> ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
>>>>>> false,
>>>>>> false,
>>>>>> Transparency.OPAQUE,
>>>>>> DataBuffer.TYPE_USHORT);
>>>>>> ComponentSampleModel imgSampleModel = new
>>>>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>>>>> imgWidth,
>>>>>> imgHeight,
>>>>>> 1,
>>>>>> imgWidth,
>>>>>> bandOffets
>>>>>> );
>>>>>> ImageTypeSpecifier imgTypeSpec = new
>>>>>> ImageTypeSpecifier(imgColorModel,
>>>>>> imgSampleModel);
>>>>>>
>>>>>> // Create the Input Streams
>>>>>> ByteArrayInputStream byteIS = new
>>>>>> ByteArrayInputStream(bytePixelArray);
>>>>>> ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
>>>>>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>>>>>> imgTypeSpec, imgOffsets, imgDims);
>>>>>>
>>>>>> // Get the RAW Image Reader
>>>>>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>>>>>> ImageReader rawReader = null;
>>>>>> if (iter.hasNext())
>>>>>> {
>>>>>> rawReader = (ImageReader) iter.next();
>>>>>> }
>>>>>>
>>>>>> // Read the image through the ImageRead JAI operator
>>>>>> if (rawReader != null)
>>>>>> {
>>>>>> ParameterBlock pb = new ParameterBlock();
>>>>>> pb.add(rawImgIS);
>>>>>> pb.add(new Integer(0));
>>>>>> pb.add(Boolean.FALSE);
>>>>>> pb.add(Boolean.FALSE);
>>>>>> pb.add(Boolean.FALSE);
>>>>>> pb.add(null);
>>>>>> pb.add(null);
>>>>>> pb.add(null);
>>>>>> pb.add(rawReader);
>>>>>>
>>>>>> retImage = JAI.create("ImageRead", pb);
>>>>>> }
>>>>>> ==========================
>>>>>> In both cases, the PlanarImage (retImage) gets correctly created. The
>>>>>> strange thing is that I obtain a slightly different image in the two
>>>>>> cases.
>>>>>> In particular, the image created in the second way appears slightly
>>>>>> "lighter" (i.e., higher brightness) than the image created with the
>>>>>> first
>>>>>> code sample. Please notice that I am applying JAI's "rescale" operator
>>>>>> before displaying, in order to adjust contrast, but with the same
>>>>>> "rescale"
>>>>>> operator, the same image created in the two ways above appears
>>>>>> different.
>>>>>> Can you guess why? Do you see something wrong in my second code sample?
>>>>>> Any help/suggestion would be greatly appreciated.
>>>>>> Regards,
>>>>>> Marco.
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>>>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>>>>>> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>>>
>>
>> ----------------
>> Brian Burkhalter
>> Java Media, Imaging, and Graphics
>> 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
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
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

Marco Sambin - NeoLogica

Hi to everybody.

As a follow-up to my recent post (reported below), I built a self-contained
test code sample which demonstrates the problem I am experiencing with the
RAW ImageReader and 16-bit grayscale images. The code sample and a test
image file are included in the attached ZIP archive.

I would really appreciate if someone could take a look at this code sample,
and let me know if you find something wrong in it, or if it may be a bug of
the RAW ImageReader.

Best regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: giovedì 22 giugno 2006 10.12
To: interest@jai-imageio.dev.java.net
Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

In my JAI Image I/O - based project, I have some 16-bit grayscale images
stored as short[] arrays containing the raw pixel values. In order to build
PlanarImage's starting from those short[] arrays, I was previously using a
code looking like this:

==========================

// This array contains pixel values for a 16-bit grayscale image
short[] pixelArray = ...;

Point origin = new Point();
DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
(imgWidth *
imgHeight),
arrayOffset);
int[] bandOffets = {0};
ComponentSampleModel sampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
WritableRaster raster = Raster.createWritableRaster(sampleModel,
dataBuffer, origin);

BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_USHORT_GRAY);
buffImg.setData(raster);
retImage = PlanarImage.wrapRenderedImage(buffImg);

==========================

Now I would like to use the "raw" ImageReader instead, which seems like a
more elegant approach. Here is my current code:

==========================

// This array contains pixel values for a 16-bit grayscale image (the
same image as the previous example)
short[] pixelArray = ...;

// TEMP: convert the pixel array to a byte array (BIG ENDIAN)
byte[] bytePixelArray = new byte[pixelArray.length * 2];
for (int i = 0; i < pixelArray.length; i++)
{
bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
}

// Create support variables for reading the RAW image
long[] imgOffsets = {0L};
int[] bandOffets = {0};
Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};

// Create Color Space, Color Model and Sample Model
ColorSpace imgColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
ComponentSampleModel imgSampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier(imgColorModel,
imgSampleModel);

// Create the Input Streams
ByteArrayInputStream byteIS = new
ByteArrayInputStream(bytePixelArray);
ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
imgTypeSpec, imgOffsets, imgDims);

// Get the RAW Image Reader
Iterator iter = ImageIO.getImageReadersByFormatName("raw");
ImageReader rawReader = null;
if (iter.hasNext())
{
rawReader = (ImageReader) iter.next();
}

// Read the image through the ImageRead JAI operator
if (rawReader != null)
{
ParameterBlock pb = new ParameterBlock();
pb.add(rawImgIS);
pb.add(new Integer(0));
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(null);
pb.add(null);
pb.add(null);
pb.add(rawReader);

retImage = JAI.create("ImageRead", pb);
}

==========================

In both cases, the PlanarImage (retImage) gets correctly created. The
strange thing is that I obtain a slightly different image in the two cases.
In particular, the image created in the second way appears slightly
"lighter" (i.e., higher brightness) than the image created with the first
code sample. Please notice that I am applying JAI's "rescale" operator
before displaying, in order to adjust contrast, but with the same "rescale"
operator, the same image created in the two ways above appears different.

Can you guess why? Do you see something wrong in my second code sample?

Any help/suggestion would be greatly appreciated.

Regards,

Marco.

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

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

Marco Sambin - NeoLogica

Hi.

Did anybody have the chance to run the provided code sample on the test
image? Any comments?
Thank you in advance.

Regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: venerdì 7 luglio 2006 17.45
To: interest@jai-imageio.dev.java.net
Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

As a follow-up to my recent post (reported below), I built a self-contained
test code sample which demonstrates the problem I am experiencing with the
RAW ImageReader and 16-bit grayscale images. The code sample and a test
image file are included in the attached ZIP archive.

I would really appreciate if someone could take a look at this code sample,
and let me know if you find something wrong in it, or if it may be a bug of
the RAW ImageReader.

Best regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: giovedì 22 giugno 2006 10.12
To: interest@jai-imageio.dev.java.net
Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

In my JAI Image I/O - based project, I have some 16-bit grayscale images
stored as short[] arrays containing the raw pixel values. In order to build
PlanarImage's starting from those short[] arrays, I was previously using a
code looking like this:

==========================

// This array contains pixel values for a 16-bit grayscale image
short[] pixelArray = ...;

Point origin = new Point();
DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
(imgWidth *
imgHeight),
arrayOffset);
int[] bandOffets = {0};
ComponentSampleModel sampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
WritableRaster raster = Raster.createWritableRaster(sampleModel,
dataBuffer, origin);

BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_USHORT_GRAY);
buffImg.setData(raster);
retImage = PlanarImage.wrapRenderedImage(buffImg);

==========================

Now I would like to use the "raw" ImageReader instead, which seems like a
more elegant approach. Here is my current code:

==========================

// This array contains pixel values for a 16-bit grayscale image (the
same image as the previous example)
short[] pixelArray = ...;

// TEMP: convert the pixel array to a byte array (BIG ENDIAN)
byte[] bytePixelArray = new byte[pixelArray.length * 2];
for (int i = 0; i < pixelArray.length; i++)
{
bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
}

// Create support variables for reading the RAW image
long[] imgOffsets = {0L};
int[] bandOffets = {0};
Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};

// Create Color Space, Color Model and Sample Model
ColorSpace imgColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
ComponentSampleModel imgSampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier(imgColorModel,
imgSampleModel);

// Create the Input Streams
ByteArrayInputStream byteIS = new
ByteArrayInputStream(bytePixelArray);
ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
imgTypeSpec, imgOffsets, imgDims);

// Get the RAW Image Reader
Iterator iter = ImageIO.getImageReadersByFormatName("raw");
ImageReader rawReader = null;
if (iter.hasNext())
{
rawReader = (ImageReader) iter.next();
}

// Read the image through the ImageRead JAI operator
if (rawReader != null)
{
ParameterBlock pb = new ParameterBlock();
pb.add(rawImgIS);
pb.add(new Integer(0));
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(null);
pb.add(null);
pb.add(null);
pb.add(rawReader);

retImage = JAI.create("ImageRead", pb);
}

==========================

In both cases, the PlanarImage (retImage) gets correctly created. The
strange thing is that I obtain a slightly different image in the two cases.
In particular, the image created in the second way appears slightly
"lighter" (i.e., higher brightness) than the image created with the first
code sample. Please notice that I am applying JAI's "rescale" operator
before displaying, in order to adjust contrast, but with the same "rescale"
operator, the same image created in the two ways above appears different.

Can you guess why? Do you see something wrong in my second code sample?

Any help/suggestion would be greatly appreciated.

Regards,

Marco.

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

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

robert engels

In locking at the BufferedImage code, I think you want to create the
ColorModel as follows:

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {16};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);

On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:

> Hi.
>
> Did anybody have the chance to run the provided code sample on the
> test
> image? Any comments?
> Thank you in advance.
>
> Regards,
>
> Marco.
>
>
>
> -----Original Message-----
> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
> Sent: venerdì 7 luglio 2006 17.45
> To: interest@jai-imageio.dev.java.net
> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>
> Hi to everybody.
>
> As a follow-up to my recent post (reported below), I built a self-
> contained
> test code sample which demonstrates the problem I am experiencing
> with the
> RAW ImageReader and 16-bit grayscale images. The code sample and a
> test
> image file are included in the attached ZIP archive.
>
> I would really appreciate if someone could take a look at this code
> sample,
> and let me know if you find something wrong in it, or if it may be
> a bug of
> the RAW ImageReader.
>
> Best regards,
>
> Marco.
>
>
>
> -----Original Message-----
> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
> Sent: giovedì 22 giugno 2006 10.12
> To: interest@jai-imageio.dev.java.net
> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>
> Hi to everybody.
>
> In my JAI Image I/O - based project, I have some 16-bit grayscale
> images
> stored as short[] arrays containing the raw pixel values. In order
> to build
> PlanarImage's starting from those short[] arrays, I was previously
> using a
> code looking like this:
>
> ==========================
>
> // This array contains pixel values for a 16-bit grayscale image
> short[] pixelArray = ...;
>
> Point origin = new Point();
> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
> (imgWidth *
> imgHeight),
> arrayOffset);
> int[] bandOffets = {0};
> ComponentSampleModel sampleModel = new
> ComponentSampleModel(DataBuffer.TYPE_USHORT,
> imgWidth,
> imgHeight,
> 1,
> imgWidth,
> bandOffets
> );
> WritableRaster raster = Raster.createWritableRaster(sampleModel,
> dataBuffer, origin);
>
> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
> BufferedImage.TYPE_USHORT_GRAY);
> buffImg.setData(raster);
> retImage = PlanarImage.wrapRenderedImage(buffImg);
>
> ==========================
>
> Now I would like to use the "raw" ImageReader instead, which seems
> like a
> more elegant approach. Here is my current code:
>
> ==========================
>
> // This array contains pixel values for a 16-bit grayscale
> image (the
> same image as the previous example)
> short[] pixelArray = ...;
>
> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
> byte[] bytePixelArray = new byte[pixelArray.length * 2];
> for (int i = 0; i < pixelArray.length; i++)
> {
> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
> }
>
> // Create support variables for reading the RAW image
> long[] imgOffsets = {0L};
> int[] bandOffets = {0};
> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>
> // Create Color Space, Color Model and Sample Model
> ColorSpace imgColorSpace = ColorSpace.getInstance
> (ColorSpace.CS_GRAY);
> ColorModel imgColorModel = new ComponentColorModel
> (imgColorSpace,
> false,
> false,
> Transparency.OPAQUE,
> DataBuffer.TYPE_USHORT);
> ComponentSampleModel imgSampleModel = new
> ComponentSampleModel(DataBuffer.TYPE_USHORT,
> imgWidth,
> imgHeight,
> 1,
> imgWidth,
> bandOffets
> );
> ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier
> (imgColorModel,
> imgSampleModel);
>
> // Create the Input Streams
> ByteArrayInputStream byteIS = new
> ByteArrayInputStream(bytePixelArray);
> ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
> imgTypeSpec, imgOffsets, imgDims);
>
> // Get the RAW Image Reader
> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
> ImageReader rawReader = null;
> if (iter.hasNext())
> {
> rawReader = (ImageReader) iter.next();
> }
>
> // Read the image through the ImageRead JAI operator
> if (rawReader != null)
> {
> ParameterBlock pb = new ParameterBlock();
> pb.add(rawImgIS);
> pb.add(new Integer(0));
> pb.add(Boolean.FALSE);
> pb.add(Boolean.FALSE);
> pb.add(Boolean.FALSE);
> pb.add(null);
> pb.add(null);
> pb.add(null);
> pb.add(rawReader);
>
> retImage = JAI.create("ImageRead", pb);
> }
>
> ==========================
>
> In both cases, the PlanarImage (retImage) gets correctly created. The
> strange thing is that I obtain a slightly different image in the
> two cases.
> In particular, the image created in the second way appears slightly
> "lighter" (i.e., higher brightness) than the image created with the
> first
> code sample. Please notice that I am applying JAI's "rescale" operator
> before displaying, in order to adjust contrast, but with the same
> "rescale"
> operator, the same image created in the two ways above appears
> different.
>
> Can you guess why? Do you see something wrong in my second code
> sample?
>
> Any help/suggestion would be greatly appreciated.
>
> Regards,
>
> Marco.
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

[att1.html]

Marco Sambin - NeoLogica

Hi Robert,

thank you for your suggestion. I tested it, but the result is unchanged: the
image obtained through the RAW image reader always looks a bit brighter than
the one created through the BufferedImage.

Just one question. You suggested to create the ColorModel in this way:

=================
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
=================

So, you are passing "hasAlpha=false" and "isAlphaPremultiplied=true". What's
the reason for this?

Thanks again for your help.

Marco.

_____

From: robert engels [mailto:rengels@ix.netcom.com]
Sent: martedì 11 luglio 2006 18.44
To: interest@jai-imageio.dev.java.net
Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

In locking at the BufferedImage code, I think you want to create the
ColorModel as follows:

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {16};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);

On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:

Hi.

Did anybody have the chance to run the provided code sample on the test
image? Any comments?
Thank you in advance.

Regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: venerdì 7 luglio 2006 17.45
To: interest@jai-imageio.dev.java.net
Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

As a follow-up to my recent post (reported below), I built a self-contained
test code sample which demonstrates the problem I am experiencing with the
RAW ImageReader and 16-bit grayscale images. The code sample and a test
image file are included in the attached ZIP archive.

I would really appreciate if someone could take a look at this code sample,
and let me know if you find something wrong in it, or if it may be a bug of
the RAW ImageReader.

Best regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: giovedì 22 giugno 2006 10.12
To: interest@jai-imageio.dev.java.net
Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

In my JAI Image I/O - based project, I have some 16-bit grayscale images
stored as short[] arrays containing the raw pixel values. In order to build
PlanarImage's starting from those short[] arrays, I was previously using a
code looking like this:

==========================

// This array contains pixel values for a 16-bit grayscale image
short[] pixelArray = ...;

Point origin = new Point();
DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
(imgWidth *
imgHeight),
arrayOffset);
int[] bandOffets = {0};
ComponentSampleModel sampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
WritableRaster raster = Raster.createWritableRaster(sampleModel,
dataBuffer, origin);

BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_USHORT_GRAY);
buffImg.setData(raster);
retImage = PlanarImage.wrapRenderedImage(buffImg);

==========================

Now I would like to use the "raw" ImageReader instead, which seems like a
more elegant approach. Here is my current code:

==========================

// This array contains pixel values for a 16-bit grayscale image (the
same image as the previous example)
short[] pixelArray = ...;

// TEMP: convert the pixel array to a byte array (BIG ENDIAN)
byte[] bytePixelArray = new byte[pixelArray.length * 2];
for (int i = 0; i < pixelArray.length; i++)
{
bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
}

// Create support variables for reading the RAW image
long[] imgOffsets = {0L};
int[] bandOffets = {0};
Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};

// Create Color Space, Color Model and Sample Model
ColorSpace imgColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
ComponentSampleModel imgSampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier(imgColorModel,
imgSampleModel);

// Create the Input Streams
ByteArrayInputStream byteIS = new
ByteArrayInputStream(bytePixelArray);
ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
imgTypeSpec, imgOffsets, imgDims);

// Get the RAW Image Reader
Iterator iter = ImageIO.getImageReadersByFormatName("raw");
ImageReader rawReader = null;
if (iter.hasNext())
{
rawReader = (ImageReader) iter.next();
}

// Read the image through the ImageRead JAI operator
if (rawReader != null)
{
ParameterBlock pb = new ParameterBlock();
pb.add(rawImgIS);
pb.add(new Integer(0));
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(null);
pb.add(null);
pb.add(null);
pb.add(rawReader);

retImage = JAI.create("ImageRead", pb);
}

==========================

In both cases, the PlanarImage (retImage) gets correctly created. The
strange thing is that I obtain a slightly different image in the two cases.
In particular, the image created in the second way appears slightly
"lighter" (i.e., higher brightness) than the image created with the first
code sample. Please notice that I am applying JAI's "rescale" operator
before displaying, in order to adjust contrast, but with the same "rescale"
operator, the same image created in the two ways above appears different.

Can you guess why? Do you see something wrong in my second code sample?

Any help/suggestion would be greatly appreciated.

Regards,

Marco.

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

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

[att1.html]

robert engels

Not completely certain :) I just took the code from the
Buffredimage.java

Another idea is that the RAW image reader is normalizing the pixel
values during the read.

You might try avoiding creating the ColorModel altogether... and use
the following ctor for the image input steam:

public RawImageInputStream(ImageInputStream source,
SampleModel sampleModel,
long[] imageOffsets,
Dimension[] imageDimensions)

And just create a 1-banded ComponentSampleModel. According to the
JavaDoc it will automatically be GRAYSCALE.

On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:

> Hi Robert,
>
> thank you for your suggestion. I tested it, but the result is
> unchanged: the image obtained through the RAW image reader always
> looks a bit brighter than the one created through the BufferedImage.
>
> Just one question. You suggested to create the ColorModel in this way:
>
> =================
> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
> true,
> Transparency.OPAQUE,
> DataBuffer.TYPE_USHORT);
> =================
>
> So, you are passing "hasAlpha=false" and
> "isAlphaPremultiplied=true". What's the reason for this?
>
> Thanks again for your help.
>
> Marco.
>
>
> From: robert engels [mailto:rengels@ix.netcom.com]
> Sent: martedì 11 luglio 2006 18.44
> To: interest@jai-imageio.dev.java.net
> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>
> In locking at the BufferedImage code, I think you want to create
> the ColorModel as follows:
>
> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
> int[] nBits = {16};
> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
> true,
> Transparency.OPAQUE,
> DataBuffer.TYPE_USHORT);
>
>
>
> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>
>> Hi.
>>
>> Did anybody have the chance to run the provided code sample on the
>> test
>> image? Any comments?
>> Thank you in advance.
>>
>> Regards,
>>
>> Marco.
>>
>>
>>
>> -----Original Message-----
>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>> Sent: venerdì 7 luglio 2006 17.45
>> To: interest@jai-imageio.dev.java.net
>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>
>> Hi to everybody.
>>
>> As a follow-up to my recent post (reported below), I built a self-
>> contained
>> test code sample which demonstrates the problem I am experiencing
>> with the
>> RAW ImageReader and 16-bit grayscale images. The code sample and a
>> test
>> image file are included in the attached ZIP archive.
>>
>> I would really appreciate if someone could take a look at this
>> code sample,
>> and let me know if you find something wrong in it, or if it may be
>> a bug of
>> the RAW ImageReader.
>>
>> Best regards,
>>
>> Marco.
>>
>>
>>
>> -----Original Message-----
>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>> Sent: giovedì 22 giugno 2006 10.12
>> To: interest@jai-imageio.dev.java.net
>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>
>> Hi to everybody.
>>
>> In my JAI Image I/O - based project, I have some 16-bit grayscale
>> images
>> stored as short[] arrays containing the raw pixel values. In order
>> to build
>> PlanarImage's starting from those short[] arrays, I was previously
>> using a
>> code looking like this:
>>
>> ==========================
>>
>> // This array contains pixel values for a 16-bit grayscale
>> image
>> short[] pixelArray = ...;
>>
>> Point origin = new Point();
>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>> (imgWidth *
>> imgHeight),
>> arrayOffset);
>> int[] bandOffets = {0};
>> ComponentSampleModel sampleModel = new
>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>> imgWidth,
>> imgHeight,
>> 1,
>> imgWidth,
>> bandOffets
>> );
>> WritableRaster raster = Raster.createWritableRaster(sampleModel,
>> dataBuffer, origin);
>>
>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>> BufferedImage.TYPE_USHORT_GRAY);
>> buffImg.setData(raster);
>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>
>> ==========================
>>
>> Now I would like to use the "raw" ImageReader instead, which seems
>> like a
>> more elegant approach. Here is my current code:
>>
>> ==========================
>>
>> // This array contains pixel values for a 16-bit grayscale
>> image (the
>> same image as the previous example)
>> short[] pixelArray = ...;
>>
>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>> for (int i = 0; i < pixelArray.length; i++)
>> {
>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) &
>> 0x00FF);
>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>> }
>>
>> // Create support variables for reading the RAW image
>> long[] imgOffsets = {0L};
>> int[] bandOffets = {0};
>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>
>> // Create Color Space, Color Model and Sample Model
>> ColorSpace imgColorSpace = ColorSpace.getInstance
>> (ColorSpace.CS_GRAY);
>> ColorModel imgColorModel = new ComponentColorModel
>> (imgColorSpace,
>> false,
>> false,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_USHORT);
>> ComponentSampleModel imgSampleModel = new
>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>> imgWidth,
>> imgHeight,
>> 1,
>> imgWidth,
>> bandOffets
>> );
>> ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier
>> (imgColorModel,
>> imgSampleModel);
>>
>> // Create the Input Streams
>> ByteArrayInputStream byteIS = new
>> ByteArrayInputStream(bytePixelArray);
>> ImageInputStream imgIS = ImageIO.createImageInputStream
>> (byteIS);
>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>> imgTypeSpec, imgOffsets, imgDims);
>>
>> // Get the RAW Image Reader
>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>> ImageReader rawReader = null;
>> if (iter.hasNext())
>> {
>> rawReader = (ImageReader) iter.next();
>> }
>>
>> // Read the image through the ImageRead JAI operator
>> if (rawReader != null)
>> {
>> ParameterBlock pb = new ParameterBlock();
>> pb.add(rawImgIS);
>> pb.add(new Integer(0));
>> pb.add(Boolean.FALSE);
>> pb.add(Boolean.FALSE);
>> pb.add(Boolean.FALSE);
>> pb.add(null);
>> pb.add(null);
>> pb.add(null);
>> pb.add(rawReader);
>>
>> retImage = JAI.create("ImageRead", pb);
>> }
>>
>> ==========================
>>
>> In both cases, the PlanarImage (retImage) gets correctly created. The
>> strange thing is that I obtain a slightly different image in the
>> two cases.
>> In particular, the image created in the second way appears slightly
>> "lighter" (i.e., higher brightness) than the image created with
>> the first
>> code sample. Please notice that I am applying JAI's "rescale"
>> operator
>> before displaying, in order to adjust contrast, but with the same
>> "rescale"
>> operator, the same image created in the two ways above appears
>> different.
>>
>> Can you guess why? Do you see something wrong in my second code
>> sample?
>>
>> Any help/suggestion would be greatly appreciated.
>>
>> Regards,
>>
>> Marco.
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>> For additional commands, e-mail: interest-help@jai-
>> imageio.dev.java.net
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
>> For additional commands, e-mail: interest-help@jai-
>> imageio.dev.java.net
>>
>

[att1.html]

Marco Sambin - NeoLogica

Hi Robert,

thanks again for your reply and suggestion.

I've just tried avoiding to explicitly create the ColorModel, as you showed
in your last message, but the result is still the same! The two images are
still different!

I believe the RAW image reader shall not normalize of manipulate the pixel
values during the read, at least not in an "uncontrollable" way...

Any comment on this is highly appreciated.

Regards,

Marco.

_____

From: robert engels [mailto:rengels@ix.netcom.com]
Sent: martedì 11 luglio 2006 19.23
To: interest@jai-imageio.dev.java.net
Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Not completely certain :) I just took the code from the Buffredimage.java

Another idea is that the RAW image reader is normalizing the pixel values
during the read.

You might try avoiding creating the ColorModel altogether... and use the
following ctor for the image input steam:

public RawImageInputStream(ImageInputStream source,
SampleModel sampleModel,
long[] imageOffsets,
Dimension[] imageDimensions)

And just create a 1-banded ComponentSampleModel. According to the JavaDoc it
will automatically be GRAYSCALE.

On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:

Hi Robert,

thank you for your suggestion. I tested it, but the result is unchanged: the
image obtained through the RAW image reader always looks a bit brighter than
the one created through the BufferedImage.

Just one question. You suggested to create the ColorModel in this way:

=================
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
=================

So, you are passing "hasAlpha=false" and "isAlphaPremultiplied=true". What's
the reason for this?

Thanks again for your help.

Marco.

_____

From: robert engels [mailto:rengels@ix.netcom.com]
Sent: martedì 11 luglio 2006 18.44
To: interest@jai-imageio.dev.java.net
Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

In locking at the BufferedImage code, I think you want to create the
ColorModel as follows:

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = {16};
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, true,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);

On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:

Hi.

Did anybody have the chance to run the provided code sample on the test
image? Any comments?
Thank you in advance.

Regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: venerdì 7 luglio 2006 17.45
To: interest@jai-imageio.dev.java.net
Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

As a follow-up to my recent post (reported below), I built a self-contained
test code sample which demonstrates the problem I am experiencing with the
RAW ImageReader and 16-bit grayscale images. The code sample and a test
image file are included in the attached ZIP archive.

I would really appreciate if someone could take a look at this code sample,
and let me know if you find something wrong in it, or if it may be a bug of
the RAW ImageReader.

Best regards,

Marco.

-----Original Message-----
From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
Sent: giovedì 22 giugno 2006 10.12
To: interest@jai-imageio.dev.java.net
Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images

Hi to everybody.

In my JAI Image I/O - based project, I have some 16-bit grayscale images
stored as short[] arrays containing the raw pixel values. In order to build
PlanarImage's starting from those short[] arrays, I was previously using a
code looking like this:

==========================

// This array contains pixel values for a 16-bit grayscale image
short[] pixelArray = ...;

Point origin = new Point();
DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
(imgWidth *
imgHeight),
arrayOffset);
int[] bandOffets = {0};
ComponentSampleModel sampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
WritableRaster raster = Raster.createWritableRaster(sampleModel,
dataBuffer, origin);

BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
BufferedImage.TYPE_USHORT_GRAY);
buffImg.setData(raster);
retImage = PlanarImage.wrapRenderedImage(buffImg);

==========================

Now I would like to use the "raw" ImageReader instead, which seems like a
more elegant approach. Here is my current code:

==========================

// This array contains pixel values for a 16-bit grayscale image (the
same image as the previous example)
short[] pixelArray = ...;

// TEMP: convert the pixel array to a byte array (BIG ENDIAN)
byte[] bytePixelArray = new byte[pixelArray.length * 2];
for (int i = 0; i < pixelArray.length; i++)
{
bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) & 0x00FF);
bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
}

// Create support variables for reading the RAW image
long[] imgOffsets = {0L};
int[] bandOffets = {0};
Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};

// Create Color Space, Color Model and Sample Model
ColorSpace imgColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel imgColorModel = new ComponentColorModel(imgColorSpace,
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT);
ComponentSampleModel imgSampleModel = new
ComponentSampleModel(DataBuffer.TYPE_USHORT,
imgWidth,
imgHeight,
1,
imgWidth,
bandOffets
);
ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier(imgColorModel,
imgSampleModel);

// Create the Input Streams
ByteArrayInputStream byteIS = new
ByteArrayInputStream(bytePixelArray);
ImageInputStream imgIS = ImageIO.createImageInputStream(byteIS);
RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
imgTypeSpec, imgOffsets, imgDims);

// Get the RAW Image Reader
Iterator iter = ImageIO.getImageReadersByFormatName("raw");
ImageReader rawReader = null;
if (iter.hasNext())
{
rawReader = (ImageReader) iter.next();
}

// Read the image through the ImageRead JAI operator
if (rawReader != null)
{
ParameterBlock pb = new ParameterBlock();
pb.add(rawImgIS);
pb.add(new Integer(0));
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(Boolean.FALSE);
pb.add(null);
pb.add(null);
pb.add(null);
pb.add(rawReader);

retImage = JAI.create("ImageRead", pb);
}

==========================

In both cases, the PlanarImage (retImage) gets correctly created. The
strange thing is that I obtain a slightly different image in the two cases.
In particular, the image created in the second way appears slightly
"lighter" (i.e., higher brightness) than the image created with the first
code sample. Please notice that I am applying JAI's "rescale" operator
before displaying, in order to adjust contrast, but with the same "rescale"
operator, the same image created in the two ways above appears different.

Can you guess why? Do you see something wrong in my second code sample?

Any help/suggestion would be greatly appreciated.

Regards,

Marco.

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

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

[att1.html]

robert engels

Your supplied code does not work:

java.lang.NullPointerException
at DisplayJAI.(DisplayJAI.java:49)
at RawReaderTester.jbInit(RawReaderTester.java:150)
at RawReaderTester.(RawReaderTester.java:28)
at RawReaderTester.main(RawReaderTester.java:168)

On Jul 11, 2006, at 12:33 PM, Marco Sambin - NeoLogica wrote:

> Hi Robert,
>
> thanks again for your reply and suggestion.
>
> I've just tried avoiding to explicitly create the ColorModel, as
> you showed in your last message, but the result is still the same!
> The two images are still different!
>
> I believe the RAW image reader shall not normalize of manipulate
> the pixel values during the read, at least not in an
> "uncontrollable" way...
>
> Any comment on this is highly appreciated.
>
> Regards,
>
> Marco.
>
>
> From: robert engels [mailto:rengels@ix.netcom.com]
> Sent: martedì 11 luglio 2006 19.23
> To: interest@jai-imageio.dev.java.net
> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>
> Not completely certain :) I just took the code from the
> Buffredimage.java
>
> Another idea is that the RAW image reader is normalizing the pixel
> values during the read.
>
> You might try avoiding creating the ColorModel altogether... and
> use the following ctor for the image input steam:
>
> public RawImageInputStream(ImageInputStream source,
> SampleModel sampleModel,
> long[] imageOffsets,
> Dimension[] imageDimensions)
>
> And just create a 1-banded ComponentSampleModel. According to the
> JavaDoc it will automatically be GRAYSCALE.
>
>
>
>
> On Jul 11, 2006, at 12:04 PM, Marco Sambin - NeoLogica wrote:
>
>> Hi Robert,
>>
>> thank you for your suggestion. I tested it, but the result is
>> unchanged: the image obtained through the RAW image reader always
>> looks a bit brighter than the one created through the BufferedImage.
>>
>> Just one question. You suggested to create the ColorModel in this
>> way:
>>
>> =================
>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
>> true,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_USHORT);
>> =================
>>
>> So, you are passing "hasAlpha=false" and
>> "isAlphaPremultiplied=true". What's the reason for this?
>>
>> Thanks again for your help.
>>
>> Marco.
>>
>>
>> From: robert engels [mailto:rengels@ix.netcom.com]
>> Sent: martedì 11 luglio 2006 18.44
>> To: interest@jai-imageio.dev.java.net
>> Subject: Re: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>
>> In locking at the BufferedImage code, I think you want to create
>> the ColorModel as follows:
>>
>> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
>> int[] nBits = {16};
>> ColorModel colorModel = new ComponentColorModel(cs, nBits, false,
>> true,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_USHORT);
>>
>>
>>
>> On Jul 11, 2006, at 11:32 AM, Marco Sambin - NeoLogica wrote:
>>
>>> Hi.
>>>
>>> Did anybody have the chance to run the provided code sample on
>>> the test
>>> image? Any comments?
>>> Thank you in advance.
>>>
>>> Regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>> Sent: venerdì 7 luglio 2006 17.45
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: RE: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>
>>> Hi to everybody.
>>>
>>> As a follow-up to my recent post (reported below), I built a self-
>>> contained
>>> test code sample which demonstrates the problem I am experiencing
>>> with the
>>> RAW ImageReader and 16-bit grayscale images. The code sample and
>>> a test
>>> image file are included in the attached ZIP archive.
>>>
>>> I would really appreciate if someone could take a look at this
>>> code sample,
>>> and let me know if you find something wrong in it, or if it may
>>> be a bug of
>>> the RAW ImageReader.
>>>
>>> Best regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Marco Sambin - NeoLogica [mailto:marco.sambin@neologica.it]
>>> Sent: giovedì 22 giugno 2006 10.12
>>> To: interest@jai-imageio.dev.java.net
>>> Subject: [JAI-IMAGEIO] "RAW" ImageReader and grayscale images
>>>
>>> Hi to everybody.
>>>
>>> In my JAI Image I/O - based project, I have some 16-bit grayscale
>>> images
>>> stored as short[] arrays containing the raw pixel values. In
>>> order to build
>>> PlanarImage's starting from those short[] arrays, I was
>>> previously using a
>>> code looking like this:
>>>
>>> ==========================
>>>
>>> // This array contains pixel values for a 16-bit grayscale
>>> image
>>> short[] pixelArray = ...;
>>>
>>> Point origin = new Point();
>>> DataBufferUShort dataBuffer = new DataBufferUShort(pixelArray,
>>> (imgWidth *
>>> imgHeight),
>>> arrayOffset);
>>> int[] bandOffets = {0};
>>> ComponentSampleModel sampleModel = new
>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>> imgWidth,
>>> imgHeight,
>>> 1,
>>> imgWidth,
>>> bandOffets
>>> );
>>> WritableRaster raster = Raster.createWritableRaster(sampleModel,
>>> dataBuffer, origin);
>>>
>>> BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight,
>>> BufferedImage.TYPE_USHORT_GRAY);
>>> buffImg.setData(raster);
>>> retImage = PlanarImage.wrapRenderedImage(buffImg);
>>>
>>> ==========================
>>>
>>> Now I would like to use the "raw" ImageReader instead, which
>>> seems like a
>>> more elegant approach. Here is my current code:
>>>
>>> ==========================
>>>
>>> // This array contains pixel values for a 16-bit grayscale
>>> image (the
>>> same image as the previous example)
>>> short[] pixelArray = ...;
>>>
>>> // TEMP: convert the pixel array to a byte array (BIG ENDIAN)
>>> byte[] bytePixelArray = new byte[pixelArray.length * 2];
>>> for (int i = 0; i < pixelArray.length; i++)
>>> {
>>> bytePixelArray[i * 2] = (byte)((pixelArray[i] >> 8) &
>>> 0x00FF);
>>> bytePixelArray[i * 2 + 1] = (byte)(pixelArray[i] & 0x00FF);
>>> }
>>>
>>> // Create support variables for reading the RAW image
>>> long[] imgOffsets = {0L};
>>> int[] bandOffets = {0};
>>> Dimension[] imgDims = {new Dimension(imgWidth, imgHeight)};
>>>
>>> // Create Color Space, Color Model and Sample Model
>>> ColorSpace imgColorSpace = ColorSpace.getInstance
>>> (ColorSpace.CS_GRAY);
>>> ColorModel imgColorModel = new ComponentColorModel
>>> (imgColorSpace,
>>> false,
>>> false,
>>> Transparency.OPAQUE,
>>> DataBuffer.TYPE_USHORT);
>>> ComponentSampleModel imgSampleModel = new
>>> ComponentSampleModel(DataBuffer.TYPE_USHORT,
>>> imgWidth,
>>> imgHeight,
>>> 1,
>>> imgWidth,
>>> bandOffets
>>> );
>>> ImageTypeSpecifier imgTypeSpec = new ImageTypeSpecifier
>>> (imgColorModel,
>>> imgSampleModel);
>>>
>>> // Create the Input Streams
>>> ByteArrayInputStream byteIS = new
>>> ByteArrayInputStream(bytePixelArray);
>>> ImageInputStream imgIS = ImageIO.createImageInputStream
>>> (byteIS);
>>> RawImageInputStream rawImgIS = new RawImageInputStream(imgIS,
>>> imgTypeSpec, imgOffsets, imgDims);
>>>
>>> // Get the RAW Image Reader
>>> Iterator iter = ImageIO.getImageReadersByFormatName("raw");
>>> ImageReader rawReader = null;
>>> if (iter.hasNext())
>>> {
>>> rawReader = (ImageReader) iter.next();
>>> }
>>>
>>> // Read the image through the ImageRead JAI operator
>>> if (rawReader != null)
>>> {
>>> ParameterBlock pb = new ParameterBlock();
>>> pb.add(rawImgIS);
>>> pb.add(new Integer(0));
>>> pb.add(Boolean.FALSE);
>>> pb.add(Boolean.FALSE);
>>> pb.add(Boolean.FALSE);
>>> pb.add(null);
>>> pb.add(null);
>>> pb.add(null);
>>> pb.add(rawReader);
>>>
>>> retImage = JAI.create("ImageRead", pb);
>>> }
>>>
>>> ==========================
>>>
>>> In both cases, the PlanarImage (retImage) gets correctly created.
>>> The
>>> strange thing is that I obtain a slightly different image in the
>>> two cases.
>>> In particular, the image created in the second way appears slightly
>>> "lighter" (i.e., higher brightness) than the image created with
>>> the first
>>> code sample. Please notice that I am applying JAI's "rescale"
>>> operator
>>> before displaying, in order to adjust contrast, but with the same
>>> "rescale"
>>> operator, the same image created in the two ways above appears
>>> different.
>>>
>>> Can you guess why? Do you see something wrong in my second code
>>> sample?
>>>
>>> Any help/suggestion would be greatly appreciated.
>>>
>>> Regards,
>>>
>>> Marco.
>>>
>>>
>>>
>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>> imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-
>>> imageio.dev.java.net
>>>
>>>
>>>
>>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: interest-unsubscribe@jai-
>>> imageio.dev.java.net
>>> For additional commands, e-mail: interest-help@jai-
>>> imageio.dev.java.net
>>>
>>
>

[att1.html]