android.support.v8.renderscript.Element Java Examples

The following examples show how to use android.support.v8.renderscript.Element. 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: 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 #2
Source File: Convolution2DTiled.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public void loadModel(String path) throws IOException {
    mInputStream = mContext.getAssets().open(path + "/W", AssetManager.ACCESS_BUFFER);
    ByteBuffer bb = readInput(mInputStream);
    FloatBuffer.wrap(W).put(bb.asFloatBuffer());

    // padding for GPU BLAS when necessary.
    int W_height_input = in_channels * ksize * ksize;
    if (padded_Y_blas == W_height_input) {
        // If the input width already satisfies the requirement, just copy to the Allocation.
        W_alloc.copyFrom(W);
    } else {
        // If not, a temp allocation needs to be created.
        Allocation input = Allocation.createTyped(mRS,
                Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
        input.copyFrom(W);
        W_alloc.copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
    }

    mInputStream = mContext.getAssets().open(path + "/b", AssetManager.ACCESS_BUFFER);
    bb = readInput(mInputStream);
    FloatBuffer.wrap(b).put(bb.asFloatBuffer());
    b_alloc.copyFrom(b);

    mInputStream.close();
    Log.v(TAG, "Convolution2D loaded: " + b[0]);
}
 
Example #3
Source File: BatchNormalization.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public BatchNormalization(Context ctx, RenderScript rs, int size) {
    super(ctx, rs);

    this.size = size;
    gamma = new float[size];
    beta = new float[size];
    avg_mean = new float[size];
    avg_var = new float[size];

    // Create RS Allocations.
    gamma_alloc = Allocation.createSized(mRS, Element.F32(mRS), size);
    beta_alloc = Allocation.createSized(mRS, Element.F32(mRS), size);
    avg_mean_alloc = Allocation.createSized(mRS, Element.F32(mRS), size);
    avg_var_alloc = Allocation.createSized(mRS, Element.F32(mRS), size);

    // Initialize the BatchNormalization kernel;
    rs_BN = new ScriptC_batchnormalization(mRS);

    // Set the global variables for the RS kernel.
    rs_BN.set_beta_alloc(beta_alloc);
    rs_BN.set_gamma_alloc(gamma_alloc);
    rs_BN.set_mean_alloc(avg_mean_alloc);
    rs_BN.set_var_alloc(avg_var_alloc);
    rs_BN.set_size(size);
}
 
Example #4
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 #5
Source File: Convolution2D.java    From style-transfer with Apache License 2.0 6 votes vote down vote up
public void loadModel(String path) throws IOException {
    mInputStream = mContext.getAssets().open(path + "/W", AssetManager.ACCESS_BUFFER);
    ByteBuffer bb = readInput(mInputStream);
    FloatBuffer.wrap(W).put(bb.asFloatBuffer());

    // padding for GPU BLAS when necessary.
    int W_height_input = in_channels * ksize * ksize;
    if (padded_Y_blas == W_height_input) {
        // If the input width already satisfies the requirement, just copy to the Allocation.
        W_alloc.copyFrom(W);
    } else {
        // If not, a temp allocation needs to be created.
        Allocation input = Allocation.createTyped(mRS,
                Type.createXY(mRS, Element.F32(mRS), W_height_input, out_channels));
        input.copyFrom(W);
        W_alloc.copy2DRangeFrom(0, 0, W_height_input, out_channels, input, 0, 0);
    }

    mInputStream = mContext.getAssets().open(path + "/b", AssetManager.ACCESS_BUFFER);
    bb = readInput(mInputStream);
    FloatBuffer.wrap(b).put(bb.asFloatBuffer());
    b_alloc.copyFrom(b);

    mInputStream.close();
    Log.v(TAG, "Convolution2D loaded: " + b[0]);
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: RSGaussian5x5Blur.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.GAUSSIAN_5x5);
    for (int i = 0; i < radius; i++) {
        script.setInput(input);
        script.forEach(output);
        input = output;
    }
    output.copyTo(bitmapOriginal);
    return bitmapOriginal;
}
 
Example #13
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 #14
Source File: YuvToRgb.java    From easyrs with MIT License 6 votes vote down vote up
/**
 * Converts a NV21 image to a Bitmap.
 * @param nv21Image the NV21 image to convert.
 */
public static Bitmap yuvToRgb(RenderScript rs, Nv21Image nv21Image) {
    long startTime = System.currentTimeMillis();

    Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.U8(rs))
            .setX(nv21Image.nv21ByteArray.length);
    Type yuvType = yuvTypeBuilder.create();
    Allocation yuvAllocation = Allocation.createTyped(rs, yuvType, Allocation.USAGE_SCRIPT);
    yuvAllocation.copyFrom(nv21Image.nv21ByteArray);

    Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
    rgbTypeBuilder.setX(nv21Image.width);
    rgbTypeBuilder.setY(nv21Image.height);
    Allocation rgbAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create());

    ScriptIntrinsicYuvToRGB yuvToRgbScript = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));
    yuvToRgbScript.setInput(yuvAllocation);
    yuvToRgbScript.forEach(rgbAllocation);

    Bitmap bitmap = Bitmap.createBitmap(nv21Image.width, nv21Image.height, Bitmap.Config.ARGB_8888);
    rgbAllocation.copyTo(bitmap);

    Log.d("NV21", "Conversion to Bitmap: " + (System.currentTimeMillis() - startTime) + "ms");
    return bitmap;
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
Source File: RSStackBlur.java    From BlurView with Apache License 2.0 5 votes vote down vote up
@Override
  public Bitmap blur(int radius, Bitmap blurred) {
radius = Math.min(radius,25);

int width = blurred.getWidth();
int height = blurred.getHeight();

ScriptC_stackblur blurScript = new ScriptC_stackblur(_rs, ctx.getResources(), R.raw.stackblur);

Allocation inAllocation = Allocation.createFromBitmap(_rs, blurred);

blurScript.set_gIn(inAllocation);
blurScript.set_width(width);
blurScript.set_height(height);
blurScript.set_radius(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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #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: 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;
}