android.support.v8.renderscript.ScriptIntrinsicBlur Java Examples

The following examples show how to use android.support.v8.renderscript.ScriptIntrinsicBlur. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From renderscript-samples with Apache License 2.0 6 votes vote down vote up
private void createScript() {
    mRS = RenderScript.create(this);

    mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn);

    mOutAllocations = new Allocation[NUM_BITMAPS];
    for (int i = 0; i < NUM_BITMAPS; ++i) {
        mOutAllocations[i] = Allocation.createFromBitmap(mRS, mBitmapsOut[i]);
    }

    // Create intrinsics.
    // RenderScript has built-in features such as blur, convolve filter etc.
    // These intrinsics are handy for specific operations without writing RenderScript kernel.
    // In the sample, it's creating blur, convolve and matrix intrinsics.

    mScriptBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mScriptConvolve = ScriptIntrinsicConvolve5x5.create(mRS,
            Element.U8_4(mRS));
    mScriptMatrix = ScriptIntrinsicColorMatrix.create(mRS,
            Element.U8_4(mRS));
}
 
Example #2
Source File: BlurBuilder.java    From react-native-simple-shadow-view with MIT License 6 votes vote down vote up
public static Bitmap blur(Context context, Bitmap image, float blurRadius, float scale) {
    int width = Math.round(image.getWidth() * scale);
    int height = Math.round(image.getHeight() * scale);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);

    ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    intrinsicBlur.setRadius(blurRadius);
    intrinsicBlur.setInput(tmpIn);
    intrinsicBlur.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example #3
Source File: BlurTransformation.java    From Beautiful-Photos with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap bitmap) {
	// Create another bitmap that will hold the results of the filter.
	Bitmap blurredBitmap = Bitmap.createBitmap(bitmap);

	// Allocate memory for Renderscript to work with
	Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT);
	Allocation output = Allocation.createTyped(rs, input.getType());

	// Load up an instance of the specific script that we want to use.
	ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
	script.setInput(input);

	// Set the blur radius
	script.setRadius(RADIUS);

	// Start the ScriptIntrinisicBlur
	script.forEach(output);

	// Copy the output to the blurred bitmap
	output.copyTo(blurredBitmap);

	return blurredBitmap;
}
 
Example #4
Source File: Blur.java    From GifImageView with MIT License 6 votes vote down vote up
public Bitmap blur(Bitmap image) {
  if (image == null)
    return null;

  image = RGB565toARGB888(image);
  if (!configured) {
    input = Allocation.createFromBitmap(rs, image);
    output = Allocation.createTyped(rs, input.getType());
    script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(BLUR_RADIUS);
    configured = true;
  } else
    input.copyFrom(image);

  script.setInput(input);
  script.forEach(output);
  output.copyTo(image);

  return image;
}
 
Example #5
Source File: BlurTransformation.java    From android-tutorials-glide with MIT License 6 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example #6
Source File: RenderscriptBlurringMachine.java    From fogger with Apache License 2.0 6 votes vote down vote up
@Override
protected Bitmap blur(Context context, Bitmap bitmapToBlur, int radius) {
    Log.i(TAG, "Current build version sdk " + Build.VERSION.SDK_INT);
    Bitmap bitmap = bitmapToBlur.copy(bitmapToBlur.getConfig(), true);

    final RenderScript renderScript = RenderScript.create(context, Build.VERSION.SDK_INT);
    final Allocation input = Allocation.createFromBitmap(renderScript, bitmapToBlur,
                                                            Allocation.MipmapControl.MIPMAP_NONE,
                                                            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    try {
        final ScriptIntrinsicBlur script = createBlurringScript(radius, renderScript, input);
        script.forEach(output);
        renderScript.finish();
        output.copyTo(bitmap);
    } finally {
        input.destroy();
        output.destroy();
        bitmapToBlur.recycle();
        renderScript.destroy();
    }
    return bitmap;
}
 
Example #7
Source File: BlurBuilder.java    From FlyWoo with Apache License 2.0 6 votes vote down vote up
public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);


    return outputBitmap;
}
 
