validatePicture(picture);
// resample as needed
IVideoPicture resamplePic = null;
final AtomicReference<JNIReference> ref =
new AtomicReference<JNIReference>(null);
try
{
if (willResample())
{
resamplePic = resample(picture, mToImageResampler);
picture = resamplePic;
}
// get picture parameters
final int w = picture.getWidth();
final int h = picture.getHeight();
// make a copy of the raw bytes in the picture and convert those to
// integers
final ByteBuffer byteBuf = picture.getByteBuffer(ref);
// now, for this class of problems, we don't want the code
// to switch byte order, so we'll pretend it's in native java order
byteBuf.order(ByteOrder.BIG_ENDIAN);
final IntBuffer intBuf = byteBuf.asIntBuffer();
final int[] ints = new int[picture.getSize() / 4];
intBuf.get(ints, 0, ints.length);
// create the data buffer from the ints
final DataBufferInt db = new DataBufferInt(ints, ints.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 SinglePixelPackedSampleModel(db.getDataType(),
w, h, mBitMasks);
final WritableRaster wr = Raster.createWritableRaster(sm, db, null);
// return a new image created from the color model and raster
return new BufferedImage(mColorModel, wr, false, null);
}
finally
{
if (resamplePic != null)
resamplePic.delete();
if (ref.get() != null)
ref.get().delete();
}
}