package defpackage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorSpace;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import com.bumptech.glide.load.ImageHeaderParser;
import defpackage.af2;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Objects;
import java.util.concurrent.locks.Lock;

/* JADX INFO: loaded from: classes.dex */
public final class bf2 {
    public static final tx6 f = tx6.a(bx1.c, "com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeFormat");
    public static final tx6 g = new tx6("com.bumptech.glide.load.resource.bitmap.Downsampler.PreferredColorSpace", null, tx6.e);
    public static final tx6 h;
    public static final tx6 i;
    public static final b j;
    public static final ArrayDeque k;
    public final xa0 a;
    public final DisplayMetrics b;
    public final ys c;
    public final ArrayList d;
    public final xz3 e = xz3.a();

    public class a implements b {
        @Override // bf2.b
        public final void a() {
        }

        @Override // bf2.b
        public final void b(xa0 xa0Var, Bitmap bitmap) {
        }
    }

    public interface b {
        void a();

        void b(xa0 xa0Var, Bitmap bitmap);
    }

    static {
        af2 af2Var = af2.a;
        Boolean bool = Boolean.FALSE;
        h = tx6.a(bool, "com.bumptech.glide.load.resource.bitmap.Downsampler.FixBitmapSize");
        i = tx6.a(bool, "com.bumptech.glide.load.resource.bitmap.Downsampler.AllowHardwareDecode");
        Collections.unmodifiableSet(new HashSet(Arrays.asList("image/vnd.wap.wbmp", "image/x-ico")));
        j = new a();
        Collections.unmodifiableSet(EnumSet.of(ImageHeaderParser.ImageType.JPEG, ImageHeaderParser.ImageType.PNG_A, ImageHeaderParser.ImageType.PNG));
        char[] cArr = hda.a;
        k = new ArrayDeque(0);
    }

    public bf2(ArrayList arrayList, DisplayMetrics displayMetrics, xa0 xa0Var, ys ysVar) {
        this.d = arrayList;
        gf7.c(displayMetrics, "Argument must not be null");
        this.b = displayMetrics;
        gf7.c(xa0Var, "Argument must not be null");
        this.a = xa0Var;
        gf7.c(ysVar, "Argument must not be null");
        this.c = ysVar;
    }

