Android - Accessing arrays in Renderscript fails

0

i have the following Renderscript code in which I pre-calculate values into arrays R[256], G[256] and B[256]. Those values in these arrays will later looked up by the kernel function. Here is my code(I simplified the code for the sake of brevity):

int rgb[256];
static int R[256];
static int G[256];
static int B[256];

void prepareChannels(){

    for (int i = 0; i < 256; i++) {
        rsDebug("rgb[i]", rgb[i]);
        R[i] = (rgb[i] << 16) & 0x00FF0000;
        rsDebug("R[i]", R[i]); // debugging reveals that here is a value, e.g R[i] 12386304  0xbd0000
        G[i] = (rgb[i] << 8) & 0x0000FF00;
        rsDebug("G[i]", G[i]); // debugging reveals that here is a value, e.g. G[i] 48384  0xbd00
        B[i] = rgb[i] & 0x000000FF;
        rsDebug("B[i]", B[i]); // debugging reveals that here is a value, e.g. B[i] 189  0xbd
    }

}

uchar4 RS_KERNEL process(uchar4 in){

    rsDebug("in", in);  // debugging reveals that here is a value, e.g. in {55, 70, 27, 255}  0x37 0x46 0x1b 0xff

    uchar4 out;
    uchar red = R[in.r];
    uchar green = G[in.g];
    uchar blue = B[in.b];

    out.a = in.a;
    out.r = red;
    out.g = green;
    out.b = blue;

    rsDebug("out", out); // debugging reveals that here is a value, e.g. out {0, 0, 4, 255}  0x0 0x0 0x4 0xff

    return out;
}

As you can see, the R[], G[] and B[] channel values exist. But at the end, in the kernel function, when I read and put them into the out variable's components, then they seem to be all 0 except the component for the blue channel. Here, I give you only 1 pixel data with the incoming rgb values {55, 70, 27, 255} which results in {0, 0, 4, 255} after the lookup, but from my Logcat output, I can say that the red and green channels will be 0ed out and the blue channel will be read as excepted. As result, I can see only the blue components of the original image.

Why is this happening ?

NOTE: from the Java side, I call :

mScript.set_rgb(rgb);
mScript.invoke_prepareChannels();
mScript.forEach_process(mAllocationIn, mAllocationOut);

UPDATE: here is a little GIF so that one can see what is happening(only the blue channel seems to be there): enter image description here

android
lookup-tables
android-renderscript
asked on Stack Overflow Aug 13, 2019 by abdullah celik • edited Aug 13, 2019 by abdullah celik

1 Answer

0

In your kernel, you write

uchar red = R[in.r];

where R is an array of type int. So you are casting an int to uchar and your array should probably not contain any values larger than 255.

In the function prepareChannels, I would try extracting the R values like this:

R[i] = (rgb[i] & 0x00FF0000) >> 16;

and similarly with G and B. It is probably a good idea to change int to uint32_t as well.

answered on Stack Overflow Aug 13, 2019 by spectralio

User contributions licensed under CC BY-SA 3.0