1

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

Jitendra Singh
  • 200
  • 4
  • 17

1 Answers1

0

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

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • thank you for answering, but the main problem is how to write code for bitmapFill.rs. I couldn't find any proper example for writing renderscript code. – Jitendra Singh Sep 04 '18 at 10:41
  • I added a bit more to the answer about the RS content. – Larry Schiefer Sep 04 '18 at 11:03
  • I appreciate your efforts but no solution, Do you know how to pass pixel array to renderscript? – Jitendra Singh Sep 04 '18 at 12:49
  • You'll have to provide much more detail about what you are doing and the problems you are encountering. Other than some possibly syntax issues (the code I provided was not run through any compiler), that all should have worked without any problems for just filling a `Bitmap` with a solid color. I suggest you watch the conf. talk I provided a link for and even pull down the sample code. – Larry Schiefer Sep 04 '18 at 14:10
  • I watched your video. I learnt lot of things from this about Renderscript. But I want to write an algorithm for flood fill, I have a Bitmap and a touchPoint. I am checking all similar points to according to touchPoint. Sir can you please write a floodfill algorithm in renderscript for me. – Jitendra Singh Sep 11 '18 at 08:44
  • Hello sir, can you please answer this [question](https://stackoverflow.com/q/53059470/4845434) – Jitendra Singh Oct 30 '18 at 08:10