Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderScript not rendering ScriptIntrinsicBlur correctly, causing the ScriptIntrinsicBlur to render a rainbow of colors

Using the glide android library I get the image as a bitmap (see glide documentation) and then I try to blur the bitmap, using renderscript and ScriptIntrinsicBlur, which is a gaussian blur. (Taken from this stackoverflow post)

 Glide.with(getApplicationContext())
    .load(ImageUrl)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(300,200) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

            RenderScript rs = RenderScript.create(mContext); // context = this. this referring to the activity

            final Allocation input = Allocation.createFromBitmap( rs, resource, 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(8f);
            script.setInput(input);
            script.forEach(output);
            output.copyTo(resource);

            mImageView.setImageBitmap(resource); 
        }
    });

The problem is that this is the output, rather than a blurred image: enter image description here

Any help would be much appreciated thanks. :)

like image 226
HaloMediaz Avatar asked Jul 28 '15 00:07

HaloMediaz


2 Answers

As it's support only U8_4 and U8 format. You'll have to convert your bitmap to ARGB_8888 before you send it to RenderScript by this example.

        Bitmap U8_4Bitmap;
        if(sentBitmap.getConfig() == Bitmap.Config.ARGB_8888) {
            U8_4Bitmap = sentBitmap;
        } else {
            U8_4Bitmap = sentBitmap.copy(Bitmap.Config.ARGB_8888, true);
        }

        //==============================

        Bitmap bitmap = Bitmap.createBitmap(U8_4Bitmap.getWidth(), U8_4Bitmap.getHeight(), U8_4Bitmap.getConfig());

        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs,
                U8_4Bitmap,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);

        final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, output.getElement());
        script.setRadius(radius);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        rs.destroy();
        return bitmap;
like image 150
zinuzoid Avatar answered Nov 12 '22 05:11

zinuzoid


Is it possible that the input image is not a U8_4 (i.e. RGBA8888)? Can you switch from using "Element.U8_4(rs)" to instead use "output.getElement()"? That would probably do the right thing. If it turns out that the image is not RGBA8888, you might at least get a Java exception describing what the underlying format is (if it isn't something supported with our Blur).

like image 35
Stephen Hines Avatar answered Nov 12 '22 04:11

Stephen Hines