com.facebook.imagepipeline.common.ImageDecodeOptions Java Examples

The following examples show how to use com.facebook.imagepipeline.common.ImageDecodeOptions. 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: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 6 votes vote down vote up
private void testCreateDefaults(WebPImage mockWebPImage, PooledByteBuffer byteBuffer) {
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeWebP(
              encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockWebPImage, imageResult.getImage());
  assertNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verifyZeroInteractions(mMockAnimatedDrawableBackendProvider);
  verifyZeroInteractions(mMockBitmapFactory);
}
 
Example #2
Source File: FrescoVitoRegionDecoder.java    From fresco with MIT License 6 votes vote down vote up
private @Nullable Rect computeRegionToDecode(
    EncodedImage encodedImage, ImageDecodeOptions options) {
  if (!(options instanceof FrescoVitoImageDecodeOptions)) {
    return null;
  }
  FrescoVitoImageDecodeOptions frescoVitoOptions = (FrescoVitoImageDecodeOptions) options;

  Rect regionToDecode = new Rect();
  Matrix matrix = new Matrix();
  frescoVitoOptions.scaleType.getTransform(
      matrix,
      frescoVitoOptions.parentBounds,
      encodedImage.getWidth(),
      encodedImage.getHeight(),
      frescoVitoOptions.focusPoint.x,
      frescoVitoOptions.focusPoint.y);
  matrix.invert(matrix);

  RectF tempRectangle = new RectF(frescoVitoOptions.parentBounds);
  matrix.mapRect(tempRectangle);

  tempRectangle.round(regionToDecode);
  return regionToDecode;
}
 
Example #3
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 #4
Source File: ImagePipelineUtilsImpl.java    From fresco with MIT License 6 votes vote down vote up
private synchronized ImageDecodeOptions getCircularImageDecodeOptions(boolean antiAliased) {
  final boolean useFastNativeRounding = mExperiments.useFastNativeRounding();
  if (antiAliased) {
    if (mCircularImageDecodeOptionsAntiAliased == null) {
      mCircularImageDecodeOptionsAntiAliased =
          ImageDecodeOptions.newBuilder()
              .setBitmapTransformation(
                  new CircularBitmapTransformation(true, useFastNativeRounding))
              .build();
    }
    return mCircularImageDecodeOptionsAntiAliased;
  } else {
    if (mCircularImageDecodeOptions == null) {
      mCircularImageDecodeOptions =
          ImageDecodeOptions.newBuilder()
              .setBitmapTransformation(
                  new CircularBitmapTransformation(false, useFastNativeRounding))
              .build();
    }
    return mCircularImageDecodeOptions;
  }
}
 
Example #5
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 #6
Source File: ImagePipelineUtilsImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testBuildImageRequest_whenRoundAsCircle_thenApplyRoundingParameters() {
  when(mFrescoExperiments.useNativeRounding()).thenReturn(true);

  final ImageOptions imageOptions =
      ImageOptions.create().round(RoundingOptions.asCircle()).build();

  ImageRequest imageRequest = mImagePipelineUtils.buildImageRequest(URI, imageOptions);

  assertThat(imageRequest).isNotNull();
  assertThat(imageRequest.getSourceUri()).isEqualTo(URI);
  ImageDecodeOptions imageDecodeOptions = imageRequest.getImageDecodeOptions();
  assertThat(imageDecodeOptions).isNotEqualTo(ImageDecodeOptions.defaults());
  assertThat(imageDecodeOptions.bitmapTransformation)
      .isInstanceOf(CircularBitmapTransformation.class);

  CircularBitmapTransformation transformation =
      (CircularBitmapTransformation) imageDecodeOptions.bitmapTransformation;
  assertThat(transformation).isNotNull();
  assertThat(transformation.isAntiAliased()).isFalse();
}
 
Example #7
Source File: ImagePipelineUtilsImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void
    testBuildImageRequest_whenRoundAsCircleWithAntiAliasing_thenApplyRoundingParameters() {
  when(mFrescoExperiments.useNativeRounding()).thenReturn(true);

  final ImageOptions imageOptions =
      ImageOptions.create().round(RoundingOptions.asCircle(true)).build();

  ImageRequest imageRequest = mImagePipelineUtils.buildImageRequest(URI, imageOptions);

  assertThat(imageRequest).isNotNull();
  assertThat(imageRequest.getSourceUri()).isEqualTo(URI);
  ImageDecodeOptions imageDecodeOptions = imageRequest.getImageDecodeOptions();
  assertThat(imageDecodeOptions).isNotEqualTo(ImageDecodeOptions.defaults());
  assertThat(imageDecodeOptions.bitmapTransformation)
      .isInstanceOf(CircularBitmapTransformation.class);

  CircularBitmapTransformation transformation =
      (CircularBitmapTransformation) imageDecodeOptions.bitmapTransformation;
  assertThat(transformation).isNotNull();
  assertThat(transformation.isAntiAliased()).isTrue();
}
 
