android.support.v8.renderscript.RenderScript Java Examples

The following examples show how to use android.support.v8.renderscript.RenderScript. 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: Histogram.java    From easyrs with MIT License 6 votes vote down vote up
/**
 * Computes a luminance histogram of a Bitmap.
 * @return a 256-length int array of integer frequencies at luminance level.
 */
public static int[] luminanceHistogram(RenderScript rs, Bitmap inputBitmap) {
    RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, inputBitmap);
    Allocation aout = Allocation.createSized(bitmapRSContext.rs, Element.I32(bitmapRSContext.rs),
            COLOR_DEPTH);

    ScriptIntrinsicHistogram histogramScript = ScriptIntrinsicHistogram.create(
            bitmapRSContext.rs, bitmapRSContext.ain.getElement());
    histogramScript.setOutput(aout);
    histogramScript.forEach(bitmapRSContext.ain);

    int[] histogram = new int[COLOR_DEPTH];
    aout.copyTo(histogram);

    return histogram;
}
 
Example #2
Source File: ImageUtils.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) {

        RenderScript rs = RenderScript.create(context);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
        Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options);

        final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate);
        final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType());
        final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs));
        script.setRadius(8f);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(blurTemplate);

        return new BitmapDrawable(context.getResources(), blurTemplate);
    }
 
Example #3
Source File: RenderScriptBlur.java    From EtsyBlur with Apache License 2.0 6 votes vote down vote up
public synchronized static boolean isAvailable(@NonNull Context context) {
    if (!isAvailabilityChecked) {
        boolean available = true;
        RenderScript rs = null;
        try {
            rs = RenderScript.create(context);
        } catch (RSRuntimeException e) {
            Log.w(TAG, "Renderscript is not available on this device.");
            available = false;
        } finally {
            if (rs != null) {
                rs.destroy();
            }
            isAvailabilityChecked = true;
            isAvailable = available;
        }
    }
    return isAvailable;
}
 
Example #4
Source File: HistogramTest.java    From easyrs with MIT License 6 votes vote down vote up
@NonNull
private int[] getExpectedHistogram(RenderScript rs, Bitmap bmpFromNv21, Op op) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout;
    int[] histogram;
    if (op == Op.LUMINANCE) {
        aout = Allocation.createSized(rs, Element.I32(rs), Histogram.COLOR_DEPTH);
        histogram = new int[Histogram.COLOR_DEPTH];
    }
    else {
        aout = Allocation.createSized(rs, Element.I32_4(rs), Histogram.COLOR_DEPTH);
        histogram = new int[Histogram.COLOR_DEPTH * Histogram.CHANNELS];
    }

    ScriptIntrinsicHistogram histogramScript = ScriptIntrinsicHistogram.create(
            rs, ain.getElement());
    histogramScript.setOutput(aout);
    histogramScript.forEach(ain);

    aout.copyTo(histogram);
    return histogram;
}
 
Example #5
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 #6
Source File: Histogram.java    From easyrs with MIT License 6 votes vote down vote up
/**
 * Computes a RGBA histogram of a Bitmap.
 * @return a 1024-length int array of integer frequencies at each channel intensity in the
 * following interleaved format [R0,G0,B0,A0,R1,G1,B1,A1...]
 */
public static int[] rgbaHistograms(RenderScript rs, Bitmap inputBitmap) {
    RSToolboxContext bitmapRSContext = RSToolboxContext.createFromBitmap(rs, inputBitmap);
    Allocation aout = Allocation.createSized(bitmapRSContext.rs, Element.I32_4(bitmapRSContext.rs),
            COLOR_DEPTH);

    ScriptIntrinsicHistogram histogramScript = ScriptIntrinsicHistogram.create(
            bitmapRSContext.rs, bitmapRSContext.ain.getElement());
    histogramScript.setOutput(aout);
    histogramScript.forEach(bitmapRSContext.ain);

    int[] histograms = new int[CHANNELS * COLOR_DEPTH];
    aout.copyTo(histograms);

    // RGBA interleaved: [R0,G0,B0,A0,R1,G1,B1,A1...
    return histograms;
}
 
