package com.kwad.sdk.glide.webp; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Rect; import android.os.Build; import android.util.Log; import android.util.TypedValue; import com.alibaba.fastjson.asm.Opcodes; import com.kwad.sdk.crash.utils.b; import com.kwad.sdk.glide.webp.WebpHeaderParser; import java.io.BufferedInputStream; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class WebpBitmapFactory { private static final int IN_TEMP_BUFFER_SIZE = 8192; private static final int MAX_WEBP_HEADER_SIZE = 21; static { try { System.loadLibrary("glide-webp"); } catch (Throwable unused) { } } private static Bitmap createBitmap(int i, int i2, BitmapFactory.Options options) { if (options != null && options.inBitmap != null && options.inBitmap.isMutable()) { return options.inBitmap; } Bitmap createBitmap = Bitmap.createBitmap(i, i2, Bitmap.Config.ARGB_8888); createBitmap.setHasAlpha(true); createBitmap.eraseColor(0); return createBitmap; } public static Bitmap decodeByteArray(byte[] bArr, int i, int i2) { return decodeByteArray(bArr, i, i2, null); } public static Bitmap decodeByteArray(byte[] bArr, int i, int i2, BitmapFactory.Options options) { if ((i | i2) < 0 || bArr.length < i + i2) { throw new ArrayIndexOutOfBoundsException(); } else if (!webpSupportRequired(bArr, i, i2)) { return BitmapFactory.decodeByteArray(bArr, i, i2, options); } else { Bitmap nativeDecodeByteArray = nativeDecodeByteArray(bArr, i, i2, options, getScaleFromOptions(options), getInTempStorageFromOptions(options)); setWebpBitmapOptions(nativeDecodeByteArray, options); return nativeDecodeByteArray; } } public static Bitmap decodeFile(String str) { return decodeFile(str, null); } public static Bitmap decodeFile(String str, BitmapFactory.Options options) { Throwable th; Exception e; FileInputStream fileInputStream; FileInputStream fileInputStream2 = null; Bitmap bitmap = null; try { fileInputStream = new FileInputStream(str); try { bitmap = decodeStream(fileInputStream, null, options); } catch (Exception e2) { e = e2; try { Log.e("WebpBitmapFactory", "Unable to decode stream: " + e); b.closeQuietly(fileInputStream); return bitmap; } catch (Throwable th2) { th = th2; fileInputStream2 = fileInputStream; } } } catch (Exception e3) { e = e3; fileInputStream = null; Log.e("WebpBitmapFactory", "Unable to decode stream: " + e); b.closeQuietly(fileInputStream); return bitmap; } catch (Throwable th3) { th = th3; b.closeQuietly(fileInputStream2); throw th; } b.closeQuietly(fileInputStream); return bitmap; } public static Bitmap decodeFileDescriptor(FileDescriptor fileDescriptor) { return decodeFileDescriptor(fileDescriptor, null, null); } public static Bitmap decodeFileDescriptor(FileDescriptor fileDescriptor, Rect rect, BitmapFactory.Options options) { Bitmap bitmap; InputStream wrapToMarkSupportedStream = wrapToMarkSupportedStream(new FileInputStream(fileDescriptor)); try { if (webpSupportRequired(getImageHeader(wrapToMarkSupportedStream), 0, 21)) { bitmap = nativeDecodeStream(wrapToMarkSupportedStream, options, getScaleFromOptions(options), getInTempStorageFromOptions(options)); setWebpBitmapOptions(bitmap, options); setDefaultPadding(rect); } else { bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, rect, options); } return bitmap; } finally { b.closeQuietly(wrapToMarkSupportedStream); } } public static Bitmap decodeResource(Resources resources, int i) { return decodeResource(resources, i, null); } public static Bitmap decodeResource(Resources resources, int i, BitmapFactory.Options options) { InputStream inputStream; Throwable th; Bitmap bitmap = null; bitmap = null; InputStream inputStream2 = null; try { TypedValue typedValue = new TypedValue(); inputStream = resources.openRawResource(i, typedValue); try { bitmap = decodeResourceStream(resources, typedValue, inputStream, null, options); } catch (Exception unused) { } catch (Throwable th2) { th = th2; inputStream2 = inputStream; b.closeQuietly(inputStream2); throw th; } } catch (Exception unused2) { inputStream = null; } catch (Throwable th3) { th = th3; b.closeQuietly(inputStream2); throw th; } b.closeQuietly(inputStream); if (bitmap != null || options == null || options.inBitmap == null) { return bitmap; } throw new IllegalArgumentException("Problem decoding into existing bitmap"); } /* JADX WARNING: Code restructure failed: missing block: B:10:0x0019, code lost: if (r2 != 65535) goto L_0x0013; */ public static Bitmap decodeResourceStream(Resources resources, TypedValue typedValue, InputStream inputStream, Rect rect, BitmapFactory.Options options) { if (options == null) { options = new BitmapFactory.Options(); } if (options.inDensity == 0 && typedValue != null) { int i = typedValue.density; if (i == 0) { i = Opcodes.IF_ICMPNE; } options.inDensity = i; } if (options.inTargetDensity == 0 && resources != null) { options.inTargetDensity = resources.getDisplayMetrics().densityDpi; } return decodeStream(inputStream, rect, options); } public static Bitmap decodeStream(InputStream inputStream) { return decodeStream(inputStream, null, null); } public static Bitmap decodeStream(InputStream inputStream, Rect rect, BitmapFactory.Options options) { if (inputStream == null) { return null; } InputStream wrapToMarkSupportedStream = wrapToMarkSupportedStream(inputStream); if (!webpSupportRequired(getImageHeader(wrapToMarkSupportedStream), 0, 21)) { return BitmapFactory.decodeStream(wrapToMarkSupportedStream, rect, options); } Bitmap nativeDecodeStream = nativeDecodeStream(wrapToMarkSupportedStream, options, getScaleFromOptions(options), getInTempStorageFromOptions(options)); setWebpBitmapOptions(nativeDecodeStream, options); setDefaultPadding(rect); return nativeDecodeStream; } private static byte[] getImageHeader(InputStream inputStream) { if (!inputStream.markSupported()) { inputStream = new BufferedInputStream(inputStream, 21); } inputStream.mark(21); byte[] bArr = new byte[21]; try { inputStream.read(bArr, 0, 21); inputStream.reset(); return bArr; } catch (IOException unused) { return null; } } private static byte[] getInTempStorageFromOptions(BitmapFactory.Options options) { return (options == null || options.inTempStorage == null) ? new byte[8192] : options.inTempStorage; } private static float getScaleFromOptions(BitmapFactory.Options options) { float f = 1.0f; if (options == null) { return 1.0f; } int i = options.inSampleSize; if (i > 1) { f = 1.0f / ((float) i); } if (!options.inScaled) { return f; } int i2 = options.inDensity; int i3 = options.inTargetDensity; return (i2 == 0 || i3 == 0 || i2 == options.inScreenDensity) ? f : ((float) i3) / ((float) i2); } private static native Bitmap nativeDecodeByteArray(byte[] bArr, int i, int i2, BitmapFactory.Options options, float f, byte[] bArr2); private static native Bitmap nativeDecodeStream(InputStream inputStream, BitmapFactory.Options options, float f, byte[] bArr); private static void setDefaultPadding(Rect rect) { if (rect != null) { rect.top = -1; rect.left = -1; rect.bottom = -1; rect.right = -1; } } private static void setDensityFromOptions(Bitmap bitmap, BitmapFactory.Options options) { if (bitmap != null && options != null) { int i = options.inDensity; if (i != 0) { bitmap.setDensity(i); int i2 = options.inTargetDensity; if (i2 != 0 && i != i2 && i != options.inScreenDensity && options.inScaled) { bitmap.setDensity(i2); } } else if (options.inBitmap != null) { bitmap.setDensity(Opcodes.IF_ICMPNE); } } } private static boolean setOutDimensions(BitmapFactory.Options options, int i, int i2) { if (options == null) { return false; } options.outWidth = i; options.outHeight = i2; return options.inJustDecodeBounds; } private static void setWebpBitmapOptions(Bitmap bitmap, BitmapFactory.Options options) { setDensityFromOptions(bitmap, options); if (options != null) { options.outMimeType = "image/webp"; } } public static boolean webpSupportRequired(byte[] bArr, int i, int i2) { WebpHeaderParser.WebpImageType webpImageType; try { webpImageType = WebpHeaderParser.b(bArr, i, i2); } catch (IOException unused) { webpImageType = WebpHeaderParser.WebpImageType.NONE_WEBP; } return Build.VERSION.SDK_INT <= 17 && WebpHeaderParser.b(webpImageType); } private static InputStream wrapToMarkSupportedStream(InputStream inputStream) { return !inputStream.markSupported() ? new BufferedInputStream(inputStream, 8192) : inputStream; } }