Search in sources :

Example 11 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class HoneycombBitmapFactory method createBitmapInternal.

/**
   * Creates a bitmap of the specified width and height.
   *
   * @param width the width of the bitmap
   * @param height the height of the bitmap
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config}
   * used to create the decoded Bitmap
   * @return a reference to the bitmap
   * @throws TooManyBitmapsException if the pool is full
   * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public CloseableReference<Bitmap> createBitmapInternal(int width, int height, Bitmap.Config bitmapConfig) {
    CloseableReference<PooledByteBuffer> jpgRef = mJpegGenerator.generate((short) width, (short) height);
    try {
        EncodedImage encodedImage = new EncodedImage(jpgRef);
        encodedImage.setImageFormat(DefaultImageFormats.JPEG);
        try {
            CloseableReference<Bitmap> bitmapRef = mPurgeableDecoder.decodeJPEGFromEncodedImage(encodedImage, bitmapConfig, jpgRef.get().size());
            bitmapRef.get().setHasAlpha(true);
            bitmapRef.get().eraseColor(Color.TRANSPARENT);
            return bitmapRef;
        } finally {
            EncodedImage.closeSafely(encodedImage);
        }
    } finally {
        jpgRef.close();
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) TargetApi(android.annotation.TargetApi)

Example 12 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class KitKatPurgeableDecoder method decodeJPEGByteArrayAsPurgeable.

/**
   * Decodes a byteArray containing jpeg encoded bytes into a purgeable bitmap
   *
   * <p> Adds a JFIF End-Of-Image marker if needed before decoding.
   *
   * @param bytesRef the byte buffer that contains the encoded bytes
   * @return
   */
@Override
protected Bitmap decodeJPEGByteArrayAsPurgeable(CloseableReference<PooledByteBuffer> bytesRef, int length, BitmapFactory.Options options) {
    byte[] suffix = endsWithEOI(bytesRef, length) ? null : EOI;
    final PooledByteBuffer pooledByteBuffer = bytesRef.get();
    Preconditions.checkArgument(length <= pooledByteBuffer.size());
    // allocate bigger array in case EOI needs to be added
    final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length + 2);
    try {
        byte[] encodedBytesArray = encodedBytesArrayRef.get();
        pooledByteBuffer.read(0, encodedBytesArray, 0, length);
        if (suffix != null) {
            putEOI(encodedBytesArray, length);
            length += 2;
        }
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
        return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
    } finally {
        CloseableReference.closeSafely(encodedBytesArrayRef);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer)

Example 13 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class KitKatPurgeableDecoder method decodeByteArrayAsPurgeable.

/**
   * Decodes a byteArray into a purgeable bitmap
   *
   * @param bytesRef the byte buffer that contains the encoded bytes
   * @return
   */
@Override
protected Bitmap decodeByteArrayAsPurgeable(CloseableReference<PooledByteBuffer> bytesRef, BitmapFactory.Options options) {
    final PooledByteBuffer pooledByteBuffer = bytesRef.get();
    final int length = pooledByteBuffer.size();
    final CloseableReference<byte[]> encodedBytesArrayRef = mFlexByteArrayPool.get(length);
    try {
        final byte[] encodedBytesArray = encodedBytesArrayRef.get();
        pooledByteBuffer.read(0, encodedBytesArray, 0, length);
        Bitmap bitmap = BitmapFactory.decodeByteArray(encodedBytesArray, 0, length, options);
        return Preconditions.checkNotNull(bitmap, "BitmapFactory returned null");
    } finally {
        CloseableReference.closeSafely(encodedBytesArrayRef);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer)

Example 14 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class StagingArea method remove.

/**
   * Removes key-value from the StagingArea. Both key and value must match.
   * @param key
   * @param encodedImage value corresponding to key
   * @return true if item was removed
   */
public synchronized boolean remove(final CacheKey key, final EncodedImage encodedImage) {
    Preconditions.checkNotNull(key);
    Preconditions.checkNotNull(encodedImage);
    Preconditions.checkArgument(EncodedImage.isValid(encodedImage));
    final EncodedImage oldValue = mMap.get(key);
    if (oldValue == null) {
        return false;
    }
    CloseableReference<PooledByteBuffer> oldRef = oldValue.getByteBufferRef();
    CloseableReference<PooledByteBuffer> ref = encodedImage.getByteBufferRef();
    try {
        if (oldRef == null || ref == null || oldRef.get() != ref.get()) {
            return false;
        }
        mMap.remove(key);
    } finally {
        CloseableReference.closeSafely(ref);
        CloseableReference.closeSafely(oldRef);
        EncodedImage.closeSafely(oldValue);
    }
    logStats();
    return true;
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 15 with PooledByteBuffer

use of com.facebook.common.memory.PooledByteBuffer in project fresco by facebook.

the class EncodedImageTest method testIsJpegCompleteAt_notComplete.

@Test
public void testIsJpegCompleteAt_notComplete() {
    byte[] encodedBytes = new byte[ENCODED_BYTES_LENGTH];
    encodedBytes[ENCODED_BYTES_LENGTH - 2] = 0;
    encodedBytes[ENCODED_BYTES_LENGTH - 1] = 0;
    PooledByteBuffer buf = new TrivialPooledByteBuffer(encodedBytes);
    EncodedImage encodedImage = new EncodedImage(CloseableReference.of(buf));
    encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    assertFalse(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)

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