com.facebook.imagepipeline.image.CloseableStaticBitmap Java Examples

The following examples show how to use com.facebook.imagepipeline.image.CloseableStaticBitmap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoxingFrescoLoader.java    From boxing with Apache License 2.0 6 votes vote down vote up
private Drawable createDrawableFromFetchedResult(Context context, CloseableImage image) {
    if (image instanceof CloseableStaticBitmap) {
        CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image;
        BitmapDrawable bitmapDrawable = createBitmapDrawable(context, closeableStaticBitmap.getUnderlyingBitmap());
        return (closeableStaticBitmap.getRotationAngle() != 0 && closeableStaticBitmap.getRotationAngle() != -1 ? new OrientedDrawable(bitmapDrawable, closeableStaticBitmap.getRotationAngle()) : bitmapDrawable);
    } else if (image instanceof CloseableAnimatedImage) {
        AnimatedDrawableFactory animatedDrawableFactory = Fresco.getImagePipelineFactory().getAnimatedFactory().getAnimatedDrawableFactory(context);
        if (animatedDrawableFactory != null) {
            AnimatedDrawable animatedDrawable = (AnimatedDrawable) animatedDrawableFactory.create(image);
            if (animatedDrawable != null) {
                return animatedDrawable;
            }
        }
    }
    throw new UnsupportedOperationException("Unrecognized image class: " + image);
}
 
Example #2
Source File: FrescoVitoRegionDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @param length if image type supports decoding incomplete image then determines where the image
 *     data should be cut for decoding.
 * @param qualityInfo quality information for the image
 * @param options options that can change decode behavior
 */
@Override
public CloseableImage decode(
    EncodedImage encodedImage, int length, QualityInfo qualityInfo, ImageDecodeOptions options) {

  Rect regionToDecode = computeRegionToDecode(encodedImage, options);

  CloseableReference<Bitmap> decodedBitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, regionToDecode, length, options.colorSpace);
  try {
    maybeApplyTransformation(options.bitmapTransformation, decodedBitmapReference);
    return new CloseableStaticBitmap(
        decodedBitmapReference,
        ImmutableQualityInfo.FULL_QUALITY,
        encodedImage.getRotationAngle(),
        encodedImage.getExifOrientation());
  } finally {
    CloseableReference.closeSafely(decodedBitmapReference);
  }
}
 
Example #3
Source File: BitmapDrawableFactory.java    From fresco with MIT License 6 votes vote down vote up
@Override
@Nullable
public Drawable createDrawable(CloseableImage closeableImage, ImageOptions imageOptions) {
  try {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.beginSection("BitmapDrawableFactory#createDrawable");
    }
    if (closeableImage instanceof CloseableStaticBitmap) {
      return handleCloseableStaticBitmap((CloseableStaticBitmap) closeableImage, imageOptions);
    }
    return null;
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}
 
Example #4
Source File: AnimatedRepeatedPostprocessorProducerTest.java    From fresco with MIT License 6 votes vote down vote up
private void verifyNewResultProcessed(int index, Bitmap destBitmap) {
  mInOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, PostprocessorProducer.NAME);
  mInOrder.verify(mPostprocessor).process(mSourceBitmap, mPlatformBitmapFactory);
  mInOrder
      .verify(mProducerListener)
      .requiresExtraMap(mProducerContext, PostprocessorProducer.NAME);
  mInOrder
      .verify(mProducerListener)
      .onProducerFinishWithSuccess(mProducerContext, PostprocessorProducer.NAME, mExtraMap);
  mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.NO_FLAGS));
  mInOrder.verifyNoMoreInteractions();

  assertEquals(index + 1, mResults.size());
  CloseableReference<CloseableImage> res0 = mResults.get(index);
  assertTrue(CloseableReference.isValid(res0));
  assertSame(destBitmap, ((CloseableStaticBitmap) res0.get()).getUnderlyingBitmap());
  res0.close();
  verify(mBitmapResourceReleaser).release(destBitmap);
}
 
