package d6; import android.graphics.Bitmap; import android.util.Log; import d6.a; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import java.util.Iterator; /* loaded from: classes2.dex */ public class d implements a { /* renamed from: u, reason: collision with root package name */ private static final String f9135u = "StandardGifDecoder"; /* renamed from: a, reason: collision with root package name */ private int[] f9136a; /* renamed from: b, reason: collision with root package name */ private final int[] f9137b; /* renamed from: c, reason: collision with root package name */ private final a.InterfaceC0135a f9138c; /* renamed from: d, reason: collision with root package name */ private ByteBuffer f9139d; /* renamed from: e, reason: collision with root package name */ private byte[] f9140e; /* renamed from: f, reason: collision with root package name */ private short[] f9141f; /* renamed from: g, reason: collision with root package name */ private byte[] f9142g; /* renamed from: h, reason: collision with root package name */ private byte[] f9143h; /* renamed from: i, reason: collision with root package name */ private byte[] f9144i; /* renamed from: j, reason: collision with root package name */ private int[] f9145j; /* renamed from: k, reason: collision with root package name */ private int f9146k; /* renamed from: l, reason: collision with root package name */ private b f9147l; /* renamed from: m, reason: collision with root package name */ private Bitmap f9148m; /* renamed from: n, reason: collision with root package name */ private boolean f9149n; /* renamed from: o, reason: collision with root package name */ private int f9150o; /* renamed from: p, reason: collision with root package name */ private int f9151p; /* renamed from: q, reason: collision with root package name */ private int f9152q; /* renamed from: r, reason: collision with root package name */ private int f9153r; /* renamed from: s, reason: collision with root package name */ private Boolean f9154s; /* renamed from: t, reason: collision with root package name */ private Bitmap.Config f9155t; public d(a.InterfaceC0135a interfaceC0135a) { this.f9137b = new int[256]; this.f9155t = Bitmap.Config.ARGB_8888; this.f9138c = interfaceC0135a; this.f9147l = new b(); } private int i(int i10, int i11, int i12) { int i13 = 0; int i14 = 0; int i15 = 0; int i16 = 0; int i17 = 0; for (int i18 = i10; i18 < this.f9151p + i10; i18++) { byte[] bArr = this.f9144i; if (i18 >= bArr.length || i18 >= i11) { break; } int i19 = this.f9136a[bArr[i18] & 255]; if (i19 != 0) { i16 += (i19 >> 24) & 255; i15 += (i19 >> 16) & 255; i14 += (i19 >> 8) & 255; i13 += i19 & 255; i17++; } } int i20 = i10 + i12; for (int i21 = i20; i21 < this.f9151p + i20; i21++) { byte[] bArr2 = this.f9144i; if (i21 >= bArr2.length || i21 >= i11) { break; } int i22 = this.f9136a[bArr2[i21] & 255]; if (i22 != 0) { i16 += (i22 >> 24) & 255; i15 += (i22 >> 16) & 255; i14 += (i22 >> 8) & 255; i13 += i22 & 255; i17++; } } if (i17 == 0) { return 0; } return ((i16 / i17) << 24) | ((i15 / i17) << 16) | ((i14 / i17) << 8) | (i13 / i17); } private Bitmap j(e eVar, e eVar2) { int i10; int i11; Bitmap bitmap; int[] iArr = this.f9145j; int i12 = 0; if (eVar2 == null) { Bitmap bitmap2 = this.f9148m; if (bitmap2 != null) { this.f9138c.c(bitmap2); } this.f9148m = null; Arrays.fill(iArr, 0); } if (eVar2 != null && eVar2.f9162g == 3 && this.f9148m == null) { Arrays.fill(iArr, 0); } if (eVar2 != null && (i11 = eVar2.f9162g) > 0) { if (i11 == 2) { if (!eVar.f9161f) { b bVar = this.f9147l; int i13 = bVar.f9129l; if (eVar.f9166k == null || bVar.f9127j != eVar.f9163h) { i12 = i13; } } int i14 = eVar2.f9159d; int i15 = this.f9151p; int i16 = i14 / i15; int i17 = eVar2.f9157b / i15; int i18 = eVar2.f9158c / i15; int i19 = eVar2.f9156a / i15; int i20 = this.f9153r; int i21 = (i17 * i20) + i19; int i22 = (i16 * i20) + i21; while (i21 < i22) { int i23 = i21 + i18; for (int i24 = i21; i24 < i23; i24++) { iArr[i24] = i12; } i21 += this.f9153r; } } else if (i11 == 3 && (bitmap = this.f9148m) != null) { int i25 = this.f9153r; bitmap.getPixels(iArr, 0, i25, 0, 0, i25, this.f9152q); } } o(eVar); if (eVar.f9160e || this.f9151p != 1) { k(eVar); } else { m(eVar); } if (this.f9149n && ((i10 = eVar.f9162g) == 0 || i10 == 1)) { if (this.f9148m == null) { this.f9148m = l(); } Bitmap bitmap3 = this.f9148m; int i26 = this.f9153r; bitmap3.setPixels(iArr, 0, i26, 0, 0, i26, this.f9152q); } Bitmap l10 = l(); int i27 = this.f9153r; l10.setPixels(iArr, 0, i27, 0, 0, i27, this.f9152q); return l10; } private void k(e eVar) { Boolean bool; int i10; int i11; int i12; int i13; int i14; int[] iArr = this.f9145j; int i15 = eVar.f9159d; int i16 = this.f9151p; int i17 = i15 / i16; int i18 = eVar.f9157b / i16; int i19 = eVar.f9158c / i16; int i20 = eVar.f9156a / i16; boolean z10 = this.f9146k == 0; int i21 = this.f9153r; int i22 = this.f9152q; byte[] bArr = this.f9144i; int[] iArr2 = this.f9136a; Boolean bool2 = this.f9154s; int i23 = 8; int i24 = 0; int i25 = 0; int i26 = 1; while (true) { bool = bool2; if (i25 >= i17) { break; } if (eVar.f9160e) { if (i24 >= i17) { int i27 = i26 + 1; i10 = i17; if (i27 == 2) { i26 = i27; i24 = 4; } else if (i27 != 3) { i26 = i27; if (i27 == 4) { i24 = 1; i23 = 2; } } else { i26 = i27; i24 = 2; i23 = 4; } } else { i10 = i17; } i11 = i24 + i23; } else { i10 = i17; i11 = i24; i24 = i25; } int i28 = i24 + i18; boolean z11 = i16 == 1; if (i28 < i22) { int i29 = i28 * i21; int i30 = i29 + i20; int i31 = i30 + i19; int i32 = i29 + i21; if (i32 < i31) { i31 = i32; } i12 = i11; int i33 = i25 * i16 * eVar.f9158c; if (z11) { int i34 = i30; while (true) { i13 = i18; if (i34 >= i31) { break; } int i35 = iArr2[bArr[i33] & 255]; if (i35 != 0) { iArr[i34] = i35; } else if (z10 && bool == null) { bool = Boolean.TRUE; } i33 += i16; i34++; i18 = i13; } } else { i13 = i18; int i36 = ((i31 - i30) * i16) + i33; int i37 = i30; while (true) { i14 = i19; if (i37 < i31) { int i38 = i(i33, i36, eVar.f9158c); if (i38 != 0) { iArr[i37] = i38; } else if (z10 && bool == null) { bool = Boolean.TRUE; } i33 += i16; i37++; i19 = i14; } } bool2 = bool; i25++; i18 = i13; i17 = i10; i19 = i14; i24 = i12; } } else { i12 = i11; i13 = i18; } i14 = i19; bool2 = bool; i25++; i18 = i13; i17 = i10; i19 = i14; i24 = i12; } if (this.f9154s == null) { this.f9154s = Boolean.valueOf(bool == null ? false : bool.booleanValue()); } } private Bitmap l() { Boolean bool = this.f9154s; Bitmap a10 = this.f9138c.a(this.f9153r, this.f9152q, (bool == null || bool.booleanValue()) ? Bitmap.Config.ARGB_8888 : this.f9155t); a10.setHasAlpha(true); return a10; } private void m(e eVar) { e eVar2 = eVar; int[] iArr = this.f9145j; int i10 = eVar2.f9159d; int i11 = eVar2.f9157b; int i12 = eVar2.f9158c; int i13 = eVar2.f9156a; boolean z10 = this.f9146k == 0; int i14 = this.f9153r; byte[] bArr = this.f9144i; int[] iArr2 = this.f9136a; int i15 = 0; byte b10 = -1; while (i15 < i10) { int i16 = (i15 + i11) * i14; int i17 = i16 + i13; int i18 = i17 + i12; int i19 = i16 + i14; if (i19 < i18) { i18 = i19; } int i20 = eVar2.f9158c * i15; int i21 = i17; while (i21 < i18) { byte b11 = bArr[i20]; int i22 = i10; int i23 = b11 & 255; if (i23 != b10) { int i24 = iArr2[i23]; if (i24 != 0) { iArr[i21] = i24; } else { b10 = b11; } } i20++; i21++; i10 = i22; } i15++; eVar2 = eVar; } Boolean bool = this.f9154s; this.f9154s = Boolean.valueOf((bool != null && bool.booleanValue()) || (this.f9154s == null && z10 && b10 != -1)); } private int n() { int p10 = p(); if (p10 <= 0) { return p10; } ByteBuffer byteBuffer = this.f9139d; byteBuffer.get(this.f9140e, 0, Math.min(p10, byteBuffer.remaining())); return p10; } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r0v10 */ /* JADX WARN: Type inference failed for: r0v15, types: [short] */ /* JADX WARN: Type inference failed for: r0v18 */ /* JADX WARN: Type inference failed for: r0v22 */ /* JADX WARN: Type inference failed for: r0v9 */ private void o(e eVar) { int i10; int i11; d dVar = this; if (eVar != null) { dVar.f9139d.position(eVar.f9165j); } if (eVar == null) { b bVar = dVar.f9147l; i10 = bVar.f9123f; i11 = bVar.f9124g; } else { i10 = eVar.f9158c; i11 = eVar.f9159d; } int i12 = i10 * i11; byte[] bArr = dVar.f9144i; if (bArr == null || bArr.length < i12) { dVar.f9144i = dVar.f9138c.e(i12); } byte[] bArr2 = dVar.f9144i; if (dVar.f9141f == null) { dVar.f9141f = new short[4096]; } short[] sArr = dVar.f9141f; if (dVar.f9142g == null) { dVar.f9142g = new byte[4096]; } byte[] bArr3 = dVar.f9142g; if (dVar.f9143h == null) { dVar.f9143h = new byte[4097]; } byte[] bArr4 = dVar.f9143h; int p10 = p(); int i13 = 1 << p10; int i14 = i13 + 1; int i15 = i13 + 2; int i16 = p10 + 1; int i17 = (1 << i16) - 1; int i18 = 0; for (int i19 = 0; i19 < i13; i19++) { sArr[i19] = 0; bArr3[i19] = (byte) i19; } byte[] bArr5 = dVar.f9140e; int i20 = i16; int i21 = i15; int i22 = i17; int i23 = 0; int i24 = 0; int i25 = 0; int i26 = 0; int i27 = 0; int i28 = -1; int i29 = 0; int i30 = 0; while (true) { if (i18 >= i12) { break; } if (i23 == 0) { i23 = n(); if (i23 <= 0) { dVar.f9150o = 3; break; } i26 = 0; } i25 += (bArr5[i26] & 255) << i24; i26++; i23--; int i31 = i24 + 8; int i32 = i28; int i33 = i29; int i34 = i21; int i35 = i20; while (true) { if (i31 < i35) { dVar = this; i21 = i34; i28 = i32; i20 = i35; i24 = i31; i29 = i33; break; } int i36 = i25 & i22; i25 >>= i35; i31 -= i35; if (i36 == i13) { i35 = i16; i34 = i15; i22 = i17; i32 = -1; } else { if (i36 == i14) { dVar = this; i24 = i31; i28 = i32; i20 = i35; i29 = i33; i21 = i34; break; } if (i32 == -1) { bArr2[i27] = bArr3[i36 == true ? 1 : 0]; i27++; i18++; i32 = i36 == true ? 1 : 0; i33 = i32; i31 = i31; } else { int i37 = i34; short s10 = i36; if (i36 >= i37) { bArr4[i30] = (byte) i33; i30++; s10 = i32; } while (s10 >= i13) { bArr4[i30] = bArr3[s10]; i30++; s10 = sArr[s10]; } int i38 = bArr3[s10] & 255; int i39 = i16; byte b10 = (byte) i38; bArr2[i27] = b10; while (true) { i27++; i18++; if (i30 <= 0) { break; } i30--; bArr2[i27] = bArr4[i30]; } i33 = i38; if (i37 < 4096) { sArr[i37] = (short) i32; bArr3[i37] = b10; i37++; if ((i37 & i22) == 0 && i37 < 4096) { i35++; i22 += i37; } } i34 = i37; i31 = i31; i32 = i36 == true ? 1 : 0; i16 = i39; } } } } Arrays.fill(bArr2, i27, i12, (byte) 0); } private int p() { return this.f9139d.get() & 255; } @Override // d6.a public synchronized Bitmap a() { if (this.f9147l.f9120c <= 0 || this.f9146k < 0) { String str = f9135u; if (Log.isLoggable(str, 3)) { Log.d(str, "Unable to decode frame, frameCount=" + this.f9147l.f9120c + ", framePointer=" + this.f9146k); } this.f9150o = 1; } int i10 = this.f9150o; if (i10 != 1 && i10 != 2) { this.f9150o = 0; if (this.f9140e == null) { this.f9140e = this.f9138c.e(255); } e eVar = (e) this.f9147l.f9122e.get(this.f9146k); int i11 = this.f9146k - 1; e eVar2 = i11 >= 0 ? (e) this.f9147l.f9122e.get(i11) : null; int[] iArr = eVar.f9166k; if (iArr == null) { iArr = this.f9147l.f9118a; } this.f9136a = iArr; if (iArr == null) { String str2 = f9135u; if (Log.isLoggable(str2, 3)) { Log.d(str2, "No valid color table found for frame #" + this.f9146k); } this.f9150o = 1; return null; } if (eVar.f9161f) { System.arraycopy(iArr, 0, this.f9137b, 0, iArr.length); int[] iArr2 = this.f9137b; this.f9136a = iArr2; iArr2[eVar.f9163h] = 0; if (eVar.f9162g == 2 && this.f9146k == 0) { this.f9154s = Boolean.TRUE; } } return j(eVar, eVar2); } String str3 = f9135u; if (Log.isLoggable(str3, 3)) { Log.d(str3, "Unable to decode frame, status=" + this.f9150o); } return null; } @Override // d6.a public void b() { this.f9146k = (this.f9146k + 1) % this.f9147l.f9120c; } @Override // d6.a public int c() { return this.f9147l.f9120c; } @Override // d6.a public void clear() { this.f9147l = null; byte[] bArr = this.f9144i; if (bArr != null) { this.f9138c.d(bArr); } int[] iArr = this.f9145j; if (iArr != null) { this.f9138c.f(iArr); } Bitmap bitmap = this.f9148m; if (bitmap != null) { this.f9138c.c(bitmap); } this.f9148m = null; this.f9139d = null; this.f9154s = null; byte[] bArr2 = this.f9140e; if (bArr2 != null) { this.f9138c.d(bArr2); } } @Override // d6.a public int d() { int i10; if (this.f9147l.f9120c <= 0 || (i10 = this.f9146k) < 0) { return 0; } return q(i10); } @Override // d6.a public void e(Bitmap.Config config) { if (config == Bitmap.Config.ARGB_8888 || config == Bitmap.Config.RGB_565) { this.f9155t = config; return; } throw new IllegalArgumentException("Unsupported format: " + config + ", must be one of " + Bitmap.Config.ARGB_8888 + " or " + Bitmap.Config.RGB_565); } @Override // d6.a public void f() { this.f9146k = -1; } @Override // d6.a public int g() { return this.f9146k; } @Override // d6.a public ByteBuffer getData() { return this.f9139d; } @Override // d6.a public int h() { return this.f9139d.limit() + this.f9144i.length + (this.f9145j.length * 4); } public int q(int i10) { if (i10 >= 0) { b bVar = this.f9147l; if (i10 < bVar.f9120c) { return ((e) bVar.f9122e.get(i10)).f9164i; } } return -1; } public synchronized void r(b bVar, ByteBuffer byteBuffer, int i10) { if (i10 <= 0) { throw new IllegalArgumentException("Sample size must be >=0, not: " + i10); } int highestOneBit = Integer.highestOneBit(i10); this.f9150o = 0; this.f9147l = bVar; this.f9146k = -1; ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer(); this.f9139d = asReadOnlyBuffer; asReadOnlyBuffer.position(0); this.f9139d.order(ByteOrder.LITTLE_ENDIAN); this.f9149n = false; Iterator it = bVar.f9122e.iterator(); while (true) { if (!it.hasNext()) { break; } else if (((e) it.next()).f9162g == 3) { this.f9149n = true; break; } } this.f9151p = highestOneBit; int i11 = bVar.f9123f; this.f9153r = i11 / highestOneBit; int i12 = bVar.f9124g; this.f9152q = i12 / highestOneBit; this.f9144i = this.f9138c.e(i11 * i12); this.f9145j = this.f9138c.b(this.f9153r * this.f9152q); } public d(a.InterfaceC0135a interfaceC0135a, b bVar, ByteBuffer byteBuffer, int i10) { this(interfaceC0135a); r(bVar, byteBuffer, i10); } }