Example usage for android.renderscript Allocation createFromBitmap

List of usage examples for android.renderscript Allocation createFromBitmap

Introduction

In this page you can find the example usage for android.renderscript Allocation createFromBitmap.

Prototype

static public Allocation createFromBitmap(RenderScript rs, Bitmap b) 

Source Link

Document

Creates an Allocation from a android.graphics.Bitmap .

Usage

From source file:Main.java

/**
 * Return the histograms for each color channel (interleaved).
 *
 * @param rs a {@link RenderScript} context to use.
 * @param bmap a {@link Bitmap} to generate the histograms for.
 * @return an array containing NUM_CHANNELS * COLOR_BIT_DEPTH histogram bucket values, with
 * the color channels interleaved.//from  w ww .ja v a2 s.c  o  m
 */
public static int[] calcHistograms(RenderScript rs, Bitmap bmap) {
    ScriptIntrinsicHistogram hist = ScriptIntrinsicHistogram.create(rs, Element.U8_4(rs));
    Allocation sums = Allocation.createSized(rs, Element.I32_4(rs), COLOR_BIT_DEPTH);

    // Setup input allocation (ARGB 8888 bitmap).
    Allocation input = Allocation.createFromBitmap(rs, bmap);

    hist.setOutput(sums);
    hist.forEach(input);
    int[] output = new int[COLOR_BIT_DEPTH * NUM_CHANNELS];
    sums.copyTo(output);
    return output;
}

From source file:com.hellofyc.base.util.ViewUtils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
static void blur(Context context, View view) {
    view.buildDrawingCache();// w  w w  .  ja v  a  2s. com
    Bitmap srcBitmap = view.getDrawingCache();
    float radius = 20f;

    Bitmap overlay = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft(), -view.getTop());
    canvas.drawBitmap(srcBitmap, 0, 0, null);

    RenderScript script = RenderScript.create(context);
    Allocation allocation = Allocation.createFromBitmap(script, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(script, allocation.getElement());
    blur.setInput(allocation);
    blur.setRadius(radius);
    blur.forEach(allocation);
    allocation.copyTo(overlay);

    view.setBackground(new BitmapDrawable(context.getResources(), overlay));

    script.destroy();
}

From source file:com.truebanana.bitmap.BitmapUtils.java

@SuppressLint("NewApi")
public static Bitmap blur(Context context, Bitmap bitmap, float scale, int radius) {
    if (SystemUtils.getSDKVersion() >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);

        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        Bitmap processedBitmap = Bitmap.createBitmap(bitmap);

        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, processedBitmap);
        sib.setRadius(radius);/*from   w w w  .j av a2  s  . c  o  m*/
        sib.setInput(tmpIn);
        sib.forEach(tmpOut);
        tmpOut.copyTo(processedBitmap);

        return processedBitmap;
    } else {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        Bitmap processedBitmap = bitmap.copy(bitmap.getConfig(), true);

        if (radius < 1) {
            return null;
        }

        int w = processedBitmap.getWidth();
        int h = processedBitmap.getHeight();

        int[] pix = new int[w * h];
        processedBitmap.getPixels(pix, 0, w, 0, 0, w, h);

        int wm = w - 1;
        int hm = h - 1;
        int wh = w * h;
        int div = radius + radius + 1;

        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
        int vmin[] = new int[Math.max(w, h)];

        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int dv[] = new int[256 * divsum];
        for (i = 0; i < 256 * divsum; i++) {
            dv[i] = (i / divsum);
        }

        yw = yi = 0;

        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] sir;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;

        for (y = 0; y < h; y++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = pix[yi + Math.min(wm, Math.max(i, 0))];
                sir = stack[i + radius];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += sir[0] * rbs;
                gsum += sir[1] * rbs;
                bsum += sir[2] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
            }
            stackpointer = radius;

            for (x = 0; x < w; x++) {
                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (y == 0) {
                    vmin[x] = Math.min(x + radius + 1, wm);
                }
                p = pix[yw + vmin[x]];

                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[(stackpointer) % div];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi++;
            }
            yw += w;
        }
        for (x = 0; x < w; x++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * w;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + x;

                sir = stack[i + radius];

                sir[0] = r[yi];
                sir[1] = g[yi];
                sir[2] = b[yi];

                rbs = r1 - Math.abs(i);

                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;

                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }

                if (i < hm) {
                    yp += w;
                }
            }
            yi = x;
            stackpointer = radius;
            for (y = 0; y < h; y++) {
                // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (x == 0) {
                    vmin[y] = Math.min(y + r1, hm) * w;
                }
                p = x + vmin[y];

                sir[0] = r[p];
                sir[1] = g[p];
                sir[2] = b[p];

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[stackpointer];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi += w;
            }
        }
        processedBitmap.setPixels(pix, 0, w, 0, 0, w, h);

        return processedBitmap;
    }
}

From source file:com.slownet5.pgprootexplorer.filemanager.ui.TabsFragmentOne.java

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 = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        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);// w  ww. j  ava 2  s  . c o  m
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
    } else {
        outputBitmap = BlurHelper.doBlur(image, (int) BLUR_RADIUS, false);
    }

    return outputBitmap;

}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap blurRenderScript(Bitmap bmImage, int radius, View v) {
    final float BITMAP_SCALE = 0.4f;
    final float BLUR_RADIUS = 7.5f;

    int width = Math.round(bmImage.getWidth() * BITMAP_SCALE);
    int height = Math.round(bmImage.getHeight() * BITMAP_SCALE);

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

    RenderScript rs = RenderScript.create(v.getContext());
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
    //Allocation tmpIn = Allocation.createFromBitmap(rs, bmImage,
    //      Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    //Allocation tmpOut = Allocation.createTyped(rs, tmpIn.getType());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    theIntrinsic.setRadius(radius);//w w w.j av a  2s.c  o m
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public Image gaussianBlurImage(Image image, float radius) {
    try {// w  w w . j a  v  a 2s.c o  m
        Bitmap outputBitmap = Bitmap.createBitmap((Bitmap) image.getImage());

        RenderScript rs = RenderScript.create(getContext());
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, (Bitmap) image.getImage());
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(radius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return new NativeImage(outputBitmap);
    } catch (Throwable t) {
        brokenGaussian = true;
        return image;
    }
}