Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

problem with creating JAI grayscale image

Thread view: 
Java_New - 25 May 2006 00:15 GMT
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++])<<8));
 }
}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
Andrey Kuznetsov - 25 May 2006 12:28 GMT
> 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
[quoted text clipped - 32 lines]
> 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

Signature

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

Java_New - 26 May 2006 13:51 GMT
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++])<<8));
 }
}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
Andrey Kuznetsov - 26 May 2006 15:49 GMT
> 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++])<<8));

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

Andrey

Signature

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

Java_New - 27 May 2006 15:24 GMT
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++])<<8));

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

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

Thanks
Andrey Kuznetsov - 27 May 2006 16:24 GMT
> 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++])<<8));
>
> to
> temp[i] = (short) (((data[index++])&0xff)+(((data[index++])&0x0F)<<8));

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

Andrey

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.