Example #8
Source File: MizLib.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a blurred bitmap. It uses a RenderScript to blur the bitmap very fast.
 * @param context
 * @param originalBitmap
 * @param radius
 * @return
 */
public static Bitmap fastBlur(Context context, Bitmap originalBitmap, int radius) {
    final RenderScript rs = RenderScript.create(context);

    final Allocation input = Allocation.createFromBitmap(rs, originalBitmap);
    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(originalBitmap);

    return originalBitmap;
}
 
Example #9
Source File: RenderScriptBlurHelper.java    From BlurDialogFragment with Apache License 2.0 5 votes vote down vote up
/**
 * blur a given bitmap
 *
 * @param sentBitmap       bitmap to blur
 * @param radius           blur radius
 * @param canReuseInBitmap true if bitmap must be reused without blur
 * @param context          used by RenderScript, can be null if RenderScript disabled
 * @return blurred bitmap
 */
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
    Bitmap bitmap;

    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }

    if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
        // RenderScript hates RGB_565 so we convert it to ARGB_8888
        // (see http://stackoverflow.com/questions/21563299/
        // defect-of-image-with-scriptintrinsicblur-from-support-library)
        bitmap = convertRGB565toARGB888(bitmap);
    }

    try {
        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        return bitmap;
    } catch (RSRuntimeException e) {
        Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 "
            + "continue with the FastBlur approach.");
    }

    return null;
}
 
Example #10
Source File: GlassView.java    From GlassView with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    mRenderScript = RenderScript.create(getContext());
    mBlur = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
    if (isPostHoneycomb() && isHardwareAccelerated()) {
        getViewTreeObserver().addOnGlobalLayoutListener(getGlobalLayoutListener());
        getViewTreeObserver().addOnScrollChangedListener(getScrollChangedListener());
    }
}
 
Example #11
Source File: BlurTransformation.java    From Sky31Radio with Apache License 2.0 5 votes vote down vote up
@Override
    public Bitmap transform(Bitmap source) {
        Timber.i("start bitmap transform");
        try {
            float radius = 10f;
            Bitmap outputBitmap;
//            if(Build.VERSION.SDK_INT >= 17){
                outputBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                RenderScript rs = RenderScript.create(ctx);
                ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
                Allocation tmpIn = Allocation.createFromBitmap(rs, source);
                Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
                sib.setRadius(radius);
                sib.setInput(tmpIn);
                sib.forEach(tmpOut);
                tmpOut.copyTo(outputBitmap);
                source.recycle();
//            }else{
//                outputBitmap = FastBlur.doBlur(source, (int) radius, true);
//            }
            Timber.d("blur bitmap success");
            return outputBitmap;
        } catch (Exception e) {
            Timber.e(e, "occur an error during blurring bitmap");
            return source;
        }
    }
 
Example #12
Source File: BitmapUtils.java    From Rocko-Android-Demos with Apache License 2.0 5 votes vote down vote up
public static Bitmap blurBitmap(Context applicationContext, Bitmap bitmap, float radius) {

        //Let's create an empty bitmap with the same size of the bitmap we want to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(applicationContext);

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
        //Set the radius of the blur
        blurScript.setRadius(radius);
        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        //recycle the original bitmap
        bitmap.recycle();

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;
    }
 
Example #13
Source File: Utils.java    From BlurZoomGallery with MIT License 5 votes vote down vote up
public static void blurImage(RenderScript renderScript, Bitmap bmp, float radius) {
    final Allocation input = Allocation.createFromBitmap(renderScript, bmp);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bmp);
}
 
