简体   繁体   English

RenderScript 删除 bitmap 处带有 alpha 的像素

[英]RenderScript remove pixels with alpha at bitmap

I've got a bitmap and I need to remove all pixels that have alpha.我有一个 bitmap,我需要删除所有具有 alpha 的像素。 Sounds easy, but I'm stuck with it.听起来很容易,但我坚持下去。 I've got this Java code:我有这个 Java 代码:

 public static Bitmap overdrawAlphaBits(Bitmap image, int color) {
    Bitmap coloredBitmap = image.copy(Bitmap.Config.ARGB_8888, true);
    for (int y = 0; y < coloredBitmap.getHeight(); y++) {
        for (int x = 0; x < coloredBitmap.getWidth(); x++) {
            int pixel = coloredBitmap.getPixel(x, y);
            if (pixel != 0) {
                coloredBitmap.setPixel(x, y, color);
            }
        }
    }
    return coloredBitmap;
}

And it works fine, but slowly, processing of one bitmap takes around 2 second.它工作正常,但速度很慢,处理一个 bitmap 大约需要 2 秒。 I'my trying with RenderScript.我正在尝试使用 RenderScript。 It works fast, but not stable.它工作得很快,但不稳定。 here is my code:这是我的代码:

public static Bitmap overdrawAlphaBits(Bitmap image, Context context) {
    Bitmap blackbitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), image.getConfig());
    RenderScript mRS = RenderScript.create(context);
    ScriptC_replace_with_main_green_color script = new ScriptC_replace_with_main_green_color(mRS);

    Allocation allocationRaster0 = Allocation.createFromBitmap(mRS, image, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation allocationRaster1 = Allocation.createTyped(mRS, allocationRaster0.getType());
    script.forEach_root(allocationRaster0, allocationRaster1);
    allocationRaster1.copyTo(blackbitmap);
    allocationRaster0.destroy();
    allocationRaster1.destroy();
    script.destroy();
    mRS.destroy();
    return blackbitmap;
}

And my.rs file:和 my.rs 文件:

void root(const uchar4 *v_in, uchar4 *v_out) {
uint32_t rValue = v_in->r;
uint32_t gValue = v_in->g;
uint32_t bValue = v_in->b;
uint32_t aValue = v_in->a;
if(rValue!=0 || gValue!=0 || bValue!=0 || aValue!=0){
   v_out->r = 0x55;
   v_out->g = 0xED;
   v_out->b = 0x69;
}
}

So I use this method on multiple bitmaps - at first bitmap is works fine, but than I receive corrupted images.所以我在多个位图上使用这种方法 - 起初 bitmap 工作正常,但我收到损坏的图像。 By the way when I apply this method again on first bitmap it also corrupts it.顺便说一句,当我在第一个 bitmap 上再次应用此方法时,它也会损坏它。 Looks like there is not closed memory allocation or shared resources, idk.貌似没有关闭 memory 分配或共享资源,idk。

Any ideas, please?请问有什么想法吗? Maybe there is an easier solution?也许有更简单的解决方案? Thanks everyone in advance!提前谢谢大家!

Actually you can use getPixels method to read all pixels in array and than manipulate them.实际上,您可以使用 getPixels 方法读取数组中的所有像素,然后对其进行操作。 It works fast enough.它工作得足够快。 The problem is that getPixel works slow.问题是 getPixel 工作缓慢。 So here is the code:所以这里是代码:

public static Bitmap overdrawAlphaBits(Bitmap image, int color) {
    int[] pixels = new int[image.getHeight() * image.getWidth()];
    image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
    for (int i = 0; i < image.getWidth() * image.getHeight(); i++) {
        if (pixels[i] != 0) {
            pixels[i] = color;
        }
    }
    image.setPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
    return image;
}

In your.rs file, I think the rValue, gValue, etc should be of type uchar, not uint32_t.在 your.rs 文件中,我认为 rValue、gValue 等应该是 uchar 类型,而不是 uint32_t。 Also the if-statement is missing an else-clause where the v_in values are copied to v_out, otherwise you get undefined output values.此外,if 语句缺少将 v_in 值复制到 v_out 的 else 子句,否则您将获得未定义的 output 值。 Note that in your Java code, the output bitmap is initialised as a copy.请注意,在您的 Java 代码中,output bitmap 被初始化为副本。 This is not the case in the renderscript code, where you create an output allocation of the same type as the input, but the values are not copied.在 renderscript 代码中不是这种情况,您在其中创建与输入相同类型的 output 分配,但不会复制值。 Therefore you need to copy the input values in the kernel.因此,您需要复制 kernel 中的输入值。

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

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