package j5;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorSpace;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Build;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.ImageHeaderParser$ImageType;
import com.bumptech.glide.load.PreferredColorSpace;
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy$SampleSizeRounding;
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.List;
import java.util.Objects;
import java.util.concurrent.locks.Lock;

/* JADX INFO: loaded from: classes.dex */
public final class n {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final a5.k f28666f = a5.k.a(DecodeFormat.f5597c, "com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeFormat");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final a5.k f28667g = a5.k.b("com.bumptech.glide.load.resource.bitmap.Downsampler.PreferredColorSpace");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final a5.k f28668h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final a5.k f28669i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final com.google.android.gms.measurement.internal.a0 f28670j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final ArrayDeque f28671k;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final c5.e f28672a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final DisplayMetrics f28673b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final c5.b f28674c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final List f28675d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final t f28676e = t.a();

    static {
        k kVar = l.f28659a;
        Boolean bool = Boolean.FALSE;
        f28668h = a5.k.a(bool, "com.bumptech.glide.load.resource.bitmap.Downsampler.FixBitmapSize");
        f28669i = a5.k.a(bool, "com.bumptech.glide.load.resource.bitmap.Downsampler.AllowHardwareDecode");
        Collections.unmodifiableSet(new HashSet(Arrays.asList("image/vnd.wap.wbmp", "image/x-ico")));
        f28670j = new com.google.android.gms.measurement.internal.a0(13);
        Collections.unmodifiableSet(EnumSet.of(ImageHeaderParser$ImageType.JPEG, ImageHeaderParser$ImageType.PNG_A, ImageHeaderParser$ImageType.PNG));
        char[] cArr = t5.p.f32977a;
        f28671k = new ArrayDeque(0);
    }

    public n(ArrayList arrayList, DisplayMetrics displayMetrics, c5.e eVar, c5.b bVar) {
        this.f28675d = arrayList;
        this.f28673b = (DisplayMetrics) t5.n.checkNotNull(displayMetrics);
        this.f28672a = (c5.e) t5.n.checkNotNull(eVar);
        this.f28674c = (c5.b) t5.n.checkNotNull(bVar);
    }

