简体   繁体   English

使用Android Renderscript填充位图的算法

[英]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. 使用Java可以正常工作,但是由于一些性能问题,我想使用renderscript填充位图。 I googled lots of things about renderscript but I haven't got anything suitable. 我用Google搜索了很多关于renderscript的东西,但没有合适的东西。 Can you please guys guide me how to fill bitmaps using renderscript. 能否请大家指导我如何使用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. 您要做的基本事情是为输入Bitmap创建Allocation ,然后为输出创建可变BitmapAllocation Assuming you have an input bitmap called inputBitmap it could look something like this: 假设您有一个名为inputBitmap的输入位图,它可能看起来像这样:

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. 如果您只是填充整个图像甚至一个区域,那么您将拥有一个RS内核,当调用内核时,它将在特定位置更改像素值。 Here's a very simple RS kernel which just fills the entire image with a solid color: 这是一个非常简单的RS内核,它用纯色填充整个图像:

#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 有关RS及其内部,性能等方面的更多信息,您可能会发现本演讲很有用: https : //youtu.be/3ynA92x8WQo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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