problem with creating JAI grayscale image

Discussion in 'Java' started by Java_New, May 25, 2006.

  1. Java_New

    Java_New Guest

    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 = (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
     
    Java_New, May 25, 2006
    #1
    1. Advertising

  2. > 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 = (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.


    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
     
    Andrey Kuznetsov, May 25, 2006
    #2
    1. Advertising

  3. Java_New

    Java_New Guest

    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 = (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 = g = b = (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
     
    Java_New, May 26, 2006
    #3
  4. > 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 = (short) (data[index++]+((data[index++])<<8));


    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
     
    Andrey Kuznetsov, May 26, 2006
    #4
  5. Java_New

    Java_New Guest

    Thanks very much for your help. That was my low level mistake. Every
    thing become ok after I change code:
    temp = (short) (data[index++]+((data[index++])<<8));

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

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

    Thanks
     
    Java_New, May 27, 2006
    #5
  6. > Thanks very much for your help. That was my low level mistake. Every
    > thing become ok after I change code:
    > temp = (short) (data[index++]+((data[index++])<<8));
    >
    > to
    > temp = (short) (((data[index++])&0xff)+(((data[index++])&0x0F)<<8));


    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
     
    Andrey Kuznetsov, May 27, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apc
    Replies:
    1
    Views:
    863
  2. Speed
    Replies:
    5
    Views:
    426
    Speed
    Mar 11, 2007
  3. harryos
    Replies:
    0
    Views:
    432
    harryos
    Jul 18, 2008
  4. harryos
    Replies:
    0
    Views:
    600
    harryos
    Jul 19, 2008
  5. voncroy14
    Replies:
    0
    Views:
    1,070
    voncroy14
    Jul 17, 2009
Loading...

Share This Page