Search in sources :

Example 21 with PooledByteBuffer

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

the class DalvikPurgeableDecoder method decodeFromEncodedImage.

/**
   * Creates a bitmap from encoded bytes.
   *
   * @param encodedImage the encoded image with reference to the encoded bytes
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config}
   * used to create the decoded Bitmap
   * @return the bitmap
   * @throws TooManyBitmapsException if the pool is full
   * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
@Override
public CloseableReference<Bitmap> decodeFromEncodedImage(final EncodedImage encodedImage, Bitmap.Config bitmapConfig) {
    BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
    CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
    Preconditions.checkNotNull(bytesRef);
    try {
        Bitmap bitmap = decodeByteArrayAsPurgeable(bytesRef, options);
        return pinBitmap(bitmap);
    } finally {
        CloseableReference.closeSafely(bytesRef);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) BitmapFactory(android.graphics.BitmapFactory)

Example 22 with PooledByteBuffer

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

the class DalvikPurgeableDecoder method decodeJPEGFromEncodedImage.

/**
   * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
   *
   * @param encodedImage the encoded image with reference to the encoded bytes
   * @param length the number of encoded bytes in the buffer
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config}
   * used to create the decoded Bitmap
   * @return the bitmap
   * @throws TooManyBitmapsException if the pool is full
   * @throws java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(final EncodedImage encodedImage, Bitmap.Config bitmapConfig, int length) {
    BitmapFactory.Options options = getBitmapFactoryOptions(encodedImage.getSampleSize(), bitmapConfig);
    final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
    Preconditions.checkNotNull(bytesRef);
    try {
        Bitmap bitmap = decodeJPEGByteArrayAsPurgeable(bytesRef, length, options);
        return pinBitmap(bitmap);
    } finally {
        CloseableReference.closeSafely(bytesRef);
    }
}
Also used : Bitmap(android.graphics.Bitmap) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) BitmapFactory(android.graphics.BitmapFactory)

Example 23 with PooledByteBuffer

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

the class WebpTranscodeProducer method transcodeLastResult.

private void transcodeLastResult(final EncodedImage originalResult, final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    Preconditions.checkNotNull(originalResult);
    final EncodedImage encodedImageCopy = EncodedImage.cloneOrNull(originalResult);
    final StatefulProducerRunnable<EncodedImage> runnable = new StatefulProducerRunnable<EncodedImage>(consumer, producerContext.getListener(), PRODUCER_NAME, producerContext.getId()) {

        @Override
        protected EncodedImage getResult() throws Exception {
            PooledByteBufferOutputStream outputStream = mPooledByteBufferFactory.newOutputStream();
            try {
                doTranscode(encodedImageCopy, outputStream);
                CloseableReference<PooledByteBuffer> ref = CloseableReference.of(outputStream.toByteBuffer());
                try {
                    EncodedImage encodedImage = new EncodedImage(ref);
                    encodedImage.copyMetaDataFrom(encodedImageCopy);
                    return encodedImage;
                } finally {
                    CloseableReference.closeSafely(ref);
                }
            } finally {
                outputStream.close();
            }
        }

        @Override
        protected void disposeResult(EncodedImage result) {
            EncodedImage.closeSafely(result);
        }

        @Override
        protected void onSuccess(EncodedImage result) {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onSuccess(result);
        }

        @Override
        protected void onFailure(Exception e) {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onFailure(e);
        }

        @Override
        protected void onCancellation() {
            EncodedImage.closeSafely(encodedImageCopy);
            super.onCancellation();
        }
    };
    mExecutor.execute(runnable);
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) PooledByteBufferOutputStream(com.facebook.common.memory.PooledByteBufferOutputStream) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 24 with PooledByteBuffer

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

the class EncodedMemoryCacheProducer method produceResults.

@Override
public void produceResults(final Consumer<EncodedImage> consumer, final ProducerContext producerContext) {
    final String requestId = producerContext.getId();
    final ProducerListener listener = producerContext.getListener();
    listener.onProducerStart(requestId, PRODUCER_NAME);
    final ImageRequest imageRequest = producerContext.getImageRequest();
    final CacheKey cacheKey = mCacheKeyFactory.getEncodedCacheKey(imageRequest, producerContext.getCallerContext());
    CloseableReference<PooledByteBuffer> cachedReference = mMemoryCache.get(cacheKey);
    try {
        if (cachedReference != null) {
            EncodedImage cachedEncodedImage = new EncodedImage(cachedReference);
            cachedEncodedImage.setEncodedCacheKey(cacheKey);
            try {
                listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "true") : null);
                consumer.onProgressUpdate(1f);
                consumer.onNewResult(cachedEncodedImage, true);
                return;
            } finally {
                EncodedImage.closeSafely(cachedEncodedImage);
            }
        }
        if (producerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE.getValue()) {
            listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
            consumer.onNewResult(null, true);
            return;
        }
        Consumer consumerOfInputProducer = new EncodedMemoryCacheConsumer(consumer, mMemoryCache, cacheKey);
        listener.onProducerFinishWithSuccess(requestId, PRODUCER_NAME, listener.requiresExtraMap(requestId) ? ImmutableMap.of(EXTRA_CACHED_VALUE_FOUND, "false") : null);
        mInputProducer.produceResults(consumerOfInputProducer, producerContext);
    } finally {
        CloseableReference.closeSafely(cachedReference);
    }
}
Also used : ImageRequest(com.facebook.imagepipeline.request.ImageRequest) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey)

Example 25 with PooledByteBuffer

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

the class EncodedImage method isCompleteAt.

/**
   * Returns true if the image is a JPEG and its data is already complete at the specified length,
   * false otherwise.
   */
public boolean isCompleteAt(int length) {
    if (mImageFormat != DefaultImageFormats.JPEG) {
        return true;
    }
    // If the image is backed by FileInputStreams return true since they will always be complete.
    if (mInputStreamSupplier != null) {
        return true;
    }
    // The image should be backed by a ByteBuffer
    Preconditions.checkNotNull(mPooledByteBufferRef);
    PooledByteBuffer buf = mPooledByteBufferRef.get();
    return (buf.read(length - 2) == (byte) JfifUtil.MARKER_FIRST_BYTE) && (buf.read(length - 1) == (byte) JfifUtil.MARKER_EOI);
}
Also used : PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer)

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