Example #8
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 #9
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes image.
 *
 * @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(
    final EncodedImage encodedImage,
    final int length,
    final QualityInfo qualityInfo,
    final ImageDecodeOptions options) {
  if (options.customImageDecoder != null) {
    return options.customImageDecoder.decode(encodedImage, length, qualityInfo, options);
  }
  ImageFormat imageFormat = encodedImage.getImageFormat();
  if (imageFormat == null || imageFormat == ImageFormat.UNKNOWN) {
    imageFormat =
        ImageFormatChecker.getImageFormat_WrapIOException(encodedImage.getInputStream());
    encodedImage.setImageFormat(imageFormat);
  }
  if (mCustomDecoders != null) {
    ImageDecoder decoder = mCustomDecoders.get(imageFormat);
    if (decoder != null) {
      return decoder.decode(encodedImage, length, qualityInfo, options);
    }
  }
  return mDefaultDecoder.decode(encodedImage, length, qualityInfo, options);
}
 
Example #10
Source File: AnimatedImageFactoryImpl.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decode a WebP into a CloseableImage.
 *
 * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data)
 * @param options the options for the decode
 * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps
 * @return a {@link CloseableImage} for the WebP image
 */
public CloseableImage decodeWebP(
    final EncodedImage encodedImage,
    final ImageDecodeOptions options,
    final Bitmap.Config bitmapConfig) {
  if (sWebpAnimatedImageDecoder == null) {
    throw new UnsupportedOperationException(
        "To encode animated webp please add the dependency " + "to the animated-webp module");
  }
  final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
  Preconditions.checkNotNull(bytesRef);
  try {
    final PooledByteBuffer input = bytesRef.get();
    AnimatedImage webPImage;
    if (input.getByteBuffer() != null) {
      webPImage = sWebpAnimatedImageDecoder.decodeFromByteBuffer(input.getByteBuffer(), options);
    } else {
      webPImage =
          sWebpAnimatedImageDecoder.decodeFromNativeMemory(
              input.getNativePtr(), input.size(), options);
    }
    return getCloseableImage(options, webPImage, bitmapConfig);
  } finally {
    CloseableReference.closeSafely(bytesRef);
  }
}
 
Example #11
Source File: AnimatedImageFactoryImpl.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Decodes a GIF into a CloseableImage.
 *
 * @param encodedImage encoded image (native byte array holding the encoded bytes and meta data)
 * @param options the options for the decode
 * @param bitmapConfig the Bitmap.Config used to generate the output bitmaps
 * @return a {@link CloseableImage} for the GIF image
 */
public CloseableImage decodeGif(
    final EncodedImage encodedImage,
    final ImageDecodeOptions options,
    final Bitmap.Config bitmapConfig) {
  if (sGifAnimatedImageDecoder == null) {
    throw new UnsupportedOperationException(
        "To encode animated gif please add the dependency " + "to the animated-gif module");
  }
  final CloseableReference<PooledByteBuffer> bytesRef = encodedImage.getByteBufferRef();
  Preconditions.checkNotNull(bytesRef);
  try {
    final PooledByteBuffer input = bytesRef.get();
    AnimatedImage gifImage;
    if (input.getByteBuffer() != null) {
      gifImage = sGifAnimatedImageDecoder.decodeFromByteBuffer(input.getByteBuffer(), options);
    } else {
      gifImage =
          sGifAnimatedImageDecoder.decodeFromNativeMemory(
              input.getNativePtr(), input.size(), options);
    }
    return getCloseableImage(options, gifImage, bitmapConfig);
  } finally {
    CloseableReference.closeSafely(bytesRef);
  }
}
 
Example #12
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCreateWithDecodeAlFramesUsingPointer() throws Exception {
  GifImage mockGifImage = mock(GifImage.class);

  Bitmap mockBitmap1 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
  Bitmap mockBitmap2 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);

  // Expect a call to GifImage.createFromByteBuffer
  TrivialPooledByteBuffer byteBuffer = createByteBuffer();
  when(mGifImageMock.decodeFromNativeMemory(
          eq(byteBuffer.getNativePtr()), eq(byteBuffer.size()), any(ImageDecodeOptions.class)))
      .thenReturn(mockGifImage);
  when(mockGifImage.getWidth()).thenReturn(50);
  when(mockGifImage.getHeight()).thenReturn(50);

  testCreateWithDecodeAlFrames(mockGifImage, mockBitmap1, mockBitmap2, byteBuffer);
}
 
