mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-10-04 09:53:21 +08:00
Compare commits
50 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
58e25e613c | ||
|
c914548296 | ||
|
cfe6dc313a | ||
|
136a5512c0 | ||
|
32d0c1de82 | ||
|
e23753044e | ||
|
decd2e58d5 | ||
|
e9d2c6d209 | ||
|
79e975d490 | ||
|
a21cfb1d86 | ||
|
c0ab4282d9 | ||
|
15effa238b | ||
|
ef39ca2782 | ||
|
bdda7cdf36 | ||
|
3890c84a0f | ||
|
e3c0c8d597 | ||
|
f7d0541a19 | ||
|
900db8bc93 | ||
|
c241532a58 | ||
|
8f63c10eb9 | ||
|
6468276c50 | ||
|
e0fb7c34e0 | ||
|
4c23e38206 | ||
|
ffae37274c | ||
|
0c6f696ba7 | ||
|
3aea08abaa | ||
|
883c131ad6 | ||
|
ee1f533999 | ||
|
68a9097962 | ||
|
6269999abc | ||
|
7e93ccd9af | ||
|
07b7d24e13 | ||
|
ca648cbc7d | ||
|
67ac3e31a4 | ||
|
1534573e8f | ||
|
a5487ade83 | ||
|
62abca0b1e | ||
|
a6bbdb0694 | ||
|
2f75284dd4 | ||
|
5772f60c61 | ||
|
cf5b0bbf1d | ||
|
a0e4705452 | ||
|
d474911010 | ||
|
5775d8211c | ||
|
3c3d2c0123 | ||
|
ccf79de0f0 | ||
|
b7f1b8cd50 | ||
|
6481449de7 | ||
|
dd94a0a16a | ||
|
bf2eaadf2d |
36
CHANGELOG.md
36
CHANGELOG.md
@@ -1,6 +1,42 @@
|
||||
Change Log
|
||||
==========
|
||||
|
||||
Version 1.0.7 *(2015-07-18)*
|
||||
----------------------------
|
||||
|
||||
Bug fix : Blur not working.
|
||||
|
||||
Version 1.0.6 *(2015-04-23)*
|
||||
----------------------------
|
||||
|
||||
add: CropType(Top, Center, Bottom) for CropTransformation.
|
||||
|
||||
Version 1.0.5 *(2015-03-09)*
|
||||
----------------------------
|
||||
|
||||
Bug fix: Transparency.
|
||||
|
||||
Version 1.0.4 *(2015-02-13)*
|
||||
----------------------------
|
||||
|
||||
Bug fix : remove original bitmap resource recycling.
|
||||
|
||||
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)*
|
||||
----------------------------
|
||||
|
||||
|
36
README.md
36
README.md
@@ -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.8@aar'
|
||||
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3@aar'
|
||||
}
|
||||
```
|
||||
|
||||
@@ -48,10 +54,25 @@ You can set a multiple transformations.
|
||||
```java
|
||||
BitmapPool pool = Glide.get(this).getBitmapPool();
|
||||
Glide.with(this).load(R.drawable.demo).bitmapTransform(
|
||||
new BlurTransformation(this, pool, 5), new CropCircleTransformation(pool))
|
||||
new BlurTransformation(this, pool, 25, 2), new CropCircleTransformation(pool))
|
||||
.into((ImageView) findViewById(R.id.image));
|
||||
```
|
||||
|
||||
## Step 4
|
||||
|
||||
If you are using `BlurTransformation`.
|
||||
|
||||
```groovy
|
||||
android {
|
||||
...
|
||||
defaultConfig {
|
||||
...
|
||||
renderscriptTargetApi 22
|
||||
renderscriptSupportModeEnabled true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Transformations
|
||||
|
||||
### Crop
|
||||
@@ -71,13 +92,22 @@ Glide.with(this).load(R.drawable.demo).bitmapTransform(
|
||||
### Other
|
||||
`RoundedCornersTransformation`
|
||||
|
||||
Applications using Glide Transformations
|
||||
---
|
||||
|
||||
Please [ping](mailto:dadadada.chop@gmail.com) me or send a pull request if you would like to be added here.
|
||||
|
||||
Icon | Application
|
||||
------------ | -------------
|
||||
<img src="https://lh6.ggpht.com/6zKH_uQY1bxCwXL4DLo_uoFEOXdShi3BgmN6XRHlaJ-oA1svmq6y1PZkmO50nWQn2Lg=w300-rw" width="48" height="48" /> | [Ameba Ownd](https://play.google.com/store/apps/details?id=jp.co.cyberagent.madrid)
|
||||
|
||||
Developed By
|
||||
-------
|
||||
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
|
||||
|
@@ -5,8 +5,8 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:1.0.+'
|
||||
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
|
||||
classpath 'com.android.tools.build:gradle:1.3.0-beta4'
|
||||
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
@@ -16,5 +16,6 @@ buildscript {
|
||||
allprojects {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
@@ -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,8 @@ def getKeyAliasPasswordProperty() {
|
||||
|
||||
dependencies {
|
||||
compile project(':transformations')
|
||||
compile 'com.android.support:appcompat-v7:+'
|
||||
compile 'com.android.support:recyclerview-v7:21.+'
|
||||
compile 'com.github.bumptech.glide:glide:3.+'
|
||||
compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}"
|
||||
compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:${GPUIMAGE_VERSION}"
|
||||
compile "com.android.support:appcompat-v7:${SUPPORT_PACKAGE_VERSION}"
|
||||
compile "com.android.support:recyclerview-v7:${SUPPORT_PACKAGE_VERSION}"
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package jp.wasabeef.example.glide;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.List;
|
||||
import jp.wasabeef.example.glide.MainAdapter.Type;
|
||||
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -22,7 +22,9 @@ public class MainActivity extends ActionBarActivity {
|
||||
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
|
||||
|
||||
List<Type> dataSet = new ArrayList<>();
|
||||
dataSet.add(Type.Crop);
|
||||
dataSet.add(Type.CropTop);
|
||||
dataSet.add(Type.CropCenter);
|
||||
dataSet.add(Type.CropBottom);
|
||||
dataSet.add(Type.CropSquare);
|
||||
dataSet.add(Type.CropCircle);
|
||||
dataSet.add(Type.ColorFilter);
|
||||
|
@@ -45,7 +45,9 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
||||
private BitmapPool mPool;
|
||||
|
||||
enum Type {
|
||||
Crop,
|
||||
CropTop,
|
||||
CropCenter,
|
||||
CropBottom,
|
||||
CropSquare,
|
||||
CropCircle,
|
||||
ColorFilter,
|
||||
@@ -79,11 +81,19 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
|
||||
Transformation<Bitmap> transformation = null;
|
||||
Transformation transformation = null;
|
||||
switch (mDataSet.get(position)) {
|
||||
case Crop:
|
||||
case CropTop:
|
||||
transformation =
|
||||
new CropTransformation(mPool, 300, 100, CropTransformation.CropType.TOP);
|
||||
break;
|
||||
case CropCenter:
|
||||
transformation = new CropTransformation(mPool, 300, 100);
|
||||
break;
|
||||
case CropBottom:
|
||||
transformation =
|
||||
new CropTransformation(mPool, 300, 100, CropTransformation.CropType.BOTTOM);
|
||||
break;
|
||||
case CropSquare:
|
||||
transformation = new CropSquareTransformation(mPool);
|
||||
break;
|
||||
@@ -100,7 +110,7 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
||||
transformation = new RoundedCornersTransformation(mPool, 100, 0);
|
||||
break;
|
||||
case Blur:
|
||||
transformation = new BlurTransformation(mContext, mPool, 10);
|
||||
transformation = new BlurTransformation(mContext, mPool, 25, 1);
|
||||
break;
|
||||
case Toon:
|
||||
transformation = new ToonFilterTransformation(mContext, mPool);
|
||||
|
@@ -6,11 +6,10 @@
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:contentDescription="@null"
|
||||
android:src="@drawable/demo"/>
|
||||
android:contentDescription="@null"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
|
@@ -1,12 +1,12 @@
|
||||
VERSION_NAME=1.0.0
|
||||
VERSION_CODE=3
|
||||
VERSION_NAME=1.0.8
|
||||
VERSION_CODE=8
|
||||
GROUP=jp.wasabeef
|
||||
ARTIFACT_ID=glide-transformations
|
||||
|
||||
COMPILE_SDK_VERSION=21
|
||||
BUILD_TOOLS_VERSION=21.1.2
|
||||
TARGET_SDK_VERSION=21
|
||||
RENDERSCRIPT_TARGET_API=21
|
||||
COMPILE_SDK_VERSION=22
|
||||
BUILD_TOOLS_VERSION=22.0.1
|
||||
TARGET_SDK_VERSION=22
|
||||
RENDERSCRIPT_TARGET_API=22
|
||||
MIN_SDK_VERSION=11
|
||||
|
||||
POM_DESCRIPTION=which provides simple Tranformations to Glide
|
||||
@@ -22,3 +22,7 @@ POM_DEVELOPER_NAME=Wasabeef
|
||||
POM_DEVELOPER_EMAIL=dadadada.chop@gmail.com
|
||||
POM_DEVELOPER_URL=wasabeef.jp
|
||||
ISSUE_URL=https://github.com/wasabeef/glide-transformations/issues
|
||||
|
||||
SUPPORT_PACKAGE_VERSION=22.2.1
|
||||
GLIDE_VERSION=3.6.1
|
||||
GPUIMAGE_VERSION=1.2.3
|
@@ -1,6 +1,7 @@
|
||||
task androidJavadocs(type: Javadoc) {
|
||||
source = android.sourceSets.main.java.srcDirs
|
||||
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
|
||||
ext.androidJar = "${android.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
|
||||
classpath += files(ext.androidJar)
|
||||
}
|
||||
|
||||
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
|
||||
|
@@ -15,8 +15,8 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile 'com.github.bumptech.glide:glide:3.4.0'
|
||||
compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}"
|
||||
compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:${GPUIMAGE_VERSION}"
|
||||
}
|
||||
|
||||
android.libraryVariants.all { variant ->
|
||||
|
Binary file not shown.
@@ -1,3 +1 @@
|
||||
<manifest
|
||||
package="jp.wasabeef.glide.transformations">
|
||||
</manifest>
|
||||
<manifest package="jp.wasabeef.glide.transformations"></manifest>
|
||||
|
@@ -23,282 +23,77 @@ 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.support.v8.renderscript.Allocation;
|
||||
import android.support.v8.renderscript.Element;
|
||||
import android.support.v8.renderscript.RenderScript;
|
||||
import android.support.v8.renderscript.ScriptIntrinsicBlur;
|
||||
|
||||
public class BlurTransformation implements Transformation<Bitmap> {
|
||||
|
||||
private static int MAX_RADIUS = 25;
|
||||
private static int DEFAULT_DOWN_SAMPLING = 1;
|
||||
|
||||
private Context mContext;
|
||||
private BitmapPool mBitmapPool;
|
||||
|
||||
private int mRadius;
|
||||
private int mSampling;
|
||||
|
||||
public BlurTransformation(Context context, BitmapPool pool) {
|
||||
this(context, pool, MAX_RADIUS);
|
||||
this(context, pool, MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
|
||||
}
|
||||
|
||||
public BlurTransformation(Context context, BitmapPool pool, int radius) {
|
||||
this(context, pool, radius, DEFAULT_DOWN_SAMPLING);
|
||||
}
|
||||
|
||||
public BlurTransformation(Context context, BitmapPool pool, int radius, int sampling) {
|
||||
mContext = context;
|
||||
mBitmapPool = pool;
|
||||
this.mRadius = radius;
|
||||
mRadius = radius;
|
||||
mSampling = sampling;
|
||||
}
|
||||
|
||||
@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();
|
||||
int scaledWidth = width / mSampling;
|
||||
int scaledHeight = height / mSampling;
|
||||
|
||||
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 bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
|
||||
if (bitmap == null) {
|
||||
bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
|
||||
// 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.scale(1 / (float) mSampling, 1 / (float) mSampling);
|
||||
Paint paint = new Paint();
|
||||
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
|
||||
canvas.drawBitmap(source, 0, 0, paint);
|
||||
|
||||
// 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 input = Allocation.createFromBitmap(rs, bitmap,
|
||||
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
|
||||
Allocation output = Allocation.createTyped(rs, input.getType());
|
||||
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
|
||||
|
||||
Bitmap bitmap = source.copy(source.getConfig(), true);
|
||||
blur.setInput(input);
|
||||
blur.setRadius(mRadius);
|
||||
blur.forEach(output);
|
||||
output.copyTo(bitmap);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "BlurTransformation(radius=" + mRadius + ")";
|
||||
return "BlurTransformation(radius=" + mRadius + ", sampling=" + mSampling + ")";
|
||||
}
|
||||
}
|
||||
|
@@ -45,14 +45,18 @@ 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);
|
||||
}
|
||||
|
@@ -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,25 +43,26 @@ 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 bitmap = mBitmapPool.get(size, size, Bitmap.Config.ARGB_8888);
|
||||
if (bitmap == null) {
|
||||
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
|
||||
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);
|
||||
if (width != 0 || height != 0) {
|
||||
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();
|
||||
|
||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||
}
|
||||
|
||||
|
@@ -33,23 +33,19 @@ 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 bitmap = Bitmap.createBitmap(source, width, height, size, size);
|
||||
if (bitmap != source) {
|
||||
source.recycle();
|
||||
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);
|
||||
}
|
||||
|
||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||
|
@@ -31,6 +31,8 @@ public class CropTransformation implements Transformation<Bitmap> {
|
||||
private int mWidth;
|
||||
private int mHeight;
|
||||
|
||||
private CropType mCropType = CropType.CENTER;
|
||||
|
||||
public CropTransformation(BitmapPool pool) {
|
||||
mBitmapPool = pool;
|
||||
}
|
||||
@@ -41,14 +43,24 @@ public class CropTransformation implements Transformation<Bitmap> {
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
public CropTransformation(BitmapPool pool, int width, int height, CropType cropType) {
|
||||
mBitmapPool = pool;
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
mCropType = cropType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||
Bitmap source = resource.get();
|
||||
if (mWidth == 0) {
|
||||
mWidth = source.getWidth();
|
||||
}
|
||||
if (mHeight == 0) {
|
||||
mHeight = source.getHeight();
|
||||
mWidth = mWidth == 0 ? source.getWidth() : mWidth;
|
||||
mHeight = mHeight == 0 ? source.getHeight() : mHeight;
|
||||
|
||||
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();
|
||||
@@ -58,19 +70,37 @@ public class CropTransformation implements Transformation<Bitmap> {
|
||||
float scaledWidth = scale * source.getWidth();
|
||||
float scaledHeight = scale * source.getHeight();
|
||||
float left = (mWidth - scaledWidth) / 2;
|
||||
float top = (mHeight - scaledHeight) / 2;
|
||||
float top = getTop(scaledHeight);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "CropTransformation(width=" + mWidth + ", height=" + mHeight + ")";
|
||||
return "CropTransformation(width=" + mWidth + ", height=" + mHeight + ", cropType="
|
||||
+ mCropType + ")";
|
||||
}
|
||||
|
||||
private float getTop(float scaledHeight) {
|
||||
switch (mCropType) {
|
||||
case TOP:
|
||||
return 0;
|
||||
case CENTER:
|
||||
return (mHeight - scaledHeight) / 2;
|
||||
case BOTTOM:
|
||||
return mHeight - scaledHeight;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public enum CropType {
|
||||
TOP,
|
||||
CENTER,
|
||||
BOTTOM
|
||||
}
|
||||
}
|
||||
|
@@ -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,7 +55,6 @@ 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);
|
||||
}
|
||||
|
@@ -48,7 +48,10 @@ 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 bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
|
||||
if (bitmap == null) {
|
||||
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
}
|
||||
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
Paint paint = new Paint();
|
||||
@@ -56,7 +59,6 @@ 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user