Skip to content

Commit c7383b2

Browse files
committed
Fix integer overflow bug in Downsampler when asked to upscale images.
This should also be a slight accuracy improvement since we're now using a somewhat more efficient density multiplier in some cases, especially when downsampling. Fixes #2459. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=171601510
1 parent 762fd37 commit c7383b2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

library/src/main/java/com/bumptech/glide/load/resource/bitmap/Downsampler.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static void calculateScaling(
446446
// densities here so we calculate the final Bitmap size correctly.
447447
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
448448
options.inTargetDensity = adjustTargetDensityForError(adjustedScaleFactor);
449-
options.inDensity = DENSITY_PRECISION_MULTIPLIER;
449+
options.inDensity = getDensityMultiplier(adjustedScaleFactor);
450450
}
451451
if (isScaling(options)) {
452452
options.inScaled = true;
@@ -473,12 +473,19 @@ static void calculateScaling(
473473
* the final scale factor is as close to our target as possible.
474474
*/
475475
private static int adjustTargetDensityForError(double adjustedScaleFactor) {
476-
int targetDensity = round(DENSITY_PRECISION_MULTIPLIER * adjustedScaleFactor);
477-
float scaleFactorWithError = targetDensity / (float) DENSITY_PRECISION_MULTIPLIER;
476+
int densityMultiplier = getDensityMultiplier(adjustedScaleFactor);
477+
int targetDensity = round(densityMultiplier * adjustedScaleFactor);
478+
float scaleFactorWithError = targetDensity / (float) densityMultiplier;
478479
double difference = adjustedScaleFactor / scaleFactorWithError;
479480
return round(difference * targetDensity);
480481
}
481482

483+
private static int getDensityMultiplier(double adjustedScaleFactor) {
484+
return (int) Math.round(
485+
Integer.MAX_VALUE
486+
* (adjustedScaleFactor <= 1D ? adjustedScaleFactor : 1 / adjustedScaleFactor));
487+
}
488+
482489
// This is weird, but it matches the logic in a bunch of Android views/framework classes for
483490
// rounding.
484491
private static int round(double value) {

0 commit comments

Comments
 (0)