Example usage for android.renderscript Element RGBA_8888

List of usage examples for android.renderscript Element RGBA_8888

Introduction

In this page you can find the example usage for android.renderscript Element RGBA_8888.

Prototype

public static Element RGBA_8888(RenderScript rs) 

Source Link

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height, Context context) {

    TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap");

    Rect rect = new Rect(0, 0, width, height);

    try {//ww  w .ja  v  a2 s.co  m
        Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV");
        Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB");
        byte[] imageData = nv21;
        if (mRS == null) {
            mRS = RenderScript.create(context);
            mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS));
            Type.Builder tb = new Type.Builder(mRS,
                    Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
            tb.setX(width);
            tb.setY(height);
            tb.setMipmaps(false);
            tb.setYuvFormat(ImageFormat.NV21);
            ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
            timings.addSplit("Prepare for ain");
            Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS));
            tb2.setX(width);
            tb2.setY(height);
            tb2.setMipmaps(false);
            aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
            timings.addSplit("Prepare for aOut");
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            timings.addSplit("Create Bitmap");
        }
        ain.copyFrom(imageData);
        timings.addSplit("ain copyFrom");
        mYuvToRgb.setInput(ain);
        timings.addSplit("setInput ain");
        mYuvToRgb.forEach(aOut);
        timings.addSplit("NV21 to ARGB forEach");
        aOut.copyTo(bitmap);
        timings.addSplit("Allocation to Bitmap");
    } catch (Exception e) {
        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        timings.addSplit("NV21 bytes to YuvImage");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(rect, 90, baos);
        byte[] cur = baos.toByteArray();
        timings.addSplit("YuvImage crop and compress to Jpeg Bytes");

        bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length);
        timings.addSplit("Jpeg Bytes to Bitmap");
    }

    timings.dumpToLog();
    return bitmap;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height, Context context) {

    TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap");

    Rect rect = new Rect(0, 0, width, height);

    try {//from  w w w  .  ja  v  a  2  s  .  c om
        Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV");
        Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB");
        byte[] imageData = nv21;
        if (mRS == null) {
            mRS = RenderScript.create(context);
            mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS));
            Type.Builder tb = new Type.Builder(mRS,
                    Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
            tb.setX(width);
            tb.setY(height);
            tb.setMipmaps(false);
            tb.setYuvFormat(ImageFormat.NV21);
            ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
            timings.addSplit("Prepare for ain");
            Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS));
            tb2.setX(width);
            tb2.setY(height);
            tb2.setMipmaps(false);
            aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
            timings.addSplit("Prepare for aOut");
            bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
            timings.addSplit("Create Bitmap");
        }
        ain.copyFrom(imageData);
        timings.addSplit("ain copyFrom");
        mYuvToRgb.setInput(ain);
        timings.addSplit("setInput ain");
        mYuvToRgb.forEach(aOut);
        timings.addSplit("NV21 to ARGB forEach");
        aOut.copyTo(bitmap);
        timings.addSplit("Allocation to Bitmap");
    } catch (Exception e) {
        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        timings.addSplit("NV21 bytes to YuvImage");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(rect, 90, baos);
        byte[] cur = baos.toByteArray();
        timings.addSplit("YuvImage crop and compress to Jpeg Bytes");

        bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length);
        timings.addSplit("Jpeg Bytes to Bitmap");
    }

    timings.dumpToLog();
    return bitmap;
}

From source file:freed.viewer.stack.StackActivity.java

private void processStack() {
    stackcounter.setText("0/" + filesToStack.length);
    closeButton.setVisibility(View.GONE);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// w ww . j  a  va  2s.  co  m
    BitmapFactory.decodeFile(filesToStack[0], options);
    final int mWidth = options.outWidth;
    final int mHeight = options.outHeight;
    Type.Builder tbIn2 = new Type.Builder(renderScriptHandler.GetRS(),
            Element.RGBA_8888(renderScriptHandler.GetRS()));
    tbIn2.setX(mWidth);
    tbIn2.setY(mHeight);
    renderScriptHandler.SetAllocsTypeBuilder(tbIn2, tbIn2, Allocation.USAGE_SCRIPT, Allocation.USAGE_SCRIPT);

    final Bitmap outputBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);

    renderScriptHandler.freedcamScript.set_Width(mWidth);
    renderScriptHandler.freedcamScript.set_Height(mHeight);
    renderScriptHandler.freedcamScript.set_yuvinput(false);
    renderScriptHandler.freedcamScript.set_gCurrentFrame(renderScriptHandler.GetIn());
    renderScriptHandler.freedcamScript.set_gLastFrame(renderScriptHandler.GetOut());
    if (stackMode == 6) {
        ScriptField_MinMaxPixel medianMinMax = new ScriptField_MinMaxPixel(renderScriptHandler.GetRS(),
                mWidth * mHeight);
        renderScriptHandler.freedcamScript.bind_medianMinMaxPixel(medianMinMax);
    }
    FreeDPool.Execute(new Runnable() {
        @Override
        public void run() {
            int count = 0;
            for (String f : filesToStack) {
                updateCounter(count++);
                BitmapFactory.decodeFile(f, options);
                if (mWidth != options.outWidth || mHeight != options.outHeight)
                    return;
                renderScriptHandler.GetIn().copyFrom(BitmapFactory.decodeFile(f));
                switch (stackMode) {
                case 0: //AVARAGE
                    renderScriptHandler.freedcamScript.forEach_stackimage_avarage(renderScriptHandler.GetOut());
                    break;
                case 1: //AVARAGE1x2
                    renderScriptHandler.freedcamScript
                            .forEach_stackimage_avarage1x2(renderScriptHandler.GetOut());
                    break;
                case 2: //AVARAGE1x3
                    renderScriptHandler.freedcamScript
                            .forEach_stackimage_avarage1x3(renderScriptHandler.GetOut());
                    break;
                case 3: // AVARAGE3x3
                    renderScriptHandler.freedcamScript
                            .forEach_stackimage_avarage3x3(renderScriptHandler.GetOut());
                    break;
                case 4: // LIGHTEN
                    renderScriptHandler.freedcamScript.forEach_stackimage_lighten(renderScriptHandler.GetOut());
                    break;
                case 5: // LIGHTEN_V
                    renderScriptHandler.freedcamScript
                            .forEach_stackimage_lightenV(renderScriptHandler.GetOut());
                    break;
                case 6: //MEDIAN
                    renderScriptHandler.freedcamScript.forEach_stackimage_median(renderScriptHandler.GetOut());
                    break;
                case 7:
                    renderScriptHandler.freedcamScript
                            .forEach_stackimage_exposure(renderScriptHandler.GetOut());
                    break;
                }
                renderScriptHandler.GetOut().copyTo(outputBitmap);
                setBitmapToImageView(outputBitmap);
            }
            File file = new File(filesToStack[0]);
            String parent = file.getParent();
            saveBitmapToFile(outputBitmap,
                    new File(parent + "/" + getStorageHandler().getNewFileDatedName("_Stack.jpg")));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    closeButton.setVisibility(View.VISIBLE);
                }
            });
        }
    });

}