Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.exifinterface.media.ExifInterface;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Synthetic;
Expand All @@ -35,7 +34,6 @@
public final class TransformationUtils {
private static final String TAG = "TransformationUtils";
public static final int PAINT_FLAGS = Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG;
private static final Paint DEFAULT_PAINT = new Paint(PAINT_FLAGS);
private static final int CIRCLE_CROP_PAINT_FLAGS = PAINT_FLAGS | Paint.ANTI_ALIAS_FLAG;
private static final Paint CIRCLE_CROP_SHAPE_PAINT = new Paint(CIRCLE_CROP_PAINT_FLAGS);
private static final Paint CIRCLE_CROP_BITMAP_PAINT;
Expand Down Expand Up @@ -132,12 +130,16 @@ public static Bitmap centerCrop(
m.setScale(scale, scale);
m.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));

Bitmap result = pool.get(width, height, getNonNullConfig(inBitmap));
Bitmap result =
Bitmap.createBitmap(inBitmap.getWidth(), inBitmap.getHeight(), getNonNullConfig(inBitmap));
Bitmap transformedBitmap =
Bitmap.createBitmap(
result, 0, 0, inBitmap.getWidth(), inBitmap.getHeight(), m, true /*filter*/);

// We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
TransformationUtils.setAlpha(inBitmap, result);
TransformationUtils.setAlpha(inBitmap, transformedBitmap);

applyMatrix(inBitmap, result, m);
return result;
return transformedBitmap;
}

/**
Expand Down Expand Up @@ -182,23 +184,31 @@ public static Bitmap fitCenter(
targetHeight = (int) (minPercentage * inBitmap.getHeight());

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

Matrix matrix = new Matrix();
matrix.setScale(minPercentage, minPercentage);

Bitmap result = Bitmap.createBitmap(inBitmap.getWidth(), inBitmap.getHeight(), config);
Bitmap transformedBitmap =
Bitmap.createBitmap(
result, 0, 0, inBitmap.getWidth(), inBitmap.getHeight(), matrix, true /*filter*/);

// We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
TransformationUtils.setAlpha(inBitmap, toReuse);
TransformationUtils.setAlpha(inBitmap, transformedBitmap);

if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "request: " + width + "x" + height);
Log.v(TAG, "toFit: " + inBitmap.getWidth() + "x" + inBitmap.getHeight());
Log.v(TAG, "toReuse: " + toReuse.getWidth() + "x" + toReuse.getHeight());
Log.v(
TAG,
"transformedBitmap: "
+ transformedBitmap.getWidth()
+ "x"
+ transformedBitmap.getHeight());
Log.v(TAG, "minPct: " + minPercentage);
}

Matrix matrix = new Matrix();
matrix.setScale(minPercentage, minPercentage);
applyMatrix(inBitmap, toReuse, matrix);

return toReuse;
return transformedBitmap;
}

/**
Expand Down Expand Up @@ -514,14 +524,14 @@ public void drawRoundedCorners(Canvas canvas, Paint paint, RectF rect) {
path.addRoundRect(
rect,
new float[] {
topLeft,
topLeft,
topRight,
topRight,
bottomRight,
bottomRight,
bottomLeft,
bottomLeft
topLeft,
topLeft,
topRight,
topRight,
bottomRight,
bottomRight,
bottomLeft,
bottomLeft
},
Path.Direction.CW);
canvas.drawPath(path, paint);
Expand Down Expand Up @@ -572,18 +582,6 @@ private static Bitmap.Config getNonNullConfig(@NonNull Bitmap bitmap) {
return bitmap.getConfig() != null ? bitmap.getConfig() : Bitmap.Config.ARGB_8888;
}

private static void applyMatrix(
@NonNull Bitmap inBitmap, @NonNull Bitmap targetBitmap, Matrix matrix) {
BITMAP_DRAWABLE_LOCK.lock();
try {
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT);
clear(canvas);
} finally {
BITMAP_DRAWABLE_LOCK.unlock();
}
}

@VisibleForTesting
static void initializeMatrixForRotation(int exifOrientation, Matrix matrix) {
switch (exifOrientation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -170,44 +168,7 @@ public void testFitCenterHandlesBitmapsWithNullConfigs() {
assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig());
}

@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
reset(bitmapPool);
when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse);

toReuse.setHasAlpha(false);
toTransform.setHasAlpha(true);

Bitmap result =
TransformationUtils.centerCrop(
bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight());

assertEquals(toReuse, result);
assertTrue(result.hasAlpha());
}

@Test
public void
testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
reset(bitmapPool);
when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse);

toReuse.setHasAlpha(true);
toTransform.setHasAlpha(false);

Bitmap result =
TransformationUtils.centerCrop(
bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight());

assertEquals(toReuse, result);
assertFalse(result.hasAlpha());
}

@Test
public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
Expand Down Expand Up @@ -246,47 +207,6 @@ public void testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlpha(
assertFalse(result.hasAlpha());
}

@Test
public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
reset(bitmapPool);
when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig())))
.thenReturn(toReuse);

toReuse.setHasAlpha(false);
toTransform.setHasAlpha(true);

Bitmap result =
TransformationUtils.fitCenter(
bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight());

assertEquals(toReuse, result);
assertTrue(result.hasAlpha());
}

@Test
public void
testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
reset(bitmapPool);
when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig())))
.thenReturn(toReuse);

toReuse.setHasAlpha(true);
toTransform.setHasAlpha(false);

Bitmap result =
TransformationUtils.fitCenter(
bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight());

assertEquals(toReuse, result);
assertFalse(result.hasAlpha());
}

@Test
public void testFitCenterSetsOutBitmapToHaveAlphaIfInBitmapHasAlpha() {
Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Expand Down
Loading