1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-10-04 18:03:21 +08:00

22 Commits
1.0.0 ... 1.0.3

Author SHA1 Message Date
wasabeef
68a9097962 refactor: Bitmap.Config 2015-02-05 20:51:12 +09:00
wasabeef
6269999abc bump up 2015-02-04 01:00:50 +09:00
wasabeef
7e93ccd9af Merge branch 'master' of github.com:wasabeef/glide-transformations 2015-02-04 00:55:18 +09:00
wasabeef
07b7d24e13 bug fix 2015-02-04 00:54:22 +09:00
Daichi Furiya
ca648cbc7d bump up 2015-02-04 00:40:08 +09:00
Daichi Furiya
67ac3e31a4 Update CHANGELOG.md 2015-02-04 00:39:19 +09:00
wasabeef
1534573e8f bump up 2015-02-03 22:59:48 +09:00
wasabeef
a5487ade83 Refactor: use BitmapPool 2015-02-03 22:59:32 +09:00
wasabeef
62abca0b1e Merge branch 'master' of github.com:wasabeef/glide-transformations 2015-02-03 22:28:46 +09:00
wasabeef
a6bbdb0694 update glide 2015-02-03 22:27:19 +09:00
Daichi Furiya
2f75284dd4 Update README.md 2015-01-27 11:17:32 +09:00
Daichi Furiya
5772f60c61 Update CHANGELOG.md 2015-01-21 00:53:11 +09:00
wasabeef
cf5b0bbf1d Organizing a dependency 2015-01-21 00:00:11 +09:00
wasabeef
a0e4705452 fix: Blur transformation not working 2015-01-20 23:54:12 +09:00
wasabeef
d474911010 Refactor: Comments 2015-01-20 23:36:25 +09:00
wasabeef
5775d8211c Merge branch 'master' of github.com:wasabeef/glide-transformations 2015-01-20 23:35:37 +09:00
wasabeef
3c3d2c0123 fix: Blur transformation not working 2015-01-20 23:34:47 +09:00
Daichi Furiya
ccf79de0f0 Update README.md 2015-01-17 14:21:59 +09:00
Daichi Furiya
b7f1b8cd50 Update README.md 2015-01-15 15:31:37 +09:00
Daichi Furiya
6481449de7 Update README.md 2015-01-14 23:52:22 +09:00
Daichi Furiya
dd94a0a16a Update README.md 2015-01-14 11:46:46 +09:00
wasabeef
bf2eaadf2d bug fix 2015-01-14 01:05:26 +09:00
14 changed files with 122 additions and 267 deletions

View File

@@ -1,6 +1,22 @@
Change Log
==========
Version 1.0.3 *(2015-02-05)*
----------------------------
Bug fix
Version 1.0.2 *(2015-02-04)*
----------------------------
Refactor: use BimapPool
Version 1.0.1 *(2015-01-21)*
----------------------------
fix: Blur Transformation now woking at Android 4.3
add: GPUImage to Gradle dependency
Version 1.0.0 *(2015-01-12)*
----------------------------

View File

