Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > problem with creating JAI grayscale image

Reply
Thread Tools

problem with creating JAI grayscale image

 
 
Java_New
Guest
Posts: n/a
 
      05-24-2006
Hi all,

I need your help.

I am trying to convert a 12 bits grayscale image to BufferedImage from
byte array. But the output image was very poor contrasted. I tried
everything I could think about (by using lut operator, indexColorModel,
etc.) to correct it. There was still no luck for me. The code is shown
below:

short temp[] = new short[width*height];
int index = 0;
try{
byte data[] = new byte[pixelDataLength];
BufferedFileInputStream.read(data);
for( int i =0; i<temp.length;i++){
temp[i] = (short) (data[index++]+((data[index++])<<);
}
}catch(IOException ex){}

DataBufferUShort dbs= new DataBufferUShort(temp ,temp.length);
SampleModel sampleModel = RasterFactory.createBandedSampleModel(
DataBuffer.TYPE_BYTE,
width, height,
1);
WritableRaster myR = RasterFactory.createWritableRaster(sm,dbs,
new Point(0,0));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int bits[] = new int[]{16};
ColorModel cm = new ComponentColorModel(cs, bits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_USHORT) ;
BufferedImage myBI = new BufferedImage(cm, myR, false,
new Hashtable());
return myBI;

I suspect that the colorModel or sampleModel was not suit for this kind
of grayscale image. The returned image was un-recognizable (brightness
was ok, but I lost 50%-80% contrast). Is there any thing wrong with my
code? Please help.

Thanks in advance

 
Reply With Quote
 
 
 
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      05-25-2006
> I am trying to convert a 12 bits grayscale image to BufferedImage from
> byte array. But the output image was very poor contrasted. I tried
> everything I could think about (by using lut operator, indexColorModel,
> etc.) to correct it. There was still no luck for me. The code is shown
> below:
>
> short temp[] = new short[width*height];
> int index = 0;
> try{
> byte data[] = new byte[pixelDataLength];
> BufferedFileInputStream.read(data);
> for( int i =0; i<temp.length;i++){
> temp[i] = (short) (data[index++]+((data[index++])<<);
> }
> }catch(IOException ex){}
>
> DataBufferUShort dbs= new DataBufferUShort(temp ,temp.length);
> SampleModel sampleModel = RasterFactory.createBandedSampleModel(
> DataBuffer.TYPE_BYTE,
> width, height,
> 1);
> WritableRaster myR = RasterFactory.createWritableRaster(sm,dbs,
> new Point(0,0));
> ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
> int bits[] = new int[]{16};
> ColorModel cm = new ComponentColorModel(cs, bits, false, false,
> Transparency.OPAQUE,
> DataBuffer.TYPE_USHORT) ;
> BufferedImage myBI = new BufferedImage(cm, myR, false,
> new Hashtable());
> return myBI;
>
> I suspect that the colorModel or sampleModel was not suit for this kind
> of grayscale image. The returned image was un-recognizable (brightness
> was ok, but I lost 50%-80% contrast). Is there any thing wrong with my
> code? Please help.


the problem is that you use 16 bit ColorSpace with 12 bit data.
you may try to
a) to use 12 bit ColorSpace

b) keep current CS and stretch your data to 16 bit or
a = (a / 16)^2

c) reduce data to 8 bit and use 8 bit CS
a = Math.sqrt(a * 16);

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
 
 
 
Java_New
Guest
Posts: n/a
 
      05-26-2006
Hi Andrey,

Thanks for your reply. I added some code as below, but the output
images still like before. I chose to use the 12 bit ColorSpace:

short temp[] = new short[width*height];
int index = 0;
try{
byte data[] = new byte[pixelDataLength];
BufferedFileInputStream.read(data);
for( int i =0; i<temp.length;i++){
temp[i] = (short) (data[index++]+((data[index++])<<);
}
}catch(IOException ex){}

DataBufferUShort dbs= new DataBufferUShort(temp, width*height);

byte r[] = new byte[0x1000];
byte g[] = new byte[0x1000];
byte b[] = new byte[0x1000];
int i;
double d = 255.0/4096;
for(i = 0;i < 4096; i++) {
byte b1 = (byte) ((d * i));
r[i] = g[i] = b[i] = (byte)(255-b1) ;
}

IndexColorModel icm = new IndexColorModel(12, 0x1000, r,g,b);

WritableRaster raster =
Raster.createInterleavedRaster(dbs,width,

height, width,

1,new int[]{0},null);
BufferedImage myBI = new BufferedImage(icm, raster, false, new
Hashtable());

Woud you think the code is ok, or I didn't get your idea.

Many thanks

 
Reply With Quote
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      05-26-2006
> short temp[] = new short[width*height];
> int index = 0;
> try{
> byte data[] = new byte[pixelDataLength];
> BufferedFileInputStream.read(data);


note that InputStream.read(byte [] buffer)
does not guaranteed to fill given buffer,
it returns how much byte was read
So, you have to read in a loop or use readFully(byte [] buffer)


> }catch(IOException ex){}

this is really BAD thing, newer swallow exception,
at least make ex.printStackTrace();


> for( int i =0; i<temp.length;i++){
> temp[i] = (short) (data[index++]+((data[index++])<<);


BTW are you sure that you are using right byte order?

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
Java_New
Guest
Posts: n/a
 
      05-27-2006
Thanks very much for your help. That was my low level mistake. Every
thing become ok after I change code:
temp[i] = (short) (data[index++]+((data[index++])<<);

to
temp[i] = (short) (((data[index++])&0xff)+(((data[index++])&0x0F)<<);

I also appreciate those comments / tips you gave to me.

Thanks

 
Reply With Quote
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      05-27-2006
> Thanks very much for your help. That was my low level mistake. Every
> thing become ok after I change code:
> temp[i] = (short) (data[index++]+((data[index++])<<);
>
> to
> temp[i] = (short) (((data[index++])&0xff)+(((data[index++])&0x0F)<<);


yes, this is frequent mistake and is difficult to find, I know it too good.

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem - creating a tiled image from multiple tiff sources in JAI voncroy14 Java 0 07-17-2009 10:17 AM
grayscale BufferedImage from any imagetype with jai api harryos Java 0 07-19-2008 02:50 AM
jai to create a grayscale img from any imagetype harryos Java 0 07-18-2008 08:34 PM
Acquire grayscale USB Video and save as a sequence of 8-bit grayscale bitmaps Speed C++ 5 03-11-2007 04:22 PM
JAI problem of creating compound image from slices Apc Java 1 06-02-2004 10:10 PM



Advertisments