Skip to content

Commit

Permalink
Handle RGBA_F16 Bitmaps in Util.getBitmapByteSize.
Browse files Browse the repository at this point in the history
Fixes #2818.
  • Loading branch information
sjudd committed Jan 17, 2018
1 parent d459561 commit a198ef6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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();
}
}
3 changes: 3 additions & 0 deletions library/src/main/java/com/bumptech/glide/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion library/test/src/test/java/com/bumptech/glide/util/UtilTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a198ef6

Please sign in to comment.