### Transformations
# How do I use it?
## Step 1
#### Gradle
```groovy
repositories {
mavenCentral()
}
dependencies {
implementation 'jp.wasabeef:glide-transformations:4.3.0'
// If you want to use the GPU Filters
implementation 'jp.co.cyberagent.android:gpuimage:2.1.0'
}
```
## Step 2
Set Glide Transform.
```kotlin
Glide.with(this).load(R.drawable.demo)
.apply(RequestOptions.bitmapTransform(BlurTransformation(25, 3)))
.into(imageView)
```
## Advanced Step 3
You can set a multiple transformations.
```kotlin
val multi = MultiTransformation
Contributions
-------
Any contributions are welcome!
Contributors
-------
* [start141](https://github.com/start141)
* [squeeish](https://github.com/squeeish)
Thanks
-------
* Inspired by `Picasso Transformations` in [TannerPerrien](https://github.com/TannerPerrien).
License
-------
Copyright (C) 2020 Wasabeef
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: build.gradle
================================================
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
kotlin_version = '1.3.72'
glide_version = '4.11.0'
gpuimage_version = '2.1.0'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0-beta04'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// TODO: Close JCenter on May 1st https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
// classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}
================================================
FILE: example/build.gradle
================================================
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion COMPILE_SDK_VERSION as int
defaultConfig {
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
versionCode VERSION_CODE as int
versionName VERSION_NAME
}
// SigningConfigs
apply from: '../signingConfigs/debug.gradle', to: android
buildTypes {
debug {
debuggable true
signingConfig signingConfigs.debug
}
release {
debuggable false
zipAlignEnabled true
minifyEnabled true
shrinkResources true
}
}
}
repositories {
// maven { url = "https://oss.sonatype.org/content/repositories/snapshots"}
}
dependencies {
// implementation project(':transformations')
implementation 'jp.wasabeef:glide-transformations:4.3.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.github.bumptech.glide:glide:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"
implementation "jp.co.cyberagent.android:gpuimage:$gpuimage_version"
implementation "androidx.appcompat:appcompat:1.3.0-beta01"
implementation "androidx.recyclerview:recyclerview:1.2.0-beta01"
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
================================================
FILE: example/proguard-rules.pro
================================================
================================================
FILE: example/src/main/AndroidManifest.xml
================================================
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.util.Util;
import java.security.MessageDigest;
public abstract class BitmapTransformation implements Transformation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.renderscript.RSRuntimeException;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;
public class BlurTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.BlurTransformation." + VERSION;
private static final int MAX_RADIUS = 25;
private static final int DEFAULT_DOWN_SAMPLING = 1;
private final int radius;
private final int sampling;
public BlurTransformation() {
this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius) {
this(radius, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius, int sampling) {
this.radius = radius;
this.sampling = sampling;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
int scaledWidth = width / sampling;
int scaledHeight = height / sampling;
Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
canvas.scale(1 / (float) sampling, 1 / (float) sampling);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(toTransform, 0, 0, paint);
try {
bitmap = RSBlur.blur(context, bitmap, radius);
} catch (RSRuntimeException e) {
bitmap = FastBlur.blur(bitmap, radius, true);
}
return bitmap;
}
@Override
public String toString() {
return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof BlurTransformation &&
((BlurTransformation) o).radius == radius &&
((BlurTransformation) o).sampling == sampling;
}
@Override
public int hashCode() {
return ID.hashCode() + radius * 1000 + sampling * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/ColorFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class ColorFilterTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.ColorFilterTransformation." + VERSION;
private final int color;
public ColorFilterTransformation(int color) {
this.color = color;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap.Config config =
toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = pool.get(width, height, config);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(toTransform, 0, 0, paint);
return bitmap;
}
@Override
public String toString() {
return "ColorFilterTransformation(color=" + color + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof ColorFilterTransformation &&
((ColorFilterTransformation) o).color == color;
}
@Override
public int hashCode() {
return ID.hashCode() + color * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + color).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/CropCircleTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.request.RequestOptions;
import java.security.MessageDigest;
/**
* @deprecated Use {@link RequestOptions#circleCrop()}.
*/
@Deprecated
public class CropCircleTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.CropCircleTransformation." + VERSION;
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
return TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);
}
@Override
public String toString() {
return "CropCircleTransformation()";
}
@Override
public boolean equals(Object o) {
return o instanceof CropCircleTransformation;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/CropCircleWithBorderTransformation.java
================================================
package jp.wasabeef.glide.transformations;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import java.security.MessageDigest;
import jp.wasabeef.glide.transformations.internal.Utils;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CropCircleWithBorderTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID = "jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation." + VERSION;
private final int borderSize;
private final int borderColor;
public CropCircleWithBorderTransformation() {
this.borderSize = Utils.toDp(4);
this.borderColor = Color.BLACK;
}
public CropCircleWithBorderTransformation(int borderSize, @ColorInt int borderColor) {
this.borderSize = borderSize;
this.borderColor = borderColor;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight);
setCanvasBitmapDensity(toTransform, bitmap);
Paint paint = new Paint();
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderSize);
paint.setAntiAlias(true);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(
outWidth / 2f,
outHeight / 2f,
Math.max(outWidth, outHeight) / 2f - borderSize / 2f,
paint
);
return bitmap;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + borderSize + borderColor).getBytes(CHARSET));
}
@Override
public boolean equals(Object o) {
return o instanceof CropCircleWithBorderTransformation &&
((CropCircleWithBorderTransformation) o).borderSize == borderSize &&
((CropCircleWithBorderTransformation) o).borderColor == borderColor;
}
@Override
public int hashCode() {
return ID.hashCode() + borderSize * 100 + borderColor + 10;
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/CropSquareTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import java.security.MessageDigest;
public class CropSquareTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.CropSquareTransformation." + VERSION;
private int size;
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
this.size = Math.max(outWidth, outHeight);
return TransformationUtils.centerCrop(pool, toTransform, size, size);
}
@Override
public String toString() {
return "CropSquareTransformation(size=" + size + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof CropSquareTransformation && ((CropSquareTransformation) o).size == size;
}
@Override
public int hashCode() {
return ID.hashCode() + size * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + size).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/CropTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class CropTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID = "jp.wasabeef.glide.transformations.CropTransformation." + VERSION;
public enum CropType {
TOP,
CENTER,
BOTTOM
}
private int width;
private int height;
private CropType cropType = CropType.CENTER;
public CropTransformation(int width, int height) {
this(width, height, CropType.CENTER);
}
public CropTransformation(int width, int height, CropType cropType) {
this.width = width;
this.height = height;
this.cropType = cropType;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
width = width == 0 ? toTransform.getWidth() : width;
height = height == 0 ? toTransform.getHeight() : height;
Bitmap.Config config =
toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = pool.get(width, height, config);
bitmap.setHasAlpha(true);
float scaleX = (float) width / toTransform.getWidth();
float scaleY = (float) height / toTransform.getHeight();
float scale = Math.max(scaleX, scaleY);
float scaledWidth = scale * toTransform.getWidth();
float scaledHeight = scale * toTransform.getHeight();
float left = (width - scaledWidth) / 2;
float top = getTop(scaledHeight);
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(toTransform, null, targetRect, null);
return bitmap;
}
private float getTop(float scaledHeight) {
switch (cropType) {
case TOP:
return 0;
case CENTER:
return (height - scaledHeight) / 2;
case BOTTOM:
return height - scaledHeight;
default:
return 0;
}
}
@Override
public String toString() {
return "CropTransformation(width=" + width + ", height=" + height + ", cropType=" + cropType + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof CropTransformation &&
((CropTransformation) o).width == width &&
((CropTransformation) o).height == height &&
((CropTransformation) o).cropType == cropType;
}
@Override
public int hashCode() {
return ID.hashCode() + width * 100000 + height * 1000 + cropType.ordinal() * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + width + height + cropType).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/GrayscaleTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class GrayscaleTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.GrayscaleTransformation." + VERSION;
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap.Config config =
toTransform.getConfig() != null ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = pool.get(width, height, config);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
ColorMatrix saturation = new ColorMatrix();
saturation.setSaturation(0f);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
canvas.drawBitmap(toTransform, 0, 0, paint);
return bitmap;
}
@Override
public String toString() {
return "GrayscaleTransformation()";
}
@Override
public boolean equals(Object o) {
return o instanceof GrayscaleTransformation;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class MaskTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.MaskTransformation." + VERSION;
private static final Paint paint = new Paint();
private final int maskId;
static {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
}
/**
* @param maskId If you change the mask file, please also rename the mask file, or Glide will get
* the cache with the old mask. Because key() return the same values if using the
* same make file name. If you have a good idea please tell us, thanks.
*/
public MaskTransformation(int maskId) {
this.maskId = maskId;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
Drawable mask = context.getDrawable(maskId);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
mask.setBounds(0, 0, width, height);
mask.draw(canvas);
canvas.drawBitmap(toTransform, 0, 0, paint);
return bitmap;
}
@Override
public String toString() {
return "MaskTransformation(maskId=" + maskId + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof MaskTransformation &&
((MaskTransformation) o).maskId == maskId;
}
@Override
public int hashCode() {
return ID.hashCode() + maskId * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + maskId).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/RoundedCornersTransformation.java
================================================
package jp.wasabeef.glide.transformations;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
public class RoundedCornersTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID = "jp.wasabeef.glide.transformations.RoundedCornersTransformation." + VERSION;
public enum CornerType {
ALL,
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
TOP, BOTTOM, LEFT, RIGHT,
OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
}
private final int radius;
private final int diameter;
private final int margin;
private final CornerType cornerType;
public RoundedCornersTransformation(int radius, int margin) {
this(radius, margin, CornerType.ALL);
}
public RoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
this.radius = radius;
this.diameter = this.radius * 2;
this.margin = margin;
this.cornerType = cornerType;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
bitmap.setHasAlpha(true);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
drawRoundRect(canvas, paint, width, height);
return bitmap;
}
private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
float right = width - margin;
float bottom = height - margin;
switch (cornerType) {
case ALL:
canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
break;
case TOP_LEFT:
drawTopLeftRoundRect(canvas, paint, right, bottom);
break;
case TOP_RIGHT:
drawTopRightRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM_LEFT:
drawBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM_RIGHT:
drawBottomRightRoundRect(canvas, paint, right, bottom);
break;
case TOP:
drawTopRoundRect(canvas, paint, right, bottom);
break;
case BOTTOM:
drawBottomRoundRect(canvas, paint, right, bottom);
break;
case LEFT:
drawLeftRoundRect(canvas, paint, right, bottom);
break;
case RIGHT:
drawRightRoundRect(canvas, paint, right, bottom);
break;
case OTHER_TOP_LEFT:
drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
break;
case OTHER_TOP_RIGHT:
drawOtherTopRightRoundRect(canvas, paint, right, bottom);
break;
case OTHER_BOTTOM_LEFT:
drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
break;
case OTHER_BOTTOM_RIGHT:
drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
break;
case DIAGONAL_FROM_TOP_LEFT:
drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
break;
case DIAGONAL_FROM_TOP_RIGHT:
drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
break;
default:
canvas.drawRoundRect(new RectF(margin, margin, right, bottom), radius, radius, paint);
break;
}
}
private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin + radius, margin + radius, bottom), paint);
canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
}
private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
canvas.drawRect(new RectF(right - radius, margin + radius, right, bottom), paint);
}
private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin, margin + diameter, bottom - radius), paint);
canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
}
private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
canvas.drawRect(new RectF(right - radius, margin, right, bottom - radius), paint);
}
private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
paint);
canvas.drawRect(new RectF(margin, margin + radius, right, bottom), paint);
}
private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
paint);
canvas.drawRect(new RectF(margin, margin, right, bottom - radius), paint);
}
private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
paint);
canvas.drawRect(new RectF(margin + radius, margin, right, bottom), paint);
}
private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
canvas.drawRect(new RectF(margin, margin, right - radius, bottom), paint);
}
private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
paint);
canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
}
private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
paint);
canvas.drawRoundRect(new RectF(margin, bottom - diameter, right, bottom), radius, radius,
paint);
canvas.drawRect(new RectF(margin + radius, margin, right, bottom - radius), paint);
}
private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
paint);
canvas.drawRoundRect(new RectF(right - diameter, margin, right, bottom), radius, radius, paint);
canvas.drawRect(new RectF(margin, margin + radius, right - radius, bottom), paint);
}
private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, right, margin + diameter), radius, radius,
paint);
canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, bottom), radius, radius,
paint);
canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
}
private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(margin, margin, margin + diameter, margin + diameter), radius,
radius, paint);
canvas.drawRoundRect(new RectF(right - diameter, bottom - diameter, right, bottom), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin + radius, right - radius, bottom), paint);
canvas.drawRect(new RectF(margin + radius, margin, right, bottom - radius), paint);
}
private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
float bottom) {
canvas.drawRoundRect(new RectF(right - diameter, margin, right, margin + diameter), radius,
radius, paint);
canvas.drawRoundRect(new RectF(margin, bottom - diameter, margin + diameter, bottom), radius,
radius, paint);
canvas.drawRect(new RectF(margin, margin, right - radius, bottom - radius), paint);
canvas.drawRect(new RectF(margin + radius, margin + radius, right, bottom), paint);
}
@Override
public String toString() {
return "RoundedTransformation(radius=" + radius + ", margin=" + margin + ", diameter="
+ diameter + ", cornerType=" + cornerType.name() + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof RoundedCornersTransformation &&
((RoundedCornersTransformation) o).radius == radius &&
((RoundedCornersTransformation) o).diameter == diameter &&
((RoundedCornersTransformation) o).margin == margin &&
((RoundedCornersTransformation) o).cornerType == cornerType;
}
@Override
public int hashCode() {
return ID.hashCode() + radius * 10000 + diameter * 1000 + margin * 100 + cornerType.ordinal() * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius + diameter + margin + cornerType).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/BrightnessFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageBrightnessFilter;
/**
* brightness value ranges from -1.0 to 1.0, with 0.0 as the normal level
*/
public class BrightnessFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation." + VERSION;
private final float brightness;
public BrightnessFilterTransformation() {
this(0.0f);
}
public BrightnessFilterTransformation(float brightness) {
super(new GPUImageBrightnessFilter());
this.brightness = brightness;
GPUImageBrightnessFilter filter = getFilter();
filter.setBrightness(this.brightness);
}
@Override
public String toString() {
return "BrightnessFilterTransformation(brightness=" + brightness + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof BrightnessFilterTransformation &&
((BrightnessFilterTransformation) o).brightness == brightness;
}
@Override
public int hashCode() {
return ID.hashCode() + (int) ((brightness + 1.0f) * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + brightness).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/ContrastFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageContrastFilter;
/**
* contrast value ranges from 0.0 to 4.0, with 1.0 as the normal level
*/
public class ContrastFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation." + VERSION;
private final float contrast;
public ContrastFilterTransformation() {
this(1.0f);
}
public ContrastFilterTransformation(float contrast) {
super(new GPUImageContrastFilter());
this.contrast = contrast;
GPUImageContrastFilter filter = getFilter();
filter.setContrast(this.contrast);
}
@Override
public String toString() {
return "ContrastFilterTransformation(contrast=" + contrast + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof ContrastFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode() + (int) (contrast * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + contrast).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/GPUFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.GPUImage;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageFilter;
import jp.wasabeef.glide.transformations.BitmapTransformation;
public class GPUFilterTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.GPUFilterTransformation." + VERSION;
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
private final GPUImageFilter gpuImageFilter;
public GPUFilterTransformation(GPUImageFilter filter) {
this.gpuImageFilter = filter;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
GPUImage gpuImage = new GPUImage(context);
gpuImage.setImage(toTransform);
gpuImage.setFilter(gpuImageFilter);
return gpuImage.getBitmapWithFilterApplied();
}
@Override
public String toString() {
return getClass().getSimpleName();
}
@SuppressWarnings("unchecked")
public
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageColorInvertFilter;
/**
* Invert all the colors in the image.
*/
public class InvertFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.InvertFilterTransformation." + VERSION;
public InvertFilterTransformation() {
super(new GPUImageColorInvertFilter());
}
@Override
public String toString() {
return "InvertFilterTransformation()";
}
@Override
public boolean equals(Object o) {
return o instanceof InvertFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageKuwaharaFilter;
/**
* Kuwahara all the colors in the image.
*
* The radius to sample from when creating the brush-stroke effect, with a default of 25.
* The larger the radius, the slower the filter.
*/
public class KuwaharaFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.KuwaharaFilterTransformation." + VERSION;
private final int radius;
public KuwaharaFilterTransformation() {
this(25);
}
public KuwaharaFilterTransformation(int radius) {
super(new GPUImageKuwaharaFilter());
this.radius = radius;
GPUImageKuwaharaFilter filter = getFilter();
filter.setRadius(this.radius);
}
@Override
public String toString() {
return "KuwaharaFilterTransformation(radius=" + radius + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof KuwaharaFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode() + radius * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/PixelationFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImagePixelationFilter;
/**
* Applies a Pixelation effect to the image.
*
* The pixel with a default of 10.0.
*/
public class PixelationFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.PixelationFilterTransformation." + VERSION;
private final float pixel;
public PixelationFilterTransformation() {
this(10f);
}
public PixelationFilterTransformation(float pixel) {
super(new GPUImagePixelationFilter());
this.pixel = pixel;
GPUImagePixelationFilter filter = getFilter();
filter.setPixel(this.pixel);
}
@Override
public String toString() {
return "PixelationFilterTransformation(pixel=" + pixel + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof PixelationFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode() + (int) (pixel * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + pixel).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SepiaFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSepiaToneFilter;
/**
* Applies a simple sepia effect.
*
* The intensity with a default of 1.0.
*/
public class SepiaFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.SepiaFilterTransformation." + VERSION;
private final float intensity;
public SepiaFilterTransformation() {
this(1.0f);
}
public SepiaFilterTransformation(float intensity) {
super(new GPUImageSepiaToneFilter());
this.intensity = intensity;
GPUImageSepiaToneFilter filter = getFilter();
filter.setIntensity(this.intensity);
}
@Override
public String toString() {
return "SepiaFilterTransformation(intensity=" + intensity + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof SepiaFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode() + (int) (intensity * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + intensity).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SketchFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSketchFilter;
public class SketchFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.SketchFilterTransformation." + VERSION;
public SketchFilterTransformation() {
super(new GPUImageSketchFilter());
}
@Override
public String toString() {
return "SketchFilterTransformation()";
}
@Override
public boolean equals(Object o) {
return o instanceof SketchFilterTransformation;
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/SwirlFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.graphics.PointF;
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageSwirlFilter;
/**
* Creates a swirl distortion on the image.
*/
public class SwirlFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.SwirlFilterTransformation." + VERSION;
private final float radius;
private final float angle;
private final PointF center;
public SwirlFilterTransformation() {
this(.5f, 1.0f, new PointF(0.5f, 0.5f));
}
/**
* @param radius from 0.0 to 1.0, default 0.5
* @param angle minimum 0.0, default 1.0
* @param center default (0.5, 0.5)
*/
public SwirlFilterTransformation(float radius, float angle, PointF center) {
super(new GPUImageSwirlFilter());
this.radius = radius;
this.angle = angle;
this.center = center;
GPUImageSwirlFilter filter = getFilter();
filter.setRadius(this.radius);
filter.setAngle(this.angle);
filter.setCenter(this.center);
}
@Override
public String toString() {
return "SwirlFilterTransformation(radius=" + radius + ",angle=" + angle + ",center="
+ center.toString() + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof SwirlFilterTransformation &&
((SwirlFilterTransformation) o).radius == radius &&
((SwirlFilterTransformation) o).angle == radius &&
((SwirlFilterTransformation) o).center.equals(center.x, center.y);
}
@Override
public int hashCode() {
return ID.hashCode() + (int) (radius * 1000) + (int) (angle * 10) + center.hashCode();
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius + angle + center.hashCode()).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/ToonFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageToonFilter;
/**
* The threshold at which to apply the edges, default of 0.2.
* The levels of quantization for the posterization of colors within the scene,
* with a default of 10.0.
*/
public class ToonFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.ToonFilterTransformation." + VERSION;
private final float threshold;
private final float quantizationLevels;
public ToonFilterTransformation() {
this(.2f, 10.0f);
}
public ToonFilterTransformation(float threshold, float quantizationLevels) {
super(new GPUImageToonFilter());
this.threshold = threshold;
this.quantizationLevels = quantizationLevels;
GPUImageToonFilter filter = getFilter();
filter.setThreshold(this.threshold);
filter.setQuantizationLevels(this.quantizationLevels);
}
@Override
public String toString() {
return "ToonFilterTransformation(threshold=" + threshold + ",quantizationLevels="
+ quantizationLevels + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof ToonFilterTransformation &&
((ToonFilterTransformation) o).threshold == threshold &&
((ToonFilterTransformation) o).quantizationLevels == quantizationLevels;
}
@Override
public int hashCode() {
return ID.hashCode() + (int) (threshold * 1000) + (int) (quantizationLevels * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + threshold + quantizationLevels).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/VignetteFilterTransformation.java
================================================
package jp.wasabeef.glide.transformations.gpu;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.graphics.PointF;
import androidx.annotation.NonNull;
import java.security.MessageDigest;
import java.util.Arrays;
import jp.co.cyberagent.android.gpuimage.filter.GPUImageVignetteFilter;
/**
* Performs a vignetting effect, fading out the image at the edges
* The directional intensity of the vignetting,
* with a default of x = 0.5, y = 0.5, start = 0, end = 0.75
*/
public class VignetteFilterTransformation extends GPUFilterTransformation {
private static final int VERSION = 1;
private static final String ID =
"jp.wasabeef.glide.transformations.gpu.VignetteFilterTransformation." + VERSION;
private final PointF center;
private final float[] vignetteColor;
private final float vignetteStart;
private final float vignetteEnd;
public VignetteFilterTransformation() {
this(new PointF(0.5f, 0.5f), new float[]{0.0f, 0.0f, 0.0f}, 0.0f, 0.75f);
}
public VignetteFilterTransformation(PointF center, float[] color, float start, float end) {
super(new GPUImageVignetteFilter());
this.center = center;
vignetteColor = color;
vignetteStart = start;
vignetteEnd = end;
GPUImageVignetteFilter filter = getFilter();
filter.setVignetteCenter(this.center);
filter.setVignetteColor(vignetteColor);
filter.setVignetteStart(vignetteStart);
filter.setVignetteEnd(vignetteEnd);
}
@Override
public String toString() {
return "VignetteFilterTransformation(center=" + center.toString() + ",color=" + Arrays.toString(
vignetteColor) + ",start=" + vignetteStart + ",end=" + vignetteEnd + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof VignetteFilterTransformation &&
((VignetteFilterTransformation) o).center.equals(center.x, center.y) &&
Arrays.equals(((VignetteFilterTransformation) o).vignetteColor, vignetteColor) &&
((VignetteFilterTransformation) o).vignetteStart == vignetteStart &&
((VignetteFilterTransformation) o).vignetteEnd == vignetteEnd;
}
@Override
public int hashCode() {
return ID.hashCode() + center.hashCode() + Arrays.hashCode(vignetteColor) +
(int) (vignetteStart * 100) + (int) (vignetteEnd * 10);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + center + Arrays.hashCode(vignetteColor) + vignetteStart + vignetteEnd).getBytes(CHARSET));
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/internal/FastBlur.java
================================================
package jp.wasabeef.glide.transformations.internal;
import android.graphics.Bitmap;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class FastBlur {
public static Bitmap blur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) {
// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class RSBlur {
public static Bitmap blur(Context context, Bitmap bitmap, int radius) throws RSRuntimeException {
RenderScript rs = null;
Allocation input = null;
Allocation output = null;
ScriptIntrinsicBlur blur = null;
try {
rs = RenderScript.create(context);
rs.setMessageHandler(new RenderScript.RSMessageHandler());
input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
output = Allocation.createTyped(rs, input.getType());
blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
blur.setInput(input);
blur.setRadius(radius);
blur.forEach(output);
output.copyTo(bitmap);
} finally {
if (rs != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
RenderScript.releaseAllContexts();
} else {
rs.destroy();
}
}
if (input != null) {
input.destroy();
}
if (output != null) {
output.destroy();
}
if (blur != null) {
blur.destroy();
}
}
return bitmap;
}
}
================================================
FILE: transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Utils.java
================================================
package jp.wasabeef.glide.transformations.internal;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
/**
* Copyright (C) 2020 Wasabeef
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public final class Utils {
private Utils() {
// Utility class.
}
public static int toDp(int px) {
return px * (int) Resources.getSystem().getDisplayMetrics().density;
}
}