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

Fix NPE transforming Bitmaps with null configs

-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=82003436
上级 6528aee7
......@@ -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;
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册