com.facebook.imagepipeline.image.ImmutableQualityInfo Java Examples

The following examples show how to use com.facebook.imagepipeline.image.ImmutableQualityInfo. 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: 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 #2
Source File: BitmapMemoryCacheGetProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  mBitmapMemoryCacheGetProducer =
      new BitmapMemoryCacheGetProducer(mMemoryCache, mCacheKeyFactory, mInputProducer);
  mCloseableImage1 = mock(CloseableImage.class);
  mFinalImageReference = CloseableReference.of(mCloseableImage1);
  when(mCloseableImage1.getQualityInfo()).thenReturn(ImmutableQualityInfo.FULL_QUALITY);

  when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
  when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
  when(mProducerContext.getId()).thenReturn(mRequestId);
  when(mProducerListener.requiresExtraMap(eq(mProducerContext), eq(PRODUCER_NAME)))
      .thenReturn(true);
  when(mProducerContext.getLowestPermittedRequestLevel())
      .thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
  when(mProducerContext.getCallerContext()).thenReturn(PRODUCER_NAME);
  when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest, PRODUCER_NAME)).thenReturn(mCacheKey);
}
 
Example #3
Source File: DecodeProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testDecode_Final() throws Exception {
  setupNetworkUri();
  produceResults();
  JobScheduler.JobRunnable jobRunnable = getJobRunnable();

  jobRunnable.run(mEncodedImage, Consumer.IS_LAST);

  InOrder inOrder = inOrder(mProducerListener, mImageDecoder);
  inOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, DecodeProducer.PRODUCER_NAME);
  inOrder
      .verify(mImageDecoder)
      .decode(mEncodedImage, IMAGE_SIZE, ImmutableQualityInfo.FULL_QUALITY, IMAGE_DECODE_OPTIONS);
  inOrder
      .verify(mProducerListener)
      .onProducerFinishWithSuccess(
          eq(mProducerContext), eq(DecodeProducer.PRODUCER_NAME), nullable(Map.class));
  verify(mProducerListener, never())
      .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
}
 
Example #4
Source File: BitmapMemoryCacheProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testDontCacheIntermediateImageAsAlreadyHaveFullQuality() {
  setupBitmapMemoryCacheGetNotFound();
  setupInputProducerStreamingSuccess();
  CloseableImage closeableImage = mock(CloseableImage.class);
  when(closeableImage.getQualityInfo()).thenReturn(ImmutableQualityInfo.FULL_QUALITY);
  CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
  setupBitmapMemoryCacheGetSuccessOnSecondRead(closeableImageRef);
  mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
  verify(mMemoryCache, never()).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
  verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
  verify(mConsumer).onNewResult(closeableImageRef, Consumer.NO_FLAGS);
  verify(mConsumer).onNewResult(mFinalImageReferenceClone, Consumer.IS_LAST);
  Assert.assertTrue(!mFinalImageReferenceClone.isValid());
  Assert.assertEquals(
      0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
  verify(mProducerListener).onProducerStart(mProducerContext, PRODUCER_NAME);
  Map<String, String> extraMap =
      ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
  verify(mProducerListener)
      .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, extraMap);
  verify(mProducerListener, never())
      .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
}
 
Example #5
Source File: BitmapMemoryCacheProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testDontCacheIntermediateImageAsAlreadyHaveSameQuality() {
  setupInputProducerStreamingSuccess();
  CloseableImage closeableImage = mock(CloseableImage.class);
  when(closeableImage.getQualityInfo())
      .thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_2, true, false));
  CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
  setupBitmapMemoryCacheGetSuccessOnSecondRead(closeableImageRef);
  mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
  verify(mMemoryCache, never()).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
  verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
  verify(mConsumer).onNewResult(closeableImageRef, Consumer.NO_FLAGS);
  verify(mConsumer).onNewResult(mFinalImageReferenceClone, Consumer.IS_LAST);
  Assert.assertTrue(!mFinalImageReferenceClone.isValid());
  Assert.assertEquals(
      0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
  verify(mProducerListener).onProducerStart(mProducerContext, PRODUCER_NAME);
  Map<String, String> extraMap =
      ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
  verify(mProducerListener)
      .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, extraMap);
  verify(mProducerListener, never())
      .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
}
 
