Java Code Examples for android.support.v8.renderscript.Allocation#copyTo()

The following examples show how to use android.support.v8.renderscript.Allocation#copyTo() . 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: 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 2
Source File: RSBox5x5Blur.java    From BlurView with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    radius = Math.min(radius,25);

    Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicConvolve5x5 script = ScriptIntrinsicConvolve5x5.create(rs, Element.U8_4(rs));
    script.setCoefficients(BlurKernels.BOX_5x5);
    for (int i = 0; i < radius; i++) {
        script.setInput(input);
        script.forEach(output);
        input = output;
    }
    output.copyTo(bitmapOriginal);
    return bitmapOriginal;
}
 
Example 3
Source File: LutTest.java    From easyrs with MIT License 6 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());

    ScriptIntrinsicLUT lutScript = ScriptIntrinsicLUT.create(rs, ain.getElement());
    for (int i = 0; i < LutParams.LUT_SIZE; i++) {
        LutParams.RGBALut rgbaLut = SampleParams.Lut.negative();
        lutScript.setAlpha(i, rgbaLut.aLut[i]);
        lutScript.setRed(i, rgbaLut.rLut[i]);
        lutScript.setGreen(i, rgbaLut.gLut[i]);
        lutScript.setBlue(i, rgbaLut.bLut[i]);
    }
    lutScript.forEach(ain, aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 4
Source File: Resize.java    From easyrs with MIT License 6 votes vote down vote up
/**
 * Resizes a Bitmap image to a target width and height.
 */
public static Bitmap resize(RenderScript rs, Bitmap inputBitmap, int targetWidth,
                            int targetHeight) {
    RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, inputBitmap);
    Bitmap.Config config = inputBitmap.getConfig();
    Bitmap outputBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
    Type outType = Type.createXY(bitmapRSContext.rs, bitmapRSContext.ain.getElement(), targetWidth,
            targetHeight);
    Allocation aout = Allocation.createTyped(bitmapRSContext.rs, outType);

    ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(bitmapRSContext.rs);
    resizeScript.setInput(bitmapRSContext.ain);
    resizeScript.forEach_bicubic(aout);

    aout.copyTo(outputBitmap);
    return outputBitmap;
}
 
Example 5
Source File: RSBox3x3Blur.java    From BlurView with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap blur(int radius, Bitmap bitmapOriginal) {
    radius = Math.min(radius,25);

    Allocation input = Allocation.createFromBitmap(rs, bitmapOriginal);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicConvolve3x3 script = ScriptIntrinsicConvolve3x3.create(rs, Element.U8_4(rs));
    script.setCoefficients(BlurKernels.BOX_3x3);
    for (int i = 0; i < radius; i++) {
        script.setInput(input);
        script.forEach(output);
        input = output;
    }
    output.copyTo(bitmapOriginal);
    return bitmapOriginal;
}
 
Example 6
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 7
Source File: ConvolveTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap3x3(RenderScript rs, Bitmap bmpFromNv21) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsicConvolve3x3 convolve3x3Script = ScriptIntrinsicConvolve3x3.create(rs, ain.getElement());
    convolve3x3Script.setInput(ain);
    convolve3x3Script.setCoefficients(SampleParams.Convolve.Kernels3x3.SOBEL_X);
    convolve3x3Script.forEach(aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 8
Source File: ColorMatrixTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21, Op op) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsicColorMatrix colorMatrixScript = ScriptIntrinsicColorMatrix.create(
            rs, ain.getElement());
    if (op == Op.GRAYSCALE)
        colorMatrixScript.setGreyscale();
    if (op == Op.RGB_TO_YUV) {
        colorMatrixScript.setRGBtoYUV();
        colorMatrixScript.setAdd(0.0f, 0.5f, 0.5f, 0.0f);
    }
    if (op == Op.MATRIX3F) {
        colorMatrixScript.setColorMatrix(MATRIX3F);
        colorMatrixScript.setAdd(ADD_TERMS);
    }
    if (op == Op.MATRIX4F) {
        colorMatrixScript.setColorMatrix(MATRIX4F);
        colorMatrixScript.setAdd(ADD_TERMS);
    }

    colorMatrixScript.forEach(ain, aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 9
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 10
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 11
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 12
Source File: ResizeTest.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);
    Bitmap expectedBitmap = Bitmap.createBitmap(TARGET_WIDTH, TARGET_HEIGHT, bmpFromNv21.getConfig());
    Type outType = Type.createXY(rs, ain.getElement(), TARGET_WIDTH, TARGET_HEIGHT);
    Allocation aout = Allocation.createTyped(rs, outType);

    ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(rs);
    resizeScript.setInput(ain);
    resizeScript.forEach_bicubic(aout);

    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 13
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 14
Source File: ConvolveTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap5x5(RenderScript rs, Bitmap bmpFromNv21) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createTyped(rs, ain.getType());

    ScriptIntrinsicConvolve5x5 convolve5x5Script = ScriptIntrinsicConvolve5x5.create(rs, ain.getElement());
    convolve5x5Script.setInput(ain);
    convolve5x5Script.setCoefficients(SampleParams.Convolve.Kernels5x5.SOBEL_X);
    convolve5x5Script.forEach(aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example 15
Source File: RSBlurProcess.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 16
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 17
Source File: Nv21Image.java    From easyrs with MIT License 4 votes vote down vote up
/**
 * Converts an android Bitmap image to NV21 format. If the image has odd dimensions the
 * conversion process will round down each dimension to its closest even integer.
 * @param dstArray is an optional byte array to receive the converted NV21 data. It
 *                 must be (1.5 * number_of_pixels) bytes long. If null is passed,
 *                 a new byte array will be created and returned.
 */
public static Nv21Image bitmapToNV21(RenderScript rs, Bitmap bitmap, byte[] dstArray) {
    long startTime = System.currentTimeMillis();

    Bitmap croppedBitmap = bitmap;

    if (bitmap.getWidth() % 2 > 0 || bitmap.getHeight() % 2 > 0) {
        croppedBitmap = Bitmap.createBitmap(bitmap, 0, 0, (bitmap.getWidth() / 2) * 2,
                (bitmap.getHeight() / 2) * 2);
    }
    Bitmap yuvImage = ColorMatrix.applyMatrix(rs, croppedBitmap,
            ColorMatrixParams.rgbToNv21Matrix(), new Float4(0.0f, 0.5f, 0.5f, 0.0f));

    RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, yuvImage);
    ScriptC_channel channelScript = new ScriptC_channel(bitmapRSContext.rs);
    Type outType = Type.createXY(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs),
            yuvImage.getWidth(), yuvImage.getHeight());
    Allocation aout = Allocation.createTyped(bitmapRSContext.rs, outType);
    channelScript.forEach_channelR(bitmapRSContext.ain, aout);
    int size = croppedBitmap.getWidth() * croppedBitmap.getHeight();

    byte[] yByteArray;
    if (dstArray == null)
        yByteArray = new byte[size + size / 2];
    else
        yByteArray = dstArray;
    aout.copyTo(yByteArray);

    Bitmap.Config config = yuvImage.getConfig();
    Bitmap resizedBmp = Bitmap.createBitmap(yuvImage.getWidth()/2, yuvImage.getHeight()/2, config);
    Type resizeoutType = Type.createXY(bitmapRSContext.rs, bitmapRSContext.ain.getElement(),
            yuvImage.getWidth()/2, yuvImage.getHeight()/2);
    Allocation resizeaout = Allocation.createTyped(bitmapRSContext.rs, resizeoutType);
    ScriptIntrinsicResize resizeScript = ScriptIntrinsicResize.create(bitmapRSContext.rs);
    resizeScript.setInput(bitmapRSContext.ain);
    resizeScript.forEach_bicubic(resizeaout);
    resizeaout.copyTo(resizedBmp);

    Allocation resizedIn = Allocation.createFromBitmap(bitmapRSContext.rs, resizedBmp);
    ScriptC_uvencode encodeScript = new ScriptC_uvencode(bitmapRSContext.rs);
    Type uvtype = Type.createX(bitmapRSContext.rs, Element.U8(bitmapRSContext.rs),
            size / 2);
    Allocation uvAllocation = Allocation.createTyped(bitmapRSContext.rs, uvtype);
    encodeScript.set_width(yuvImage.getWidth());
    encodeScript.set_height(yuvImage.getHeight());
    encodeScript.set_gOut(uvAllocation);
    encodeScript.forEach_root(resizedIn);

    byte[] uvByteArray = new byte[size / 2];

    uvAllocation.copyTo(uvByteArray);
    System.arraycopy(uvByteArray, 0, yByteArray, size, uvByteArray.length);

    Log.d("NV21", "Conversion to NV21: " + (System.currentTimeMillis() - startTime) + "ms");
    return new Nv21Image(yByteArray, yuvImage.getWidth(), yuvImage.getHeight());
}
 
Example 18
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 19
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 20
Source File: ConvertingTool.java    From easyrs with MIT License 3 votes vote down vote up
private Bitmap doComputation(RenderScript rs, Bitmap inputBitmap, Bitmap outputBitmap, T scriptParams) {

        RSToolboxContext rsToolboxContext = RSToolboxContext.createFromBitmap(rs, inputBitmap);

        Allocation aout = Allocation.createTyped(rsToolboxContext.rs, rsToolboxContext.ain.getType());

        this.tool.runScript(rsToolboxContext, aout, scriptParams);

        aout.copyTo(outputBitmap);

        return outputBitmap;
    }