Example #5
Source File: BitmapDrawableFactoryTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCreateDrawable_whenRoundAsCircle_thenReturnBitmapDrawable() {
  final CloseableStaticBitmap closeableImage = mock(CloseableStaticBitmap.class);
  final Bitmap bitmap = mock(Bitmap.class);
  when(closeableImage.getUnderlyingBitmap()).thenReturn(bitmap);

  final ImageOptions options = mock(ImageOptions.class);
  when(options.getRoundingOptions()).thenReturn(RoundingOptions.asCircle());

  when(mExperiments.useNativeRounding()).thenReturn(false);

  Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
  assertThat(drawable).isNotNull();
  assertThat(drawable).isInstanceOf(RoundedBitmapDrawable.class);

  when(mExperiments.useNativeRounding()).thenReturn(true);

  drawable = mDrawableFactory.createDrawable(closeableImage, options);
  assertThat(drawable).isNotNull();
  assertThat(drawable).isInstanceOf(BitmapDrawable.class);
}
 
Example #6
Source File: BaseBitmapReferenceDataSubscriber.java    From fresco with MIT License 6 votes vote down vote up
@Override
public void onNewResultImpl(@Nonnull DataSource<CloseableReference<CloseableImage>> dataSource) {
  if (!dataSource.isFinished()) {
    return;
  }

  CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
  CloseableReference<Bitmap> bitmapReference = null;
  if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableStaticBitmap) {
    bitmapReference =
        ((CloseableStaticBitmap) closeableImageRef.get()).cloneUnderlyingBitmapReference();
  }

  try {
    onNewResultImpl(bitmapReference);
  } finally {
    CloseableReference.closeSafely(bitmapReference);
    CloseableReference.closeSafely(closeableImageRef);
  }
}
 
Example #7
Source File: FrescoFrameCache.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Converts the given image reference to a bitmap reference and closes the original image
 * reference.
 *
 * @param closeableImage the image to convert. It will be closed afterwards and will be invalid
 * @return the closeable bitmap reference to be used
 */
@VisibleForTesting
@Nullable
static CloseableReference<Bitmap> convertToBitmapReferenceAndClose(
    final @Nullable CloseableReference<CloseableImage> closeableImage) {
  try {
    if (CloseableReference.isValid(closeableImage)
        && closeableImage.get() instanceof CloseableStaticBitmap) {

      CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage.get();
      if (closeableStaticBitmap != null) {
        // We return a clone of the underlying bitmap reference that has to be manually closed
        // and then close the passed CloseableStaticBitmap in order to preserve correct
        // cache size calculations.
        return closeableStaticBitmap.cloneUnderlyingBitmapReference();
      }
    }
    // Not a bitmap reference, so we return null
    return null;
  } finally {
    CloseableReference.closeSafely(closeableImage);
  }
}
 
Example #8
Source File: PostprocessorProducer.java    From fresco with MIT License 6 votes vote down vote up
private CloseableReference<CloseableImage> postprocessInternal(CloseableImage sourceImage) {
  CloseableStaticBitmap staticBitmap = (CloseableStaticBitmap) sourceImage;
  Bitmap sourceBitmap = staticBitmap.getUnderlyingBitmap();
  CloseableReference<Bitmap> bitmapRef = mPostprocessor.process(sourceBitmap, mBitmapFactory);
  int rotationAngle = staticBitmap.getRotationAngle();
  int exifOrientation = staticBitmap.getExifOrientation();
  try {
    CloseableStaticBitmap closeableStaticBitmap =
        new CloseableStaticBitmap(
            bitmapRef, sourceImage.getQualityInfo(), rotationAngle, exifOrientation);
    closeableStaticBitmap.setImageExtras(staticBitmap.getExtras());
    return CloseableReference.<CloseableImage>of(closeableStaticBitmap);
  } finally {
    CloseableReference.closeSafely(bitmapRef);
  }
}
 
Example #9
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * @param encodedImage input image (encoded bytes plus meta data)
 * @return a CloseableStaticBitmap
 */
public CloseableStaticBitmap decodeStaticImage(
    final EncodedImage encodedImage, ImageDecodeOptions options) {
  CloseableReference<Bitmap> bitmapReference =
      mPlatformDecoder.decodeFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, null, options.colorSpace);
  try {
    maybeApplyTransformation(options.bitmapTransformation, bitmapReference);
    return new CloseableStaticBitmap(
        bitmapReference,
        ImmutableQualityInfo.FULL_QUALITY,
        encodedImage.getRotationAngle(),
        encodedImage.getExifOrientation());
  } finally {
    bitmapReference.close();
  }
}
 
Example #10
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @param length amount of currently available data in bytes
 * @param qualityInfo quality info for the image
 * @return a CloseableStaticBitmap
 */
