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);
        }
Example #2
0
        //Retrieves a blurred image
        public static Drawable Difuminar(Drawable papelTapiz, short blurRadius)
        {
            Bitmap              originalBitmap = ((BitmapDrawable)papelTapiz).Bitmap;
            Bitmap              blurredBitmap  = Bitmap.CreateScaledBitmap(originalBitmap, originalBitmap.Width, originalBitmap.Height, false);
            RenderScript        rs             = RenderScript.Create(Application.Context);
            Allocation          input          = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
            Allocation          output         = Allocation.CreateTyped(rs, input.Type);
            ScriptIntrinsicBlur script         = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));

            script.SetInput(input);
            if (blurRadius < maxRadius)
            {
                script.SetRadius(blurRadius);
            }
            script.ForEach(output);
            output.CopyTo(blurredBitmap);
            Drawable papelTapizDifuminado = new BitmapDrawable(Android.Content.Res.Resources.System, blurredBitmap);

            originalBitmap.Recycle();
            originalBitmap.Dispose();
            blurredBitmap.Recycle();
            blurredBitmap.Dispose();
            input.Dispose();
            output.Dispose();
            return(papelTapizDifuminado);
        }
Example #3
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();
        }
Example #4
0
        public static Bitmap CreateBlurredImageFromBitmap(Context context, int radius, Bitmap image)
        {
            // Load a clean bitmap and work from that.
            Bitmap originalBitmap = image;


            if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1)
            {
                try
                {
                    Bitmap blurredBitmap;
                    blurredBitmap = Bitmap.CreateBitmap(originalBitmap);
                    RenderScript        rs     = RenderScript.Create(context);
                    Allocation          input  = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
                    Allocation          output = Allocation.CreateTyped(rs, input.Type);
                    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
                    script.SetInput(input);
                    script.SetRadius(radius);
                    script.ForEach(output);
                    output.CopyTo(blurredBitmap);
                    output.Dispose();
                    script.Dispose();
                    input.Dispose();
                    rs.Dispose();
                    return(blurredBitmap);
                }
                catch (Exception)
                {
                    return(originalBitmap);
                }
            }
            else
            {
                return(originalBitmap);
            }
        }
Example #5
0
        protected override void OnDraw(Canvas canvas)
        {
            canvas.Save();

            try
            {
                if (rs != null)
                {
                    var mainLayoutRenderer = WebViewPage.Current.MainLayoutRenderer;
                    var sourceView         = mainLayoutRenderer.View;

                    if (lastSourceView != sourceView)
                    {
                        if (lastSourceView != null)
                        {
                            lastSourceView.ViewTreeObserver.PreDraw -= ViewTreeObserver_PreDraw;
                        }

                        sourceView.ViewTreeObserver.PreDraw += ViewTreeObserver_PreDraw;
                        lastSourceView = sourceView;
                    }

                    // Create bitmaps if needed
                    if ((sourceView.Width + 50) != source?.Width || (sourceView.Height + 50) != source?.Height)
                    {
                        sourceCanvas?.Dispose();
                        source?.Dispose();
                        blurred?.Dispose();

                        source = Bitmap.CreateBitmap(sourceView.Width + 50, sourceView.Height + 50, Bitmap.Config.Argb8888);
                        source.EraseColor(Ao3TrackReader.Resources.Colors.Alt.High.ToAndroid());


                        blurred      = Bitmap.CreateBitmap(sourceView.Width + 50, sourceView.Height + 50, Bitmap.Config.Argb8888);
                        sourceCanvas = new Canvas(source);
                    }


                    int[] sourceLoc = new int[2];
                    sourceView.GetLocationInWindow(sourceLoc);

                    int[] destLoc = new int[2];
                    GetLocationInWindow(destLoc);

                    var xOffset = sourceLoc[0] - sourceView.TranslationX - 25 - destLoc[0] + TranslationX;
                    var yOffset = sourceLoc[1] - sourceView.TranslationY - 25 - destLoc[1] + TranslationY;

                    sourceCanvas.Save();
                    sourceCanvas.ClipRect((int)-xOffset, (int)-yOffset, (int)-xOffset + Width + 50, (int)-yOffset + Height + 50);
                    sourceCanvas.Translate(25, 25);
                    sourceView.Draw(sourceCanvas);
                    sourceCanvas.Restore();

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

                    // Load up an instance of the specific script that we want to use.
                    script.SetInput(input);

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


                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                    {
                        var lo = new Script.LaunchOptions();
                        lo.SetX((int)-xOffset, (int)-xOffset + Width);
                        lo.SetY((int)-yOffset, (int)-yOffset + Height);
                        script.ForEach(output, lo);
                    }
                    else
                    {
                        script.ForEach(output);
                    }

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

                    input.Dispose();
                    output.Dispose();

                    canvas.ClipRect(0, 0, Width, Height);
                    canvas.Translate(xOffset, yOffset);
                    canvas.DrawBitmap(blurred, 0, 0, null);
                    canvas.DrawColor(tint);
                }
            }
            catch
            {
                script?.Dispose();
                script = null;
                sourceCanvas?.Dispose();
                sourceCanvas = null;
                source?.Dispose();
                source = null;
                blurred?.Dispose();
                blurred = null;
                rs?.Dispose();
                rs = null;

                useBlur = false;
                App.Database.GetVariableEvents("PaneViewRenderer.useBlur").Updated -= DatabaseVariable_Updated;

                UpdateBackgroundColor();
            }
            canvas.Restore();

            base.OnDraw(canvas);
        }
Example #6
0
 /// <summary>
 /// Frees the large object heap allocation.
 /// </summary>
 public void FreeLargeObject() => _largeObjectAlloc.Dispose();
Example #7
0
    private Allocation _memoryAllocation = new Allocation(0x100000); // 1 MiB

    public void Dispose()
    {
        _memoryAllocation.Dispose();
    }