Search code examples
variablestypeselementrenderscript

RenderScript Variable types and Element types, simple example


I clearly see the need to deepen my knowledge in RenderScript memory allocation and data types (I'm still confused about the sheer number of data types and finding the correct corresponding types on either side - allocations and elements. (or when to refer the forEach to input, to output or to both, etc.) Therefore I will read and re-read the documentation, which is really not bad - but it needs some time to get the necessary "intuition" how to use it correctly. But for now, please help me with this basic one (and I will return later with hopefully less stupid questions...). I need a very simple kernel that takes an ARGB Color Bitmap and returns an integer Array of gray-values. My attempt was the following:

#pragma version(1)
#pragma rs java_package_name(com.example.xxxx)
#pragma rs_fp_relaxed

uint __attribute__((kernel)) grauInt(uchar4 in) {
uint gr= (uint) (0.2125*in.r + 0.7154*in.g + 0.0721*in.b);
return gr;  
} 

and Java side:

  int[] data1 = new int[width*height];

  ScriptC_gray graysc;
  graysc=new ScriptC_gray(rs);

  Type.Builder TypeOut = new Type.Builder(rs, Element.U8(rs));
  TypeOut.setX(width).setY(height);
  Allocation outAlloc = Allocation.createTyped(rs, TypeOut.create());

  Allocation inAlloc = Allocation.createFromBitmap(rs, bmpfoto1,       
  Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    graysc.forEach_grauInt(inAlloc, outAlloc);
    outAlloc.copyTo(data1);

This crashed with the message cannot locate symbol "convert_uint". What's wrong with this conversion? Is the code otherwise correct?

UPDATE: isn't that ridiculous? I don't get this "easy one" run, even after 2 hours trying. I still struggle with the different Element- and variable-types. Let's recap: Input is a Bitmap. Output is an int[] Array. So, why doesnt it work when I use U8 in the Java-side Out-allocation, createFromBitmap in the Java-side In-allocation, uchar4 as kernel Input and uint as the kernel Output (RSRuntimeException: Type mismatch with U32) ?


Solution

  • There is no convert_uint() function. How about simple casting? Other than that, the code looks alright (assuming width and height have correct values).

    UPDATE: I have just noticed that you allocate Element.I32 (i.e. signed integer type), but return uint from the kernel. These should match. And in any case, unless you need more than 8-bit precision, you should be able to fit your result in U8.

    UPDATE: If you are changing the output type, make sure you change it in all places, e.g. if the kernel returns an uint, the allocation should use U32. If the kernel returns a char, the allocation should use I8. And so on...