Skip to content

Commit

Permalink
Retain the hasAlphaBit when rotating images in TransformationUtils.
Browse files Browse the repository at this point in the history
This fixes a bug where camera images written with non-zero exif
orientations are cached in Glide's disk cache as slower PNGs instead of
JPEGs. Camera images without exif orientaiton or with 0 values for the
exif orientation are unaffected.

Images with exif orientations are always rotated by Downsampler using
this method when they're decoded. The modified method writes the rotated
image into a Bitmap acquired from the BitmapPool. Bitmaps in the pool
default to having hasAlpha true because it's safer to retain
transparency than not. The modified method previously did not update the
newly obtained bitmap's hasAlpha flag with the value from the unrotated
original. As a result, every image with exif orientation ended up in a
bitmap with the hasAlpha flag set to true. In turn, BitmapEncoder would
write all of these images to disk as PNGs because it assumes images with
hasAlpha set to true might have transparency. Writing and reading PNGs
is much slower than writing/reading JPEGs in general, so this causes a
performance issue for Camera images, which, as a rule, do not have
transparency (and have hasAlpha set to false).

To avoid the performance hit, this change sets the hasAlpha flag on
rotated Bitmaps based on the value from the original unmodified Bitmap.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218380242
  • Loading branch information
sjudd committed Dec 19, 2018
1 parent be51b4e commit e515f47
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap i
matrix.postTranslate(-newRect.left, -newRect.top);

applyMatrix(inBitmap, result, matrix);

result.setHasAlpha(inBitmap.hasAlpha());

return result;
}

Expand Down

0 comments on commit e515f47

Please sign in to comment.