Example #14
Source File: DefaultBlur.java    From ngAndroid with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #15
Source File: Blur.java    From AllAngleExpandableButton with Apache License 2.0 5 votes vote down vote up
@WorkerThread
private Bitmap getBlurBitmap(Context context, Bitmap inBitmap, float radius) {
    if (context == null || inBitmap == null) {
        throw new IllegalArgumentException("have not called setParams() before call execute()");
    }

    int width = Math.round(inBitmap.getWidth() * SCALE);
    int height = Math.round(inBitmap.getHeight() * SCALE);

    Bitmap in = Bitmap.createScaledBitmap(inBitmap, width, height, false);
    Bitmap out = Bitmap.createBitmap(in);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allocationIn = Allocation.createFromBitmap(rs, in);
    Allocation allocationOut = Allocation.createFromBitmap(rs, out);

    blurScript.setRadius(radius);
    blurScript.setInput(allocationIn);
    blurScript.forEach(allocationOut);
    allocationOut.copyTo(out);

    allocationIn.destroy();
    allocationOut.destroy();
    blurScript.destroy();
    rs.destroy();

    return out;
}
 
Example #16
Source File: ImageHelper.java    From FrostyBackgroundTestApp with Apache License 2.0 5 votes vote down vote up
public static void blurBitmapWithRenderscript(RenderScript rs, Bitmap bitmap2) {
    //this will blur the bitmapOriginal with a radius of 25 and save it in bitmapOriginal
    final Allocation input = Allocation.createFromBitmap(rs, bitmap2); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    // must be >0 and <= 25
    script.setRadius(25f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap2);
}
 
Example #17
Source File: RenderScriptGaussianBlur.java    From AndroidMaterialDesign with Apache License 2.0 5 votes vote down vote up
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    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(bitmapOriginal);
    return bitmapOriginal;
}
 
Example #18
Source File: RSGaussianBlur.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@Override
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    radius = Math.min(radius,25);

    final Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    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(bitmapOriginal);
    return bitmapOriginal;
}
 
Example #19
Source File: FastStyleModel.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public Bitmap processImage(Bitmap bitmap) {
    if (!mLoaded) {
        try {
            loadModel();
        } catch (IOException e) {

        }
    }
    // convert the bitmap to RGB * (h * w);
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    // Crop the image.
    Bitmap outImgBig = Bitmap.createBitmap(bitmap, (width - MAX_IMG_SIZE) / 2,
            (height - MAX_IMG_SIZE) / 2, MAX_IMG_SIZE, MAX_IMG_SIZE);
    // Process the cropped image through the neural net.
    Allocation outImgBigAlloc = processImgChunk(outImgBig);

    // Blur the output image a bit.
    Allocation blurredAlloc = Allocation.createFromBitmap(mRS, outImgBig);
    ScriptIntrinsicBlur mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mBlur.setInput(outImgBigAlloc);
    mBlur.setRadius(1.5f);
    mBlur.forEach(blurredAlloc);
    blurredAlloc.copyTo(outImgBig);

    logBenchmarkResult();
    return outImgBig;
}
 
Example #20
Source File: BlurBuilder.java    From talk-android with MIT License 5 votes vote down vote up
public static void RsBlur(RenderScript rs, Bitmap original, float radius) {
    // use this constructor for best performance, because it uses USAGE_SHARED mode which
    // reuses memory
    final Allocation input = Allocation.createFromBitmap(rs, original);
    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(original);
    rs.destroy();
}
 
Example #21
Source File: BlurBuilder.java    From talk-android with MIT License 5 votes vote down vote up
public static void RsBlur(RenderScript rs, Bitmap original) {
    // use this constructor for best performance, because it uses USAGE_SHARED mode which
    // reuses memory
    final Allocation input = Allocation.createFromBitmap(rs, original);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(original);
    rs.destroy();
}
 
Example #22
Source File: BlurTransformation.java    From Dota2Helper with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    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);

    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();

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example #23
Source File: BlurBitmapUtil.java    From Recyclerview-Gallery with Apache License 2.0 5 votes vote down vote up
/**
 * 模糊图片的具体方法
 *
 * @param context 上下文对象
 * @param image   需要模糊的图片
 * @return 模糊处理后的图片
 */
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
    // 计算图片缩小后的长宽
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    // 将缩小后的图片做为预渲染的图片
    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    // 创建一张渲染后的输出图片
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    // 创建RenderScript内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(blurRadius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);

    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example #24
