Skip to content

Commit

Permalink
Avoid converting RGBA_F16 to ARGB_8888 in TransformationUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Jan 17, 2018
1 parent a198ef6 commit 73277df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

<!-- Allow common trailing comments used to describe suppressions -->
<module name="TrailingComment">
<property name="legalComment" value="^Public API.?$|^NOPMD .*$" />
<property name="legalComment" value="^Public API.?$|^NOPMD.*$" />
</module>

<!-- Checks for imports. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.test.ConcurrencyHelper;
import com.bumptech.glide.test.GlideApp;
import com.bumptech.glide.test.ResourceIds;
Expand Down Expand Up @@ -155,6 +156,38 @@ public void load_withSmallerWideGamutInPool_decodesBitmap() {
assertThat(bitmap).isNotNull();
}

@Test
public void circleCrop_withWideGamutBitmap_producesWideGamutBitmap() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.RGBA_F16);
byte[] data = asPng(bitmap);

Bitmap result =
concurrency.get(
GlideApp.with(context)
.asBitmap()
.load(data)
.circleCrop()
.submit());
assertThat(result).isNotNull();
assertThat(result.getConfig()).isEqualTo(Config.RGBA_F16);
}

@Test
public void roundedCorners_withWideGamutBitmap_producesWideGamutBitmap() {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.RGBA_F16);
byte[] data = asPng(bitmap);

Bitmap result =
concurrency.get(
GlideApp.with(context)
.asBitmap()
.load(data)
.transform(new RoundedCorners(/*roundingRadius=*/ 10))
.submit());
assertThat(result).isNotNull();
assertThat(result.getConfig()).isEqualTo(Config.RGBA_F16);
}

private static byte[] asJpeg(Bitmap bitmap) {
return toByteArray(bitmap, CompressFormat.JPEG);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -137,7 +138,7 @@ public static Bitmap centerCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitm
m.setScale(scale, scale);
m.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));

Bitmap result = pool.get(width, height, getSafeConfig(inBitmap));
Bitmap result = pool.get(width, height, getNonNullConfig(inBitmap));
// We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
TransformationUtils.setAlpha(inBitmap, result);

Expand Down Expand Up @@ -186,7 +187,7 @@ public static Bitmap fitCenter(@NonNull BitmapPool pool, @NonNull Bitmap inBitma
targetWidth = (int) (minPercentage * inBitmap.getWidth());
targetHeight = (int) (minPercentage * inBitmap.getHeight());

Bitmap.Config config = getSafeConfig(inBitmap);
Bitmap.Config config = getNonNullConfig(inBitmap);
Bitmap toReuse = pool.get(targetWidth, targetHeight, config);

// We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
Expand Down Expand Up @@ -324,7 +325,7 @@ public static Bitmap rotateImageExif(@NonNull BitmapPool pool, @NonNull Bitmap i
final int newWidth = Math.round(newRect.width());
final int newHeight = Math.round(newRect.height());

Bitmap.Config config = getSafeConfig(inBitmap);
Bitmap.Config config = getNonNullConfig(inBitmap);
Bitmap result = pool.get(newWidth, newHeight, config);

matrix.postTranslate(-newRect.left, -newRect.top);
Expand Down Expand Up @@ -384,7 +385,8 @@ public static Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitm
// Alpha is required for this transformation.
Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);

Bitmap result = pool.get(destMinEdge, destMinEdge, Bitmap.Config.ARGB_8888);
Bitmap.Config outConfig = getAlphaSafeConfig(inBitmap);
Bitmap result = pool.get(destMinEdge, destMinEdge, outConfig);
result.setHasAlpha(true);

BITMAP_DRAWABLE_LOCK.lock();
Expand All @@ -406,21 +408,34 @@ public static Bitmap circleCrop(@NonNull BitmapPool pool, @NonNull Bitmap inBitm
return result;
}

private static Bitmap getAlphaSafeBitmap(@NonNull BitmapPool pool,
@NonNull Bitmap maybeAlphaSafe) {
if (Bitmap.Config.ARGB_8888.equals(maybeAlphaSafe.getConfig())) {
private static Bitmap getAlphaSafeBitmap(
@NonNull BitmapPool pool, @NonNull Bitmap maybeAlphaSafe) {
Bitmap.Config safeConfig = getAlphaSafeConfig(maybeAlphaSafe);
if (safeConfig.equals(maybeAlphaSafe.getConfig())) {
return maybeAlphaSafe;
}

Bitmap argbBitmap = pool.get(maybeAlphaSafe.getWidth(), maybeAlphaSafe.getHeight(),
Bitmap.Config.ARGB_8888);
Bitmap argbBitmap =
pool.get(maybeAlphaSafe.getWidth(), maybeAlphaSafe.getHeight(), safeConfig);
new Canvas(argbBitmap).drawBitmap(maybeAlphaSafe, 0 /*left*/, 0 /*top*/, null /*paint*/);

// We now own this Bitmap. It's our responsibility to replace it in the pool outside this method
// when we're finished with it.
return argbBitmap;
}

@NonNull
private static Config getAlphaSafeConfig(@NonNull Bitmap inBitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Avoid short circuiting the sdk check.
if (Bitmap.Config.RGBA_F16.equals(inBitmap.getConfig())) { // NOPMD
return Bitmap.Config.RGBA_F16;
}
}

return Bitmap.Config.ARGB_8888;
}

/**
* Creates a bitmap from a source bitmap and rounds the corners.
*
Expand Down Expand Up @@ -462,9 +477,9 @@ public static Bitmap roundedCorners(
Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");

// Alpha is required for this transformation.
Bitmap.Config safeConfig = getAlphaSafeConfig(inBitmap);
Bitmap toTransform = getAlphaSafeBitmap(pool, inBitmap);
Bitmap result =
pool.get(toTransform.getWidth(), toTransform.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap result = pool.get(toTransform.getWidth(), toTransform.getHeight(), safeConfig);

result.setHasAlpha(true);

Expand Down Expand Up @@ -496,7 +511,8 @@ private static void clear(Canvas canvas) {
canvas.setBitmap(null);
}

private static Bitmap.Config getSafeConfig(Bitmap bitmap) {
@NonNull
private static Bitmap.Config getNonNullConfig(@NonNull Bitmap bitmap) {
return bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888;
}

Expand Down

0 comments on commit 73277df

Please sign in to comment.