public CloseableStaticBitmap decodeJpeg(
    final EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  CloseableReference<Bitmap> bitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, null, length, options.colorSpace);
  try {
    maybeApplyTransformation(options.bitmapTransformation, bitmapReference);
    return new CloseableStaticBitmap(
        bitmapReference,
        qualityInfo,
        encodedImage.getRotationAngle(),
        encodedImage.getExifOrientation());
  } finally {
    bitmapReference.close();
  }
}
 
Example #11
Source File: CloseableImageCopier.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private CloseableReference<CloseableImage> copyCloseableStaticBitmap(
    final CloseableReference<CloseableImage> closeableStaticBitmapRef) {
  Bitmap sourceBitmap = ((CloseableStaticBitmap) closeableStaticBitmapRef.get())
      .getUnderlyingBitmap();
  CloseableReference<Bitmap> bitmapRef = mPlatformBitmapFactory.createBitmap(
      sourceBitmap.getWidth(),
      sourceBitmap.getHeight());
  try {
    Bitmap destinationBitmap = bitmapRef.get();
    Preconditions.checkState(!destinationBitmap.isRecycled());
    Preconditions.checkState(destinationBitmap.isMutable());
    Bitmaps.copyBitmap(destinationBitmap, sourceBitmap);
    return CloseableReference.<CloseableImage>of(
        new CloseableStaticBitmap(bitmapRef, ImmutableQualityInfo.FULL_QUALITY));
  } finally {
    bitmapRef.close();
  }
}
 
Example #12
Source File: RepeatedPostprocessorProducerTest.java    From fresco with MIT License 6 votes vote down vote up
private void verifyNewResultProcessed(int index, Bitmap destBitmap) {
  mInOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, PostprocessorProducer.NAME);
  mInOrder.verify(mPostprocessor).process(mSourceBitmap, mPlatformBitmapFactory);
  mInOrder
      .verify(mProducerListener)
      .requiresExtraMap(mProducerContext, PostprocessorProducer.NAME);
  mInOrder
      .verify(mProducerListener)
      .onProducerFinishWithSuccess(
          eq(mProducerContext), eq(PostprocessorProducer.NAME), eq(mExtraMap));
  mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.NO_FLAGS));
  mInOrder.verifyNoMoreInteractions();

  assertEquals(index + 1, mResults.size());
  CloseableReference<CloseableImage> res0 = mResults.get(index);
  assertTrue(CloseableReference.isValid(res0));
  assertSame(destBitmap, ((CloseableStaticBitmap) res0.get()).getUnderlyingBitmap());
  res0.close();
  verify(mBitmapResourceReleaser).release(destBitmap);
}
 
Example #13
Source File: OverlayMarker.java    From react-native-baidu-map with MIT License 6 votes vote down vote up
@Override
public void onFinalImageSet(String id, final ImageInfo imageInfo, Animatable animatable) {
    Log.i("onFinalImageSet", id);
    CloseableReference<CloseableImage> imageReference = null;
    try {
        imageReference = dataSource.getResult();
        if (imageReference != null) {
            CloseableImage image = imageReference.get();
            if (image != null && image instanceof CloseableStaticBitmap) {
                CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image;
                Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap();
                if (bitmap != null) {
                    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                    iconBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
                    BITMAP_DESCRIPTOR_MAP.put(iconInfo.getUri(), iconBitmapDescriptor);
                }
            }
        }
    } finally {
        dataSource.close();
        if (imageReference != null) {
            CloseableReference.closeSafely(imageReference);
        }
        loadingImage = false;
    }
}
 
Example #14
Source File: BitmapPrepareProducer.java    From fresco with MIT License 5 votes vote down vote up
private void internalPrepareBitmap(CloseableReference<CloseableImage> newResult) {
  if (newResult == null || !newResult.isValid()) {
    return;
  }

  final CloseableImage closeableImage = newResult.get();
  if (closeableImage == null || closeableImage.isClosed()) {
    return;
  }

  if (closeableImage instanceof CloseableStaticBitmap) {
    final CloseableStaticBitmap staticBitmap = (CloseableStaticBitmap) closeableImage;
    final Bitmap bitmap = staticBitmap.getUnderlyingBitmap();
    if (bitmap == null) {
      return;
    }

    final int bitmapByteCount = bitmap.getRowBytes() * bitmap.getHeight();
    if (bitmapByteCount < mMinBitmapSizeBytes) {
      return;
    }
    if (bitmapByteCount > mMaxBitmapSizeBytes) {
      return;
    }

    bitmap.prepareToDraw();
  }
}
 
