Search in sources :

Example 1 with LimitedInputStream

use of com.facebook.common.streams.LimitedInputStream 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 LimitedInputStream

use of com.facebook.common.streams.LimitedInputStream 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)

Example 3 with LimitedInputStream

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

the class GingerbreadPurgeableDecoder method copyToMemoryFile.

private static MemoryFile copyToMemoryFile(CloseableReference<PooledByteBuffer> bytesRef, int inputLength, @Nullable byte[] suffix) throws IOException {
    int outputLength = inputLength + (suffix == null ? 0 : suffix.length);
    MemoryFile memoryFile = new MemoryFile(null, outputLength);
    memoryFile.allowPurging(false);
    PooledByteBufferInputStream pbbIs = null;
    LimitedInputStream is = null;
    OutputStream os = null;
    try {
        pbbIs = new PooledByteBufferInputStream(bytesRef.get());
        is = new LimitedInputStream(pbbIs, inputLength);
        os = memoryFile.getOutputStream();
        ByteStreams.copy(is, os);
        if (suffix != null) {
            memoryFile.writeBytes(suffix, 0, inputLength, suffix.length);
        }
        return memoryFile;
    } finally {
        CloseableReference.closeSafely(bytesRef);
        Closeables.closeQuietly(pbbIs);
        Closeables.closeQuietly(is);
        Closeables.close(os, true);
    }
}
Also used : MemoryFile(android.os.MemoryFile) PooledByteBufferInputStream(com.facebook.common.memory.PooledByteBufferInputStream) LimitedInputStream(com.facebook.common.streams.LimitedInputStream) OutputStream(java.io.OutputStream)

Aggregations

LimitedInputStream (com.facebook.common.streams.LimitedInputStream)3 BitmapFactory (android.graphics.BitmapFactory)2 TailAppendingInputStream (com.facebook.common.streams.TailAppendingInputStream)2 InputStream (java.io.InputStream)2 MemoryFile (android.os.MemoryFile)1 PooledByteBufferInputStream (com.facebook.common.memory.PooledByteBufferInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1