提交 c7383b2e 编写于 作者: J judds 提交者: Sam Judd

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
上级 762fd379
......@@ -446,7 +446,7 @@ public final class Downsampler {
// densities here so we calculate the final Bitmap size correctly.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
options.inTargetDensity = adjustTargetDensityForError(adjustedScaleFactor);
options.inDensity = DENSITY_PRECISION_MULTIPLIER;
options.inDensity = getDensityMultiplier(adjustedScaleFactor);
}
if (isScaling(options)) {
options.inScaled = true;
......@@ -473,12 +473,19 @@ public final class Downsampler {
* the final scale factor is as close to our target as possible.
*/
private static int adjustTargetDensityForError(double adjustedScaleFactor) {
int targetDensity = round(DENSITY_PRECISION_MULTIPLIER * adjustedScaleFactor);
float scaleFactorWithError = targetDensity / (float) DENSITY_PRECISION_MULTIPLIER;
int densityMultiplier = getDensityMultiplier(adjustedScaleFactor);
int targetDensity = round(densityMultiplier * adjustedScaleFactor);
float scaleFactorWithError = targetDensity / (float) densityMultiplier;
double difference = adjustedScaleFactor / scaleFactorWithError;
return round(difference * targetDensity);
}
private static int getDensityMultiplier(double adjustedScaleFactor) {
return (int) Math.round(
Integer.MAX_VALUE
* (adjustedScaleFactor <= 1D ? adjustedScaleFactor : 1 / adjustedScaleFactor));
}
// This is weird, but it matches the logic in a bunch of Android views/framework classes for
// rounding.
private static int round(double value) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册