    /* JADX WARN: Code restructure failed: missing block: B:24:?, code lost:
    
        throw r4;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Bitmap c(hi4 hi4Var, BitmapFactory.Options options, b bVar, xa0 xa0Var) {
        if (!options.inJustDecodeBounds) {
            bVar.a();
            hi4Var.b();
        }
        int i2 = options.outWidth;
        int i3 = options.outHeight;
        String str = options.outMimeType;
        Lock lock = ht9.d;
        lock.lock();
        try {
            try {
                Bitmap bitmapA = hi4Var.a(options);
                lock.unlock();
                return bitmapA;
            } catch (IllegalArgumentException e) {
                StringBuilder sbR = vw2.r("Exception decoding bitmap, outWidth: ", i2, ", outHeight: ", ", outMimeType: ", i3);
                sbR.append(str);
                sbR.append(", inBitmap: ");
                sbR.append(d(options.inBitmap));
                IOException iOException = new IOException(sbR.toString(), e);
                Bitmap bitmap = options.inBitmap;
                if (bitmap == null) {
                    throw iOException;
                }
                try {
                    xa0Var.d(bitmap);
                    options.inBitmap = null;
                    Bitmap bitmapC = c(hi4Var, options, bVar, xa0Var);
                    ht9.d.unlock();
                    return bitmapC;
                } catch (IOException unused) {
                    throw iOException;
                }
            }
        } catch (Throwable th) {
            ht9.d.unlock();
            throw th;
        }
    }

    public static String d(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig() + (" (" + bitmap.getAllocationByteCount() + ")");
    }

    public static void e(BitmapFactory.Options options) {
        options.inTempStorage = null;
        options.inDither = false;
        options.inScaled = false;
        options.inSampleSize = 1;
        options.inPreferredConfig = null;
        options.inJustDecodeBounds = false;
        options.inDensity = 0;
        options.inTargetDensity = 0;
        options.inPreferredColorSpace = null;
        options.outColorSpace = null;
        options.outConfig = null;
        options.outWidth = 0;
        options.outHeight = 0;
        options.outMimeType = null;
        options.inBitmap = null;
        options.inMutable = true;
    }

    public final bb0 a(hi4 hi4Var, int i2, int i3, my6 my6Var, b bVar) {
        ArrayDeque arrayDeque;
        BitmapFactory.Options options;
        byte[] bArr = (byte[]) this.c.c(byte[].class, 65536);
        synchronized (bf2.class) {
            arrayDeque = k;
            synchronized (arrayDeque) {
                options = (BitmapFactory.Options) arrayDeque.poll();
            }
            if (options == null) {
                options = new BitmapFactory.Options();
                e(options);
            }
        }
        options.inTempStorage = bArr;
        bx1 bx1Var = (bx1) my6Var.c(f);
        sg7 sg7Var = (sg7) my6Var.c(g);
        af2 af2Var = (af2) my6Var.c(af2.f);
        boolean zBooleanValue = ((Boolean) my6Var.c(h)).booleanValue();
        tx6 tx6Var = i;
        try {
            bb0 bb0VarB = bb0.b(this.a, b(hi4Var, options, af2Var, bx1Var, sg7Var, my6Var.c(tx6Var) != null && ((Boolean) my6Var.c(tx6Var)).booleanValue(), i2, i3, zBooleanValue, bVar));
            e(options);
            synchronized (arrayDeque) {
                arrayDeque.offer(options);
            }
            this.c.put(bArr);
            return bb0VarB;
        } catch (Throwable th) {
            e(options);
            ArrayDeque arrayDeque2 = k;
            synchronized (arrayDeque2) {
                arrayDeque2.offer(options);
                this.c.put(bArr);
                throw th;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:103:0x0210  */
    /* JADX WARN: Removed duplicated region for block: B:104:0x0218  */
    /* JADX WARN: Removed duplicated region for block: B:106:0x021b  */
    /* JADX WARN: Removed duplicated region for block: B:107:0x021d  */
    /* JADX WARN: Removed duplicated region for block: B:126:0x0254  */
    /* JADX WARN: Removed duplicated region for block: B:132:0x0260  */
    /* JADX WARN: Removed duplicated region for block: B:134:0x0263  */
    /* JADX WARN: Removed duplicated region for block: B:135:0x0269  */
    /* JADX WARN: Removed duplicated region for block: B:145:0x029b  */
    /* JADX WARN: Removed duplicated region for block: B:148:0x02a4  */
    /* JADX WARN: Removed duplicated region for block: B:162:0x02d2  */
    /* JADX WARN: Removed duplicated region for block: B:164:0x02e6  */
    /* JADX WARN: Removed duplicated region for block: B:185:0x0382 A[ORIG_RETURN, RETURN] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bitmap b(hi4 hi4Var, BitmapFactory.Options options, af2 af2Var, bx1 bx1Var, sg7 sg7Var, boolean z, int i2, int i3, boolean z2, b bVar) {
        char c;
        boolean z3;
        int iRound;
        boolean z4;
        float f2;
        String str;
        int i4;
        bf2 bf2Var;
        boolean zC;
        boolean z5;
        boolean zHasAlpha;
        boolean z6;
        int i5;
        Bitmap bitmapC;
        Bitmap bitmap;
        ColorSpace colorSpace;
        Bitmap.Config config;
        Bitmap.Config config2;
        int i6;
        int i7;
        int iFloor;
        int iFloor2;
        int i8 = zg5.a;
        SystemClock.elapsedRealtimeNanos();
        options.inJustDecodeBounds = true;
        xa0 xa0Var = this.a;
        c(hi4Var, options, bVar, xa0Var);
        options.inJustDecodeBounds = false;
        int[] iArr = {options.outWidth, options.outHeight};
        int i9 = iArr[0];
        int i10 = iArr[1];
        boolean z7 = (i9 == -1 || i10 == -1) ? false : z;
        int iC = hi4Var.c();
        switch (iC) {
            case 3:
            case 4:
                c = 180;
                break;
            case 5:
            case 6:
                c = 'Z';
                break;
            case 7:
            case 8:
                c = 270;
                break;
            default:
                c = 0;
                break;
        }
        switch (iC) {
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
                z3 = true;
                break;
            default:
                z3 = false;
                break;
        }
        int i11 = i2;
        if (i11 != Integer.MIN_VALUE) {
            iRound = i3;
        } else if (c == 'Z' || c == 270) {
            iRound = i3;
            i11 = i10;
        } else {
            iRound = i3;
            i11 = i9;
        }
        if (iRound == Integer.MIN_VALUE) {
            iRound = (c == 'Z' || c == 270) ? i9 : i10;
        }
        ImageHeaderParser.ImageType imageTypeD = hi4Var.d();
        if (i9 <= 0) {
            z4 = z7;
            f2 = 1.0f;
            str = "Downsampler";
            i4 = 3;
        } else {
            if (i10 > 0) {
                f2 = 1.0f;
                if (c == 'Z' || c == 270) {
                    i6 = i10;
                    i7 = i9;
                } else {
                    i7 = i10;
                    i6 = i9;
                }
                float fB = af2Var.b(i6, i7, i11, iRound);
                if (fB <= 0.0f) {
                    throw new IllegalArgumentException("Cannot scale with factor: " + fB + " from: " + af2Var + ", source: [" + i9 + "x" + i10 + "], target: [" + i11 + "x" + iRound + "]");
                }
                z4 = z7;
                af2.g gVarA = af2Var.a(i6, i7, i11, iRound);
                if (gVarA == null) {
                    throw new IllegalArgumentException("Cannot round with null rounding");
                }
                float f3 = i6;
                int i12 = i6;
                float f4 = i7;
                int i13 = i12 / ((int) (((double) (fB * f3)) + 0.5d));
                int i14 = i7 / ((int) (((double) (fB * f4)) + 0.5d));
                af2.g gVar = af2.g.a;
                int iMax = Math.max(1, Integer.highestOneBit(gVarA == gVar ? Math.max(i13, i14) : Math.min(i13, i14)));
                if (gVarA == gVar && iMax < 1.0f / fB) {
                    iMax <<= 1;
                }
                options.inSampleSize = iMax;
                if (imageTypeD == ImageHeaderParser.ImageType.JPEG) {
                    float fMin = Math.min(iMax, 8);
                    iFloor = (int) Math.ceil(f3 / fMin);
                    iFloor2 = (int) Math.ceil(f4 / fMin);
                    int i15 = iMax / 8;
                    if (i15 > 0) {
                        iFloor /= i15;
                        iFloor2 /= i15;
                    }
                } else if (imageTypeD == ImageHeaderParser.ImageType.PNG || imageTypeD == ImageHeaderParser.ImageType.PNG_A) {
                    float f5 = iMax;
                    iFloor = (int) Math.floor(f3 / f5);
                    iFloor2 = (int) Math.floor(f4 / f5);
                } else if (imageTypeD.isWebp()) {
                    float f6 = iMax;
                    iFloor = Math.round(f3 / f6);
                    iFloor2 = Math.round(f4 / f6);
                } else if (i12 % iMax == 0 && i7 % iMax == 0) {
                    iFloor = i12 / iMax;
                    iFloor2 = i7 / iMax;
                } else {
                    options.inJustDecodeBounds = true;
                    c(hi4Var, options, bVar, xa0Var);
                    options.inJustDecodeBounds = false;
                    int[] iArr2 = {options.outWidth, options.outHeight};
                    iFloor = iArr2[0];
                    iFloor2 = iArr2[1];
                }
                double dB = af2Var.b(iFloor, iFloor2, i11, iRound);
                int iRound2 = (int) Math.round((dB <= 1.0d ? dB : 1.0d / dB) * 2.147483647E9d);
                int i16 = (int) ((((double) iRound2) * dB) + 0.5d);
                options.inTargetDensity = (int) (((dB / ((double) (i16 / iRound2))) * ((double) i16)) + 0.5d);
                int iRound3 = (int) Math.round((dB <= 1.0d ? dB : 1.0d / dB) * 2.147483647E9d);
                options.inDensity = iRound3;
                int i17 = options.inTargetDensity;
                if (i17 <= 0 || iRound3 <= 0 || i17 == iRound3) {
                    options.inTargetDensity = 0;
                    options.inDensity = 0;
                } else {
                    options.inScaled = true;
                }
                bf2Var = this;
                str = "Downsampler";
                zC = bf2Var.e.c(i11, iRound, z4, z3);
                if (zC) {
                    z5 = false;
                } else {
                    options.inPreferredConfig = Bitmap.Config.HARDWARE;
                    z5 = false;
                    options.inMutable = false;
                }
                if (!zC) {
                    if (bx1Var != bx1.a) {
                        try {
                            zHasAlpha = hi4Var.d().hasAlpha();
                        } catch (IOException unused) {
                            if (Log.isLoggable(str, 3)) {
                                Objects.toString(bx1Var);
                            }
                            zHasAlpha = z5;
                        }
                        Bitmap.Config config3 = zHasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                        options.inPreferredConfig = config3;
                        if (config3 == Bitmap.Config.RGB_565) {
                            z6 = true;
                            options.inDither = true;
                        } else {
                            z6 = true;
                        }
                    } else {
                        z6 = true;
                        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    }
                }
                if (i9 >= 0 || i10 < 0 || !z2) {
                    int i18 = options.inTargetDensity;
                    float f7 = !((i18 > 0 || (i5 = options.inDensity) <= 0 || i18 == i5) ? z5 : z6) ? i18 / options.inDensity : f2;
                    float f8 = options.inSampleSize;
                    int iCeil = (int) Math.ceil(i9 / f8);
                    int iCeil2 = (int) Math.ceil(i10 / f8);
                    int iRound4 = Math.round(iCeil * f7);
                    iRound = Math.round(iCeil2 * f7);
                    i11 = iRound4;
                }
                if (i11 > 0 && iRound > 0 && (config = options.inPreferredConfig) != Bitmap.Config.HARDWARE) {
                    config2 = options.outConfig;
                    if (config2 != null) {
                        config = config2;
                    }
                    options.inBitmap = xa0Var.c(i11, iRound, config);
                }
                if (sg7Var != null) {
                    options.inPreferredColorSpace = ColorSpace.get((sg7Var != sg7.a || (colorSpace = options.outColorSpace) == null || !colorSpace.isWideGamut()) ? z5 : z6 ? ColorSpace.Named.DISPLAY_P3 : ColorSpace.Named.SRGB);
                }
                bitmapC = c(hi4Var, options, bVar, xa0Var);
                bVar.b(xa0Var, bitmapC);
                if (Log.isLoggable(str, 2)) {
                    d(bitmapC);
                    d(options.inBitmap);
                    Thread.currentThread().getName();
                    SystemClock.elapsedRealtimeNanos();
                }
                if (bitmapC != null) {
                    return null;
                }
                bitmapC.setDensity(bf2Var.b.densityDpi);
                switch (iC) {
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        Matrix matrix = new Matrix();
                        switch (iC) {
                            case 2:
                                matrix.setScale(-1.0f, f2);
                                break;
                            case 3:
                                matrix.setRotate(180.0f);
                                break;
                            case 4:
                                matrix.setRotate(180.0f);
                                matrix.postScale(-1.0f, f2);
                                break;
                            case 5:
                                matrix.setRotate(90.0f);
                                matrix.postScale(-1.0f, f2);
                                break;
                            case 6:
                                matrix.setRotate(90.0f);
                                break;
                            case 7:
                                matrix.setRotate(-90.0f);
                                matrix.postScale(-1.0f, f2);
                                break;
                            case 8:
                                matrix.setRotate(-90.0f);
                                break;
                        }
                        RectF rectF = new RectF(0.0f, 0.0f, bitmapC.getWidth(), bitmapC.getHeight());
                        matrix.mapRect(rectF);
                        Bitmap bitmapE = xa0Var.e(Math.round(rectF.width()), Math.round(rectF.height()), bitmapC.getConfig() != null ? bitmapC.getConfig() : Bitmap.Config.ARGB_8888);
                        matrix.postTranslate(-rectF.left, -rectF.top);
                        bitmapE.setHasAlpha(bitmapC.hasAlpha());
                        ht9.a(bitmapC, bitmapE, matrix);
                        bitmap = bitmapE;
                        break;
                    default:
                        bitmap = bitmapC;
                        break;
                }
                if (bitmapC.equals(bitmap)) {
                    return bitmap;
                }
                xa0Var.d(bitmapC);
                return bitmap;
            }
            z4 = z7;
            str = "Downsampler";
            i4 = 3;
            f2 = 1.0f;
        }
        if (Log.isLoggable(str, i4)) {
            Objects.toString(imageTypeD);
        }
        bf2Var = this;
        zC = bf2Var.e.c(i11, iRound, z4, z3);
        if (zC) {
        }
        if (!zC) {
        }
        if (i9 >= 0) {
            int i182 = options.inTargetDensity;
            if (i182 > 0) {
                if (!((i182 > 0 || (i5 = options.inDensity) <= 0 || i182 == i5) ? z5 : z6)) {
                }
                float f82 = options.inSampleSize;
                int iCeil3 = (int) Math.ceil(i9 / f82);
                int iCeil22 = (int) Math.ceil(i10 / f82);
                int iRound42 = Math.round(iCeil3 * f7);
                iRound = Math.round(iCeil22 * f7);
                i11 = iRound42;
            }
        }
        if (i11 > 0) {
            config2 = options.outConfig;
            if (config2 != null) {
            }
            options.inBitmap = xa0Var.c(i11, iRound, config);
        }
        if (sg7Var != null) {
        }
        bitmapC = c(hi4Var, options, bVar, xa0Var);
        bVar.b(xa0Var, bitmapC);
        if (Log.isLoggable(str, 2)) {
        }
        if (bitmapC != null) {
        }
    }
}