Example #13
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 6 votes vote down vote up
private void testCreateDefaults(GifImage mockGifImage, PooledByteBuffer byteBuffer) {
  EncodedImage encodedImage =
      new EncodedImage(CloseableReference.of(byteBuffer, FAKE_RESOURCE_RELEASER));
  encodedImage.setImageFormat(ImageFormat.UNKNOWN);

  CloseableAnimatedImage closeableImage =
      (CloseableAnimatedImage)
          mAnimatedImageFactory.decodeGif(
              encodedImage, ImageDecodeOptions.defaults(), DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockGifImage, imageResult.getImage());
  assertNull(imageResult.getPreviewBitmap());
  assertFalse(imageResult.hasDecodedFrame(0));

  // Should not have interacted with these.
  verifyZeroInteractions(mMockAnimatedDrawableBackendProvider);
  verifyZeroInteractions(mMockBitmapFactory);
}
 
Example #14
Source File: AnimatedImageFactory.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
private CloseableImage getCloseableImage(ImageDecodeOptions options, AnimatedImage image) {
  int frameForPreview = options.useLastFrameForPreview ? image.getFrameCount() - 1 : 0;
  CloseableReference<Bitmap> previewBitmap = null;
  if (options.decodePreviewFrame) {
    previewBitmap = createPreviewBitmap(image, frameForPreview);
  }
  try {
    AnimatedImageResult animatedImageResult = AnimatedImageResult.newBuilder(image)
        .setPreviewBitmap(previewBitmap)
        .setFrameForPreview(frameForPreview)
        .build();
    return new CloseableAnimatedImage(animatedImageResult);
  } finally {
    CloseableReference.closeSafely(previewBitmap);
  }
}
 
Example #15
Source File: DefaultImageDecoder.java    From fresco with MIT License 6 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  ImageFormat imageFormat = encodedImage.getImageFormat();
  if (imageFormat == DefaultImageFormats.JPEG) {
    return decodeJpeg(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == DefaultImageFormats.GIF) {
    return decodeGif(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == DefaultImageFormats.WEBP_ANIMATED) {
    return decodeAnimatedWebp(encodedImage, length, qualityInfo, options);
  } else if (imageFormat == ImageFormat.UNKNOWN) {
    throw new DecodeException("unknown image format", encodedImage);
  }
  return decodeStaticImage(encodedImage, options);
}
 
Example #16
Source File: ImageFormatGifFragment.java    From fresco with MIT License 6 votes vote down vote up
private void setAnimationUri(Uri uri) {
  final PipelineDraweeControllerBuilder controllerBuilder =
      Fresco.newDraweeControllerBuilder()
          .setAutoPlayAnimations(true)
          .setOldController(mSimpleDraweeView.getController());
  final ImageDecodeOptionsBuilder optionsBuilder =
      ImageDecodeOptions.newBuilder().setMaxDimensionPx(4000);

  if (mGifDecoder != null) {
    optionsBuilder.setCustomImageDecoder(mGifDecoder);
  }

  controllerBuilder.setImageRequest(
      ImageRequestBuilder.newBuilderWithSource(uri)
          .setImageDecodeOptions(optionsBuilder.build())
          .build());
  mSimpleDraweeView.setController(controllerBuilder.build());
}
 
Example #17
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCreateWithDecodeAlFramesUsingByteBuffer() throws Exception {
  GifImage mockGifImage = mock(GifImage.class);

  Bitmap mockBitmap1 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
  Bitmap mockBitmap2 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);

  // Expect a call to GifImage.createFromByteBuffer
  TrivialBufferPooledByteBuffer byteBuffer = createDirectByteBuffer();
  when(mGifImageMock.decodeFromByteBuffer(
          eq(byteBuffer.getByteBuffer()), any(ImageDecodeOptions.class)))
      .thenReturn(mockGifImage);
  when(mockGifImage.getWidth()).thenReturn(50);
  when(mockGifImage.getHeight()).thenReturn(50);

  testCreateWithDecodeAlFrames(mockGifImage, mockBitmap1, mockBitmap2, byteBuffer);
}
 
