From 24fc713306c650683581531d18c08e3c20bb8373 Mon Sep 17 00:00:00 2001 From: judds Date: Fri, 12 Dec 2014 12:18:19 -0800 Subject: [PATCH] Fix NPE transforming Bitmaps with null configs ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=82003436 --- .../bitmap/TransformationUtilsTest.java | 39 ++++++++++++++++++- .../resource/bitmap/TransformationUtils.java | 14 ++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java index 123f1fb61..42e761a37 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/resource/bitmap/TransformationUtilsTest.java @@ -152,6 +152,14 @@ public class TransformationUtilsTest { assertTrue(toCrop == transformed); } + @Test + public void testFitCenterHandlesBitmapsWithNullConfigs() { + Bitmap toFit = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); + Robolectric.shadowOf(toFit).setConfig(null); + Bitmap transformed = TransformationUtils.fitCenter(toFit, mock(BitmapPool.class), 50, 50); + assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig()); + } + @Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); @@ -185,7 +193,6 @@ public class TransformationUtilsTest { @Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); - BitmapPool pool = mock(BitmapPool.class); toTransform.setHasAlpha(true); @@ -195,6 +202,16 @@ public class TransformationUtilsTest { assertTrue(result.hasAlpha()); } + @Test + public void testCenterCropHandlesBitmapsWithNullConfigs() { + Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); + Robolectric.shadowOf(toTransform).setConfig(null); + + Bitmap transformed = TransformationUtils.centerCrop(null /*recycled*/, toTransform, 50, 50); + + assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig()); + } + @Test public void testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); @@ -346,6 +363,16 @@ public class TransformationUtilsTest { assertNotNull(TransformationUtils.rotateImageExif(toRotate, bitmapPool, ExifInterface.ORIENTATION_ROTATE_90)); } + @Test + public void testRotateImageExifHandlesBitmapsWithNullConfigs() { + Bitmap toRotate = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); + Robolectric.shadowOf(toRotate).setConfig(null); + BitmapPool bitmapPool = mock(BitmapPool.class); + Bitmap rotated = TransformationUtils.rotateImageExif(toRotate, bitmapPool, + ExifInterface.ORIENTATION_ROTATE_180); + assertEquals(Bitmap.Config.ARGB_8888, rotated.getConfig()); + } + @Test public void testInitializeMatrixSetsScaleIfFlipHorizontal() { Matrix matrix = mock(Matrix.class); @@ -393,6 +420,16 @@ public class TransformationUtilsTest { private boolean hasAlpha; + @Implementation + public static Bitmap createBitmap(int width, int height, Bitmap.Config config) { + // Robolectric doesn't match the framework behavior with null configs, so we have to do + // so here. + if (config == null) { + throw new NullPointerException("config must not be null"); + } + return ShadowBitmap.createBitmap(width, height, config); + } + @Implementation public void setHasAlpha(boolean hasAlpha) { this.hasAlpha = hasAlpha; diff --git a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java index 10a341ebe..49d074988 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/bitmap/TransformationUtils.java @@ -59,8 +59,7 @@ public final class TransformationUtils { if (recycled != null) { result = recycled; } else { - result = Bitmap.createBitmap(width, height, toCrop.getConfig() == null - ? Bitmap.Config.ARGB_8888 : toCrop.getConfig()); + result = Bitmap.createBitmap(width, height, getSafeConfig(toCrop)); } // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given. @@ -107,7 +106,7 @@ public final class TransformationUtils { return toFit; } - Bitmap.Config config = toFit.getConfig() != null ? toFit.getConfig() : Bitmap.Config.ARGB_8888; + Bitmap.Config config = getSafeConfig(toFit); Bitmap toReuse = pool.get(targetWidth, targetHeight, config); if (toReuse == null) { toReuse = Bitmap.createBitmap(targetWidth, targetHeight, config); @@ -267,9 +266,10 @@ public final class TransformationUtils { final int newWidth = Math.round(newRect.width()); final int newHeight = Math.round(newRect.height()); - Bitmap result = pool.get(newWidth, newHeight, toOrient.getConfig()); + Bitmap.Config config = getSafeConfig(toOrient); + Bitmap result = pool.get(newWidth, newHeight, config); if (result == null) { - result = Bitmap.createBitmap(newWidth, newHeight, toOrient.getConfig()); + result = Bitmap.createBitmap(newWidth, newHeight, config); } matrix.postTranslate(-newRect.left, -newRect.top); @@ -281,6 +281,10 @@ public final class TransformationUtils { return result; } + private static Bitmap.Config getSafeConfig(Bitmap bitmap) { + return bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888; + } + // Visible for testing. static void initializeMatrixForRotation(int exifOrientation, Matrix matrix) { switch (exifOrientation) { -- GitLab