Search in sources :

Example 1 with ImageFormat

use of com.facebook.imageformat.ImageFormat in project fresco by facebook.

the class EncodedImage method parseMetaData.

/**
   * Sets the encoded image meta data.
   */
public void parseMetaData() {
    final ImageFormat imageFormat = ImageFormatChecker.getImageFormat_WrapIOException(getInputStream());
    mImageFormat = imageFormat;
    // BitmapUtil.decodeDimensions has a bug where it will return 100x100 for some WebPs even though
    // those are not its actual dimensions
    final Pair<Integer, Integer> dimensions;
    if (DefaultImageFormats.isWebpFormat(imageFormat)) {
        dimensions = readWebPImageSize();
    } else {
        dimensions = readImageSize();
    }
    if (imageFormat == DefaultImageFormats.JPEG && mRotationAngle == UNKNOWN_ROTATION_ANGLE) {
        // Load the JPEG rotation angle only if we have the dimensions
        if (dimensions != null) {
            mRotationAngle = JfifUtil.getAutoRotateAngleFromOrientation(JfifUtil.getOrientation(getInputStream()));
        }
    } else {
        mRotationAngle = 0;
    }
}
Also used : ImageFormat(com.facebook.imageformat.ImageFormat)

Example 2 with ImageFormat

use of com.facebook.imageformat.ImageFormat in project fresco by facebook.

the class DefaultImageDecoder method decode.

