Renderscript get RGB from allocation - Android

673 views Asked by At

I currently read rgb pixel from an Allocation Object like this:

float4 elementF4TL = rsUnpackColor8888(*(uchar*)rsGetElementAt(inPixels, x-1, y-1));
uchar4 pixelCharsTL = rsPackColorTo8888(elementF4TL);
float3 pixelTL = convert_float4(pixelCharsTL).rgb;

And then I get the pixelTL.rgb that returns the RGB values I want. Is there an more elegant way of doing this ?

For example, rsUnpackColor8888 takes a uchar4 as a parameter. If I pass a variable that is set from rsGetElementAt_uchar4(...) that returns a uchar4 it breaks.

I do not 100% understand what this line means: *(uchar*)rsGetElementAt(inPixel....

1

There are 1 answers

1
monoeci On BEST ANSWER

You seem to be doing a lot of unnecessary conversion and casting. For example, you're unpacking the 8888 color representation only to pack it again in the next line.

Assuming inPixels is your rs_allocation object for a uchar type image, you can use the normal rsGetElementAt_*(...) functions. You might be able to do something like this:

float3 pixelTL = convert_float4(rsGetElementAt_uchar4(inPixels, x-1, y-1)).rgb;

This function will extract a uchar4 vector at position (x-1, y-1) in the allocation, convert it to a float4 and then the .rgb suffix grabs only the first 3 of those elements to make a float3 which gets assigned to your variable.


And just to answer your original question about *(uchar*)...:

Normally, you'd use a specific type for the function call such as rsGetElementAt_uchar4(...), that then returns the actual uchar4 vector as specified.

However, if you call only the generic rsGetElementAt(...), it returns a void pointer -- a void* -- to the first element. Thus, you must cast that pointer to the data type you need, (uchar*)rsGetElementAt(...) in that snippet, and then you must de-reference it to get the actual value at that pointer's location, *(uchar*)rsGetElementAt(...). But yeah, lot's of back and forth for what can be accomplished much more simply as shown above.