use of android.support.v8.renderscript.Allocation in project BlurKit-Android by flurgle.
the class BlurKit method blur.
public Bitmap blur(Bitmap src, int radius) {
final Allocation input = Allocation.createFromBitmap(rs, src);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(radius);
script.setInput(input);
script.forEach(output);
output.copyTo(src);
return src;
}
use of android.support.v8.renderscript.Allocation in project android-stackblur by kikoso.
the class RSBlurProcess method blur.
@Override
public Bitmap blur(Bitmap original, float radius) {
int width = original.getWidth();
int height = original.getHeight();
Bitmap blurred = original.copy(Bitmap.Config.ARGB_8888, true);
ScriptC_blur blurScript = new ScriptC_blur(_rs, context.getResources(), R.raw.blur);
Allocation inAllocation = Allocation.createFromBitmap(_rs, blurred, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
blurScript.set_gIn(inAllocation);
blurScript.set_width(width);
blurScript.set_height(height);
blurScript.set_radius((int) radius);
int[] row_indices = new int[height];
for (int i = 0; i < height; i++) {
row_indices[i] = i;
}
Allocation rows = Allocation.createSized(_rs, Element.U32(_rs), height, Allocation.USAGE_SCRIPT);
rows.copyFrom(row_indices);
row_indices = new int[width];
for (int i = 0; i < width; i++) {
row_indices[i] = i;
}
Allocation columns = Allocation.createSized(_rs, Element.U32(_rs), width, Allocation.USAGE_SCRIPT);
columns.copyFrom(row_indices);
blurScript.forEach_blur_h(rows);
blurScript.forEach_blur_v(columns);
inAllocation.copyTo(blurred);
return blurred;
}
use of android.support.v8.renderscript.Allocation in project T-MVP by north2016.
the class BlurTransformation method transform.
/**
* Transforms the given {@link Bitmap} based on the given dimensions and returns the transformed
* result.
* <p/>
* <p>
* The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically
* recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations
* should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning
* the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing
* errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may
* safely be returned to the BitmapPool and/or recycled.
* </p>
* <p/>
* <p>
* outWidth and outHeight will never be {@link Target#SIZE_ORIGINAL}, this
* class converts them to be the size of the Bitmap we're going to transform before calling this method.
* </p>
*
* @param pool A {@link BitmapPool} that can be used to obtain and
* return intermediate {@link Bitmap}s used in this transformation. For every
* {@link Bitmap} obtained from the pool during this transformation, a
* {@link Bitmap} must also be returned.
* @param toTransform The {@link Bitmap} to transform.
* @param outWidth The ideal width of the transformed bitmap (the transformed width does not need to match exactly).
* @param outHeight The ideal height of the transformed bitmap (the transformed heightdoes not need to match
*/
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
boolean needScaled = mSampling == DEFAULT_SAMPLING;
int originWidth = toTransform.getWidth();
int originHeight = toTransform.getHeight();
int width, height;
if (needScaled) {
width = originWidth;
height = originHeight;
} else {
width = (int) (originWidth / mSampling);
height = (int) (originHeight / mSampling);
}
//find a re-use bitmap
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
if (mSampling != DEFAULT_SAMPLING) {
canvas.scale(1 / mSampling, 1 / mSampling);
}
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
PorterDuffColorFilter filter = new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
paint.setColorFilter(filter);
canvas.drawBitmap(toTransform, 0, 0, paint);
// TIPS: Glide will take care of returning our original Bitmap to the BitmapPool for us,
// we needn't to recycle it.
// toTransform.recycle(); <--- Just for tips. by Ligboy
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));
blur.setInput(input);
blur.setRadius(mRadius);
blur.forEach(output);
output.copyTo(bitmap);
rs.destroy();
if (needScaled) {
return bitmap;
} else {
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, originWidth, originHeight, true);
bitmap.recycle();
return scaled;
}
}
use of android.support.v8.renderscript.Allocation in project BlurView by Dimezis.
the class RenderScriptBlur method blur.
/**
* @param bitmap bitmap to blur
* @param blurRadius blur radius (1..25)
* @return blurred bitmap
*/
@Override
public final Bitmap blur(Bitmap bitmap, float blurRadius) {
//Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag
Allocation inAllocation = Allocation.createFromBitmap(renderScript, bitmap);
Bitmap outputBitmap;
if (canModifyBitmap) {
outputBitmap = bitmap;
} else {
outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
}
if (!canReuseAllocation(bitmap)) {
if (outAllocation != null) {
outAllocation.destroy();
}
outAllocation = Allocation.createTyped(renderScript, inAllocation.getType());
lastBitmapWidth = bitmap.getWidth();
lastBitmapHeight = bitmap.getHeight();
}
blurScript.setRadius(blurRadius);
blurScript.setInput(inAllocation);
//do not use inAllocation in forEach. it will cause visual artifacts on blurred Bitmap
blurScript.forEach(outAllocation);
outAllocation.copyTo(outputBitmap);
inAllocation.destroy();
return outputBitmap;
}
use of android.support.v8.renderscript.Allocation in project ngAndroid by davityle.
the class DefaultBlur method blurBitmap.
private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context) {
RenderScript rs = RenderScript.create(context);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);
final Allocation input = Allocation.createFromBitmap(rs, src, 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(blurRadius);
script.setInput(input);
script.forEach(output);
output.copyTo(blurredBitmap);
return blurredBitmap;
}
Aggregations