Example #15
Source File: SingleUsePostprocessorProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  mTestExecutorService = new TestExecutorService(new FakeClock());
  mPostprocessorProducer =
      new PostprocessorProducer(mInputProducer, mPlatformBitmapFactory, mTestExecutorService);

  when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
  when(mProducerContext.getId()).thenReturn(mRequestId);
  when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
  when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);

  mResults = new ArrayList<>();
  when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME);
  when(mProducerListener.requiresExtraMap(mProducerContext, PRODUCER_NAME)).thenReturn(true);
  doAnswer(
          new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
              mResults.add(
                  ((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone());
              return null;
            }
          })
      .when(mConsumer)
      .onNewResult(any(CloseableReference.class), anyInt());
  mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer);

  mSourceBitmap = mock(Bitmap.class);
  mSourceCloseableStaticBitmap = mock(CloseableStaticBitmap.class);
  when(mSourceCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mSourceBitmap);
  mSourceCloseableImageRef = CloseableReference.<CloseableImage>of(mSourceCloseableStaticBitmap);
  mDestinationBitmap = mock(Bitmap.class);
  mDestinationCloseableBitmapRef =
      CloseableReference.of(mDestinationBitmap, mBitmapResourceReleaser);
}
 
Example #16
Source File: SingleUsePostprocessorProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testSuccess() {
  SingleUsePostprocessorConsumer postprocessorConsumer = produceResults();
  doReturn(mDestinationCloseableBitmapRef)
      .when(mPostprocessor)
      .process(mSourceBitmap, mPlatformBitmapFactory);
  postprocessorConsumer.onNewResult(mSourceCloseableImageRef, Consumer.IS_LAST);
  mSourceCloseableImageRef.close();
  mTestExecutorService.runUntilIdle();

  mInOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, PostprocessorProducer.NAME);
  mInOrder.verify(mPostprocessor).process(mSourceBitmap, mPlatformBitmapFactory);
  mInOrder
      .verify(mProducerListener)
      .requiresExtraMap(mProducerContext, PostprocessorProducer.NAME);
  mInOrder
      .verify(mProducerListener)
      .onProducerFinishWithSuccess(mProducerContext, PostprocessorProducer.NAME, mExtraMap);
  mInOrder.verify(mConsumer).onNewResult(any(CloseableReference.class), eq(Consumer.IS_LAST));
  mInOrder.verifyNoMoreInteractions();

  assertEquals(1, mResults.size());
  CloseableReference<CloseableImage> res0 = mResults.get(0);
  assertTrue(CloseableReference.isValid(res0));
  assertSame(mDestinationBitmap, ((CloseableStaticBitmap) res0.get()).getUnderlyingBitmap());
  res0.close();

  verify(mBitmapResourceReleaser).release(mDestinationBitmap);
  verify(mSourceCloseableStaticBitmap).close();
}
 
Example #17
Source File: BitmapDrawableFactoryTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testCreateDrawable_whenRoundWithCornerRadii_thenReturnBitmapDrawable() {
  final CloseableStaticBitmap closeableImage = mock(CloseableStaticBitmap.class);
  final Bitmap bitmap = mock(Bitmap.class);
  when(closeableImage.getUnderlyingBitmap()).thenReturn(bitmap);

  final ImageOptions options = mock(ImageOptions.class);
  when(options.getRoundingOptions()).thenReturn(RoundingOptions.forCornerRadii(1, 2, 3, 4));

  Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
  assertThat(drawable).isNotNull();
  assertThat(drawable).isInstanceOf(RoundedBitmapDrawable.class);
  assertThat(((RoundedBitmapDrawable) drawable).getRadii())
      .isEqualTo(new float[] {1, 1, 2, 2, 3, 3, 4, 4});
}
 
Example #18
Source File: BitmapDrawableFactoryTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testCreateDrawable_whenRoundWithCornerRadius_thenReturnBitmapDrawable() {
  final CloseableStaticBitmap closeableImage = mock(CloseableStaticBitmap.class);
  final Bitmap bitmap = mock(Bitmap.class);
  when(closeableImage.getUnderlyingBitmap()).thenReturn(bitmap);

  final ImageOptions options = mock(ImageOptions.class);
  when(options.getRoundingOptions()).thenReturn(RoundingOptions.forCornerRadiusPx(123));

  Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
  assertThat(drawable).isNotNull();
  assertThat(drawable).isInstanceOf(RoundedBitmapDrawable.class);
  assertThat(((RoundedBitmapDrawable) drawable).getRadii())
      .isEqualTo(new float[] {123, 123, 123, 123, 123, 123, 123, 123});
}
 
