Search in sources :

Example 26 with PooledByteBuffer

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));
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)

Example 27 with PooledByteBuffer

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());
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer) TrivialPooledByteBuffer(com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)

Example 28 with PooledByteBuffer

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;
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) InputStream(java.io.InputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) IOException(java.io.IOException)

Example 29 with PooledByteBuffer

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);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) BitmapFactory(android.graphics.BitmapFactory) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) TargetApi(android.annotation.TargetApi)

Example 30 with PooledByteBuffer

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);
    }
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) AnimatedImage(com.facebook.imagepipeline.animated.base.AnimatedImage) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage)

Aggregations

PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)34 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)14 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)7 InputStream (java.io.InputStream)7 Bitmap (android.graphics.Bitmap)6 BitmapFactory (android.graphics.BitmapFactory)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 TargetApi (android.annotation.TargetApi)2 Uri (android.net.Uri)2 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)2 AnimatedImage (com.facebook.imagepipeline.animated.base.AnimatedImage)2 CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)2 BitmapCounter (com.facebook.imagepipeline.memory.BitmapCounter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 Rect (android.graphics.Rect)1 ExifInterface (android.media.ExifInterface)1 BinaryResource (com.facebook.binaryresource.BinaryResource)1 CacheKey (com.facebook.cache.common.CacheKey)1 PooledByteBufferOutputStream (com.facebook.common.memory.PooledByteBufferOutputStream)1