Search in sources :

Example 6 with CloseableAnimatedImage

use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.

the class AnimatedRepeatedPostprocessorProducerTest method testNonStaticBitmapIsPassedOn.

@Test
public void testNonStaticBitmapIsPassedOn() {
    RepeatedPostprocessorConsumer postprocessorConsumer = produceResults();
    RepeatedPostprocessorRunner repeatedPostprocessorRunner = getRunner();
    CloseableAnimatedImage sourceCloseableAnimatedImage = mock(CloseableAnimatedImage.class);
    CloseableReference<CloseableImage> sourceCloseableImageRef = CloseableReference.<CloseableImage>of(sourceCloseableAnimatedImage);
    postprocessorConsumer.onNewResult(sourceCloseableImageRef, Consumer.IS_LAST);
    sourceCloseableImageRef.close();
    mTestExecutorService.runUntilIdle();
    mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.NO_FLAGS));
    mInOrder.verifyNoMoreInteractions();
    assertEquals(1, mResults.size());
    CloseableReference<CloseableImage> res0 = mResults.get(0);
    assertTrue(CloseableReference.isValid(res0));
    assertSame(sourceCloseableAnimatedImage, res0.get());
    res0.close();
    performCancelAndVerifyOnCancellation();
    verify(sourceCloseableAnimatedImage).close();
}
Also used : RepeatedPostprocessorConsumer(com.facebook.imagepipeline.producers.PostprocessorProducer.RepeatedPostprocessorConsumer) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) RepeatedPostprocessorRunner(com.facebook.imagepipeline.request.RepeatedPostprocessorRunner)

Example 7 with CloseableAnimatedImage

use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.

the class AnimatedImageFactoryImpl method getCloseableImage.

private CloseableImage getCloseableImage(ImageDecodeOptions options, AnimatedImage image, Bitmap.Config bitmapConfig) {
    List<CloseableReference<Bitmap>> decodedFrames = null;
    CloseableReference<Bitmap> previewBitmap = null;
    try {
        final int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0;
        if (options.forceStaticImage) {
            return new CloseableStaticBitmap(createPreviewBitmap(image, bitmapConfig, frameForPreview), ImmutableQualityInfo.FULL_QUALITY, 0);
        }
        if (options.decodeAllFrames) {
            decodedFrames = decodeAllFrames(image, bitmapConfig);
            previewBitmap = CloseableReference.cloneOrNull(decodedFrames.get(frameForPreview));
        }
        if (options.decodePreviewFrame && previewBitmap == null) {
            previewBitmap = createPreviewBitmap(image, bitmapConfig, frameForPreview);
        }
        AnimatedImageResult animatedImageResult = AnimatedImageResult.newBuilder(image).setPreviewBitmap(previewBitmap).setFrameForPreview(frameForPreview).setDecodedFrames(decodedFrames).setBitmapTransformation(options.bitmapTransformation).build();
        return new CloseableAnimatedImage(animatedImageResult);
    } finally {
        CloseableReference.closeSafely(previewBitmap);
        CloseableReference.closeSafely(decodedFrames);
    }
}
Also used : Bitmap(android.graphics.Bitmap) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) CloseableReference(com.facebook.common.references.CloseableReference) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) SuppressLint(android.annotation.SuppressLint)

Example 8 with CloseableAnimatedImage

use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.

the class GifDecoder method decode.

@Override
public CloseableImage decode(final EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) {
    InputStream is = encodedImage.getInputStream();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GifMetadataDecoder decoder = GifMetadataDecoder.create(is, out);
        if (out.size() > 0) {
            // let's use the fixed gif version if exists
            is.close();
            is = new ByteArrayInputStream(out.toByteArray());
        }
        is.reset();
        Movie movie = Movie.decodeStream(is);
        MovieDrawer drawer = new MovieDrawer(movie);
        MovieFrame[] frames = new MovieFrame[decoder.getFrameCount()];
        int currTime = 0;
        for (int frameNumber = 0, N = frames.length; frameNumber < N; frameNumber++) {
            int frameDuration = decoder.getFrameDurationMs(frameNumber);
            currTime += frameDuration;
            frames[frameNumber] = new MovieFrame(drawer, currTime, frameDuration, movie.width(), movie.height(), translateFrameDisposal(decoder.getFrameDisposal(frameNumber)));
        }
        return new CloseableAnimatedImage(AnimatedImageResult.forAnimatedImage(new MovieAnimatedImage(frames, encodedImage.getSize(), movie.duration(), decoder.getLoopCount())), false);
    } catch (IOException e) {
        throw new RuntimeException("Error while decoding gif", e);
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : Movie(android.graphics.Movie) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) GifMetadataDecoder(com.facebook.animated.giflite.decoder.GifMetadataDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) MovieFrame(com.facebook.animated.giflite.draw.MovieFrame) MovieDrawer(com.facebook.animated.giflite.draw.MovieDrawer) MovieAnimatedImage(com.facebook.animated.giflite.draw.MovieAnimatedImage)

Example 9 with CloseableAnimatedImage

use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.

the class AnimatedImageFactoryGifImplTest method testCreateWithPreviewBitmap.

