Search code examples
renderscript

Renderscript Greyscale not quite working


This is my renderscript code for now:

#pragma version(1)
#pragma rs java_package_name(com.apps.foo.bar)

rs_allocation inPixels;

    uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {

      uchar4 pixel = in.rgba;

      pixel.r = (pixel.r + pixel.g + pixel.b)/3;
      pixel.g = (pixel.r + pixel.g + pixel.b)/3;
      pixel.b = (pixel.r + pixel.g + pixel.b)/3;

      return pixel;
    }

My phone shows a "greyscaled" picture. I say "grayscaled" because red for example, is still kinda red...It is gray-ish but you can still see that is red. I know I can use more sophisticated methods, but I would like to stick to the simple one for now.

I would like to know if my renderscript code is wrong. Should I be converting the char to another type?


Solution

  • Use a temporary variable to hold the result as you compute it. Otherwise, in the first line you're modifying pixel.r, and in the very next one you are using it to calculate pixel.g. No wonder you get artifacts.

    Also, don't forget to assign the alpha value to avoid surprises with "invisible" output.