Skip to content

Commit

Permalink
Use accurate rounding in FitCenter when checking to see if we can skip
Browse files Browse the repository at this point in the history
floor is necessary to make sure that we populate all pixels in the
Bitmap, but it's not necessary to use to check if our input Bitmap has
already been transformed and can be returned as is. Using floor can
cause us to re-draw a Bitmap unnecessarily.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167789795
  • Loading branch information
sjudd committed Sep 11, 2017
1 parent a41cc9b commit f5ba374
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitma
final float heightPercentage = height / (float) inBitmap.getHeight();
final float minPercentage = Math.min(widthPercentage, heightPercentage);

// take the floor of the target width/height, not round. If the matrix
// passed into drawBitmap rounds differently, we want to slightly
// overdraw, not underdraw, to avoid artifacts from bitmap reuse.
final int targetWidth = (int) (minPercentage * inBitmap.getWidth());
final int targetHeight = (int) (minPercentage * inBitmap.getHeight());
// Round here in case we've decoded exactly the image we want, but take the floor below to
// avoid a line of garbage or blank pixels in images.
int targetWidth = Math.round(minPercentage * inBitmap.getWidth());
int targetHeight = Math.round(minPercentage * inBitmap.getHeight());

if (inBitmap.getWidth() == targetWidth && inBitmap.getHeight() == targetHeight) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Expand All @@ -140,6 +139,12 @@ public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitma
return inBitmap;
}

// Take the floor of the target width/height, not round. If the matrix
// passed into drawBitmap rounds differently, we want to slightly
// overdraw, not underdraw, to avoid artifacts from bitmap reuse.
targetWidth = (int) (minPercentage * inBitmap.getWidth());
targetHeight = (int) (minPercentage * inBitmap.getHeight());

Bitmap.Config config = getSafeConfig(inBitmap);
Bitmap toReuse = pool.get(targetWidth, targetHeight, config);

Expand Down

0 comments on commit f5ba374

Please sign in to comment.