Example #18
Source File: KeyframesDecoderExample.java    From fresco with MIT License 6 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  InputStream encodedInputStream = null;
  try {
    encodedInputStream = encodedImage.getInputStream();
    return new CloseableKeyframesImage(KFImageDeserializer.deserialize(encodedInputStream));
  } catch (IOException e) {
    e.printStackTrace();
    return null;
  } finally {
    Closeables.closeQuietly(encodedInputStream);
  }
}
 
Example #19
Source File: BitmapMemoryCacheKey.java    From fresco with MIT License 6 votes vote down vote up
public BitmapMemoryCacheKey(
    String sourceString,
    @Nullable ResizeOptions resizeOptions,
    RotationOptions rotationOptions,
    ImageDecodeOptions imageDecodeOptions,
    @Nullable CacheKey postprocessorCacheKey,
    @Nullable String postprocessorName,
    Object callerContext) {
  mSourceString = Preconditions.checkNotNull(sourceString);
  mResizeOptions = resizeOptions;
  mRotationOptions = rotationOptions;
  mImageDecodeOptions = imageDecodeOptions;
  mPostprocessorCacheKey = postprocessorCacheKey;
  mPostprocessorName = postprocessorName;
  mHash =
      HashCodeUtil.hashCode(
          sourceString.hashCode(),
          (resizeOptions != null) ? resizeOptions.hashCode() : 0,
          rotationOptions.hashCode(),
          mImageDecodeOptions,
          mPostprocessorCacheKey,
          postprocessorName);
  mCallerContext = callerContext;
  mCacheTime = RealtimeSinceBootClock.get().now();
}
 
Example #20
Source File: ImageFormatOverrideExample.java    From fresco with MIT License 6 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.drawee_view);

  ImageDecodeOptions imageDecodeOptionsWithCustomDecoder =
      new ImageDecodeOptionsBuilder().setCustomImageDecoder(CUSTOM_COLOR_DECODER).build();

  AbstractDraweeController controller =
      Fresco.newDraweeControllerBuilder()
          .setImageRequest(
              ImageRequestBuilder.newBuilderWithResourceId(R.raw.custom_color1)
                  .setImageDecodeOptions(imageDecodeOptionsWithCustomDecoder)
                  .build())
          .build();
  simpleDraweeView.setController(controller);
}
 
Example #21
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCreateWithDecodeAlFramesUsingPointer() throws Exception {
  WebPImage mockWebPImage = mock(WebPImage.class);

  Bitmap mockBitmap1 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
  Bitmap mockBitmap2 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);

  // Expect a call to WebPImage.createFromByteBuffer
  TrivialPooledByteBuffer byteBuffer = createByteBuffer();
  when(mWebPImageMock.decodeFromNativeMemory(
          eq(byteBuffer.getNativePtr()), eq(byteBuffer.size()), any(ImageDecodeOptions.class)))
      .thenReturn(mockWebPImage);
  when(mockWebPImage.getWidth()).thenReturn(50);
  when(mockWebPImage.getHeight()).thenReturn(50);

  testCreateWithDecodeAlFrames(mockWebPImage, byteBuffer, mockBitmap1, mockBitmap2);
}
 
Example #22
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 6 votes vote down vote up
@Test
public void testCreateWithDecodeAlFramesUsingByteBuffer() throws Exception {
  WebPImage mockWebPImage = mock(WebPImage.class);

  Bitmap mockBitmap1 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);
  Bitmap mockBitmap2 = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);

  // Expect a call to WebPImage.createFromByteBuffer
  TrivialBufferPooledByteBuffer byteBuffer = createDirectByteBuffer();
  when(mWebPImageMock.decodeFromByteBuffer(
          eq(byteBuffer.getByteBuffer()), any(ImageDecodeOptions.class)))
      .thenReturn(mockWebPImage);
  when(mockWebPImage.getWidth()).thenReturn(50);
  when(mockWebPImage.getHeight()).thenReturn(50);

  testCreateWithDecodeAlFrames(mockWebPImage, byteBuffer, mockBitmap1, mockBitmap2);
}
 
Example #23
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testCreateWithPreviewBitmapUsingByteBuffer() throws Exception {
  GifImage mockGifImage = mock(GifImage.class);
  Bitmap mockBitmap = MockBitmapFactory.create(50, 50, DEFAULT_BITMAP_CONFIG);

  // Expect a call to GifImage.createFromByteBuffer
  TrivialBufferPooledByteBuffer byteBuffer = createDirectByteBuffer();
  when(mGifImageMock.decodeFromByteBuffer(
          eq(byteBuffer.getByteBuffer()), any(ImageDecodeOptions.class)))
      .thenReturn(mockGifImage);
  when(mockGifImage.getWidth()).thenReturn(50);
  when(mockGifImage.getHeight()).thenReturn(50);

  testCreateWithPreviewBitmap(mockGifImage, mockBitmap, byteBuffer);
}
 