    /* JADX WARN: Code restructure failed: missing block: B:25:?, code lost:
    
        throw r0;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Bitmap c(x xVar, BitmapFactory.Options options, m mVar, c5.e eVar) {
        if (!options.inJustDecodeBounds) {
            mVar.e();
            xVar.b();
        }
        int i10 = options.outWidth;
        int i11 = options.outHeight;
        String str = options.outMimeType;
        Lock lock = c0.f28643b;
        lock.lock();
        try {
            try {
                Bitmap bitmapDecodeBitmap = xVar.decodeBitmap(options);
                lock.unlock();
                return bitmapDecodeBitmap;
            } catch (IllegalArgumentException e10) {
                IOException iOExceptionD = d(e10, i10, i11, str, options);
                Bitmap bitmap = options.inBitmap;
                if (bitmap == null) {
                    throw iOExceptionD;
                }
                try {
                    eVar.a(bitmap);
                    options.inBitmap = null;
                    Bitmap bitmapC = c(xVar, options, mVar, eVar);
                    c0.f28643b.unlock();
                    return bitmapC;
                } catch (IOException unused) {
                    throw iOExceptionD;
                }
            }
        } catch (Throwable th) {
            c0.f28643b.unlock();
            throw th;
        }
    }

    public static IOException d(IllegalArgumentException illegalArgumentException, int i10, int i11, String str, BitmapFactory.Options options) {
        StringBuilder sbR = defpackage.e.r("Exception decoding bitmap, outWidth: ", i10, ", outHeight: ", i11, ", outMimeType: ");
        sbR.append(str);
        sbR.append(", inBitmap: ");
        sbR.append(getBitmapString(options.inBitmap));
        return new IOException(sbR.toString(), illegalArgumentException);
    }

    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;
        if (Build.VERSION.SDK_INT >= 26) {
            options.inPreferredColorSpace = null;
            options.outColorSpace = null;
            options.outConfig = null;
        }
        options.outWidth = 0;
        options.outHeight = 0;
        options.outMimeType = null;
        options.inBitmap = null;
        options.inMutable = true;
    }

    @Nullable
    @TargetApi(19)
    private static String getBitmapString(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig() + (" (" + bitmap.getAllocationByteCount() + ")");
    }

    public final d a(x xVar, int i10, int i11, a5.l lVar, m mVar) {
        ArrayDeque arrayDeque;
        BitmapFactory.Options options;
        BitmapFactory.Options options2;
        byte[] bArr = (byte[]) ((c5.k) this.f28674c).c(C.DEFAULT_BUFFER_SEGMENT_SIZE, byte[].class);
        synchronized (n.class) {
            arrayDeque = f28671k;
            synchronized (arrayDeque) {
                options = (BitmapFactory.Options) arrayDeque.poll();
            }
            if (options == null) {
                options = new BitmapFactory.Options();
                e(options);
            }
            options2 = options;
        }
        options2.inTempStorage = bArr;
        DecodeFormat decodeFormat = (DecodeFormat) lVar.get(f28666f);
        PreferredColorSpace preferredColorSpace = (PreferredColorSpace) lVar.get(f28667g);
        l lVar2 = (l) lVar.get(l.f28664f);
        boolean zBooleanValue = ((Boolean) lVar.get(f28668h)).booleanValue();
        a5.k kVar = f28669i;
        try {
            d dVarObtain = d.obtain(b(xVar, options2, lVar2, decodeFormat, preferredColorSpace, lVar.get(kVar) != null && ((Boolean) lVar.get(kVar)).booleanValue(), i10, i11, zBooleanValue, mVar), this.f28672a);
            e(options2);
            synchronized (arrayDeque) {
                arrayDeque.offer(options2);
            }
            ((c5.k) this.f28674c).g(bArr);
            return dVarObtain;
        } catch (Throwable th) {
            e(options2);
            ArrayDeque arrayDeque2 = f28671k;
            synchronized (arrayDeque2) {
                arrayDeque2.offer(options2);
                ((c5.k) this.f28674c).g(bArr);
                throw th;
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:105:0x021f  */
    /* JADX WARN: Removed duplicated region for block: B:108:0x022b  */
    /* JADX WARN: Removed duplicated region for block: B:126:0x0262  */
    /* JADX WARN: Removed duplicated region for block: B:132:0x0271  */
    /* JADX WARN: Removed duplicated region for block: B:138:0x029b  */
    /* JADX WARN: Removed duplicated region for block: B:142:0x02a9  */
    /* JADX WARN: Removed duplicated region for block: B:144:0x02ac  */
    /* JADX WARN: Removed duplicated region for block: B:148:0x02b8  */
    /* JADX WARN: Removed duplicated region for block: B:163:0x02fc  */
    /* JADX WARN: Removed duplicated region for block: B:165:0x0310  */
    /* JADX WARN: Type inference failed for: r1v1, types: [android.graphics.Bitmap, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r34v0, types: [j5.m] */
    /* JADX WARN: Type inference failed for: r3v10 */
    /* JADX WARN: Type inference failed for: r3v11, types: [android.graphics.Bitmap] */
    /* JADX WARN: Type inference failed for: r3v17 */
    /* JADX WARN: Type inference failed for: r3v18, types: [java.lang.Object] */
    /* JADX WARN: Type inference failed for: r3v19 */
    /* JADX WARN: Type inference failed for: r7v0, types: [c5.e] */
    /* 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(x xVar, BitmapFactory.Options options, l lVar, DecodeFormat decodeFormat, PreferredColorSpace preferredColorSpace, boolean z10, int i10, int i11, boolean z11, m mVar) {
        char c6;
        boolean z12;
        int i12;
        int i13;
        int i14;
        boolean z13;
        int i15;
        String str;
        n nVar;
        boolean zB;
        int i16;
        int iRound;
        int i17;
        ?? C;
        Bitmap.Config config;
        int i18;
        int i19;
        int iFloor;
        int iFloor2;
        int i20 = t5.i.f32966a;
        SystemClock.elapsedRealtimeNanos();
        options.inJustDecodeBounds = true;
        ?? r72 = this.f28672a;
        c(xVar, options, mVar, r72);
        options.inJustDecodeBounds = false;
        int[] iArr = {options.outWidth, options.outHeight};
        int i21 = iArr[0];
        int i22 = iArr[1];
        boolean z14 = (i21 == -1 || i22 == -1) ? false : z10;
        int iA = xVar.a();
        switch (iA) {
            case 3:
            case 4:
                c6 = 180;
                break;
            case 5:
            case 6:
                c6 = 'Z';
                break;
            case 7:
            case 8:
                c6 = 270;
                break;
            default:
                c6 = 0;
                break;
        }
        switch (iA) {
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
                z12 = true;
                break;
            default:
                z12 = false;
                break;
        }
        int iRound2 = i10;
        if (iRound2 != Integer.MIN_VALUE) {
            i12 = i11;
        } else if (c6 == 'Z' || c6 == 270) {
            i12 = i11;
            iRound2 = i22;
        } else {
            i12 = i11;
            iRound2 = i21;
        }
        if (i12 == Integer.MIN_VALUE) {
            i12 = (c6 == 'Z' || c6 == 270) ? i21 : i22;
        }
        ImageHeaderParser$ImageType imageHeaderParser$ImageTypeC = xVar.c();
        if (i21 <= 0) {
            i13 = i22;
            i14 = i21;
            z13 = z14;
            i15 = i12;
            str = "Downsampler";
        } else {
            if (i22 > 0) {
                if (c6 == 'Z' || c6 == 270) {
                    i18 = i22;
                    i19 = i21;
                } else {
                    i19 = i22;
                    i18 = i21;
                }
                float fB = lVar.b(i18, i19, iRound2, i12);
                if (fB <= 0.0f) {
                    throw new IllegalArgumentException("Cannot scale with factor: " + fB + " from: " + lVar + ", source: [" + i21 + "x" + i22 + "], target: [" + iRound2 + "x" + i12 + "]");
                }
                z13 = z14;
                DownsampleStrategy$SampleSizeRounding downsampleStrategy$SampleSizeRoundingA = lVar.a(i18, i19, iRound2, i12);
                if (downsampleStrategy$SampleSizeRoundingA == null) {
                    throw new IllegalArgumentException("Cannot round with null rounding");
                }
                float f10 = i18;
                float f11 = i19;
                int i23 = i12;
                int i24 = i18 / ((int) (((double) (fB * f10)) + 0.5d));
                int i25 = i19 / ((int) (((double) (fB * f11)) + 0.5d));
                DownsampleStrategy$SampleSizeRounding downsampleStrategy$SampleSizeRounding = DownsampleStrategy$SampleSizeRounding.f5811a;
                int iMax = Math.max(1, Integer.highestOneBit(downsampleStrategy$SampleSizeRoundingA == downsampleStrategy$SampleSizeRounding ? Math.max(i24, i25) : Math.min(i24, i25)));
                if (downsampleStrategy$SampleSizeRoundingA == downsampleStrategy$SampleSizeRounding && iMax < 1.0f / fB) {
                    iMax <<= 1;
                }
                options.inSampleSize = iMax;
                if (imageHeaderParser$ImageTypeC == ImageHeaderParser$ImageType.JPEG) {
                    float fMin = Math.min(iMax, 8);
                    iFloor = (int) Math.ceil(f10 / fMin);
                    iFloor2 = (int) Math.ceil(f11 / fMin);
                    int i26 = iMax / 8;
                    if (i26 > 0) {
                        iFloor /= i26;
                        iFloor2 /= i26;
                    }
                } else if (imageHeaderParser$ImageTypeC == ImageHeaderParser$ImageType.PNG || imageHeaderParser$ImageTypeC == ImageHeaderParser$ImageType.PNG_A) {
                    float f12 = iMax;
                    iFloor = (int) Math.floor(f10 / f12);
                    iFloor2 = (int) Math.floor(f11 / f12);
                } else if (imageHeaderParser$ImageTypeC.isWebp()) {
                    float f13 = iMax;
                    iFloor = Math.round(f10 / f13);
                    iFloor2 = Math.round(f11 / f13);
                } else if (i18 % iMax == 0 && i19 % iMax == 0) {
                    iFloor = i18 / iMax;
                    iFloor2 = i19 / iMax;
                } else {
                    options.inJustDecodeBounds = true;
                    c(xVar, options, mVar, r72);
                    options.inJustDecodeBounds = false;
                    int[] iArr2 = {options.outWidth, options.outHeight};
                    int i27 = iArr2[0];
                    iFloor2 = iArr2[1];
                    iFloor = i27;
                }
                i15 = i23;
                double dB = lVar.b(iFloor, iFloor2, iRound2, i15);
                int iRound3 = (int) Math.round((dB <= 1.0d ? dB : 1.0d / dB) * 2.147483647E9d);
                int i28 = (int) ((((double) iRound3) * dB) + 0.5d);
                options.inTargetDensity = (int) (((dB / ((double) (i28 / iRound3))) * ((double) i28)) + 0.5d);
                if (dB > 1.0d) {
                    dB = 1.0d / dB;
                }
                int iRound4 = (int) Math.round(dB * 2.147483647E9d);
                options.inDensity = iRound4;
                int i29 = options.inTargetDensity;
                if (i29 <= 0 || iRound4 <= 0 || i29 == iRound4) {
                    options.inTargetDensity = 0;
                    options.inDensity = 0;
                } else {
                    options.inScaled = true;
                }
                nVar = this;
                str = "Downsampler";
                i14 = i21;
                i13 = i22;
                zB = nVar.f28676e.b(iRound2, i15, z13, z12);
                if (zB) {
                    options.inPreferredConfig = Bitmap.Config.HARDWARE;
                    options.inMutable = false;
                }
                if (!zB) {
                    if (decodeFormat != DecodeFormat.f5595a) {
                        try {
                        } catch (IOException unused) {
                            if (Log.isLoggable(str, 3)) {
                                Objects.toString(decodeFormat);
                            }
                        }
                        Bitmap.Config config2 = xVar.c().hasAlpha() ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                        options.inPreferredConfig = config2;
                        if (config2 == Bitmap.Config.RGB_565) {
                            options.inDither = true;
                        }
                    } else {
                        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    }
                }
                i16 = Build.VERSION.SDK_INT;
                if (i14 >= 0 || i13 < 0 || !z11) {
                    int i30 = options.inTargetDensity;
                    float f14 = (i30 > 0 || (i17 = options.inDensity) <= 0 || i30 == i17) ? 1.0f : i30 / i17;
                    float f15 = options.inSampleSize;
                    int iCeil = (int) Math.ceil(i14 / f15);
                    int iCeil2 = (int) Math.ceil(i13 / f15);
                    iRound2 = Math.round(iCeil * f14);
                    iRound = Math.round(iCeil2 * f14);
                } else {
                    iRound = i15;
                }
                ?? r32 = 0;
                if (iRound2 > 0 && iRound > 0) {
                    if (i16 >= 26) {
                        config = null;
                    } else if (options.inPreferredConfig != Bitmap.Config.HARDWARE) {
                        config = options.outConfig;
                    }
                    if (config == null) {
                        config = options.inPreferredConfig;
                    }
                    options.inBitmap = r72.b(iRound2, iRound, config);
                }
                if (preferredColorSpace != null) {
                    if (i16 >= 28) {
                        options.inPreferredColorSpace = ColorSpace.get((preferredColorSpace == PreferredColorSpace.f5603a && options.outColorSpace != null && options.outColorSpace.isWideGamut()) ? ColorSpace.Named.DISPLAY_P3 : ColorSpace.Named.SRGB);
                    } else if (i16 >= 26) {
                        options.inPreferredColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
                    }
                }
                C = c(xVar, options, mVar, r72);
                mVar.c(C, r72);
                if (Log.isLoggable(str, 2)) {
                    getBitmapString(C);
                    getBitmapString(options.inBitmap);
                    Thread.currentThread().getName();
                    SystemClock.elapsedRealtimeNanos();
                }
                if (C != 0) {
                    C.setDensity(nVar.f28673b.densityDpi);
                    switch (iA) {
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                            Matrix matrix = new Matrix();
                            switch (iA) {
                                case 2:
                                    matrix.setScale(-1.0f, 1.0f);
                                    break;
                                case 3:
                                    matrix.setRotate(180.0f);
                                    break;
                                case 4:
                                    matrix.setRotate(180.0f);
                                    matrix.postScale(-1.0f, 1.0f);
                                    break;
                                case 5:
                                    matrix.setRotate(90.0f);
                                    matrix.postScale(-1.0f, 1.0f);
                                    break;
                                case 6:
                                    matrix.setRotate(90.0f);
                                    break;
                                case 7:
                                    matrix.setRotate(-90.0f);
                                    matrix.postScale(-1.0f, 1.0f);
                                    break;
                                case 8:
                                    matrix.setRotate(-90.0f);
                                    break;
                            }
                            RectF rectF = new RectF(0.0f, 0.0f, C.getWidth(), C.getHeight());
                            matrix.mapRect(rectF);
                            Bitmap bitmap = r72.get(Math.round(rectF.width()), Math.round(rectF.height()), C.getConfig() != null ? C.getConfig() : Bitmap.Config.ARGB_8888);
                            matrix.postTranslate(-rectF.left, -rectF.top);
                            bitmap.setHasAlpha(C.hasAlpha());
                            c0.a(C, bitmap, matrix);
                            r32 = bitmap;
                            break;
                        default:
                            r32 = C;
                            break;
                    }
                    if (!C.equals(r32)) {
                        r72.a(C);
                    }
                }
                return r32;
            }
            i13 = i22;
            i14 = i21;
            z13 = z14;
            str = "Downsampler";
            i15 = i12;
        }
        if (Log.isLoggable(str, 3)) {
            Objects.toString(imageHeaderParser$ImageTypeC);
        }
        nVar = this;
        zB = nVar.f28676e.b(iRound2, i15, z13, z12);
        if (zB) {
        }
        if (!zB) {
        }
        i16 = Build.VERSION.SDK_INT;
        if (i14 >= 0) {
            int i302 = options.inTargetDensity;
            if (i302 > 0) {
                float f152 = options.inSampleSize;
                int iCeil3 = (int) Math.ceil(i14 / f152);
                int iCeil22 = (int) Math.ceil(i13 / f152);
                iRound2 = Math.round(iCeil3 * f14);
                iRound = Math.round(iCeil22 * f14);
            }
        }
        ?? r322 = 0;
        if (iRound2 > 0) {
            if (i16 >= 26) {
            }
            if (config == null) {
            }
            options.inBitmap = r72.b(iRound2, iRound, config);
        }
        if (preferredColorSpace != null) {
        }
        C = c(xVar, options, mVar, r72);
        mVar.c(C, r72);
        if (Log.isLoggable(str, 2)) {
        }
        if (C != 0) {
        }
        return r322;
    }
}
