use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class EncodedImageTest method testIsJpegCompleteAt_Complete.
@Test
public void testIsJpegCompleteAt_Complete() {
byte[] encodedBytes = new byte[ENCODED_BYTES_LENGTH];
encodedBytes[ENCODED_BYTES_LENGTH - 2] = (byte) JfifUtil.MARKER_FIRST_BYTE;
encodedBytes[ENCODED_BYTES_LENGTH - 1] = (byte) JfifUtil.MARKER_EOI;
PooledByteBuffer buf = new TrivialPooledByteBuffer(encodedBytes);
EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
encodedImage.setImageFormat(DefaultImageFormats.JPEG);
assertTrue(encodedImage.isCompleteAt(ENCODED_BYTES_LENGTH));
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class EncodedImageTest method checkWebpImage.
private void checkWebpImage(final String imagePath, final ImageFormat imageFormat, final int expectedWidth, final int expectedHeight) throws IOException {
PooledByteBuffer buf = new TrivialPooledByteBuffer(ByteStreams.toByteArray(EncodedImageTest.class.getResourceAsStream(imagePath)));
EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
encodedImage.parseMetaData();
assertSame(imageFormat, encodedImage.getImageFormat());
assertEquals(expectedWidth, encodedImage.getWidth());
assertEquals(expectedHeight, encodedImage.getHeight());
assertEquals(0, encodedImage.getRotationAngle());
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class BufferedDiskCache method readFromDiskCache.
/**
* Performs disk cache read. In case of any exception null is returned.
*/
private PooledByteBuffer readFromDiskCache(final CacheKey key) throws IOException {
try {
FLog.v(TAG, "Disk cache read for %s", key.getUriString());
final BinaryResource diskCacheResource = mFileCache.getResource(key);
if (diskCacheResource == null) {
FLog.v(TAG, "Disk cache miss for %s", key.getUriString());
mImageCacheStatsTracker.onDiskCacheMiss();
return null;
} else {
FLog.v(TAG, "Found entry in disk cache for %s", key.getUriString());
mImageCacheStatsTracker.onDiskCacheHit();
}
PooledByteBuffer byteBuffer;
final InputStream is = diskCacheResource.openStream();
try {
byteBuffer = mPooledByteBufferFactory.newByteBuffer(is, (int) diskCacheResource.size());
} finally {
is.close();
}
FLog.v(TAG, "Successful read from disk cache for %s", key.getUriString());
return byteBuffer;
} catch (IOException ioe) {
// TODO: 3697790 log failures
// TODO: 5258772 - uncomment line below
// mFileCache.remove(key);
FLog.w(TAG, ioe, "Exception reading from cache for %s", key.getUriString());
mImageCacheStatsTracker.onDiskCacheGetFail();
throw ioe;
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class HoneycombBitmapCreator method createNakedBitmap.
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public Bitmap createNakedBitmap(int width, int height, Bitmap.Config bitmapConfig) {
CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate((short) width, (short) height);
EncodedImage encodedImage = null;
CloseableReference<byte[]> encodedBytesArrayRef = null;
try {
encodedImage = new EncodedImage(jpgRef);
encodedImage.setImageFormat(DefaultImageFormats.JPEG);
BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
int length = jpgRef.get().size();
final PooledByteBuffer pooledByteBuffer = jpgRef.get();
encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
byte[] encodedBytesArray = encodedBytesArrayRef.get();
pooledByteBuffer.read(0, encodedBytesArray, 0, length);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
bitmap.setHasAlpha(true);
bitmap.eraseColor(Color.TRANSPARENT);
return bitmap;
} finally {
CloseableReference.closeSafely(encodedBytesArrayRef);
EncodedImage.closeSafely(encodedImage);
CloseableReference.closeSafely(jpgRef);
}
}
use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.
the class AnimatedImageFactoryImpl method decodeWebP.
/**
* Decode a WebP into a CloseableImage.
* @param encodedImage encoded image (native byte array holding the encoded bytes and meta data)
* @param options the options for the decode
* @param bitmapConfig the Bitmap.Config used to generate the output bitmaps
* @return a {@link CloseableImage} for the WebP image
*/
public CloseableImage decodeWebP(final EncodedImage encodedImage, final ImageDecodeOptions options, final Bitmap.Config bitmapConfig) {
if (sWebpAnimatedImageDecoder == null) {
throw new UnsupportedOperationException("To encode animated webp please add the dependency " + "to the animated-webp module");
}
final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
Preconditions.checkNotNull(bytesRef);
try {
final PooledByteBuffer input = bytesRef.get();
AnimatedImage webPImage = sWebpAnimatedImageDecoder.decode(input.getNativePtr(), input.size());
return getCloseableImage(options, webPImage, bitmapConfig);
} finally {
CloseableReference.closeSafely(bytesRef);
}
}
Aggregations