private void testCreateWithPreviewBitmap(GifImage mockGifImage, Bitmap mockBitmap, PooledByteBuffer byteBuffer) throws Exception {
    // For decoding preview frame, expect some calls.
    final AnimatedDrawableBackend mockAnimatedDrawableBackend = createAnimatedDrawableBackendMock(1);
    when(mMockAnimatedDrawableBackendProvider.get(any(AnimatedImageResult.class), isNull(Rect.class))).thenReturn(mockAnimatedDrawableBackend);
    when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG)).thenReturn(CloseableReference.of(mockBitmap, FAKE_BITMAP_RESOURCE_RELEASER));
    AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
    PowerMockito.whenNew(AnimatedImageCompositor.class).withAnyArguments().thenReturn(mockCompositor);
    ImageDecodeOptions imageDecodeOptions = ImageDecodeOptions.newBuilder().setDecodePreviewFrame(true).build();
    EncodedImage encodedImage = new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
    encodedImage.setImageFormat(ImageFormat.UNKNOWN);
    CloseableAnimatedImage closeableImage = (CloseableAnimatedImage) mAnimatedImageFactory.decodeGif(encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);
    // Verify we got the right result
    AnimatedImageResult imageResult = closeableImage.getImageResult();
    assertSame(mockGifImage, imageResult.getImage());
    assertNotNull(imageResult.getPreviewBitmap());
    assertFalse(imageResult.hasDecodedFrame(0));
    // Should not have interacted with these.
    verify(mMockAnimatedDrawableBackendProvider).get(any(AnimatedImageResult.class), isNull(Rect.class));
    verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
    verify(mMockBitmapFactory).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
    verifyNoMoreInteractions(mMockBitmapFactory);
    verify(mockCompositor).renderFrame(0, mockBitmap);
}
Also used : AnimatedDrawableBackend(com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend) Rect(android.graphics.Rect) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) AnimatedImageCompositor(com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor) ImageDecodeOptions(com.facebook.imagepipeline.common.ImageDecodeOptions) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Example 10 with CloseableAnimatedImage

use of com.facebook.imagepipeline.image.CloseableAnimatedImage in project fresco by facebook.

the class AnimatedImageFactoryGifImplTest method testCreateWithDecodeAlFrames.

private void testCreateWithDecodeAlFrames(GifImage mockGifImage, Bitmap mockBitmap1, Bitmap mockBitmap2, PooledByteBuffer byteBuffer) throws Exception {
    // For decoding preview frame, expect some calls.
    final AnimatedDrawableBackend mockAnimatedDrawableBackend = createAnimatedDrawableBackendMock(2);
    when(mMockAnimatedDrawableBackendProvider.get(any(AnimatedImageResult.class), isNull(Rect.class))).thenReturn(mockAnimatedDrawableBackend);
    when(mMockBitmapFactory.createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG)).thenReturn(CloseableReference.of(mockBitmap1, FAKE_BITMAP_RESOURCE_RELEASER)).thenReturn(CloseableReference.of(mockBitmap2, FAKE_BITMAP_RESOURCE_RELEASER));
    AnimatedImageCompositor mockCompositor = mock(AnimatedImageCompositor.class);
    PowerMockito.whenNew(AnimatedImageCompositor.class).withAnyArguments().thenReturn(mockCompositor);
    ImageDecodeOptions imageDecodeOptions = ImageDecodeOptions.newBuilder().setDecodePreviewFrame(true).setDecodeAllFrames(true).build();
    EncodedImage encodedImage = new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
    encodedImage.setImageFormat(ImageFormat.UNKNOWN);
    CloseableAnimatedImage closeableImage = (CloseableAnimatedImage) mAnimatedImageFactory.decodeGif(encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);
    // Verify we got the right result
    AnimatedImageResult imageResult = closeableImage.getImageResult();
    assertSame(mockGifImage, imageResult.getImage());
    assertNotNull(imageResult.getDecodedFrame(0));
    assertNotNull(imageResult.getDecodedFrame(1));
    assertNotNull(imageResult.getPreviewBitmap());
    // Should not have interacted with these.
    verify(mMockAnimatedDrawableBackendProvider).get(any(AnimatedImageResult.class), isNull(Rect.class));
    verifyNoMoreInteractions(mMockAnimatedDrawableBackendProvider);
    verify(mMockBitmapFactory, times(2)).createBitmapInternal(50, 50, DEFAULT_BITMAP_CONFIG);
    verifyNoMoreInteractions(mMockBitmapFactory);
    verify(mockCompositor).renderFrame(0, mockBitmap1);
    verify(mockCompositor).renderFrame(1, mockBitmap2);
}
Also used : AnimatedDrawableBackend(com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend) Rect(android.graphics.Rect) CloseableAnimatedImage(com.facebook.imagepipeline.image.CloseableAnimatedImage) AnimatedImageResult(com.facebook.imagepipeline.animated.base.AnimatedImageResult) AnimatedImageCompositor(com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor) ImageDecodeOptions(com.facebook.imagepipeline.common.ImageDecodeOptions) EncodedImage(com.facebook.imagepipeline.image.EncodedImage)

Aggregations

CloseableAnimatedImage (com.facebook.imagepipeline.image.CloseableAnimatedImage)19 AnimatedImageResult (com.facebook.imagepipeline.animated.base.AnimatedImageResult)14 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)12 Rect (android.graphics.Rect)8 AnimatedDrawableBackend (com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend)8 AnimatedImageCompositor (com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor)8 ImageDecodeOptions (com.facebook.imagepipeline.common.ImageDecodeOptions)8 Bitmap (android.graphics.Bitmap)6 TrivialPooledByteBuffer (com.facebook.imagepipeline.testing.TrivialPooledByteBuffer)6 Test (org.junit.Test)6 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)6 CloseableReference (com.facebook.common.references.CloseableReference)4 GifImage (com.facebook.animated.gif.GifImage)3 WebPImage (com.facebook.animated.webp.WebPImage)3 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)3 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)2 SuppressLint (android.annotation.SuppressLint)1 Movie (android.graphics.Movie)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Uri (android.net.Uri)1