简体   繁体   中英

Algorithm to floodfill bitmap using android Renderscript

I an trying to make an app which fill color to images. It is working fine using Java, but due to some performance issue I want to fill bitmaps using renderscript. I googled lots of things about renderscript but I haven't got anything suitable. Can you please guys guide me how to fill bitmaps using renderscript. Any help will be appreciated. Thanks

The basic thing you'll have to do is create an Allocation for the input Bitmap then a mutable Bitmap and Allocation for the output. Assuming you have an input bitmap called inputBitmap it could look something like this:

private RenderScript        mRsCtx;   //  RenderScript context, created during init
private ScriptC_bitmapFill  mFill;    //  RS kernel instance, created during init
.
.
.
public Bitmap doFill(Bitmap inputBitmap) {

    //  Ensure your input bitmap is also in ARGB8888
    Bitmap  output = Bitmap.createBitmap(inputBitmap.getWidth(),
                                         inputBitmap.getHeight(),
                                         Bitmap.Config.ARGB_8888);
    Allocation  outAlloc = Allocation.createFromBitmap(mRsCtx, output);
    Allocation  inAlloc = Allocation.createFromBitmap(mRsCtx, inputBitmap);

    //  Now call your kernel then copy back the results
    mFill.forEach_root(inAlloc, outAlloc);
    outAlloc.copyTo(outBitmap);
    return outBitmap;
} 

If you are just filling the entire image or even a region, you'll then have a RS kernel which will change the pixel value at specific locations when the kernel is called for it. Here's a very simple RS kernel which just fills the entire image with a solid color:

#pragma version(1)

#pragma rs java_package_name(com.example.bitmapfill)

void root(const uchar4 *v_in, uchar4 *v_out) {
    v_out->r = 0x12;
    v_out->g = 0x34;
    v_out->b = 0x56;
}

Note that since you're not really doing anything with the input allocation/bitmap in this case (just filling the entire thing), you could just leave out the input allocation and use the dimensions. But, if you are only going to manipulate a portion of the input (a small subsection), then you'll have to copy the other pixels from input to output instead of filling.

For additional information about RS and some of its internals, performance, etc. you may find this talk useful: https://youtu.be/3ynA92x8WQo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM