Java Code Examples for android.renderscript.Allocation#createFromBitmap()

The following examples show how to use android.renderscript.Allocation#createFromBitmap() . 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: BayerScript.java    From retroboy with MIT License 6 votes vote down vote up
@Override
public Bitmap apply(Bitmap input) {
       Allocation imagebuf = Allocation.createFromBitmap(
       	_renderContext, input, 
       	Allocation.MipmapControl.MIPMAP_NONE, 
       	Allocation.USAGE_SCRIPT);

       _filter.set_gIn(imagebuf);
       _filter.set_gOut(imagebuf);
       _filter.set_gScript(_filter);
       _filter.invoke_filter();
       
       // Reuse the input bitmap
       imagebuf.copyTo(input);
       return input;
}
 
Example 2
Source File: RSGaussianBlurTransformation.java    From picasso-transformations with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    if (Build.VERSION.SDK_INT < 17) {
        return source;
    }
    
    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, source, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(mRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(source);
    return source;
}
 
Example 3
Source File: AtkinsonScript.java    From retroboy with MIT License 6 votes vote down vote up
@Override
public Bitmap apply(Bitmap input) {
       Allocation imagebuf = Allocation.createFromBitmap(
       	_renderContext, input, 
       	Allocation.MipmapControl.MIPMAP_NONE, 
       	Allocation.USAGE_SCRIPT);

       _filter.set_gIn(imagebuf);
       _filter.set_gOut(imagebuf);
       _filter.set_gMonoScript(_mono);
       _filter.invoke_filter();
       
       // Reuse the input bitmap
       imagebuf.copyTo(input);
       return input;
}
 
Example 4
Source File: OtsuScript.java    From retroboy with MIT License 6 votes vote down vote up
@Override
public Bitmap apply(Bitmap input) {
       Allocation imagebuf = Allocation.createFromBitmap(
       	_renderContext, input, 
       	Allocation.MipmapControl.MIPMAP_NONE, 
       	Allocation.USAGE_SCRIPT);

       _filter.set_gIn(imagebuf);
       _filter.set_gOut(imagebuf);
       _filter.set_gScript(_filter);
       _filter.invoke_filter();
       
       // Reuse the input bitmap
       imagebuf.copyTo(input);
       return input;
}
 
Example 5
Source File: ImageUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * renderScript模糊图片
 * <p>API大于17</p>
 *
 * @param src     源图片
 * @param radius  模糊半径(0...25)
 * @return 模糊后的图片
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(Utils.getContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation
                .USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
Example 6
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap six(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix6 = ScriptIntrinsicColorMatrix.create(renderScript, Element.U8_4(renderScript));
    colorMatrix6.setColorMatrix(new android.renderscript.Matrix4f(new float[]
            {
                    1.2f, 0.1f, 0.2f, 0.7f,

                    0.7f, 1f, 0f, -0.5f,
                    -0.7f, 0.2f, 0.5f, 1.3f,
                    0, -0.1f, 0f, 0.9f
            }));
    colorMatrix6.forEach(inputAllocation, outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 7
Source File: BlurTransformation.java    From XKnife-Android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@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 8
Source File: TicketView.java    From TicketView with Apache License 2.0 5 votes vote down vote up
private void generateShadow() {
    if (isJellyBeanAndAbove() && !isInEditMode()) {
        if (mShadowBlurRadius == 0f) return;

        if (mShadow == null) {
            mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8);
        } else {
            mShadow.eraseColor(TRANSPARENT);
        }
        Canvas c = new Canvas(mShadow);
        c.drawPath(mPath, mShadowPaint);
        if (mShowBorder) {
            c.drawPath(mPath, mShadowPaint);
        }
        RenderScript rs = RenderScript.create(getContext());
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs));
        Allocation input = Allocation.createFromBitmap(rs, mShadow);
        Allocation output = Allocation.createTyped(rs, input.getType());
        blur.setRadius(mShadowBlurRadius);
        blur.setInput(input);
        blur.forEach(output);
        output.copyTo(mShadow);
        input.destroy();
        output.destroy();
        blur.destroy();
    }
}
 