@@ -8,6 +8,10 @@ An Android transformation library providing a variety of image transformations f
Please feel free to use this.
#### Are you using Picasso?
[Picasso Transformations](https://github.com/wasabeef/picasso-transformations)
# Demo
### Original Image
@@ -24,10 +28,12 @@ Please feel free to use this.
```groovy
repositories {
jcenter()
mavenCentral() // GPUImage for Android
}
dependencies {
compile 'jp.wasabeef:glide-transformations:1.0.0'
compile 'jp.wasabeef:glide-transformations:1.0.3@aar'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3@aar'
}
```
@@ -52,6 +58,21 @@ Glide.with(this).load(R.drawable.demo).bitmapTransform(
.into((ImageView) findViewById(R.id.image));
```
## Step 4
If you are using `BlurTransformation`.
```groovy
android {
...
defaultConfig {
...
renderscriptTargetApi 21
renderscriptSupportModeEnabled true
}
}
```
## Transformations
### Crop
@@ -77,7 +98,7 @@ Daichi Furiya (Wasabeef) - <dadadada.chop@gmail.com>
<a href="https://twitter.com/wasabeef_jp">
<img alt="Follow me on Twitter"
src="https://raw.githubusercontent.com/wasabeef/wasabeef.github.io/master/art/twitter.png" width="75"/>
src="https://raw.githubusercontent.com/wasabeef/art/master/twitter.png" width="75"/>
</a>
Thanks

View File

@@ -16,5 +16,6 @@ buildscript {
allprojects {
repositories {
jcenter()
mavenCentral()
}
}

View File

@@ -9,6 +9,8 @@ android {
targetSdkVersion TARGET_SDK_VERSION as int
versionCode VERSION_CODE as int
versionName VERSION_NAME
renderscriptTargetApi RENDERSCRIPT_TARGET_API as int
renderscriptSupportModeEnabled true
}
signingConfigs {
@@ -51,7 +53,9 @@ def getKeyAliasPasswordProperty() {
dependencies {
compile project(':transformations')
compile 'com.android.support:appcompat-v7:+'
// compile 'jp.wasabeef:glide-transformations:1.0.3'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3'
compile 'com.android.support:appcompat-v7:21.+'
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.github.bumptech.glide:glide:3.+'
compile 'com.github.bumptech.glide:glide:3.5.1'
}

View File

@@ -1,4 +1,4 @@
VERSION_NAME=1.0.0
VERSION_NAME=1.0.3
VERSION_CODE=3
GROUP=jp.wasabeef
ARTIFACT_ID=glide-transformations

View File

@@ -16,7 +16,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.github.bumptech.glide:glide:3.4.0'
compile 'com.github.bumptech.glide:glide:3.5.1'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3'
}
android.libraryVariants.all { variant ->

View File

@@ -23,11 +23,12 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
public class BlurTransformation implements Transformation<Bitmap> {
@@ -45,254 +46,36 @@ public class BlurTransformation implements Transformation<Bitmap> {
public BlurTransformation(Context context, BitmapPool pool, int radius) {
mContext = context;
mBitmapPool = pool;
this.mRadius = radius;
mRadius = radius;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
if (Build.VERSION.SDK_INT > 16) {
Bitmap bitmap = source.copy(source.getConfig(), true);
int width = source.getWidth();
int height = source.getHeight();
final RenderScript rs = RenderScript.create(mContext);
final Allocation input = Allocation.createFromBitmap(rs, source,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(mRadius /* e.g. 3.f */);
script.setInput(input);
script.forEach(output);
output.copyTo(bitmap);
return BitmapResource.obtain(bitmap, mBitmapPool);
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(width, height, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, config);
}
// Stack Blur v1.0 from
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
//
// Java Author: Mario Klingemann <mario at quasimondo.com>
// http://incubator.quasimondo.com
// created Feburary 29, 2004
// Android port : Yahel Bouaziz <yahel at kayenko.com>
// http://www.kayenko.com
// ported april 5th, 2012
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(source, 0, 0, null);
// This is a compromise between Gaussian Blur and Box blur
// It creates much better looking blurs than Box Blur, but is
// 7x faster than my Gaussian Blur implementation.
//
// I called it Stack Blur because this describes best how this
// filter works internally: it creates a kind of moving stack
// of colors whilst scanning through the image. Thereby it
// just has to add one new block of color to the right side
// of the stack and remove the leftmost color. The remaining
// colors on the topmost layer of the stack are either added on
// or reduced by one, depending on if they are on the right or
// on the left side of the stack.
//
// If you are using this algorithm in your code please add
// the following line:
//
// Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
RenderScript rs = RenderScript.create(mContext);
Allocation overlayAlloc = Allocation.createFromBitmap(rs, bitmap);
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(mRadius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(bitmap);
Bitmap bitmap = source.copy(source.getConfig(), true);
if (mRadius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = mRadius + mRadius + 1;
int r[] = new int[wh];
int g[] = new int[wh];
int b[] = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int vmin[] = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = mRadius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -mRadius; i <= mRadius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + mRadius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = mRadius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - mRadius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + mRadius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -mRadius * w;
for (i = -mRadius; i <= mRadius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + mRadius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = mRadius;
for (y = 0; y < h; y++) {
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - mRadius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];
sir[0] = r[p];
sir[1] = g[p];
sir[2] = b[p];
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[stackpointer];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi += w;
}
}
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
source.recycle();
rs.destroy();
return BitmapResource.obtain(bitmap, mBitmapPool);
}

View File

@@ -45,13 +45,19 @@ public class ColorFilterTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(width, height, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, config);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(source, 0, 0, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@@ -24,6 +24,7 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
public class CropCircleTransformation implements Transformation<Bitmap> {
@@ -42,24 +43,27 @@ public class CropCircleTransformation implements Transformation<Bitmap> {
int width = (source.getWidth() - size) / 2;
int height = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, width, height, size, size);
if (squaredBitmap != source) {
source.recycle();
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(size, size, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(size, size, config);
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
Matrix matrix = new Matrix();
matrix.setTranslate(-width, -height);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);
}

View File

@@ -33,21 +33,21 @@ public class CropSquareTransformation implements Transformation<Bitmap> {
this.mBitmapPool = pool;
}
public CropSquareTransformation(BitmapPool pool, int width, int height) {
mBitmapPool = pool;
mWidth = width;
mHeight = height;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int size = Math.min(source.getWidth(), source.getHeight());
int width = (source.getWidth() - size) / 2;
int height = (source.getHeight() - size) / 2;
mWidth = (source.getWidth() - size) / 2;
mHeight = (source.getHeight() - size) / 2;
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
}
Bitmap bitmap = Bitmap.createBitmap(source, width, height, size, size);
if (bitmap != source) {
source.recycle();
}

View File

@@ -51,6 +51,13 @@ public class CropTransformation implements Transformation<Bitmap> {
mHeight = source.getHeight();
}
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(mWidth, mHeight, config);
}
float scaleX = (float) mWidth / source.getWidth();
float scaleY = (float) mHeight / source.getHeight();
float scale = Math.max(scaleX, scaleY);
@@ -61,9 +68,9 @@ public class CropTransformation implements Transformation<Bitmap> {
float top = (mHeight - scaledHeight) / 2;
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, source.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(source, null, targetRect, null);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@@ -42,7 +42,12 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(width, height, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, config);
}
Canvas canvas = new Canvas(bitmap);
ColorMatrix saturation = new ColorMatrix();
@@ -50,6 +55,7 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
canvas.drawBitmap(source, 0, 0, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);

View File

@@ -48,7 +48,12 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
int width = source.getWidth();
int height = source.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap.Config config =
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
Bitmap bitmap = mBitmapPool.get(width, height, config);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, config);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
@@ -56,6 +61,7 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
radius, radius, paint);
source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool);