jimu-decompiled/sources/com/bumptech/glide/load/resource/bitmap/Downsampler.java
2025-05-13 19:24:51 +02:00

472 lines
21 KiB
Java

package com.bumptech.glide.load.resource.bitmap;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.ImageHeaderParser;
import com.bumptech.glide.load.ImageHeaderParserUtils;
import com.bumptech.glide.load.Option;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy;
import com.bumptech.glide.util.LogTime;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/* loaded from: classes.dex */
public final class Downsampler {
public static final Option<DecodeFormat> f = Option.a("com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeFormat", DecodeFormat.DEFAULT);
public static final Option<Boolean> g;
public static final Option<Boolean> h;
private static final Set<String> i;
private static final DecodeCallbacks j;
private static final Set<ImageHeaderParser.ImageType> k;
private static final Queue<BitmapFactory.Options> l;
private final BitmapPool a;
private final DisplayMetrics b;
private final ArrayPool c;
private final List<ImageHeaderParser> d;
private final HardwareConfigState e = HardwareConfigState.a();
public interface DecodeCallbacks {
void a();
void a(BitmapPool bitmapPool, Bitmap bitmap) throws IOException;
}
static {
Option<DownsampleStrategy> option = DownsampleStrategy.f;
g = Option.a("com.bumptech.glide.load.resource.bitmap.Downsampler.FixBitmapSize", false);
h = Option.a("com.bumtpech.glide.load.resource.bitmap.Downsampler.AllowHardwareDecode");
i = Collections.unmodifiableSet(new HashSet(Arrays.asList("image/vnd.wap.wbmp", "image/x-ico")));
j = new DecodeCallbacks() { // from class: com.bumptech.glide.load.resource.bitmap.Downsampler.1
@Override // com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeCallbacks
public void a() {
}
@Override // com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeCallbacks
public void a(BitmapPool bitmapPool, Bitmap bitmap) {
}
};
k = Collections.unmodifiableSet(EnumSet.of(ImageHeaderParser.ImageType.JPEG, ImageHeaderParser.ImageType.PNG_A, ImageHeaderParser.ImageType.PNG));
l = Util.a(0);
}
public Downsampler(List<ImageHeaderParser> list, DisplayMetrics displayMetrics, BitmapPool bitmapPool, ArrayPool arrayPool) {
this.d = list;
Preconditions.a(displayMetrics);
this.b = displayMetrics;
Preconditions.a(bitmapPool);
this.a = bitmapPool;
Preconditions.a(arrayPool);
this.c = arrayPool;
}
private static int b(double d) {
if (d > 1.0d) {
d = 1.0d / d;
}
return (int) Math.round(d * 2.147483647E9d);
}
private static int c(double d) {
return (int) (d + 0.5d);
}
private static void c(BitmapFactory.Options options) {
d(options);
synchronized (l) {
l.offer(options);
}
}
private static void d(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.outWidth = 0;
options.outHeight = 0;
options.outMimeType = null;
options.inBitmap = null;
options.inMutable = true;
}
public Resource<Bitmap> a(InputStream inputStream, int i2, int i3, Options options) throws IOException {
return a(inputStream, i2, i3, options, j);
}
public boolean a(InputStream inputStream) {
return true;
}
public boolean a(ByteBuffer byteBuffer) {
return true;
}
private static int[] b(InputStream inputStream, BitmapFactory.Options options, DecodeCallbacks decodeCallbacks, BitmapPool bitmapPool) throws IOException {
options.inJustDecodeBounds = true;
a(inputStream, options, decodeCallbacks, bitmapPool);
options.inJustDecodeBounds = false;
return new int[]{options.outWidth, options.outHeight};
}
public Resource<Bitmap> a(InputStream inputStream, int i2, int i3, Options options, DecodeCallbacks decodeCallbacks) throws IOException {
Preconditions.a(inputStream.markSupported(), "You must provide an InputStream that supports mark()");
byte[] bArr = (byte[]) this.c.b(65536, byte[].class);
BitmapFactory.Options a = a();
a.inTempStorage = bArr;
DecodeFormat decodeFormat = (DecodeFormat) options.a(f);
try {
return BitmapResource.a(a(inputStream, a, (DownsampleStrategy) options.a(DownsampleStrategy.f), decodeFormat, decodeFormat == DecodeFormat.PREFER_ARGB_8888_DISALLOW_HARDWARE ? false : options.a(h) != null && ((Boolean) options.a(h)).booleanValue(), i2, i3, ((Boolean) options.a(g)).booleanValue(), decodeCallbacks), this.a);
} finally {
c(a);
this.c.put(bArr);
}
}
private static boolean b(BitmapFactory.Options options) {
int i2;
int i3 = options.inTargetDensity;
return i3 > 0 && (i2 = options.inDensity) > 0 && i3 != i2;
}
private Bitmap a(InputStream inputStream, BitmapFactory.Options options, DownsampleStrategy downsampleStrategy, DecodeFormat decodeFormat, boolean z, int i2, int i3, boolean z2, DecodeCallbacks decodeCallbacks) throws IOException {
Downsampler downsampler;
int round;
int round2;
int i4;
long a = LogTime.a();
int[] b = b(inputStream, options, decodeCallbacks, this.a);
int i5 = b[0];
int i6 = b[1];
String str = options.outMimeType;
boolean z3 = (i5 == -1 || i6 == -1) ? false : z;
int a2 = ImageHeaderParserUtils.a(this.d, inputStream, this.c);
int a3 = TransformationUtils.a(a2);
boolean b2 = TransformationUtils.b(a2);
int i7 = i2 == Integer.MIN_VALUE ? i5 : i2;
int i8 = i3 == Integer.MIN_VALUE ? i6 : i3;
ImageHeaderParser.ImageType b3 = ImageHeaderParserUtils.b(this.d, inputStream, this.c);
a(b3, inputStream, decodeCallbacks, this.a, downsampleStrategy, a3, i5, i6, i7, i8, options);
a(inputStream, decodeFormat, z3, b2, options, i7, i8);
boolean z4 = Build.VERSION.SDK_INT >= 19;
if (options.inSampleSize == 1 || z4) {
downsampler = this;
if (downsampler.a(b3)) {
if (i5 < 0 || i6 < 0 || !z2 || !z4) {
float f2 = b(options) ? options.inTargetDensity / options.inDensity : 1.0f;
int i9 = options.inSampleSize;
float f3 = i9;
int ceil = (int) Math.ceil(i5 / f3);
int ceil2 = (int) Math.ceil(i6 / f3);
round = Math.round(ceil * f2);
round2 = Math.round(ceil2 * f2);
if (Log.isLoggable("Downsampler", 2)) {
Log.v("Downsampler", "Calculated target [" + round + "x" + round2 + "] for source [" + i5 + "x" + i6 + "], sampleSize: " + i9 + ", targetDensity: " + options.inTargetDensity + ", density: " + options.inDensity + ", density multiplier: " + f2);
}
} else {
round = i7;
round2 = i8;
}
if (round > 0 && round2 > 0) {
a(options, downsampler.a, round, round2);
}
}
} else {
downsampler = this;
}
Bitmap a4 = a(inputStream, options, decodeCallbacks, downsampler.a);
decodeCallbacks.a(downsampler.a, a4);
if (Log.isLoggable("Downsampler", 2)) {
i4 = a2;
a(i5, i6, str, options, a4, i2, i3, a);
} else {
i4 = a2;
}
Bitmap bitmap = null;
if (a4 != null) {
a4.setDensity(downsampler.b.densityDpi);
bitmap = TransformationUtils.a(downsampler.a, a4, i4);
if (!a4.equals(bitmap)) {
downsampler.a.a(a4);
}
}
return bitmap;
}
private static void a(ImageHeaderParser.ImageType imageType, InputStream inputStream, DecodeCallbacks decodeCallbacks, BitmapPool bitmapPool, DownsampleStrategy downsampleStrategy, int i2, int i3, int i4, int i5, int i6, BitmapFactory.Options options) throws IOException {
float b;
int min;
int i7;
int floor;
double floor2;
int i8;
if (i3 <= 0 || i4 <= 0) {
if (Log.isLoggable("Downsampler", 3)) {
Log.d("Downsampler", "Unable to determine dimensions for: " + imageType + " with target [" + i5 + "x" + i6 + "]");
return;
}
return;
}
if (i2 != 90 && i2 != 270) {
b = downsampleStrategy.b(i3, i4, i5, i6);
} else {
b = downsampleStrategy.b(i4, i3, i5, i6);
}
if (b > 0.0f) {
DownsampleStrategy.SampleSizeRounding a = downsampleStrategy.a(i3, i4, i5, i6);
if (a != null) {
float f2 = i3;
float f3 = i4;
int c = i3 / c(b * f2);
int c2 = i4 / c(b * f3);
if (a == DownsampleStrategy.SampleSizeRounding.MEMORY) {
min = Math.max(c, c2);
} else {
min = Math.min(c, c2);
}
if (Build.VERSION.SDK_INT > 23 || !i.contains(options.outMimeType)) {
int max = Math.max(1, Integer.highestOneBit(min));
i7 = (a != DownsampleStrategy.SampleSizeRounding.MEMORY || ((float) max) >= 1.0f / b) ? max : max << 1;
} else {
i7 = 1;
}
options.inSampleSize = i7;
if (imageType == ImageHeaderParser.ImageType.JPEG) {
float min2 = Math.min(i7, 8);
floor = (int) Math.ceil(f2 / min2);
i8 = (int) Math.ceil(f3 / min2);
int i9 = i7 / 8;
if (i9 > 0) {
floor /= i9;
i8 /= i9;
}
} else {
if (imageType != ImageHeaderParser.ImageType.PNG && imageType != ImageHeaderParser.ImageType.PNG_A) {
if (imageType != ImageHeaderParser.ImageType.WEBP && imageType != ImageHeaderParser.ImageType.WEBP_A) {
if (i3 % i7 == 0 && i4 % i7 == 0) {
floor = i3 / i7;
i8 = i4 / i7;
} else {
int[] b2 = b(inputStream, options, decodeCallbacks, bitmapPool);
int i10 = b2[0];
i8 = b2[1];
floor = i10;
}
} else if (Build.VERSION.SDK_INT >= 24) {
float f4 = i7;
floor = Math.round(f2 / f4);
i8 = Math.round(f3 / f4);
} else {
float f5 = i7;
floor = (int) Math.floor(f2 / f5);
floor2 = Math.floor(f3 / f5);
}
} else {
float f6 = i7;
floor = (int) Math.floor(f2 / f6);
floor2 = Math.floor(f3 / f6);
}
i8 = (int) floor2;
}
double b3 = downsampleStrategy.b(floor, i8, i5, i6);
if (Build.VERSION.SDK_INT >= 19) {
options.inTargetDensity = a(b3);
options.inDensity = b(b3);
}
if (b(options)) {
options.inScaled = true;
} else {
options.inTargetDensity = 0;
options.inDensity = 0;
}
if (Log.isLoggable("Downsampler", 2)) {
Log.v("Downsampler", "Calculate scaling, source: [" + i3 + "x" + i4 + "], target: [" + i5 + "x" + i6 + "], power of two scaled: [" + floor + "x" + i8 + "], exact scale factor: " + b + ", power of 2 sample size: " + i7 + ", adjusted scale factor: " + b3 + ", target density: " + options.inTargetDensity + ", density: " + options.inDensity);
return;
}
return;
}
throw new IllegalArgumentException("Cannot round with null rounding");
}
throw new IllegalArgumentException("Cannot scale with factor: " + b + " from: " + downsampleStrategy + ", source: [" + i3 + "x" + i4 + "], target: [" + i5 + "x" + i6 + "]");
}
private static int a(double d) {
return c((d / (r1 / r0)) * c(b(d) * d));
}
private boolean a(ImageHeaderParser.ImageType imageType) {
if (Build.VERSION.SDK_INT >= 19) {
return true;
}
return k.contains(imageType);
}
private void a(InputStream inputStream, DecodeFormat decodeFormat, boolean z, boolean z2, BitmapFactory.Options options, int i2, int i3) {
if (this.e.a(i2, i3, options, decodeFormat, z, z2)) {
return;
}
if (decodeFormat != DecodeFormat.PREFER_ARGB_8888 && decodeFormat != DecodeFormat.PREFER_ARGB_8888_DISALLOW_HARDWARE && Build.VERSION.SDK_INT != 16) {
boolean z3 = false;
try {
z3 = ImageHeaderParserUtils.b(this.d, inputStream, this.c).hasAlpha();
} catch (IOException e) {
if (Log.isLoggable("Downsampler", 3)) {
Log.d("Downsampler", "Cannot determine whether the image has alpha or not from header, format " + decodeFormat, e);
}
}
options.inPreferredConfig = z3 ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
if (options.inPreferredConfig == Bitmap.Config.RGB_565) {
options.inDither = true;
return;
}
return;
}
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
/* JADX WARN: Code restructure failed: missing block: B:25:?, code lost:
throw r1;
*/
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
private static android.graphics.Bitmap a(java.io.InputStream r6, android.graphics.BitmapFactory.Options r7, com.bumptech.glide.load.resource.bitmap.Downsampler.DecodeCallbacks r8, com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool r9) throws java.io.IOException {
/*
java.lang.String r0 = "Downsampler"
boolean r1 = r7.inJustDecodeBounds
if (r1 == 0) goto Lc
r1 = 10485760(0xa00000, float:1.469368E-38)
r6.mark(r1)
goto Lf
Lc:
r8.a()
Lf:
int r1 = r7.outWidth
int r2 = r7.outHeight
java.lang.String r3 = r7.outMimeType
java.util.concurrent.locks.Lock r4 = com.bumptech.glide.load.resource.bitmap.TransformationUtils.a()
r4.lock()
r4 = 0
android.graphics.Bitmap r8 = android.graphics.BitmapFactory.decodeStream(r6, r4, r7) // Catch: java.lang.Throwable -> L30 java.lang.IllegalArgumentException -> L32
java.util.concurrent.locks.Lock r9 = com.bumptech.glide.load.resource.bitmap.TransformationUtils.a()
r9.unlock()
boolean r7 = r7.inJustDecodeBounds
if (r7 == 0) goto L2f
r6.reset()
L2f:
return r8
L30:
r6 = move-exception
goto L5f
L32:
r5 = move-exception
java.io.IOException r1 = a(r5, r1, r2, r3, r7) // Catch: java.lang.Throwable -> L30
r2 = 3
boolean r2 = android.util.Log.isLoggable(r0, r2) // Catch: java.lang.Throwable -> L30
if (r2 == 0) goto L43
java.lang.String r2 = "Failed to decode with inBitmap, trying again without Bitmap re-use"
android.util.Log.d(r0, r2, r1) // Catch: java.lang.Throwable -> L30
L43:
android.graphics.Bitmap r0 = r7.inBitmap // Catch: java.lang.Throwable -> L30
if (r0 == 0) goto L5e
r6.reset() // Catch: java.lang.Throwable -> L30 java.io.IOException -> L5d
android.graphics.Bitmap r0 = r7.inBitmap // Catch: java.lang.Throwable -> L30 java.io.IOException -> L5d
r9.a(r0) // Catch: java.lang.Throwable -> L30 java.io.IOException -> L5d
r7.inBitmap = r4 // Catch: java.lang.Throwable -> L30 java.io.IOException -> L5d
android.graphics.Bitmap r6 = a(r6, r7, r8, r9) // Catch: java.lang.Throwable -> L30 java.io.IOException -> L5d
java.util.concurrent.locks.Lock r7 = com.bumptech.glide.load.resource.bitmap.TransformationUtils.a()
r7.unlock()
return r6
L5d:
throw r1 // Catch: java.lang.Throwable -> L30
L5e:
throw r1 // Catch: java.lang.Throwable -> L30
L5f:
java.util.concurrent.locks.Lock r7 = com.bumptech.glide.load.resource.bitmap.TransformationUtils.a()
r7.unlock()
throw r6
*/
throw new UnsupportedOperationException("Method not decompiled: com.bumptech.glide.load.resource.bitmap.Downsampler.a(java.io.InputStream, android.graphics.BitmapFactory$Options, com.bumptech.glide.load.resource.bitmap.Downsampler$DecodeCallbacks, com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool):android.graphics.Bitmap");
}
private static void a(int i2, int i3, String str, BitmapFactory.Options options, Bitmap bitmap, int i4, int i5, long j2) {
Log.v("Downsampler", "Decoded " + a(bitmap) + " from [" + i2 + "x" + i3 + "] " + str + " with inBitmap " + a(options) + " for [" + i4 + "x" + i5 + "], sample size: " + options.inSampleSize + ", density: " + options.inDensity + ", target density: " + options.inTargetDensity + ", thread: " + Thread.currentThread().getName() + ", duration: " + LogTime.a(j2));
}
private static String a(BitmapFactory.Options options) {
return a(options.inBitmap);
}
@TargetApi(19)
private static String a(Bitmap bitmap) {
String str;
if (bitmap == null) {
return null;
}
if (Build.VERSION.SDK_INT >= 19) {
str = " (" + bitmap.getAllocationByteCount() + ")";
} else {
str = "";
}
return "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig() + str;
}
private static IOException a(IllegalArgumentException illegalArgumentException, int i2, int i3, String str, BitmapFactory.Options options) {
return new IOException("Exception decoding bitmap, outWidth: " + i2 + ", outHeight: " + i3 + ", outMimeType: " + str + ", inBitmap: " + a(options), illegalArgumentException);
}
@TargetApi(26)
private static void a(BitmapFactory.Options options, BitmapPool bitmapPool, int i2, int i3) {
Bitmap.Config config;
if (Build.VERSION.SDK_INT < 26) {
config = null;
} else if (options.inPreferredConfig == Bitmap.Config.HARDWARE) {
return;
} else {
config = options.outConfig;
}
if (config == null) {
config = options.inPreferredConfig;
}
options.inBitmap = bitmapPool.b(i2, i3, config);
}
private static synchronized BitmapFactory.Options a() {
BitmapFactory.Options poll;
synchronized (Downsampler.class) {
synchronized (l) {
poll = l.poll();
}
if (poll == null) {
poll = new BitmapFactory.Options();
d(poll);
}
}
return poll;
}
}