Example #6
Source File: BitmapMemoryCacheProducerTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCacheIntermediateImageAsBetterScan() {
  setupInputProducerStreamingSuccess();
  CloseableImage closeableImage = mock(CloseableImage.class);
  when(closeableImage.getQualityInfo())
      .thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_1, false, false));
  CloseableReference<CloseableImage> closeableImageRef = CloseableReference.of(closeableImage);
  setupBitmapMemoryCacheGetSuccessOnSecondRead(closeableImageRef);
  mBitmapMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
  verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mIntermediateImageReference);
  verify(mMemoryCache).cache(mBitmapMemoryCacheKey, mFinalImageReference);
  verify(mConsumer).onNewResult(mIntermediateImageReferenceClone, Consumer.NO_FLAGS);
  verify(mConsumer).onNewResult(mFinalImageReferenceClone, Consumer.IS_LAST);
  Assert.assertTrue(!mIntermediateImageReferenceClone.isValid());
  Assert.assertTrue(!mFinalImageReferenceClone.isValid());
  Assert.assertEquals(
      0, closeableImageRef.getUnderlyingReferenceTestOnly().getRefCountTestOnly());
  verify(mProducerListener).onProducerStart(mProducerContext, PRODUCER_NAME);
  Map<String, String> extraMap =
      ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "false");
  verify(mProducerListener)
      .onProducerFinishWithSuccess(mProducerContext, PRODUCER_NAME, extraMap);
  verify(mProducerListener, never())
      .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: SimpleProgressiveJpegConfig.java    From fresco with MIT License 5 votes vote down vote up
@Override
public QualityInfo getQualityInfo(int scanNumber) {
  return ImmutableQualityInfo.of(
      scanNumber,
      /* isOfGoodEnoughQuality */ scanNumber >= mDynamicValueConfig.getGoodEnoughScanNumber(),
      /* isOfFullQuality */ false);
}
 
Example #13
Source File: BitmapMemoryCacheProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  mBitmapMemoryCacheProducer =
      new BitmapMemoryCacheProducer(mMemoryCache, mCacheKeyFactory, mInputProducer);
  mBitmapMemoryCacheKey = mock(BitmapMemoryCacheKey.class);
  mCloseableImage1 = mock(CloseableImage.class);
  when(mCloseableImage1.getQualityInfo()).thenReturn(ImmutableQualityInfo.FULL_QUALITY);
  mCloseableImage2 = mock(CloseableImage.class);
  when(mCloseableImage2.getQualityInfo())
      .thenReturn(ImmutableQualityInfo.of(INTERMEDIATE_SCAN_2, true, false));
  mFinalImageReference = CloseableReference.of(mCloseableImage1);
  mIntermediateImageReference = CloseableReference.of(mCloseableImage2);
  mFinalImageReferenceClone = mFinalImageReference.clone();
  mIntermediateImageReferenceClone = mIntermediateImageReference.clone();

  when(mMemoryCache.cache(mBitmapMemoryCacheKey, mIntermediateImageReference))
      .thenReturn(mIntermediateImageReferenceClone);
  when(mMemoryCache.cache(mBitmapMemoryCacheKey, mFinalImageReference))
      .thenReturn(mFinalImageReferenceClone);
  when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
  when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
  when(mProducerContext.getLowestPermittedRequestLevel())
      .thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
  when(mProducerContext.getCallerContext()).thenReturn(PRODUCER_NAME);
  when(mProducerListener.requiresExtraMap(mProducerContext, PRODUCER_NAME)).thenReturn(true);
  when(mCacheKeyFactory.getBitmapCacheKey(mImageRequest, PRODUCER_NAME))
      .thenReturn(mBitmapMemoryCacheKey);

  when(mImageRequest.isMemoryCacheEnabled()).thenReturn(true);
}
 