Example 9
Source File: BitmapUtils.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlur(Context context, Bitmap sentBitmap, int radius) {
    Bitmap overlay = Bitmap.createBitmap(sentBitmap.getWidth(), sentBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);
    Allocation overlayAlloc = Allocation.createFromBitmap(rs, sentBitmap);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);
    rs.destroy();
    return overlay;
}
 
Example 10
Source File: GaussianBlur.java    From AndroidUI with MIT License 5 votes vote down vote up
/**
 * 通过RenderScript进行图片模糊
 * @param bkg       需要模糊的bitmap
 * @param radius    模糊半径,RenderScript规定范围为[1,25]
 * @param view      显示模糊图片的ImageView
 * @param context   上下文
 * @return          消耗时间,单位毫秒(ms)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static long blurByRenderScript(Bitmap bkg,int radius, ImageView view,Context context)
{
    long startMs = System.currentTimeMillis();
    float scaleFactor = 8;

    int width = (int)(view.getMeasuredWidth()/scaleFactor);
    int height = (int)(view.getMeasuredHeight()/scaleFactor);

    Bitmap overlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.scale(1 / scaleFactor, 1 / scaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bkg, 0, 0, paint);

    RenderScript rs = RenderScript.create(context);

    Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);

    view.setImageBitmap(overlay);

    rs.destroy();

    return System.currentTimeMillis() - startMs;
}
 
Example 11
Source File: ImageUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Return the blur bitmap using render script.
 *
 * @param src     The source of bitmap.
 * @param radius  The radius(0...25).
 * @param recycle True to recycle the source of bitmap, false otherwise.
 * @return the blur bitmap
 */
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src,
                                      @FloatRange(
                                              from = 0, to = 25, fromInclusive = false
                                      ) final float radius,
                                      final boolean recycle) {
    RenderScript rs = null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    try {
        rs = RenderScript.create(Utils.getApp());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs,
                ret,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(ret);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return ret;
}
 
Example 12
Source File: BlurTransformation.java    From AcgClub with MIT License 5 votes vote down vote up
private Bitmap doBlur(Context context, Bitmap bitmap, int radius)
    throws RSRuntimeException {
  RenderScript rs = null;
  Allocation input = null;
  Allocation output = null;
  ScriptIntrinsicBlur blur = null;
  try {
    rs = RenderScript.create(context);
    rs.setMessageHandler(new RenderScript.RSMessageHandler());
    input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT);
    output = Allocation.createTyped(rs, input.getType());
    blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(radius);
    blur.forEach(output);
    output.copyTo(bitmap);
  } finally {
    if (rs != null) {
      rs.destroy();
    }
    if (input != null) {
      input.destroy();
    }
    if (output != null) {
      output.destroy();
    }
    if (blur != null) {
      blur.destroy();
    }
  }

  return bitmap;
}
 
Example 13
Source File: ImageUtils.java    From CameraBlur with Apache License 2.0 5 votes vote down vote up
public static Bitmap RenderBlur(Context context, Bitmap image, int BLUR_RADIUS) {
    Bitmap outputBitmap = Bitmap.createBitmap(image);

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

    return outputBitmap;
}
 
Example 14
Source File: DisplayUtils.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
/**
 * android系统的模糊方法
 *
 * @param bitmap 要模糊的图片
 * @param radius 模糊等级 >=0 && <=25
 */
public static Bitmap blurBitmap(Context context, Bitmap bitmap, int radius) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //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(context);
        //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;
    } else {
        return bitmap;
    }
}
 