Example #7
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 #8
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 #9
Source File: BlendTest.java    From easyrs with MIT License 5 votes vote down vote up
@NonNull
private Bitmap getExpectedBitmap(RenderScript rs, Bitmap bmpFromNv21, Bitmap bmpToAdd) {
    Allocation ain = Allocation.createFromBitmap(rs, bmpFromNv21);
    Allocation aout = Allocation.createFromBitmap(rs, bmpToAdd);

    ScriptIntrinsicBlend blendScript = ScriptIntrinsicBlend.create(rs, ain.getElement());
    blendScript.forEachAdd(ain, aout);

    Bitmap expectedBitmap = Bitmap.createBitmap(bmpFromNv21.getWidth(), bmpFromNv21.getHeight(), bmpFromNv21.getConfig());
    aout.copyTo(expectedBitmap);
    return expectedBitmap;
}
 
Example #10
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 #11
Source File: CardViewActivity.java    From FrostyBackgroundTestApp with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mBinding = DataBindingUtil.setContentView(this, R.layout.activity_card_view);

  mBinding.scrollView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v,
                               int left, int top,
                               int right, int bottom,
                               int oldLeft, int oldTop,
                               int oldRight, int oldBottom) {
      mBitmap1 = loadBitmap(mBinding.imgBg, mBinding.cardview);
      setBackgroundOnView(mBinding.cardview, mBitmap1);
      mBitmap2 = loadBitmap(mBinding.imgBg, mBinding.cardview2);
      setBackgroundOnView(mBinding.cardview2, mBitmap2);
    }
  });

  mBinding.scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
      if (mBitmap1 != null) {
        mBitmap1.recycle();
      }
      if (mBitmap2 != null) {
        mBitmap2.recycle();
      }
      mBitmap1 = loadBitmap(mBinding.imgBg, mBinding.cardview);
      setBackgroundOnView(mBinding.cardview, mBitmap1);
      mBitmap2 = loadBitmap(mBinding.imgBg, mBinding.cardview2);
      setBackgroundOnView(mBinding.cardview2, mBitmap2);
    }
  });
  rs = RenderScript.create(this);
}
 
Example #12
Source File: BaseFilter.java    From ImageCropRotateFilter with MIT License 5 votes vote down vote up
public void initBaseFilter(Context mContent, RenderScript mRS, int mWidth, int mHeight,
                           Allocation mInPixelsAllocation, Allocation mOutPixelsAllocation) {
    this.mContent = mContent;
    this.mRS = mRS;
    this.mWidth = mWidth;
    this.mHeight = mHeight;
    this.mInPixelsAllocation = mInPixelsAllocation;
    this.mOutPixelsAllocation = mOutPixelsAllocation;

    createFilter(this.mContent.getResources());
}
 
Example #13
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 #14
Source File: ColorMatrix.java    From easyrs with MIT License 5 votes vote down vote up
private static byte[] applyMatrix(RenderScript rs, byte[] nv21ByteArray, int width, int height,
                                  Matrix4f matrix4f, Optional<Float4> addTerms) {
    ConvertingTool<ColorMatrixParams> convertingTool = new ConvertingTool<>(colorMatrixToolScript);
    ColorMatrixParams matrixParam = ColorMatrixParams.createWithMatrix(matrix4f,
            addTerms);
    return convertingTool.doComputation(rs, nv21ByteArray, width, height, matrixParam);
}
 
Example #15
Source File: Lut3DTest.java    From easyrs with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();
    setContext(context);
    createApplication();
    rs = RenderScript.create(getApplication());
}
 
Example #16
Source File: ResidualBlockTiled.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public ResidualBlockTiled(Context ctx, RenderScript rs, int n_in, int n_out) {
    super(ctx, rs);
    this.n_in = n_in;
    this.n_out = n_out;

    double w = Math.sqrt(2);
    c1 = new Convolution2DTiled(ctx, rs, n_in, n_out, ksize, stride, 1);
    c2 = new Convolution2DTiled(ctx, rs, n_out, n_out, ksize, 1, 1);
    b1 = new BatchNormalization(ctx, rs, n_out);
    b2 = new BatchNormalization(ctx, rs, n_out);

    // Initialize the RS kernels;
    mResidualBlock = new ScriptC_residualblock(mRS);
    mActivation = new ScriptC_activation(mRS);
}
 