Source File: BlurTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(
            rs, ain.getElement());
    blurScript.setInput(ain);
    blurScript.setRadius(RADIUS);
    blurScript.forEach(aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example #25
Source File: Blur.java    From easyrs with MIT License 5 votes vote down vote up
@Override
public void runScript(RSToolboxContext rsToolboxContext, Allocation aout, BlurParams scriptParams) {
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(
            rsToolboxContext.rs, rsToolboxContext.ain.getElement());
    blurScript.setInput(rsToolboxContext.ain);
    blurScript.setRadius(scriptParams.radius);
    blurScript.forEach(aout);
}
 
Example #26
Source File: BlurTransformation.java    From GracefulMovies with Apache License 2.0 4 votes vote down vote up
/**
     * 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 {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;
        }
    }
 
Example #27
Source File: GBlurPic.java    From AndroidGaussianBlur with MIT License 4 votes vote down vote up
public GBlurPic(Context context) {
	super();
	this.mRS = RenderScript.create(context);
	this.mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
}
 
Example #28
Source File: FastStyleModelTiled.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
public Bitmap processImage(Bitmap bitmap) {
    ScriptC_network script = new ScriptC_network(mRS);

    int numElements = 100;
    Element floatElement = Element.I32(mRS);
    Type arrayType = Type.createX(mRS, floatElement,  numElements);
    Allocation inputAlloc = Allocation.createTyped(mRS, arrayType);
    Allocation outputAlloc = Allocation.createTyped(mRS, arrayType);

    script.forEach_mapper(inputAlloc, outputAlloc);

    int[] output = new int[numElements];
    outputAlloc.copyTo(output);
    Log.i(TAG, output[0] + " " + output[99]);


    if (!mLoaded) {
        try {
            loadModel();
        } catch (IOException e) {

        }
    }
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    // Crop the image.
    Bitmap outImgBig = Bitmap.createBitmap(bitmap, (width - MAX_IMG_SIZE) / 2,
            (height - MAX_IMG_SIZE) / 2, MAX_IMG_SIZE, MAX_IMG_SIZE);
    // Process the cropped image through the neural net.
    Allocation outImgBigAlloc = processImgChunk(outImgBig);

    // Blur the output image a bit.
    Allocation blurredAlloc = Allocation.createFromBitmap(mRS, outImgBig);
    ScriptIntrinsicBlur mBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mBlur.setInput(outImgBigAlloc);
    mBlur.setRadius(1.5f);
    mBlur.forEach(blurredAlloc);
    blurredAlloc.copyTo(outImgBig);

    // outImgBigAlloc.copyTo(outImgBig);
    ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS));
    float[] matrix_sharpen =
                    { 0, -1, 0,
                     -1, 5, -1,
                      0, -1, 0};
    convolution.setInput(blurredAlloc);
    convolution.setCoefficients(matrix_sharpen);
    convolution.forEach(outImgBigAlloc);
    outImgBigAlloc.copyTo(outImgBig);

    logBenchmarkResult();
    return outImgBig;
}
 
Example #29
Source File: BlurFilter.java    From ImageCropRotateFilter with MIT License 4 votes vote down vote up
@Override
public void createFilter(Resources res) {
    mIntrinsic = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    mIntrinsic.setInput(mInPixelsAllocation);
    mIntrinsic.setRadius(mRadius);
}
 
Example #30
Source File: RenderScriptBlur.java    From EtsyBlur with Apache License 2.0 4 votes vote down vote up
public RenderScriptBlur(@NonNull Context context, @NonNull BlurConfig blurConfig) {
    super(blurConfig);
    rs = RenderScript.create(context);
    scriptBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
}