package coil.decode;

import A4.d;
import A4.k;
import C4.c;
import D.C0135f;
import V4.C0759f0;
import V4.E;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import coil.ImageLoader;
import coil.decode.Decoder;
import coil.decode.ImageSource;
import coil.fetch.SourceResult;
import coil.request.Options;
import coil.size.Size;
import coil.size.Sizes;
import coil.util.Bitmaps;
import coil.util.Utils;
import e5.e;
import e5.j;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import m3.AbstractC2467b;
import p5.AbstractC2732b;
import p5.C2739i;
import p5.C2740j;
import p5.J;
import p5.P;
import p5.w;
import w4.InterfaceC3141c;

/* JADX INFO: loaded from: classes.dex */
public final class BitmapFactoryDecoder implements Decoder {
    public static final Companion Companion = new Companion(null);
    public static final int DEFAULT_MAX_PARALLELISM = 4;
    private final ExifOrientationPolicy exifOrientationPolicy;
    private final Options options;
    private final e parallelismLock;
    private final ImageSource source;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private Companion() {
        }
    }

    public static final class ExceptionCatchingSource extends w {
        private Exception exception;

        public ExceptionCatchingSource(P p6) {
            super(p6);
        }

        public final Exception getException() {
            return this.exception;
        }

        @Override // p5.w, p5.P
        public long read(C2740j c2740j, long j4) throws Exception {
            try {
                return super.read(c2740j, j4);
            } catch (Exception e6) {
                this.exception = e6;
                throw e6;
            }
        }
    }

    /* JADX INFO: renamed from: coil.decode.BitmapFactoryDecoder$decode$1, reason: invalid class name */
    @C4.e(c = "coil.decode.BitmapFactoryDecoder", f = "BitmapFactoryDecoder.kt", l = {231, 46}, m = "decode")
    public static final class AnonymousClass1 extends c {
        Object L$0;
        Object L$1;
        int label;
        /* synthetic */ Object result;

        public AnonymousClass1(d dVar) {
            super(dVar);
        }

        @Override // C4.a
        public final Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return BitmapFactoryDecoder.this.decode(this);
        }
    }

    public BitmapFactoryDecoder(ImageSource imageSource, Options options, e eVar, ExifOrientationPolicy exifOrientationPolicy) {
        this.source = imageSource;
        this.options = options;
        this.parallelismLock = eVar;
        this.exifOrientationPolicy = exifOrientationPolicy;
    }

    private final void configureConfig(BitmapFactory.Options options, ExifData exifData) {
        Bitmap.Config config = this.options.getConfig();
        if (exifData.isFlipped() || ExifUtilsKt.isRotated(exifData)) {
            config = Bitmaps.toSoftware(config);
        }
        if (this.options.getAllowRgb565() && config == Bitmap.Config.ARGB_8888 && o.c(options.outMimeType, "image/jpeg")) {
            config = Bitmap.Config.RGB_565;
        }
        if (Build.VERSION.SDK_INT >= 26 && options.outConfig == Bitmap.Config.RGBA_F16 && config != Bitmap.Config.HARDWARE) {
            config = Bitmap.Config.RGBA_F16;
        }
        options.inPreferredConfig = config;
    }

    private final void configureScale(BitmapFactory.Options options, ExifData exifData) {
        ImageSource.Metadata metadata = this.source.getMetadata();
        if ((metadata instanceof ResourceMetadata) && Sizes.isOriginal(this.options.getSize())) {
            options.inSampleSize = 1;
            options.inScaled = true;
            options.inDensity = ((ResourceMetadata) metadata).getDensity();
            options.inTargetDensity = this.options.getContext().getResources().getDisplayMetrics().densityDpi;
            return;
        }
        if (options.outWidth <= 0 || options.outHeight <= 0) {
            options.inSampleSize = 1;
            options.inScaled = false;
            return;
        }
        int i6 = ExifUtilsKt.isSwapped(exifData) ? options.outHeight : options.outWidth;
        int i7 = ExifUtilsKt.isSwapped(exifData) ? options.outWidth : options.outHeight;
        Size size = this.options.getSize();
        int px = Sizes.isOriginal(size) ? i6 : Utils.toPx(size.getWidth(), this.options.getScale());
        Size size2 = this.options.getSize();
        int px2 = Sizes.isOriginal(size2) ? i7 : Utils.toPx(size2.getHeight(), this.options.getScale());
        int iCalculateInSampleSize = DecodeUtils.calculateInSampleSize(i6, i7, px, px2, this.options.getScale());
        options.inSampleSize = iCalculateInSampleSize;
        double dComputeSizeMultiplier = DecodeUtils.computeSizeMultiplier(((double) i6) / ((double) iCalculateInSampleSize), ((double) i7) / ((double) iCalculateInSampleSize), px, px2, this.options.getScale());
        if (this.options.getAllowInexactSize() && dComputeSizeMultiplier > 1.0d) {
            dComputeSizeMultiplier = 1.0d;
        }
        boolean z6 = dComputeSizeMultiplier == 1.0d;
        options.inScaled = !z6;
        if (z6) {
            return;
        }
        if (dComputeSizeMultiplier > 1.0d) {
            options.inDensity = L4.a.c0(((double) Integer.MAX_VALUE) / dComputeSizeMultiplier);
            options.inTargetDensity = Integer.MAX_VALUE;
        } else {
            options.inDensity = Integer.MAX_VALUE;
            options.inTargetDensity = L4.a.c0(((double) Integer.MAX_VALUE) * dComputeSizeMultiplier);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final DecodeResult decode$lambda$1$lambda$0(BitmapFactoryDecoder bitmapFactoryDecoder) {
        return bitmapFactoryDecoder.decode(new BitmapFactory.Options());
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    @Override // coil.decode.Decoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object decode(d dVar) throws Throwable {
        AnonymousClass1 anonymousClass1;
        Object obj;
        BitmapFactoryDecoder bitmapFactoryDecoder;
        Object obj2;
        Throwable th;
        if (dVar instanceof AnonymousClass1) {
            anonymousClass1 = (AnonymousClass1) dVar;
            int i6 = anonymousClass1.label;
            if ((i6 & Integer.MIN_VALUE) != 0) {
                anonymousClass1.label = i6 - Integer.MIN_VALUE;
            } else {
                anonymousClass1 = new AnonymousClass1(dVar);
            }
        }
        Object obj3 = anonymousClass1.result;
        B4.a aVar = B4.a.f492a;
        int i7 = anonymousClass1.label;
        try {
            if (i7 == 0) {
                AbstractC2467b.l(obj3);
                obj = this.parallelismLock;
                anonymousClass1.L$0 = this;
                anonymousClass1.L$1 = obj;
                anonymousClass1.label = 1;
                if (((e5.h) obj).a(anonymousClass1) != aVar) {
                    bitmapFactoryDecoder = this;
                }
                return aVar;
            }
            if (i7 != 1) {
                if (i7 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                obj2 = (e) anonymousClass1.L$0;
                try {
                    AbstractC2467b.l(obj3);
                    DecodeResult decodeResult = (DecodeResult) obj3;
                    ((e5.h) obj2).c();
                    return decodeResult;
                } catch (Throwable th2) {
                    th = th2;
                    ((e5.h) obj2).c();
                    throw th;
                }
            }
            e eVar = (e) anonymousClass1.L$1;
            bitmapFactoryDecoder = (BitmapFactoryDecoder) anonymousClass1.L$0;
            AbstractC2467b.l(obj3);
            obj = eVar;
            C0135f c0135f = new C0135f(bitmapFactoryDecoder, 27);
            anonymousClass1.L$0 = obj;
            anonymousClass1.L$1 = null;
            anonymousClass1.label = 2;
            Object objH = E.H(k.f106a, new C0759f0(c0135f, null), anonymousClass1);
            if (objH != aVar) {
                obj2 = obj;
                obj3 = objH;
                DecodeResult decodeResult2 = (DecodeResult) obj3;
                ((e5.h) obj2).c();
                return decodeResult2;
            }
            return aVar;
        } catch (Throwable th3) {
            obj2 = obj;
            th = th3;
            ((e5.h) obj2).c();
            throw th;
        }
    }

    public static final class Factory implements Decoder.Factory {
        private final ExifOrientationPolicy exifOrientationPolicy;
        private final e parallelismLock;

        public Factory(int i6, ExifOrientationPolicy exifOrientationPolicy) {
            this.exifOrientationPolicy = exifOrientationPolicy;
            this.parallelismLock = j.a(i6);
        }

        @Override // coil.decode.Decoder.Factory
        public Decoder create(SourceResult sourceResult, Options options, ImageLoader imageLoader) {
            return new BitmapFactoryDecoder(sourceResult.getSource(), options, this.parallelismLock, this.exifOrientationPolicy);
        }

        public boolean equals(Object obj) {
            return obj instanceof Factory;
        }

        public int hashCode() {
            return Factory.class.hashCode();
        }

        public /* synthetic */ Factory(int i6, ExifOrientationPolicy exifOrientationPolicy, int i7, h hVar) {
            this((i7 & 1) != 0 ? 4 : i6, (i7 & 2) != 0 ? ExifOrientationPolicy.RESPECT_PERFORMANCE : exifOrientationPolicy);
        }

        /* JADX WARN: Multi-variable type inference failed */
        public Factory() {
            this(0, null, 3, 0 == true ? 1 : 0);
        }

        public /* synthetic */ Factory(int i6, int i7, h hVar) {
            this((i7 & 1) != 0 ? 4 : i6);
        }

        /* JADX WARN: Multi-variable type inference failed */
        @InterfaceC3141c
        public /* synthetic */ Factory(int i6) {
            this(i6, null, 2, 0 == true ? 1 : 0);
        }
    }

    public /* synthetic */ BitmapFactoryDecoder(ImageSource imageSource, Options options, e eVar, ExifOrientationPolicy exifOrientationPolicy, int i6, h hVar) {
        this(imageSource, options, (i6 & 4) != 0 ? j.a(Integer.MAX_VALUE) : eVar, (i6 & 8) != 0 ? ExifOrientationPolicy.RESPECT_PERFORMANCE : exifOrientationPolicy);
    }

    private final DecodeResult decode(BitmapFactory.Options options) throws Exception {
        ExceptionCatchingSource exceptionCatchingSource = new ExceptionCatchingSource(this.source.source());
        J jC = AbstractC2732b.c(exceptionCatchingSource);
        boolean z6 = true;
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new C2739i(jC.peek(), 1), null, options);
        Exception exception = exceptionCatchingSource.getException();
        if (exception == null) {
            options.inJustDecodeBounds = false;
            ExifUtils exifUtils = ExifUtils.INSTANCE;
            ExifData exifData = exifUtils.getExifData(options.outMimeType, jC, this.exifOrientationPolicy);
            Exception exception2 = exceptionCatchingSource.getException();
            if (exception2 == null) {
                options.inMutable = false;
                if (Build.VERSION.SDK_INT >= 26 && this.options.getColorSpace() != null) {
                    options.inPreferredColorSpace = this.options.getColorSpace();
                }
                options.inPremultiplied = this.options.getPremultipliedAlpha();
                configureConfig(options, exifData);
                configureScale(options, exifData);
                try {
                    Bitmap bitmapDecodeStream = BitmapFactory.decodeStream(new C2739i(jC, 1), null, options);
                    jC.close();
                    Exception exception3 = exceptionCatchingSource.getException();
                    if (exception3 != null) {
                        throw exception3;
                    }
                    if (bitmapDecodeStream != null) {
                        bitmapDecodeStream.setDensity(this.options.getContext().getResources().getDisplayMetrics().densityDpi);
                        BitmapDrawable bitmapDrawable = new BitmapDrawable(this.options.getContext().getResources(), exifUtils.reverseTransformations(bitmapDecodeStream, exifData));
                        if (options.inSampleSize <= 1 && !options.inScaled) {
                            z6 = false;
                        }
                        return new DecodeResult(bitmapDrawable, z6);
                    }
                    throw new IllegalStateException("BitmapFactory returned a null bitmap. Often this means BitmapFactory could not decode the image data read from the input source (e.g. network, disk, or memory) as it's not encoded as a valid image format.");
                } finally {
                }
            } else {
                throw exception2;
            }
        } else {
            throw exception;
        }
    }

    @InterfaceC3141c
    public /* synthetic */ BitmapFactoryDecoder(ImageSource imageSource, Options options) {
        this(imageSource, options, null, null, 12, null);
    }

    public /* synthetic */ BitmapFactoryDecoder(ImageSource imageSource, Options options, e eVar, int i6, h hVar) {
        this(imageSource, options, (i6 & 4) != 0 ? j.a(Integer.MAX_VALUE) : eVar);
    }

    @InterfaceC3141c
    public /* synthetic */ BitmapFactoryDecoder(ImageSource imageSource, Options options, e eVar) {
        this(imageSource, options, eVar, null, 8, null);
    }
}
