From 5c8051acff042e23c8c806c385e47051c37723a1 Mon Sep 17 00:00:00 2001 From: wasabeef Date: Mon, 12 Jan 2015 21:36:55 +0900 Subject: [PATCH] add Kuwahara --- README.md | 2 +- .../wasabeef/example/glide/MainActivity.java | 1 + .../wasabeef/example/glide/MainAdapter.java | 7 +- .../gpu/KuwaharaFilterTransformation.java | 72 +++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java diff --git a/README.md b/README.md index 4d85ace..0443ecd 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Glide.with(this).load(R.drawable.demo).bitmapTransform( ### Filter (use GPUImage) `ToonFilterTransformation`, `SepiaFilterTransformation`, `ContrastFilterTransformation` `InvertFilterTransformation`, `PixelationFilterTransformation`, `SketchFilterTransformation` -`SwirlFilterTransformation` +`SwirlFilterTransformation`, `KuwaharaFilterTransformation` ### Other `RoundedCornersTransformation` diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java b/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java index c3e692e..28be2de 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainActivity.java @@ -37,6 +37,7 @@ public class MainActivity extends ActionBarActivity { dataSet.add(Type.Sketch); dataSet.add(Type.Swirl); dataSet.add(Type.Brightness); + dataSet.add(Type.Kuawahara); recyclerView.setAdapter(new MainAdapter(this, dataSet)); } diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java index b066269..c524fde 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -27,6 +27,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation; import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation; import jp.wasabeef.glide.transformations.gpu.InvertFilterTransformation; +import jp.wasabeef.glide.transformations.gpu.KuwaharaFilterTransformation; import jp.wasabeef.glide.transformations.gpu.PixelationFilterTransformation; import jp.wasabeef.glide.transformations.gpu.SepiaFilterTransformation; import jp.wasabeef.glide.transformations.gpu.SketchFilterTransformation; @@ -57,7 +58,8 @@ public class MainAdapter extends RecyclerView.Adapter { Pixel, Sketch, Swirl, - Brightness + Brightness, + Kuawahara } public MainAdapter(Context context, List dataSet) { @@ -123,6 +125,9 @@ public class MainAdapter extends RecyclerView.Adapter { case Brightness: transformation = new BrightnessFilterTransformation(mContext, mPool, 0.5f); break; + case Kuawahara: + transformation = new KuwaharaFilterTransformation(mContext, mPool, 25); + break; } Glide.with(mContext).load(R.drawable.demo) diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java new file mode 100644 index 0000000..656b426 --- /dev/null +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/gpu/KuwaharaFilterTransformation.java @@ -0,0 +1,72 @@ +package jp.wasabeef.glide.transformations.gpu; + +/** + * Copyright (C) 2015 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 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 android.content.Context; +import android.graphics.Bitmap; + +import jp.co.cyberagent.android.gpuimage.GPUImage; +import jp.co.cyberagent.android.gpuimage.GPUImageKuwaharaFilter; + +/** + * The radius to sample from when creating the brush-stroke effect, with a default of 3. + * The larger the radius, the slower the filter. + */ +public class KuwaharaFilterTransformation implements Transformation { + + private Context mContext; + private BitmapPool mBitmapPool; + + private GPUImageKuwaharaFilter mFilter = new GPUImageKuwaharaFilter(); + private int mRadius; + + public KuwaharaFilterTransformation(Context context, BitmapPool pool) { + mContext = context; + mBitmapPool = pool; + } + + public KuwaharaFilterTransformation(Context context, BitmapPool pool, int radius) { + mContext = context; + mBitmapPool = pool; + mRadius = radius; + mFilter.setRadius(mRadius); + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + Bitmap source = resource.get(); + + GPUImage gpuImage = new GPUImage(mContext); + gpuImage.setImage(source); + gpuImage.setFilter(mFilter); + Bitmap bitmap = gpuImage.getBitmapWithFilterApplied(); + + source.recycle(); + + return BitmapResource.obtain(bitmap, mBitmapPool); + } + + @Override + public String getId() { + return "KuwaharaFilterTransformation(radius=" + mRadius + ")"; + } +}