package defpackage;

import android.graphics.Bitmap;
import android.util.Log;
import defpackage.ru3;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Iterator;

/* JADX INFO: loaded from: classes.dex */
public class e19 implements ru3 {
    public int[] a;
    public final ru3.a c;
    public ByteBuffer d;
    public byte[] e;
    public short[] f;
    public byte[] g;
    public byte[] h;
    public byte[] i;
    public final int[] j;
    public int k;
    public av3 l;
    public Bitmap m;
    public final boolean n;
    public int o;
    public final int p;
    public final int q;
    public final int r;
    public Boolean s;
    public final int[] b = new int[256];
    public Bitmap.Config t = Bitmap.Config.ARGB_8888;

    public e19(ru3.a aVar, av3 av3Var, ByteBuffer byteBuffer, int i) {
        this.c = aVar;
        this.l = new av3();
        synchronized (this) {
            try {
                if (i <= 0) {
                    throw new IllegalArgumentException("Sample size must be >=0, not: " + i);
                }
                int iHighestOneBit = Integer.highestOneBit(i);
                this.o = 0;
                this.l = av3Var;
                this.k = -1;
                ByteBuffer byteBufferAsReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
                this.d = byteBufferAsReadOnlyBuffer;
                byteBufferAsReadOnlyBuffer.position(0);
                this.d.order(ByteOrder.LITTLE_ENDIAN);
                this.n = false;
                Iterator it = av3Var.e.iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    } else if (((xu3) it.next()).g == 3) {
                        this.n = true;
                        break;
                    }
                }
                this.p = iHighestOneBit;
                int i2 = av3Var.f;
                this.r = i2 / iHighestOneBit;
                int i3 = av3Var.g;
                this.q = i3 / iHighestOneBit;
                this.i = this.c.b(i2 * i3);
                this.j = this.c.d(this.r * this.q);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // defpackage.ru3
    public final synchronized Bitmap a() {
        try {
            if (this.l.c <= 0 || this.k < 0) {
                if (Log.isLoggable("e19", 3)) {
                    int i = this.l.c;
                }
                this.o = 1;
            }
            int i2 = this.o;
            if (i2 != 1 && i2 != 2) {
                this.o = 0;
                if (this.e == null) {
                    this.e = this.c.b(255);
                }
                xu3 xu3Var = (xu3) this.l.e.get(this.k);
                int i3 = this.k - 1;
                xu3 xu3Var2 = i3 >= 0 ? (xu3) this.l.e.get(i3) : null;
                int[] iArr = xu3Var.k;
                if (iArr == null) {
                    iArr = this.l.a;
                }
                this.a = iArr;
                if (iArr == null) {
                    this.o = 1;
                    return null;
                }
                if (xu3Var.f) {
                    System.arraycopy(iArr, 0, this.b, 0, iArr.length);
                    int[] iArr2 = this.b;
                    this.a = iArr2;
                    iArr2[xu3Var.h] = 0;
                    if (xu3Var.g == 2 && this.k == 0) {
                        this.s = Boolean.TRUE;
                    }
                }
                return e(xu3Var, xu3Var2);
            }
            return null;
        } finally {
        }
    }

    public final void b() {
        this.k = (this.k + 1) % this.l.c;
    }

    public final Bitmap c() {
        Boolean bool = this.s;
        Bitmap bitmapC = this.c.c(this.r, this.q, (bool == null || bool.booleanValue()) ? Bitmap.Config.ARGB_8888 : this.t);
        bitmapC.setHasAlpha(true);
        return bitmapC;
    }

    public final void d(Bitmap.Config config) {
        Bitmap.Config config2;
        Bitmap.Config config3 = Bitmap.Config.ARGB_8888;
        if (config == config3 || config == (config2 = Bitmap.Config.RGB_565)) {
            this.t = config;
            return;
        }
        throw new IllegalArgumentException("Unsupported format: " + config + ", must be one of " + config3 + " or " + config2);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:26:0x0043  */
    /* JADX WARN: Removed duplicated region for block: B:94:0x01cb A[PHI: r5
      0x01cb: PHI (r5v43 int) = (r5v37 int), (r5v45 int), (r5v45 int) binds: [B:89:0x01b7, B:91:0x01c2, B:92:0x01c4] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Type inference failed for: r6v22 */
    /* JADX WARN: Type inference failed for: r6v23 */
    /* JADX WARN: Type inference failed for: r6v24 */
    /* JADX WARN: Type inference failed for: r6v31, types: [short] */
    /* JADX WARN: Type inference failed for: r6v33 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bitmap e(xu3 xu3Var, xu3 xu3Var2) {
        int[] iArr;
        byte b;
        int i;
        int i2;
        int i3;
        int i4;
        int[] iArr2;
        int i5;
        short[] sArr;
        int i6;
        short s;
        int i7;
        Bitmap bitmap;
        ru3.a aVar = this.c;
        byte b2 = 0;
        int[] iArr3 = this.j;
        if (xu3Var2 == null) {
            Bitmap bitmap2 = this.m;
            if (bitmap2 != null) {
                aVar.a(bitmap2);
            }
            this.m = null;
            Arrays.fill(iArr3, 0);
        }
        if (xu3Var2 != null && xu3Var2.g == 3 && this.m == null) {
            Arrays.fill(iArr3, 0);
        }
        if (xu3Var2 != null && (i7 = xu3Var2.g) > 0) {
            if (i7 == 2) {
                if (!xu3Var.f) {
                    av3 av3Var = this.l;
                    int i8 = av3Var.k;
                    if (xu3Var.k != null && av3Var.j == xu3Var.h) {
                        i8 = 0;
                    }
                    int i9 = xu3Var2.d;
                    int i10 = this.p;
                    int i11 = i9 / i10;
                    int i12 = xu3Var2.b / i10;
                    int i13 = xu3Var2.c / i10;
                    int i14 = xu3Var2.a / i10;
                    int i15 = this.r;
                    int i16 = (i12 * i15) + i14;
                    int i17 = (i11 * i15) + i16;
                    while (i16 < i17) {
                        int i18 = i16 + i13;
                        for (int i19 = i16; i19 < i18; i19++) {
                            iArr3[i19] = i8;
                        }
                        i16 += this.r;
                    }
                }
            } else if (i7 == 3 && (bitmap = this.m) != null) {
                int i20 = this.q;
                int i21 = this.r;
                bitmap.getPixels(iArr3, 0, i21, 0, 0, i21, i20);
            }
        }
        this.d.position(xu3Var.j);
        int i22 = xu3Var.c * xu3Var.d;
        byte[] bArr = this.i;
        if (bArr == null || bArr.length < i22) {
            this.i = aVar.b(i22);
        }
        byte[] bArr2 = this.i;
        if (this.f == null) {
            this.f = new short[4096];
        }
        short[] sArr2 = this.f;
        if (this.g == null) {
            this.g = new byte[4096];
        }
        byte[] bArr3 = this.g;
        if (this.h == null) {
            this.h = new byte[4097];
        }
        byte[] bArr4 = this.h;
        int i23 = this.d.get() & 255;
        int i24 = 1;
        int i25 = 1 << i23;
        int i26 = i25 + 1;
        int i27 = i25 + 2;
        int i28 = i23 + 1;
        int i29 = (1 << i28) - 1;
        int i30 = 0;
        while (i30 < i25) {
            sArr2[i30] = 0;
            bArr3[i30] = (byte) i30;
            i30++;
            i24 = i24;
        }
        int i31 = i24;
        byte[] bArr5 = this.e;
        int i32 = 0;
        int i33 = 0;
        int i34 = 0;
        int i35 = 0;
        int i36 = 0;
        int i37 = 0;
        int i38 = 0;
        int i39 = 0;
        int i40 = i28;
        int i41 = i27;
        int i42 = i29;
        int i43 = -1;
        while (true) {
            if (i32 >= i22) {
                iArr = iArr3;
                b = b2;
                break;
            }
            if (i33 == 0) {
                i6 = -1;
                int i44 = this.d.get() & 255;
                if (i44 <= 0) {
                    sArr = sArr2;
                    iArr = iArr3;
                } else {
                    ByteBuffer byteBuffer = this.d;
                    sArr = sArr2;
                    iArr = iArr3;
                    byteBuffer.get(this.e, 0, Math.min(i44, byteBuffer.remaining()));
                }
                if (i44 <= 0) {
                    this.o = 3;
                    b = 0;
                    break;
                }
                i33 = i44;
                i34 = 0;
            } else {
                sArr = sArr2;
                iArr = iArr3;
                i6 = -1;
            }
            i36 += (bArr5[i34] & 255) << i35;
            i34++;
            i33--;
            int i45 = i35 + 8;
            int i46 = i41;
            int i47 = i40;
            int i48 = i43;
            int i49 = i38;
            while (true) {
                i35 = i45;
                if (i45 < i47) {
                    i43 = i48;
                    i38 = i49;
                    i40 = i47;
                    iArr3 = iArr;
                    b2 = 0;
                    i41 = i46;
                    sArr2 = sArr;
                    break;
                }
                int i50 = i36 & i42;
                i36 >>= i47;
                i35 -= i47;
                if (i50 == i25) {
                    i47 = i28;
                    i46 = i27;
                    i42 = i29;
                    i45 = i35;
                    i48 = i6;
                } else {
                    if (i50 == i26) {
                        i41 = i46;
                        i40 = i47;
                        i43 = i48;
                        i38 = i49;
                        sArr2 = sArr;
                        iArr3 = iArr;
                        b2 = 0;
                        break;
                    }
                    int i51 = i47;
                    if (i48 == i6) {
                        bArr2[i37] = bArr3[i50];
                        i37++;
                        i32++;
                        i48 = i50;
                        i49 = i48;
                        i45 = i35;
                        i47 = i51;
                    } else {
                        if (i50 >= i46) {
                            bArr4[i39] = (byte) i49;
                            i39++;
                            s = i48;
                        } else {
                            s = i50;
                        }
                        while (s >= i25) {
                            bArr4[i39] = bArr3[s];
                            i39++;
                            s = sArr[s];
                        }
                        i49 = bArr3[s] & 255;
                        byte b3 = (byte) i49;
                        bArr2[i37] = b3;
                        while (true) {
                            i37++;
                            i32++;
                            if (i39 <= 0) {
                                break;
                            }
                            i39--;
                            bArr2[i37] = bArr4[i39];
                        }
                        if (i46 < 4096) {
                            sArr[i46] = (short) i48;
                            bArr3[i46] = b3;
                            i46++;
                            if ((i46 & i42) != 0 || i46 >= 4096) {
                                i47 = i51;
                            } else {
                                i47 = i51 + 1;
                                i42 += i46;
                            }
                            i45 = i35;
                            i48 = i50;
                        }
                    }
                    i6 = -1;
                }
            }
        }
        Arrays.fill(bArr2, i37, i22, b);
        if (xu3Var.e || this.p != i31) {
            int i52 = xu3Var.d;
            int i53 = this.p;
            int i54 = i52 / i53;
            int i55 = xu3Var.b / i53;
            int i56 = xu3Var.c / i53;
            int i57 = xu3Var.a / i53;
            boolean z = this.k == 0;
            byte[] bArr6 = this.i;
            int[] iArr4 = this.a;
            Boolean bool = this.s;
            int i58 = 8;
            int i59 = 0;
            int i60 = 1;
            int i61 = 0;
            while (i61 < i54) {
                if (xu3Var.e) {
                    if (i59 >= i54) {
                        i60++;
                        if (i60 == 2) {
                            i59 = 4;
                        } else if (i60 == 3) {
                            i58 = 4;
                            i59 = 2;
                        } else if (i60 == 4) {
                            i59 = 1;
                            i58 = 2;
                        }
                    }
                    i = i59 + i58;
                } else {
                    i = i59;
                    i59 = i61;
                }
                int i62 = i59 + i55;
                int i63 = i54;
                boolean z2 = i53 == 1;
                if (i62 < this.q) {
                    int i64 = this.r;
                    int i65 = i62 * i64;
                    int i66 = i65 + i57;
                    int i67 = i66 + i56;
                    int i68 = i65 + i64;
                    if (i68 < i67) {
                        i67 = i68;
                    }
                    i2 = i53;
                    int i69 = i61 * i53 * xu3Var.c;
                    int[] iArr5 = this.j;
                    if (z2) {
                        int i70 = i66;
                        while (i70 < i67) {
                            int i71 = i70;
                            int i72 = iArr4[bArr6[i69] & 255];
                            if (i72 != 0) {
                                iArr5[i71] = i72;
                            } else if (z && bool == null) {
                                bool = Boolean.TRUE;
                            }
                            i69 += i2;
                            i70 = i71 + 1;
                        }
                    } else {
                        int i73 = ((i67 - i66) * i2) + i69;
                        i3 = i55;
                        int i74 = i66;
                        while (i74 < i67) {
                            int i75 = i67;
                            int i76 = xu3Var.c;
                            int i77 = i74;
                            int i78 = i69;
                            int i79 = 0;
                            int i80 = 0;
                            int i81 = 0;
                            int i82 = 0;
                            int i83 = 0;
                            while (true) {
                                if (i78 >= this.p + i69) {
                                    i4 = i56;
                                    break;
                                }
                                byte[] bArr7 = this.i;
                                i4 = i56;
                                if (i78 >= bArr7.length || i78 >= i73) {
                                    break;
                                }
                                int i84 = this.a[bArr7[i78] & 255];
                                if (i84 != 0) {
                                    i79 += (i84 >> 24) & 255;
                                    i80 += (i84 >> 16) & 255;
                                    i81 += (i84 >> 8) & 255;
                                    i82 += i84 & 255;
                                    i83++;
                                }
                                i78++;
                                i56 = i4;
                            }
                            int i85 = i69 + i76;
                            int i86 = i85;
                            while (i86 < this.p + i85) {
                                byte[] bArr8 = this.i;
                                int i87 = i85;
                                if (i86 >= bArr8.length || i86 >= i73) {
                                    break;
                                }
                                int i88 = this.a[bArr8[i86] & 255];
                                if (i88 != 0) {
                                    i79 += (i88 >> 24) & 255;
                                    i80 += (i88 >> 16) & 255;
                                    i81 += (i88 >> 8) & 255;
                                    i82 += i88 & 255;
                                    i83++;
                                }
                                i86++;
                                i85 = i87;
                            }
                            int i89 = i83 == 0 ? 0 : ((i79 / i83) << 24) | ((i80 / i83) << 16) | ((i81 / i83) << 8) | (i82 / i83);
                            if (i89 != 0) {
                                iArr5[i77] = i89;
                            } else if (z && bool == null) {
                                bool = Boolean.TRUE;
                            }
                            i69 += i2;
                            i74 = i77 + 1;
                            i67 = i75;
                            i56 = i4;
                        }
                        i61++;
                        i59 = i;
                        i54 = i63;
                        i55 = i3;
                        i53 = i2;
                        i56 = i56;
                    }
                } else {
                    i2 = i53;
                }
                i3 = i55;
                i61++;
                i59 = i;
                i54 = i63;
                i55 = i3;
                i53 = i2;
                i56 = i56;
            }
            if (this.s == null) {
                this.s = Boolean.valueOf(bool == null ? false : bool.booleanValue());
            }
        } else {
            int i90 = xu3Var.d;
            int i91 = xu3Var.b;
            int i92 = xu3Var.c;
            int i93 = xu3Var.a;
            byte b4 = this.k == 0 ? (byte) 1 : b;
            byte[] bArr9 = this.i;
            int[] iArr6 = this.a;
            byte b5 = -1;
            for (int i94 = b; i94 < i90; i94++) {
                int i95 = this.r;
                int i96 = (i94 + i91) * i95;
                int i97 = i96 + i93;
                int i98 = i97 + i92;
                int i99 = i96 + i95;
                if (i99 < i98) {
                    i98 = i99;
                }
                int i100 = xu3Var.c * i94;
                while (i97 < i98) {
                    byte b6 = bArr9[i100];
                    int i101 = b6 & 255;
                    if (i101 != b5) {
                        int i102 = iArr6[i101];
                        if (i102 != 0) {
                            this.j[i97] = i102;
                        } else {
                            b5 = b6;
                        }
                    }
                    i100++;
                    i97++;
                }
            }
            Boolean bool2 = this.s;
            this.s = Boolean.valueOf((bool2 != null && bool2.booleanValue()) || !(this.s != null || b4 == 0 || b5 == -1));
        }
        if (this.n && ((i5 = xu3Var.g) == 0 || i5 == 1)) {
            if (this.m == null) {
                this.m = c();
            }
            Bitmap bitmap3 = this.m;
            int i103 = this.q;
            int i104 = this.r;
            iArr2 = iArr;
            bitmap3.setPixels(iArr2, 0, i104, 0, 0, i104, i103);
        } else {
            iArr2 = iArr;
        }
        Bitmap bitmapC = c();
        int i105 = this.q;
        int i106 = this.r;
        bitmapC.setPixels(iArr2, 0, i106, 0, 0, i106, i105);
        return bitmapC;
    }
}
