Android Renderscript 设置相邻像素透明

Android Renderscript set neighbor pixel transparent

我有一个脚本,可以清除某些颜色的像素。

uchar red = 100;
uchar green = 100;
uchar blue = 100;
float treshold = 100;

uchar4 __attribute__((kernel)) saturation(uchar4 in,uint32_t x, uint32_t y)
{
    float ddd = ((in.r - red)*(in.r - red) + (in.g - green)*(in.g - green) + (in.b - blue)*(in.b - blue));
    float dif = sqrt( ddd );
    if (dif <= treshold){
        in.a = 0;
        in.r = 0;
        in.g = 0;
        in.b = 0;
    }
    return in;
}

我 运行 在 Java 里:

        mScript.set_red((short)r);
        mScript.set_blue((short)b);
        mScript.set_green((short)g);
        mScript.set_treshold(treshold);
        mScript.forEach_saturation(mInAllocation, mOutAllocations);

它有效,但我需要在 RenderScript 中具有特定颜色像素的清晰像素邻居?在饱和状态下,我们处理每个像素,我不知道如何访问所有像素。

使用全局rs_allocation变量,然后使用rsGetElementAt_uchar4函数在其他位置采样图像:

#pragma rs_fp_relaxed
rs_allocation image;
int width_minus_one;
void RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {
    int newX = min(x + 1, width_minus_one);
    uchar4 pixel = rsGetElementAt_uchar4(image, newX, y);
}

Java:

mScript.set_image(mInAllocation);
mScript.set_width_minus_one(mInAllocation.getType().getX() - 1);