Search in sources :

Example 1 with DoNotStrip

use of com.facebook.common.internal.DoNotStrip in project fresco by facebook.

the class WebpBitmapFactoryImpl method getScaleFromOptions.

@DoNotStrip
private static float getScaleFromOptions(BitmapFactory.Options options) {
    float scale = 1.0f;
    if (options != null) {
        int sampleSize = options.inSampleSize;
        if (sampleSize > 1) {
            scale = 1.0f / (float) sampleSize;
        }
        if (options.inScaled) {
            int density = options.inDensity;
            int targetDensity = options.inTargetDensity;
            int screenDensity = options.inScreenDensity;
            if (density != 0 && targetDensity != 0 && density != screenDensity) {
                scale = targetDensity / (float) density;
            }
        }
    }
    return scale;
}
Also used : SuppressLint(android.annotation.SuppressLint) DoNotStrip(com.facebook.common.internal.DoNotStrip)

Example 2 with DoNotStrip

use of com.facebook.common.internal.DoNotStrip in project fresco by facebook.

the class WebpBitmapFactoryImpl method hookDecodeResource.

@DoNotStrip
public static Bitmap hookDecodeResource(Resources res, int id, BitmapFactory.Options opts) {
    Bitmap bm = null;
    TypedValue value = new TypedValue();
    try (InputStream is = res.openRawResource(id, value)) {
        bm = hookDecodeResourceStream(res, value, is, null, opts);
    } catch (Exception e) {
    // Keep resulting bitmap as null
    }
    if (IN_BITMAP_SUPPORTED && bm == null && opts != null && opts.inBitmap != null) {
        throw new IllegalArgumentException("Problem decoding into existing bitmap");
    }
    return bm;
}
Also used : Bitmap(android.graphics.Bitmap) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) TypedValue(android.util.TypedValue) DoNotStrip(com.facebook.common.internal.DoNotStrip)

Example 3 with DoNotStrip

use of com.facebook.common.internal.DoNotStrip in project fresco by facebook.

the class WebpBitmapFactoryImpl method hookDecodeStream.

@DoNotStrip
public static Bitmap hookDecodeStream(InputStream inputStream, Rect outPadding, BitmapFactory.Options opts) {
    StaticWebpNativeLoader.ensure();
    inputStream = wrapToMarkSupportedStream(inputStream);
    Bitmap bitmap;
    byte[] header = getWebpHeader(inputStream, opts);
    if (WebpSupportStatus.sIsWebpSupportRequired && isWebpHeader(header, 0, HEADER_SIZE)) {
        bitmap = nativeDecodeStream(inputStream, opts, getScaleFromOptions(opts), getInTempStorageFromOptions(opts));
        // We notify that the direct decoder failed
        if (bitmap == null) {
            sendWebpErrorLog("webp_direct_decode_stream");
        }
        setWebpBitmapOptions(bitmap, opts);
        setPaddingDefaultValues(outPadding);
    } else {
        bitmap = originalDecodeStream(inputStream, outPadding, opts);
        if (bitmap == null) {
            sendWebpErrorLog("webp_direct_decode_stream_failed_on_no_webp");
        }
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) DoNotStrip(com.facebook.common.internal.DoNotStrip)

Example 4 with DoNotStrip

use of com.facebook.common.internal.DoNotStrip in project fresco by facebook.

the class WebpBitmapFactoryImpl method hookDecodeFileDescriptor.

@DoNotStrip
public static Bitmap hookDecodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) {
    StaticWebpNativeLoader.ensure();
    Bitmap bitmap;
    long originalSeekPosition = nativeSeek(fd, 0, false);
    if (originalSeekPosition != -1) {
        InputStream inputStream = wrapToMarkSupportedStream(new FileInputStream(fd));
        try {
            byte[] header = getWebpHeader(inputStream, opts);
            if (WebpSupportStatus.sIsWebpSupportRequired && isWebpHeader(header, 0, HEADER_SIZE)) {
                bitmap = nativeDecodeStream(inputStream, opts, getScaleFromOptions(opts), getInTempStorageFromOptions(opts));
                // We send error if the direct decode failed
                if (bitmap == null) {
                    sendWebpErrorLog("webp_direct_decode_fd");
                }
                setPaddingDefaultValues(outPadding);
                setWebpBitmapOptions(bitmap, opts);
            } else {
                nativeSeek(fd, originalSeekPosition, true);
                bitmap = originalDecodeFileDescriptor(fd, outPadding, opts);
                if (bitmap == null) {
                    sendWebpErrorLog("webp_direct_decode_fd_failed_on_no_webp");
                }
            }
        } finally {
            try {
                inputStream.close();
            } catch (Throwable t) {
            /* ignore */
            }
        }
    } else {
        bitmap = hookDecodeStream(new FileInputStream(fd), outPadding, opts);
        setPaddingDefaultValues(outPadding);
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) DoNotStrip(com.facebook.common.internal.DoNotStrip)

Example 5 with DoNotStrip

use of com.facebook.common.internal.DoNotStrip in project fresco by facebook.

the class WebpBitmapFactoryImpl method hookDecodeByteArray.

@DoNotStrip
public static Bitmap hookDecodeByteArray(byte[] array, int offset, int length, BitmapFactory.Options opts) {
    StaticWebpNativeLoader.ensure();
    Bitmap bitmap;
    if (WebpSupportStatus.sIsWebpSupportRequired && isWebpHeader(array, offset, length)) {
        bitmap = nativeDecodeByteArray(array, offset, length, opts, getScaleFromOptions(opts), getInTempStorageFromOptions(opts));
        // We notify that the direct decoding failed
        if (bitmap == null) {
            sendWebpErrorLog("webp_direct_decode_array");
        }
        setWebpBitmapOptions(bitmap, opts);
    } else {
        bitmap = originalDecodeByteArray(array, offset, length, opts);
        if (bitmap == null) {
            sendWebpErrorLog("webp_direct_decode_array_failed_on_no_webp");
        }
    }
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) DoNotStrip(com.facebook.common.internal.DoNotStrip)

Aggregations

DoNotStrip (com.facebook.common.internal.DoNotStrip)5 Bitmap (android.graphics.Bitmap)4 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 SuppressLint (android.annotation.SuppressLint)1 TypedValue (android.util.TypedValue)1 IOException (java.io.IOException)1