Example #14
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 #15
Source File: SimpleProgressiveJpegConfig.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public QualityInfo getQualityInfo(int scanNumber) {
  return ImmutableQualityInfo.of(
      scanNumber,
      /* isOfGoodEnoughQuality */ scanNumber >= mGoodEnoughScanNumber,
      /* isOfFullQuality */ false);
}
 
Example #16
Source File: DecodeProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testDecode_Intermediate_pJPEG() throws Exception {
  setupNetworkUri();
  produceResults();
  JobScheduler.JobRunnable jobRunnable = getJobRunnable();

  when(mProgressiveJpegParser.isJpeg()).thenReturn(true);
  when(mProgressiveJpegParser.getBestScanEndOffset()).thenReturn(200);
  when(mProgressiveJpegParser.getBestScanNumber()).thenReturn(PREVIEW_SCAN);
  jobRunnable.run(mEncodedImage, Consumer.NO_FLAGS);

  InOrder inOrder = inOrder(mProducerListener, mImageDecoder);
  inOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, DecodeProducer.PRODUCER_NAME);
  inOrder
      .verify(mImageDecoder)
      .decode(
          mEncodedImage,
          200,
          ImmutableQualityInfo.of(PREVIEW_SCAN, false, false),
          IMAGE_DECODE_OPTIONS);
  inOrder
      .verify(mProducerListener)
      .onProducerFinishWithSuccess(
          eq(mProducerContext), eq(DecodeProducer.PRODUCER_NAME), nullable(Map.class));
  inOrder.verifyNoMoreInteractions();
}
 
Example #17
Source File: DecodeProducerTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testDecode_Failure() throws Exception {
  setupNetworkUri();
  produceResults();
  JobScheduler.JobRunnable jobRunnable = getJobRunnable();

  Exception exception = new RuntimeException();
  when(mImageDecoder.decode(
          mEncodedImage, IMAGE_SIZE, ImmutableQualityInfo.FULL_QUALITY, IMAGE_DECODE_OPTIONS))
      .thenThrow(exception);
  jobRunnable.run(mEncodedImage, Consumer.IS_LAST);

  InOrder inOrder = inOrder(mProducerListener, mImageDecoder);
  inOrder
      .verify(mProducerListener)
      .onProducerStart(mProducerContext, DecodeProducer.PRODUCER_NAME);
  inOrder
      .verify(mImageDecoder)
      .decode(mEncodedImage, IMAGE_SIZE, ImmutableQualityInfo.FULL_QUALITY, IMAGE_DECODE_OPTIONS);
  inOrder
      .verify(mProducerListener)
      .onProducerFinishWithFailure(
          eq(mProducerContext),
          eq(DecodeProducer.PRODUCER_NAME),
          eq(exception),
          nullable(Map.class));
  verify(mProducerListener, never())
      .onUltimateProducerReached(eq(mProducerContext), anyString(), anyBoolean());
}
 
Example #18
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
private void decodeFinalImage(final CloseableReference<PooledByteBuffer> imageBytesRef) {
  closeStoredIntermediateImageBytes();
  if (imageBytesRef == null) {
    handleResult(null, true);
  } else {
    scheduleImageDecode(
        imageBytesRef,
        imageBytesRef.get().size(),
        getImageFormat(imageBytesRef),
        ImmutableQualityInfo.FULL_QUALITY,
        /* isFinal */ true);
  }
}
 
Example #19
Source File: DecodeProducer.java    From fresco with MIT License 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo() {
  return ImmutableQualityInfo.of(0, false, false);
}
 
Example #20
Source File: DecodeProducer.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected QualityInfo getQualityInfo(CloseableReference<PooledByteBuffer> imageBytesRef) {
  return ImmutableQualityInfo.of(0, false, false);
}