// TODO: optimize other cases.
final DataBufferByte db = new DataBufferByte(new byte[][] {bytes}, bytes.length);
final ComponentSampleModel sm = new ComponentSampleModel(DataBuffer.TYPE_BYTE, w, h, pixelStride, lineStride, new int[] {redMask - 1, greenMask -1, blueMask -1});
final WritableRaster r = Raster.createWritableRaster(sm, db, new Point(0, 0));
// construction borrowed from BufferedImage constructor, for BufferedImage.TYPE_3BYTE_BGR
final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8};
//int[] bOffs = {2, 1, 0};
final ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);
final BufferedImage bi = new BufferedImage(colorModel, r, false, null);
return bi;
}
else if (bitsPerPixel == 32)
{
final DataBufferByte db = new DataBufferByte(new byte[][] {bytes}, bytes.length);
final ComponentSampleModel sm = new ComponentSampleModel(DataBuffer.TYPE_BYTE, w, h, pixelStride, lineStride, new int[] {redMask - 1, greenMask -1, blueMask -1, 3}); // TODO: what to do with alpha?
final WritableRaster r = Raster.createWritableRaster(sm, db, new Point(0, 0));
// construction borrowed from BufferedImage constructor, for BufferedImage.TYPE_4BYTE_ABGR
final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = {8, 8, 8, 8};
//int[] bOffs = {3, 2, 1, 0};
final ColorModel colorModel = new ComponentColorModel(cs, nBits, true, false,
Transparency.TRANSLUCENT,
DataBuffer.TYPE_BYTE);