-1

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?

2 Answers2

3

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.

Miloslaw Smyk
  • 1,305
  • 10
  • 15
0

Also I would recommend not to use equal weights for r, g and b but the weights as below. See e.g. http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/

char4 __attribute__((kernel)) gray(uchar4 in) {
uchar4 out;
float gr= 0.2125*in.r + 0.7154*in.g + 0.0721*in.b;
   out.r = out.g = out.b = gr;
   out.a = in.a;
return out;
}
Settembrini
  • 1,366
  • 3
  • 20
  • 32