Ejemplo n.º 1
0
        public Bitmap Blur(Bitmap bitmap, float blurRadius)
        {
            // Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag
            using (var inAllocation = Allocation.CreateFromBitmap(_renderScript, bitmap)) {
                if (!CanReuseAllocation(bitmap))
                {
                    if (_outAllocation != null)
                    {
                        _outAllocation.Destroy();
                        _outAllocation.Dispose();
                    }

                    _outAllocation    = Allocation.CreateTyped(_renderScript, inAllocation.Type);
                    _lastBitmapWidth  = bitmap.Width;
                    _lastBitmapHeight = bitmap.Height;
                }

                _blurScript.SetRadius(blurRadius);
                _blurScript.SetInput(inAllocation);
                // Do not use inAllocation in forEach, it will caue visual artifacts on blurred bitmap
                _blurScript.ForEach(_outAllocation);
                _outAllocation.CopyTo(bitmap);

                inAllocation.Destroy();
            }

            return(bitmap);
        }
Ejemplo n.º 2
0
        private void YUV_420_888_toRGBIntrinsicsProcess(int width, int height, byte[] yuv)
        {
            if (yuv.Length != _yuvLength)
            {
                Android.Renderscripts.Type.Builder yuvType =
                    new Android.Renderscripts.Type.Builder(rs, Element.U8(rs)).SetX(yuv.Length);
                if (_input != null)
                {
                    _input.Destroy();
                }
                _input     = Allocation.CreateTyped(rs, yuvType.Create(), AllocationUsage.Script);
                _yuvLength = yuv.Length;
            }

            if (_width != width || _height != height)
            {
                Android.Renderscripts.Type.Builder rgbaType = new Android.Renderscripts.Type.Builder(rs, Element.RGBA_8888(rs)).SetX(width).SetY(height);
                if (_output != null)
                {
                    _output.Destroy();
                }
                _output = Allocation.CreateTyped(rs, rgbaType.Create(), AllocationUsage.Script);
                _width  = width;
                _height = height;
            }


            _input.CopyFromUnchecked(yuv);

            _yuvToRgbIntrinsic.SetInput(_input);
            _yuvToRgbIntrinsic.ForEach(_output);
        }
Ejemplo n.º 3
0
        public void Release()
        {
            if (_mBlurInput != null)
            {
                _mBlurInput.Destroy();
                _mBlurInput = null;
            }

            if (_mBlurOutput != null)
            {
                _mBlurOutput.Destroy();
                _mBlurOutput = null;
            }

            if (_mBlurScript != null)
            {
                _mBlurScript.Destroy();
                _mBlurScript = null;
            }

            if (_mRenderScript != null)
            {
                _mRenderScript.Destroy();
                _mRenderScript = null;
            }
        }
Ejemplo n.º 4
0
        private void ReleaseBitmap()
        {
            if (mBlurInput != null)
            {
                mBlurInput.Destroy();
                mBlurInput = null;
            }

            if (mBlurOutput != null)
            {
                mBlurOutput.Destroy();
                mBlurOutput = null;
            }

            if (mBitmapToBlur != null)
            {
                mBitmapToBlur.Recycle();
                mBitmapToBlur = null;
            }

            if (mBlurredBitmap != null)
            {
                mBlurredBitmap.Recycle();
                mBlurredBitmap = null;
            }
        }
Ejemplo n.º 5
0
        public void Release()
        {
            if (!_mBlurInput.IsNullOrDisposed())
            {
                _mBlurInput.Destroy();
                _mBlurInput = null;
            }

            if (!_mBlurOutput.IsNullOrDisposed())
            {
                _mBlurOutput.Destroy();
                _mBlurOutput = null;
            }

            if (!_mBlurScript.IsNullOrDisposed())
            {
                _mBlurScript.Destroy();
                _mBlurScript = null;
            }

            if (!_mRenderScript.IsNullOrDisposed())
            {
                _mRenderScript.Destroy();
                _mRenderScript = null;
            }
        }
