Search in sources :

Example 1 with TailAppendingInputStream

use of com.facebook.common.streams.TailAppendingInputStream in project fresco by facebook.

the class ArtDecoder method decodeJPEGFromEncodedImage.

/**
   * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
   * @param encodedImage the encoded image with reference to the encoded bytes
   * @param bitmapConfig the {@link android.graphics.Bitmap.Config}
   * used to create the decoded Bitmap
   * @param length the number of encoded bytes in the buffer
   * @return the bitmap
   * @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
   */
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImage(EncodedImage encodedImage, Bitmap.Config bitmapConfig, int length) {
    boolean isJpegComplete = encodedImage.isCompleteAt(length);
    final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);
    InputStream jpegDataStream = encodedImage.getInputStream();
    // At this point the InputStream from the encoded image should not be null since in the
    // pipeline,this comes from a call stack where this was checked before. Also this method needs
    // the InputStream to decode the image so this can't be null.
    Preconditions.checkNotNull(jpegDataStream);
    if (encodedImage.getSize() > length) {
        jpegDataStream = new LimitedInputStream(jpegDataStream, length);
    }
    if (!isJpegComplete) {
        jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
    }
    boolean retryOnFail = options.inPreferredConfig != Bitmap.Config.ARGB_8888;
    try {
        return decodeStaticImageFromStream(jpegDataStream, options);
    } catch (RuntimeException re) {
        if (retryOnFail) {
            return decodeFromEncodedImage(encodedImage, Bitmap.Config.ARGB_8888);
        }
        throw re;
    }
}
Also used : TailAppendingInputStream(com.facebook.common.streams.TailAppendingInputStream) TailAppendingInputStream(com.facebook.common.streams.TailAppendingInputStream) InputStream(java.io.InputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) BitmapFactory(android.graphics.BitmapFactory)

Example 2 with TailAppendingInputStream

use of com.facebook.common.streams.TailAppendingInputStream in project fresco by facebook.

the class DefaultDecoder method decodeJPEGFromEncodedImageWithColorSpace.

/**
 * Creates a bitmap from encoded JPEG bytes. Supports a partial JPEG image.
 *
 * @param encodedImage the encoded image with reference to the encoded bytes
 * @param bitmapConfig the {@link android.graphics.Bitmap.Config} used to create the decoded
 *     Bitmap
 * @param regionToDecode optional image region to decode or null to decode the whole image
 * @param length the number of encoded bytes in the buffer
 * @param colorSpace the target color space of the decoded bitmap, must be one of the named color
 *     space in {@link android.graphics.ColorSpace.Named}. If null, then SRGB color space is
 *     assumed if the SDK version >= 26.
 * @return the bitmap
 * @exception java.lang.OutOfMemoryError if the Bitmap cannot be allocated
 */
@Override
public CloseableReference<Bitmap> decodeJPEGFromEncodedImageWithColorSpace(EncodedImage encodedImage, Bitmap.Config bitmapConfig, @Nullable Rect regionToDecode, int length, @Nullable final ColorSpace colorSpace) {
    boolean isJpegComplete = encodedImage.isCompleteAt(length);
    final BitmapFactory.Options options = getDecodeOptionsForStream(encodedImage, bitmapConfig);
    InputStream jpegDataStream = encodedImage.getInputStream();
    // At this point the InputStream from the encoded image should not be null since in the
    // pipeline,this comes from a call stack where this was checked before. Also this method needs
    // the InputStream to decode the image so this can't be null.
    Preconditions.checkNotNull(jpegDataStream);
    if (encodedImage.getSize() > length) {
        jpegDataStream = new LimitedInputStream(jpegDataStream, length);
    }
    if (!isJpegComplete) {
        jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL);
    }
    boolean retryOnFail = options.inPreferredConfig != Bitmap.Config.ARGB_8888;
    try {
        return decodeFromStream(jpegDataStream, options, regionToDecode, colorSpace);
    } catch (RuntimeException re) {
        if (retryOnFail) {
            return decodeJPEGFromEncodedImageWithColorSpace(encodedImage, Bitmap.Config.ARGB_8888, regionToDecode, length, colorSpace);
        }
        throw re;
    } finally {
        try {
            jpegDataStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : TailAppendingInputStream(com.facebook.common.streams.TailAppendingInputStream) TailAppendingInputStream(com.facebook.common.streams.TailAppendingInputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) InputStream(java.io.InputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException)

Aggregations

BitmapFactory (android.graphics.BitmapFactory)2 LimitedInputStream (com.facebook.common.streams.LimitedInputStream)2 TailAppendingInputStream (com.facebook.common.streams.TailAppendingInputStream)2 InputStream (java.io.InputStream)2 IOException (java.io.IOException)1