Example #17
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 #18
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 #19
Source File: LutTest.java    From easyrs with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();
    setContext(context);
    createApplication();
    rs = RenderScript.create(getApplication());
}
 
Example #20
Source File: HistogramTest.java    From easyrs with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();
    setContext(context);
    createApplication();
    rs = RenderScript.create(getApplication());
}
 
Example #21
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 #22
Source File: Convolve.java    From easyrs with MIT License 5 votes vote down vote up
/**
 * Applies a 3x3 convolution to a NV21 image.
 * @param nv21ByteArray the original NV21 byte array.
 * @param width the original NV21 image width.
 * @param height the original NV21 image height.
 * @param coefficients the 3x3 convolution coefficients.
 */
public static byte[] convolve3x3(RenderScript rs, byte[] nv21ByteArray, int width, int height,
                                        float[] coefficients) {
    ConvertingTool<ConvolveParams> convolveTool = new ConvertingTool<>(
            convolveToolScript(convolve3x3Script));
    return convolveTool.doComputation(rs, nv21ByteArray, width, height,
            new ConvolveParams(coefficients));
}
 
Example #23
Source File: ColorMatrix.java    From easyrs with MIT License 5 votes vote down vote up
public static byte[] convertToGrayScale(RenderScript rs, byte[] nv21ByteArray, int width,
                                        int height) {
    ConvertingTool<ColorMatrixParams> convertingTool = new ConvertingTool<>(colorMatrixToolScript);
    return convertingTool.doComputation(rs, nv21ByteArray, width, height,
            ColorMatrixParams.GRAYSCALE);

}
 
Example #24
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 #25
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 #26
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 #27
Source File: MainActivity.java    From renderscript-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize RenderScript.
 *
 * <p>In the sample, it creates RenderScript kernel that performs saturation manipulation.</p>
 */
private void createScript() {
    // Initialize RS
    RenderScript rs = RenderScript.create(this);

    // Allocate buffers
    mInAllocation = Allocation.createFromBitmap(rs, mBitmapIn);
    mOutAllocations = new Allocation[NUM_BITMAPS];
    for (int i = 0; i < NUM_BITMAPS; ++i) {
        mOutAllocations[i] = Allocation.createFromBitmap(rs, mBitmapsOut[i]);
    }

    // Load script
    mScript = new ScriptC_saturation(rs);
}
 
Example #28
Source File: ResizeTest.java    From easyrs with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();
    setContext(context);
    createApplication();
    rs = RenderScript.create(getApplication());
}
 
Example #29
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 #30
Source File: Deconvolution2DTiled.java    From style-transfer with Apache License 2.0 5 votes vote down vote up
public Deconvolution2DTiled(Context ctx, RenderScript rs, int in_channels, int out_channels, int ksize, int stride, int pad) {
    super(ctx, rs);

    this.in_channels = in_channels;
    this.out_channels = out_channels;
    this.ksize = ksize;
    this.stride = stride;
    this.pad = pad;
    // X dimension for W: in_channels * ksize * ksize
    // Y dimension for W: out_channels
    this.W = new float[out_channels * ksize * ksize * in_channels];
    this.b = new float[out_channels];

    // Pad the width of W to be multiple of 8.
    padded_Y_blas = out_channels * ksize * ksize;
    if (padded_Y_blas % 8 > 0) {
        padded_Y_blas = (padded_Y_blas / 8 + 1) * 8;
    }

    // Create Allocations for W and b.
    W_alloc = Allocation.createTyped(mRS,
            Type.createXY(mRS, Element.F32(mRS), in_channels, padded_Y_blas));
    b_alloc = Allocation.createSized(mRS, Element.F32(mRS), out_channels);

    // Initialize the 2D deconvolution kernel;
    mConvovle = new ScriptC_deconvolve2d(mRS);

    // Set the global variables for the RS kernel.
    mConvovle.set_tile_h(TILE_Y);
    mConvovle.set_col_h(TILE_Y);

    mConvovle.set_kernel_h(ksize);
    mConvovle.set_kernel_w(ksize);
    mConvovle.set_step_x(stride);
    mConvovle.set_step_y(stride);
    mConvovle.set_pad_h(pad);
    mConvovle.set_pad_w(pad);
    mConvovle.set_beta_alloc(b_alloc);
}