From a198ef627d3537d460d2eb5d64d15de1803f0f6c Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Tue, 16 Jan 2018 20:57:32 -0800 Subject: [PATCH] Handle RGBA_F16 Bitmaps in Util.getBitmapByteSize. Fixes #2818. --- .../com/bumptech/glide/WideGamutTest.java | 51 +++++++++++++++---- .../java/com/bumptech/glide/util/Util.java | 3 ++ .../com/bumptech/glide/util/UtilTest.java | 11 +++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java b/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java index 5ae25c4ac3..60bef4c88e 100644 --- a/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java +++ b/instrumentation/src/androidTest/java/com/bumptech/glide/WideGamutTest.java @@ -6,12 +6,14 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; +import android.graphics.Bitmap.Config; import android.graphics.ColorSpace; import android.graphics.ColorSpace.Named; import android.os.Build; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import com.bumptech.glide.load.DecodeFormat; +import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool; import com.bumptech.glide.test.ConcurrencyHelper; import com.bumptech.glide.test.GlideApp; @@ -86,9 +88,7 @@ public void load_withEncodedPngWideGamutImage_decodesWideGamut() { Bitmap.createBitmap( 100, 100, Bitmap.Config.RGBA_F16, /*hasAlpha=*/ true, ColorSpace.get(Named.DCI_P3)); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - assertThat(toCompress.compress(CompressFormat.PNG, 100, os)).isTrue(); - byte[] data = os.toByteArray(); + byte[] data = asPng(toCompress); Bitmap bitmap = concurrency.get( @@ -108,9 +108,7 @@ public void load_withEncodedJpegWideGamutImage_decodesArgb8888() { Bitmap.createBitmap( 100, 100, Bitmap.Config.RGBA_F16, /*hasAlpha=*/ true, ColorSpace.get(Named.DCI_P3)); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - assertThat(toCompress.compress(CompressFormat.JPEG, 100, os)).isTrue(); - byte[] data = os.toByteArray(); + byte[] data = asJpeg(toCompress); Bitmap bitmap = concurrency.get( @@ -127,9 +125,7 @@ public void load_withEncodedWebpWideGamutImage_decodesArgb8888() { Bitmap.createBitmap( 100, 100, Bitmap.Config.RGBA_F16, /*hasAlpha=*/ true, ColorSpace.get(Named.DCI_P3)); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - assertThat(toCompress.compress(CompressFormat.WEBP, 100, os)).isTrue(); - byte[] data = os.toByteArray(); + byte[] data = asWebp(toCompress); Bitmap bitmap = concurrency.get( @@ -139,4 +135,41 @@ public void load_withEncodedWebpWideGamutImage_decodesArgb8888() { .submit()); assertThat(bitmap.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888); } + + @Test + public void load_withSmallerWideGamutInPool_decodesBitmap() { + BitmapPool bitmapPool = Glide.get(context).getBitmapPool(); + Bitmap toPut = Bitmap.createBitmap(300, 298, Config.RGBA_F16); + bitmapPool.put(toPut); + // Add a second Bitmap to account for the InputStream decode. + bitmapPool.put(Bitmap.createBitmap(toPut)); + + Bitmap wideGamut = Bitmap.createBitmap(300, 300, Config.RGBA_F16); + byte[] data = asPng(wideGamut); + Bitmap bitmap = + concurrency.get( + Glide.with(context) + .asBitmap() + .load(data) + .submit()); + assertThat(bitmap).isNotNull(); + } + + private static byte[] asJpeg(Bitmap bitmap) { + return toByteArray(bitmap, CompressFormat.JPEG); + } + + private static byte[] asPng(Bitmap bitmap) { + return toByteArray(bitmap, CompressFormat.PNG); + } + + private static byte[] asWebp(Bitmap bitmap) { + return toByteArray(bitmap, CompressFormat.WEBP); + } + + private static byte[] toByteArray(Bitmap bitmap, CompressFormat format) { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + assertThat(bitmap.compress(format, 100, os)).isTrue(); + return os.toByteArray(); + } } diff --git a/library/src/main/java/com/bumptech/glide/util/Util.java b/library/src/main/java/com/bumptech/glide/util/Util.java index b5cae5cbad..de4b3784b2 100644 --- a/library/src/main/java/com/bumptech/glide/util/Util.java +++ b/library/src/main/java/com/bumptech/glide/util/Util.java @@ -111,6 +111,9 @@ private static int getBytesPerPixel(@Nullable Bitmap.Config config) { case ARGB_4444: bytesPerPixel = 2; break; + case RGBA_F16: + bytesPerPixel = 8; + break; case ARGB_8888: default: bytesPerPixel = 4; diff --git a/library/test/src/test/java/com/bumptech/glide/util/UtilTest.java b/library/test/src/test/java/com/bumptech/glide/util/UtilTest.java index 92d16e95b7..416095784c 100644 --- a/library/test/src/test/java/com/bumptech/glide/util/UtilTest.java +++ b/library/test/src/test/java/com/bumptech/glide/util/UtilTest.java @@ -1,5 +1,6 @@ package com.bumptech.glide.util; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import android.graphics.Bitmap; @@ -9,7 +10,7 @@ import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) -@Config(manifest = Config.NONE, sdk = 18) +@Config(manifest = Config.NONE, sdk = 27) public class UtilTest { @Test @@ -70,4 +71,12 @@ public void testReturnsLargestSizeForNullConfig() { int size = Util.getBitmapByteSize(width, height, null); assertEquals(width * height * 4, size); } + + @Test + public void getBitmapByteSize_withRGBA_F16_returnsCorrectSize() { + int width = 100; + int height = 200; + assertThat(Util.getBitmapByteSize(width, height, Bitmap.Config.RGBA_F16)) + .isEqualTo(width * height * 8); + } }