Example 15
Source File: BlurEffectRS.java    From media-for-mobile with Apache License 2.0 5 votes vote down vote up
@Override
public void applyEffect(int inputTextureId, long l, float[] floats) {

    TextureRenderer.FillMode prevFillMode = getFillMode();
    setFillMode(TextureRenderer.FillMode.PreserveSize);

    super.applyEffect(inputTextureId, l, floats);

    setFillMode(prevFillMode);

    if (inputResolution.width() != width || inputResolution.height() != height) {
        width = inputResolution.width();
        height = inputResolution.height();
        outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        inputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        allocationOut = Allocation.createFromBitmap(renderScript, outputBitmap);
        buffer = IntBuffer.wrap(new int[width * height]);
    }

    buffer.position(0);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
    buffer.rewind();

    checkGlError("GLES20.glReadPixels");

    applyRenderScriptEffect();

    textureRenderer.drawFrame2D(floats, rsOutTexture, 0f, prevFillMode);
}
 
Example 16
Source File: PhotoFilter.java    From FilterLibrary with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap two(Context context, Bitmap bitmap){
    renderScript=RenderScript.create(context);
    outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    inputAllocation=Allocation.createFromBitmap(renderScript,bitmap);
    outputAllocation=Allocation.createTyped(renderScript,inputAllocation.getType());
    final ScriptIntrinsicColorMatrix colorMatrix2=ScriptIntrinsicColorMatrix.create(renderScript,Element.U8_4(renderScript));
    colorMatrix2.setGreyscale();
    colorMatrix2.forEach(inputAllocation,outputAllocation);
    outputAllocation.copyTo(outBitmap);
    return outBitmap;
}
 
Example 17
Source File: BlurTransformation.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

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

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}
 
Example 18
Source File: BlurTransformation.java    From Social 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 {@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;
        }
    }
 
Example 19
Source File: Healing.java    From style-transfer with Apache License 2.0 4 votes vote down vote up
/**
 * This function only assumes mPointsXY, mPasteOffX, mPasteOffY
 *
 * @param healing
 * @param rs
 * @param image
 */
