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);
final BufferedImage bi = new BufferedImage(colorModel, r, false, null);
return bi;
}
else if (bitsPerPixel == 8)
{
final DataBufferByte db = new DataBufferByte(new byte[][] {bytes}, bytes.length);
final SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, w, h, lineStride, new int[] {redMask, greenMask, blueMask});
final WritableRaster r = Raster.createWritableRaster(sm, db, new Point(0, 0));
final ColorModel colorModel = new DirectColorModel(bitsPerPixel, redMask, greenMask, blueMask);
final BufferedImage bi = new BufferedImage(colorModel, r, false, null);
return bi;
}
else
{
final BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
final int [] pixels = new int[w * h];
int pixelIndex = 0;
int lineOffset = 0;
if (flipped)
lineOffset = (h - 1) * lineStride;
for (int y = 0; y < h; ++y)
{
int off = lineOffset;
for (int x = 0; x < w; ++x)
{
final byte r = bytes[off + redMask - 1];
final byte g = bytes[off + greenMask - 1];
final byte b = bytes[off + blueMask - 1];
int pixel = 0;
pixel += r & 0xff; // red
pixel *= 256;
pixel += g & 0xff; // green
pixel *= 256;
pixel += b & 0xff; // blue
pixels[pixelIndex++] = pixel;
off += pixelStride;
}
if (flipped)
lineOffset -= lineStride;
else
lineOffset += lineStride;
}
bi.setRGB(0,0,w,h,pixels,0,w);
return bi;
}
}else if (dataType == Format.shortArray) {
final short[] shorts = (short[]) buffer.getData();
if (bitsPerPixel == 16)
{
final DataBufferUShort db = new DataBufferUShort(new short[][] {shorts}, shorts.length);
final SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, w, h, lineStride, new int[] {redMask, greenMask, blueMask});
final WritableRaster r = Raster.createWritableRaster(sm, db, new Point(0, 0));
final ColorModel colorModel = new DirectColorModel(bitsPerPixel, redMask, greenMask, blueMask);
final BufferedImage bi = new BufferedImage(colorModel, r, false, null);
return bi;
}else{
throw new UnsupportedOperationException(); // TODO
}
}
else if (dataType == Format.intArray)
{
// if (true)
{
// optimized, don't copy data or iterate through pixels:
final int[] bytes = (int[]) buffer.getData();
final DataBufferInt db = new DataBufferInt(new int[][] {bytes}, bytes.length);
final SinglePixelPackedSampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h, new int[] {redMask, greenMask, blueMask});
final WritableRaster r = Raster.createWritableRaster(sm, db, new Point(0, 0));
final ColorModel colorModel = new DirectColorModel(24,
redMask, // Red
greenMask, // Green
blueMask, // Blue
0x0 // Alpha
);