// test that the picture is valid
validatePicture(picture);
// resample as needed
IVideoPicture resamplePicture = null;
AtomicReference<JNIReference> ref =
new AtomicReference<JNIReference>(null);
try
{
if (willResample())
{
resamplePicture = resample(picture, mToImageResampler);
picture = resamplePicture;
}
// get picture parameters
final int w = picture.getWidth();
final int h = picture.getHeight();
// make a copy of the raw bytes int a DataBufferByte which the
// writable raster can operate on
final ByteBuffer byteBuf = picture.getByteBuffer(ref);
final byte[] bytes = new byte[picture.getSize()];
byteBuf.get(bytes, 0, bytes.length);
// create the data buffer from the bytes
final DataBufferByte db = new DataBufferByte(bytes, bytes.length);
// create an a sample model which matches the byte layout of the
// image data and raster which contains the data which now can be
// properly interpreted
final SampleModel sm = new PixelInterleavedSampleModel(
db.getDataType(), w, h, 3, 3 * w, mBandOffsets);
final WritableRaster wr = Raster.createWritableRaster(sm, db, null);
// create a color model
final ColorModel colorModel = new ComponentColorModel(
mColorSpace, false, false, ColorModel.OPAQUE, db.getDataType());
// return a new image created from the color model and raster
return new BufferedImage(colorModel, wr, false, null);
}
finally
{
if (resamplePicture!=null)
resamplePicture.delete();
if (ref.get()!=null)
ref.get().delete();
}
}