Example #24
Source File: ColorImageExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    // Read the file as a string
    String text = new String(ByteStreams.toByteArray(encodedImage.getInputStream()));

    // Check if the string matches "<color>#"
    if (!text.startsWith(COLOR_TAG + "#")) {
      return null;
    }

    // Parse the int value between # and <
    int startIndex = COLOR_TAG.length() + 1;
    int endIndex = text.lastIndexOf('<');
    int color = Integer.parseInt(text.substring(startIndex, endIndex), 16);

    // Add the alpha component so that we actually see the color
    color = ColorUtils.setAlphaComponent(color, 255);

    // Return the CloseableImage
    return new CloseableColorImage(color);
  } catch (IOException e) {
    e.printStackTrace();
  }
  // Return nothing if an error occurred
  return null;
}
 
Example #25
Source File: SvgDecoderExample.java    From fresco with MIT License 5 votes vote down vote up
@Override
public CloseableImage decode(
    EncodedImage encodedImage,
    int length,
    QualityInfo qualityInfo,
    ImageDecodeOptions options) {
  try {
    SVG svg = SVG.getFromInputStream(encodedImage.getInputStream());
    return new CloseableSvgImage(svg);
  } catch (SVGParseException e) {
    e.printStackTrace();
  }
  return null;
}
 
Example #26
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 5 votes vote down vote up
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);
}
 
Example #27
Source File: ImagePipelineRegionDecodingFragment.java    From fresco with MIT License 5 votes vote down vote up
private void updateRegion() {
  if (mImageInfo == null) {
    return;
  }
  int left = 0;
  int top = 0;
  int right =
      mSelectedRegion.getMeasuredWidth()
          * mImageInfo.getWidth()
          / mFullDraweeView.getMeasuredWidth();
  int bottom =
      mSelectedRegion.getMeasuredHeight()
          * mImageInfo.getHeight()
          / mFullDraweeView.getMeasuredHeight();

  ImageDecoder regionDecoder = createRegionDecoder(left, top, right, bottom);
  mRegionDraweeView.setController(
      Fresco.newDraweeControllerBuilder()
          .setImageRequest(
              ImageRequestBuilder.newBuilderWithSource(mUri)
                  .setImageDecodeOptions(
                      ImageDecodeOptions.newBuilder()
                          .setCustomImageDecoder(regionDecoder)
                          .build())
                  .build())
          .build());
}
 
Example #28
Source File: AnimatedImageFactoryGifImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testCreateDefaultsUsingPointer() {
  GifImage mockGifImage = mock(GifImage.class);

  // Expect a call to GifImage.createFromByteBuffer
  TrivialPooledByteBuffer byteBuffer = createByteBuffer();
  when(mGifImageMock.decodeFromNativeMemory(
          eq(byteBuffer.getNativePtr()), eq(byteBuffer.size()), any(ImageDecodeOptions.class)))
      .thenReturn(mockGifImage);

  testCreateDefaults(mockGifImage, byteBuffer);
}
 
Example #29
Source File: AnimatedImageFactoryWebPImplTest.java    From fresco with MIT License 5 votes vote down vote up
private void testCreateWithPreviewBitmap(
    WebPImage mockWebPImage, PooledByteBuffer byteBuffer, Bitmap mockBitmap) 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.decodeWebP(
              encodedImage, imageDecodeOptions, DEFAULT_BITMAP_CONFIG);

  // Verify we got the right result
  AnimatedImageResult imageResult = closeableImage.getImageResult();
  assertSame(mockWebPImage, 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);
}
 
Example #30
Source File: DefaultImageDecoder.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Decodes gif into CloseableImage.
 *
 * @param encodedImage input image (encoded bytes plus meta data)
 * @return a CloseableImage
 */
public CloseableImage decodeGif(
    final EncodedImage encodedImage,
    final int length,
    final QualityInfo qualityInfo,
    final ImageDecodeOptions options) {
  if (encodedImage.getWidth() == EncodedImage.UNKNOWN_WIDTH
      || encodedImage.getHeight() == EncodedImage.UNKNOWN_HEIGHT) {
    throw new DecodeException("image width or height is incorrect", encodedImage);
  }
  if (!options.forceStaticImage && mAnimatedGifDecoder != null) {
    return mAnimatedGifDecoder.decode(encodedImage, length, qualityInfo, options);
  }
  return decodeStaticImage(encodedImage, options);
}