Example #19
Source File: FrescoUtil.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
public static void putIntoPool(Bitmap bitmap,String uriString) {
        final ImageRequest requestBmp = ImageRequest.fromUri(uriString); // 赋值

// 获得 Key
        CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getBitmapCacheKey(requestBmp, ImageLoader.context);

// 获得 closeableReference
        CloseableReference<CloseableImage> closeableReference = CloseableReference.<CloseableImage>of(
            new CloseableStaticBitmap(bitmap,
                SimpleBitmapReleaser.getInstance(),
                ImmutableQualityInfo.FULL_QUALITY, 0));
// 存入 Fresco
        Fresco.getImagePipelineFactory().getBitmapMemoryCache().cache(cacheKey, closeableReference);
    }
 
Example #20
Source File: BitmapDrawableFactoryTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testCreateDrawable_whenImageIsStaticBitmap_thenReturnBitmapDrawable() {
  final CloseableStaticBitmap closeableImage = mock(CloseableStaticBitmap.class);
  final Bitmap bitmap = mock(Bitmap.class);
  when(closeableImage.getUnderlyingBitmap()).thenReturn(bitmap);

  final ImageOptions options = mock(ImageOptions.class);

  Drawable drawable = mDrawableFactory.createDrawable(closeableImage, options);
  assertThat(drawable).isNotNull();
  assertThat(drawable).isInstanceOf(BitmapDrawable.class);
}
 
Example #21
Source File: ImagePipelineRegionDecodingFragment.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  CloseableReference<Bitmap> decodedBitmapReference =
      mPlatformDecoder.decodeJPEGFromEncodedImageWithColorSpace(
          encodedImage, options.bitmapConfig, mRegion, length, options.colorSpace);
  try {
    return new CloseableStaticBitmap(decodedBitmapReference, qualityInfo, 0);
  } finally {
    CloseableReference.closeSafely(decodedBitmapReference);
  }
}
 
Example #22
Source File: AMapMarker.java    From react-native-amap with MIT License 5 votes vote down vote up
@Override
public void onFinalImageSet(
        String id,
        @Nullable final ImageInfo imageInfo,
        @Nullable Animatable animatable) {
    CloseableReference<CloseableImage> imageReference = null;
    try {
        imageReference = dataSource.getResult();
        if (imageReference != null) {
            CloseableImage image = imageReference.get();
            if (image != null && image instanceof CloseableStaticBitmap) {
                CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image;
                Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap();
                if (bitmap != null) {
                    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                    iconBitmap = bitmap;
                    iconBitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
                }
            }
        }
    } finally {
        dataSource.close();
        if (imageReference != null) {
            CloseableReference.closeSafely(imageReference);
        }
    }
    update();
}
 
Example #23
Source File: BitmapDrawableFactory.java    From fresco with MIT License 5 votes vote down vote up
protected Drawable rotatedDrawable(
    CloseableStaticBitmap closeableStaticBitmap, Drawable drawable) {
  if (!hasTransformableRotationAngle(closeableStaticBitmap)
      && !hasTransformableExifOrientation(closeableStaticBitmap)) {
    // Return the bitmap drawable directly as there's nothing to transform in it
    return drawable;
  } else {
    return new OrientedDrawable(
        drawable,
        closeableStaticBitmap.getRotationAngle(),
        closeableStaticBitmap.getExifOrientation());
  }
}
 
Example #24
Source File: ImageDecoder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param pooledByteBufferRef input image (encoded bytes)
 * @return a CloseableStaticBitmap
 */
public synchronized CloseableStaticBitmap decodeStaticImage(
    final CloseableReference<PooledByteBuffer> pooledByteBufferRef) {
  CloseableReference<Bitmap> bitmapReference =
      mBitmapFactoryWithPool.decodeFromPooledByteBuffer(pooledByteBufferRef);
  try {
    return new CloseableStaticBitmap(bitmapReference, ImmutableQualityInfo.FULL_QUALITY);
  } finally {
    bitmapReference.close();
  }
}
 
