J
Java_New
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
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