public void heal_orig(ScriptC_healing healing, RenderScript rs, Bitmap image, Bitmap output) {
    long time = System.nanoTime();
    Type.Builder floatImage = new Type.Builder(rs, Element.F32_3(rs));
    floatImage.setX(mRoiBounds.width());
    floatImage.setY(mRoiBounds.height());

    Bitmap maskBitmap = buildMask(mRoiBounds, mPointsXY);

    Allocation dest1 = Allocation.createTyped(rs, floatImage.create());
    Allocation dest2 = Allocation.createTyped(rs, floatImage.create());
    healing.set_dest1(dest1);
    healing.set_dest2(dest2);

    Bitmap destBitmap = createMutableBitmap(image, mRoiBounds.left, mRoiBounds.top,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation dest_uc4 = Allocation.createFromBitmap(rs, destBitmap);
    healing.forEach_convert_to_f(dest_uc4, dest1);

    Bitmap src = createMutableBitmap(image, mCutOffsetX, mCutOffsetY,
            mRoiBounds.width(), mRoiBounds.height());
    Allocation src_f3 = Allocation.createTyped(rs, floatImage.create());
    Allocation src_uc4 = Allocation.createFromBitmap(rs, src);
    healing.forEach_convert_to_f(src_uc4, src_f3);
    healing.set_src(src_f3);

    Allocation mask = Allocation.createFromBitmap(rs, maskBitmap);
    healing.set_mask(mask);

    Allocation laplace_f3 = Allocation.createTyped(rs, floatImage.create());
    healing.set_laplace(laplace_f3);

    Script.LaunchOptions options = new Script.LaunchOptions();
    options.setX(1, mRoiBounds.width() - 1);
    options.setY(1, mRoiBounds.height() - 1);
    healing.forEach_laplacian(laplace_f3, options);
    healing.forEach_copyMasked(mask, dest1);

    int steps = (int) Math.hypot(mRoiBounds.width(), mRoiBounds.height()); // match RS Single source
    Log.v(TAG, "Healing_orig  :steps = " + steps);
    for (int i = 0; i < steps; i++) {
        healing.forEach_solve1(mask, dest2);
        healing.forEach_solve2(mask, dest1);
    }

    healing.forEach_convert_to_uc(dest1, dest_uc4);
    rs.finish();

    healing.forEach_alphaMask(dest_uc4, dest_uc4);
    rs.finish();

    dest_uc4.copyTo(destBitmap);
    rs.finish();
    destBitmap.setHasAlpha(true);
    rs.finish();
    // build the undo
    mUndoBitmap = Bitmap.createBitmap(mRoiBounds.width(), mRoiBounds.height(),
            Bitmap.Config.ARGB_8888);
    Canvas undoCanvas = new Canvas(mUndoBitmap);
    Rect undoRect = new Rect(0, 0, mRoiBounds.width(), mRoiBounds.height());
    undoCanvas.drawBitmap(output, mRoiBounds, undoRect, null);

    Canvas c = new Canvas(output);
    c.drawBitmap(image, 0, 0, null);
    c.drawBitmap(destBitmap, mRoiBounds.left, mRoiBounds.top, null);
    Log.v(TAG, " time to smart paste = " + (System.nanoTime() - time) / 1E6f + "ms");
}
 
Example 20
Source File: ResizeBitmapHelper.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
static Bitmap resizeNice(@NonNull final RenderScript rs, final Bitmap src, float xScale, float yScale) {
        // Calculate gaussian's radius
        float sigma = (1 / xScale) / (float) Math.PI;
        // https://android.googlesource.com/platform/frameworks/rs/+/master/cpu_ref/rsCpuIntrinsicBlur.cpp
        float radius = 2.5f * sigma/* - 1.5f*/; // Works better that way
        radius = Math.min(25, Math.max(0.0001f, radius));
        Timber.d(">> using sigma=%s for xScale=%s => radius=%s", sigma, xScale, radius);

        // Defensive programming in case the threading/view recycling recycles a bitmap just before that methods is reached
        if (null == src || src.isRecycled()) return src;

        Bitmap.Config bitmapConfig = src.getConfig();
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int dstWidth = Math.round(srcWidth * xScale);
        int dstHeight = Math.round(srcHeight * yScale);
        src.setHasAlpha(false);

        // Gaussian filter
        Allocation tmpIn = Allocation.createFromBitmap(rs, src);
        Allocation tmpFiltered = Allocation.createTyped(rs, tmpIn.getType());
        ScriptIntrinsicBlur blurInstrinsic = ScriptIntrinsicBlur.create(rs, tmpIn.getElement());

        blurInstrinsic.setRadius(radius);
        blurInstrinsic.setInput(tmpIn);
        blurInstrinsic.forEach(tmpFiltered);

        src.recycle();
        tmpIn.destroy();
        blurInstrinsic.destroy();


        // Resize
        Bitmap dst = Bitmap.createBitmap(dstWidth, dstHeight, bitmapConfig);
        Type t = Type.createXY(rs, tmpFiltered.getElement(), dstWidth, dstHeight);
        Allocation tmpOut = Allocation.createTyped(rs, t);
        ScriptIntrinsicResize resizeIntrinsic = ScriptIntrinsicResize.create(rs);

        resizeIntrinsic.setInput(tmpFiltered);
        resizeIntrinsic.forEach_bicubic(tmpOut);
        tmpOut.copyTo(dst);

        tmpFiltered.destroy();
        resizeIntrinsic.destroy();
        tmpOut.destroy();
/*
        // Additional sharpen script just in case (WIP)
        Allocation tmpSharpOut = Allocation.createTyped(rs, t);
        //ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, tmpOut.getElement());
        ScriptIntrinsicConvolve3x3 sharpen = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
        sharpen.setCoefficients(getSharpenCoefficients());
        sharpen.setInput(tmpOut);
        sharpen.forEach(tmpSharpOut);

        tmpSharpOut.copyTo(dst);

        tmpOut.destroy();
        tmpSharpOut.destroy();
        sharpen.destroy();
*/

        return dst;
    }