Example #25
Source File: AnimatedSingleUsePostprocessorProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  mTestExecutorService = new TestExecutorService(new FakeClock());
  mPostprocessorProducer =
      new PostprocessorProducer(mInputProducer, mPlatformBitmapFactory, mTestExecutorService);

  when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
  when(mProducerContext.getId()).thenReturn(mRequestId);
  when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
  when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);

  mResults = new ArrayList<>();
  when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME);
  when(mProducerListener.requiresExtraMap(eq(mProducerContext), eq(POSTPROCESSOR_NAME)))
      .thenReturn(true);
  doAnswer(
          new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
              mResults.add(
                  ((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone());
              return null;
            }
          })
      .when(mConsumer)
      .onNewResult(any(CloseableReference.class), anyInt());
  mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer);

  mSourceBitmap = mock(Bitmap.class);
  mSourceCloseableStaticBitmap = mock(CloseableStaticBitmap.class);
  when(mSourceCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mSourceBitmap);
  mSourceCloseableImageRef = CloseableReference.<CloseableImage>of(mSourceCloseableStaticBitmap);
  mDestinationBitmap = mock(Bitmap.class);
  mDestinationCloseableBitmapRef =
      CloseableReference.of(mDestinationBitmap, mBitmapResourceReleaser);
}
 
Example #26
Source File: AnimatedImageFactoryImpl.java    From fresco with MIT License 5 votes vote down vote up
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);
  }
}
 
Example #27
Source File: FrescoFrameCache.java    From fresco with MIT License 5 votes vote down vote up
@Nullable
private static CloseableReference<CloseableImage> createImageReference(
    CloseableReference<Bitmap> bitmapReference) {
  // The given CloseableStaticBitmap will be cached and then released by the resource releaser
  // of the closeable reference
  CloseableImage closeableImage =
      new CloseableStaticBitmap(bitmapReference, ImmutableQualityInfo.FULL_QUALITY, 0);
  return CloseableReference.of(closeableImage);
}
 
Example #28
Source File: ImageDecoder.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Decodes a partial jpeg.
 *
 * @param pooledByteBufferRef
 * @param length amount of currently available data in bytes
 * @param qualityInfo quality info for the image
 * @return a CloseableStaticBitmap
 */
public synchronized CloseableStaticBitmap decodeJpeg(
    final CloseableReference<PooledByteBuffer> pooledByteBufferRef,
    int length,
    QualityInfo qualityInfo) {
  CloseableReference<Bitmap> bitmapReference =
      mBitmapFactoryWithPool.decodeJPEGFromPooledByteBuffer(pooledByteBufferRef, length);
  try {
    return new CloseableStaticBitmap(bitmapReference, qualityInfo);
  } finally {
    bitmapReference.close();
  }
}
 
Example #29
Source File: PipelineDraweeController.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Drawable createDrawable(CloseableReference<CloseableImage> image) {
  Preconditions.checkState(CloseableReference.isValid(image));
  CloseableImage closeableImage = image.get();
  if (closeableImage instanceof CloseableStaticBitmap) {
    CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
    return new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
  } else if (closeableImage instanceof CloseableAnimatedImage) {
    return mAnimatedDrawableFactory.create(
        ((CloseableAnimatedImage) closeableImage).getImageResult());
  } else {
    throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
  }
}
 
Example #30
Source File: DefaultDrawableFactory.java    From fresco with MIT License 5 votes vote down vote up
@Override
@Nullable
public Drawable createDrawable(CloseableImage closeableImage) {
  try {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.beginSection("DefaultDrawableFactory#createDrawable");
    }
    if (closeableImage instanceof CloseableStaticBitmap) {
      CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) closeableImage;
      Drawable bitmapDrawable =
          new BitmapDrawable(mResources, closeableStaticBitmap.getUnderlyingBitmap());
      if (!hasTransformableRotationAngle(closeableStaticBitmap)
          && !hasTransformableExifOrientation(closeableStaticBitmap)) {
        // Return the bitmap drawable directly as there's nothing to transform in it
        return bitmapDrawable;
      } else {
        return new OrientedDrawable(
            bitmapDrawable,
            closeableStaticBitmap.getRotationAngle(),
            closeableStaticBitmap.getExifOrientation());
      }
    } else if (mAnimatedDrawableFactory != null
        && mAnimatedDrawableFactory.supportsImageType(closeableImage)) {
      return mAnimatedDrawableFactory.createDrawable(closeableImage);
    }
    return null;
  } finally {
    if (FrescoSystrace.isTracing()) {
      FrescoSystrace.endSection();
    }
  }
}