Guy
Guy

Reputation: 325

16 bit (565) image read

I'm reading an image byte array now the image is 16 bit (r: 5, g: 6, b: 5) and I would like to read it to BufferedImage.

I have tried something like:


int[] nBits = {5, 6, 5};
int[] bOffs = {0, 0, 0};
ColorModel colorModel = new ComponentColorModel(
        cs, nBits, false, false,
        Transparency.OPAQUE,
        DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(
        new DataBufferByte(screenBuffer,screenBuffer.length),
        foundWidth, foundHight,
        foundWidth * 2, 2,bOffs, null);

BufferedImage imgReconstructed = new BufferedImage(
        colorModel,raster,false,null);


My issue is with the bandOffsets, how should I set it for 16 bit image. Is it the right way? Thanks, Guy

Upvotes: 3

Views: 682

Answers (1)

Sean Kleinjung
Sean Kleinjung

Reputation: 3175

The createInterleavedRaster method would be used to create an image with a sample model where each data element contained color information for a single band (i.e. red, green, or blue). If you want an image where each 16-bit data element contains all 3 color bands, you want to use one of the createPackedRaster methods instead.

The only sixteen bit data type supported by these models is unsigned short, so you will want to pass a DataBufferUShort instance.

I do not have access to a compiler at the moment to put together a working code sample for you, but the SinglePixelPackedSampleModel API documentation will help explain the scanlineStride and band mask parameters a little bit. If needed, I will update this response later with more details.

Upvotes: 1

Related Questions