/**
 * 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) {
        InputStream inputStream = encodedImage.getInputStream();
        if (inputStream != null) {
            imageFormat = ImageFormatChecker.getImageFormat_WrapIOException(inputStream);
            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);
}
Also used : InputStream(java.io.InputStream) ImageFormat(com.facebook.imageformat.ImageFormat)

Example 3 with ImageFormat

use of com.facebook.imageformat.ImageFormat in project fresco by facebook.

the class DiskCacheWriteProducerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mDiskCacheWriteProducer = new DiskCacheWriteProducer(mDefaultBufferedDiskCache, mSmallImageBufferedDiskCache, mCacheKeyFactory, mInputProducer);
    List<CacheKey> keys = new ArrayList<>(1);
    keys.add(new SimpleCacheKey("http://dummy.uri"));
    mCacheKey = new MultiCacheKey(keys);
    mIntermediatePooledByteBuffer = mock(PooledByteBuffer.class);
    mFinalPooledByteBuffer = mock(PooledByteBuffer.class);
    mIntermediateImageReference = CloseableReference.of(mIntermediatePooledByteBuffer);
    mFinalImageReference = CloseableReference.of(mFinalPooledByteBuffer);
    mIntermediateEncodedImage = new EncodedImage(mIntermediateImageReference);
    mFinalEncodedImageFormatUnknown = new EncodedImage(mFinalImageReference);
    mFinalEncodedImage = new EncodedImage(mFinalImageReference);
    mFinalEncodedImage.setImageFormat(new ImageFormat("jpeg", null));
    mFinalEncodedImage.setWidth(100);
    mFinalEncodedImage.setHeight(100);
    mProducerContext = new SettableProducerContext(mImageRequest, mRequestId, mProducerListener, mCallerContext, ImageRequest.RequestLevel.FULL_FETCH, false, true, Priority.MEDIUM, mConfig);
    mLowestLevelProducerContext = new SettableProducerContext(mImageRequest, mRequestId, mProducerListener, mCallerContext, ImageRequest.RequestLevel.DISK_CACHE, false, true, Priority.MEDIUM, mConfig);
    when(mProducerListener.requiresExtraMap(mProducerContext, PRODUCER_NAME)).thenReturn(true);
    when(mCacheKeyFactory.getEncodedCacheKey(mImageRequest, mCallerContext)).thenReturn(mCacheKey);
    when(mImageRequest.getCacheChoice()).thenReturn(ImageRequest.CacheChoice.DEFAULT);
    setUpCacheEnabled(true);
}
Also used : MultiCacheKey(com.facebook.cache.common.MultiCacheKey) ArrayList(java.util.ArrayList) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImageFormat(com.facebook.imageformat.ImageFormat) Before(org.junit.Before)

Example 4 with ImageFormat

use of com.facebook.imageformat.ImageFormat in project fresco by facebook.

the class WebpTranscodeProducer method doTranscode.

private static void doTranscode(final EncodedImage encodedImage, final PooledByteBufferOutputStream outputStream) throws Exception {
    InputStream is = Preconditions.checkNotNull(encodedImage.getInputStream());
    ImageFormat imageFormat = ImageFormatChecker.getImageFormat_WrapIOException(is);
    if (imageFormat == DefaultImageFormats.WEBP_SIMPLE || imageFormat == DefaultImageFormats.WEBP_EXTENDED) {
        WebpTranscoderFactory.getWebpTranscoder().transcodeWebpToJpeg(is, outputStream, DEFAULT_JPEG_QUALITY);
        encodedImage.setImageFormat(DefaultImageFormats.JPEG);
    } else if (imageFormat == DefaultImageFormats.WEBP_LOSSLESS || imageFormat == DefaultImageFormats.WEBP_EXTENDED_WITH_ALPHA) {
        // In this case we always transcode to PNG
        WebpTranscoderFactory.getWebpTranscoder().transcodeWebpToPng(is, outputStream);
        encodedImage.setImageFormat(DefaultImageFormats.PNG);
    } else {
        throw new IllegalArgumentException("Wrong image format");
    }
}
Also used : InputStream(java.io.InputStream) ImageFormat(com.facebook.imageformat.ImageFormat)

Example 5 with ImageFormat

use of com.facebook.imageformat.ImageFormat in project fresco by facebook.

the class EncodedMemoryCacheProducerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mEncodedMemoryCacheProducer = new EncodedMemoryCacheProducer(mMemoryCache, mCacheKeyFactory, mInputProducer);
    mPooledByteBuffer1 = mock(PooledByteBuffer.class);
    mPooledByteBuffer2 = mock(PooledByteBuffer.class);
    mFinalImageReference = CloseableReference.of(mPooledByteBuffer1);
    mIntermediateImageReference = CloseableReference.of(mPooledByteBuffer2);
    mFinalImageReferenceClone = mFinalImageReference.clone();
    mFinalEncodedImage = new EncodedImage(mFinalImageReference);
    mFinalEncodedImage.setImageFormat(new ImageFormat("jpeg", null));
    mFinalEncodedImage.setWidth(100);
    mFinalEncodedImage.setHeight(100);
    mImagePipelineConfig = mock(ImagePipelineConfig.class);
    mImagePipelineExperiments = mock(ImagePipelineExperiments.class);
    mFinalEncodedImageFormatUnknown = new EncodedImage(mFinalImageReference);
    mIntermediateEncodedImage = new EncodedImage(mIntermediateImageReference);
    mFinalEncodedImageClone = new EncodedImage(mFinalImageReferenceClone);
    List<CacheKey> list = new ArrayList<>();
    list.add(new SimpleCacheKey("http://dummy.uri"));
    mCacheKey = new MultiCacheKey(list);
    when(mCacheKeyFactory.getEncodedCacheKey(mImageRequest, mCallerContext)).thenReturn(mCacheKey);
    when(mMemoryCache.cache(mCacheKey, mFinalImageReference)).thenReturn(mFinalImageReferenceClone);
    when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
    when(mProducerContext.getCallerContext()).thenReturn(mCallerContext);
    when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
    when(mProducerListener.requiresExtraMap(mProducerContext, PRODUCER_NAME)).thenReturn(true);
    when(mProducerContext.getLowestPermittedRequestLevel()).thenReturn(ImageRequest.RequestLevel.FULL_FETCH);
    when(mProducerContext.getImagePipelineConfig()).thenReturn(mImagePipelineConfig);
    when(mImagePipelineConfig.getExperiments()).thenReturn(mImagePipelineExperiments);
    when(mImagePipelineExperiments.isEncodedCacheEnabled()).thenReturn(true);
    setUpCacheEnabled(true);
}
Also used : ImagePipelineExperiments(com.facebook.imagepipeline.core.ImagePipelineExperiments) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) ArrayList(java.util.ArrayList) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) EncodedImage(com.facebook.imagepipeline.image.EncodedImage) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImageFormat(com.facebook.imageformat.ImageFormat)

Aggregations

ImageFormat (com.facebook.imageformat.ImageFormat)7 InputStream (java.io.InputStream)3 CacheKey (com.facebook.cache.common.CacheKey)2 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)2 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)2 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)2 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)2 ArrayList (java.util.ArrayList)2 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)1 ImagePipelineExperiments (com.facebook.imagepipeline.core.ImagePipelineExperiments)1 WebpTranscoder (com.facebook.imagepipeline.nativecode.WebpTranscoder)1 Before (org.junit.Before)1