【问题标题】:RenderScript remove pixels with alpha at bitmapRenderScript 删除位图上带有 alpha 的像素
【发布时间】:2020-09-18 08:43:09
【问题描述】:

我有一个位图,我需要删除所有具有 alpha 的像素。听起来很简单,但我还是坚持了下来。 我有这个 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;
}

它工作正常,但速度很慢,处理一张位图大约需要 2 秒。 我正在尝试使用 RenderScript。它工作得很快,但不稳定。 这是我的代码:

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;
}

还有我的 .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;
}
}

所以我在多个位图上使用此方法 - 第一个位图工作正常,但我收到损坏的图像。顺便说一句,当我在第一个位图上再次应用此方法时,它也会损坏它。 看起来没有关闭的内存分配或共享资源,idk。

有什么想法吗? 也许有更简单的解决方案? 提前谢谢大家!

【问题讨论】:

    标签: android image-processing bitmap renderscript


    【解决方案1】:

    实际上,您可以使用 getPixels 方法读取数组中的所有像素,然后对其进行操作。它工作得足够快。问题是 getPixel 工作缓慢。 所以这里是代码:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      在您的 .rs 文件中,我认为 rValue、gValue 等应该是 uchar 类型,而不是 uint32_t。此外,if 语句缺少将 v_in 值复制到 v_out 的 else 子句,否则您将获得未定义的输出值。请注意,在您的 Java 代码中,输出位图被初始化为副本。在 renderscript 代码中不是这种情况,您可以创建与输入相同类型的输出分配,但不会复制值。因此,您需要复制内核中的输入值。

      【讨论】:

        猜你喜欢
        • 2015-04-30
        • 2012-05-30
        • 1970-01-01
        • 2023-04-02
        • 2014-11-28
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多