Ejemplo n.º 6
0
    public Bitmap blur(Bitmap bitmap, float radius, int repeat)
    {
        if (!IS_BLUR_SUPPORTED)
        {
            return(null);
        }

        if (radius > MAX_RADIUS)
        {
            radius = MAX_RADIUS;
        }

        int width  = bitmap.Width;
        int height = bitmap.Height;

        // Create allocation type
        Type bitmapType = new Type.Builder(rs, Element.RGBA_8888(rs))
                          .SetX(width)
                          .SetY(height)
                          .SetMipmaps(false) // We are using MipmapControl.MIPMAP_NONE
                          .Create();

        // Create allocation
        Allocation allocation = Allocation.CreateTyped(rs, bitmapType);

        // Create blur script
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

        blurScript.SetRadius(radius);

        // Copy data to allocation
        allocation.CopyFrom(bitmap);

        // set blur script input
        blurScript.SetInput(allocation);

        // invoke the script to blur
        blurScript.ForEach(allocation);

        // Repeat the blur for extra effect
        for (int i = 0; i < repeat; i++)
        {
            blurScript.ForEach(allocation);
        }

        // copy data back to the bitmap
        allocation.CopyTo(bitmap);

        // release memory
        allocation.Destroy();
        blurScript.Destroy();
        allocation = null;
        blurScript = null;

        return(bitmap);
    }
Ejemplo n.º 7
0
        Bitmap CreateBlurredImage(int radius)
        {
            Bitmap originalBitmap      = BitmapFactory.DecodeResource(Resources, Resource.Drawable.imagebackground);
            Bitmap blurredBitmap       = Bitmap.CreateBitmap(originalBitmap);
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));
            Allocation          input  = Allocation.CreateFromBitmap(renderScript, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);

            script.SetInput(input);
            script.SetRadius(radius);
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);

            script.ForEach(output);
            output.CopyTo(blurredBitmap);
            output.Destroy();
            input.Destroy();
            script.Destroy();
            return(blurredBitmap);
        }
Ejemplo n.º 8
0
        private void CreateBlurredImage(int radius)
        {
            // Load a clean bitmap and work from that.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            if (blurredBmp != null)
            {
                blurredBmp.Recycle();
                blurredBmp.Dispose();
                blurredBmp = null;
            }

            // Create another bitmap that will hold the results of the filter.
            blurredBmp = Bitmap.CreateBitmap(originalBitmap);

            // Create the Renderscript instance that will do the work.
            RenderScript rs = RenderScript.Create(this);

            // Allocate memory for Renderscript to work with
            Allocation input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, Allocation.UsageScript);
            Allocation output = Allocation.CreateTyped(rs, input.Type);

            // 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 Renderscript working.
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBmp);

            input.Destroy();
            input.Dispose();

            output.Destroy();
            output.Dispose();
        }
Ejemplo n.º 9
0
        Bitmap CreateBlurredImage(int radius)
        {
            // Load a clean bitmap to work from.
            Bitmap originalBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.dog_and_monkeys);

            // Create another bitmap that will hold the results of the filter.
            Bitmap blurredBitmap = Bitmap.CreateBitmap(originalBitmap);

            // Load up an instance of the specific script that we want to use.
            // An Element is similar to a C type. The second parameter, Element.U8_4,
            // tells the Allocation is made up of 4 fields of 8 unsigned bits.
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(renderScript, Element.U8_4(renderScript));

            // Create an Allocation for the kernel inputs.
            Allocation input = Allocation.CreateFromBitmap(renderScript, originalBitmap,
                                                           Allocation.MipmapControl.MipmapFull,
                                                           AllocationUsage.Script);

            // Assign the input Allocation to the script.
            script.SetInput(input);

            // Set the blur radius
            script.SetRadius(radius);

            // Finally we need to create an output allocation to hold the output of the Renderscript.
            Allocation output = Allocation.CreateTyped(renderScript, input.Type);

            // Next, run the script. This will run the script over each Element in the Allocation, and copy it's
            // output to the allocation we just created for this purpose.
            script.ForEach(output);

            // Copy the output to the blurred bitmap
            output.CopyTo(blurredBitmap);

            // Cleanup.
            output.Destroy();
            input.Destroy();
            script.Destroy();

            return(blurredBitmap);
        }