Android RenderScript - Syntax Error

122 views Asked by At

My current script is:

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

rs_allocation inPixels;
int height;
int width;
int threeBythree[];

void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    float3 pixel = convert_float4(in[0]).rgb;

    if(x==0 || x==width || y==0 || y==height){
        pixel.r = 0;
        pixel.g = 191;
        pixel.b = 255;
    }else{ //do image processing here

        float3 pixelNH = convert_float4(rsGetElementAt_uchar4(inPixels, x+1, y)).rgb;
        float3 pixelNV = convert_float4(rsGetElementAt_uchar4(inPixels, x, y+1)).rgb;

        int grayAvg = (pixel.r + pixel.g + pixel.b)/3;
        int grayAvgNH = (pixelNH.r + pixelNH.g + pixelNH.b)/3;
        int grayAvgNV = (pixelNV.r + pixelNV.g + pixelNV.b)/3;

        int edgeOperatorValue = 2*grayAvg - grayAvgNH - grayAvgNV;

        if(edgeOperatorValue < 0){
            edgeOperatorValue = -1 * edgeOperatorValue;
        };

        pixel.r = edgeOperatorValue;
        pixel.g = edgeOperatorValue;
        pixel.b = edgeOperatorValue;
    };

    out->xyz = convert_uchar3(pixel);
}

after following this advice here: Renderscript, what is the `in` parameter?

I replaced my code with that one (it doesnt matter if they do not do the same thing I just want it to compile for now):

uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {
  // x and y aren't used, so you can remove those from the above signature too.
  uchar4 out;
  float3 pixel = convert_float4(in).rgb;

  pixel.r = (pixel.r + pixel.g + pixel.b)/3;
  // This seems buggy to me below, since pixel.r was just modified.
  // I think you need another temporary variable (assuming you are trying to make this work and getting weird behavior).
  pixel.g = (pixel.r + pixel.g + pixel.b)/3;
  pixel.b = (pixel.r + pixel.g + pixel.b)/3;

  //int topRight
  //float4 f4 = rsUnpackColor8888(*(uchar*)rsGetElementAt(inPixels, x+1, y+1));

  out.xyz = convert_uchar3(pixel);
  return out;
}

and this is what I get now:

/home/Projects/android/bar/app/src/main/rs/edgedetect.rs:8:17: error: expected ';' after top level declarator

What is it complaining about ? (line 8 btw is uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {)

EDIT: In my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        applicationId "foo.bar"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"

        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true //not applicable for rs targetapi 21+
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
2

There are 2 answers

2
Gil Moshayof On

This type of error appears when you use variables which have not been defined. I'll agree that it's not very clear.

If you notice, you defined the following variable:

uchar4 out;

Then later, you use it like this:

out.xyz = convert_uchar3(pixel);

Except, you're trying to access a property of a variable which was never defined.

Try initializing it like this:

uchar4 out = in;

If this gets rid of your error, then this was your problem. If for some reason you cant use out = in, then look into other ways of initializing the out variable.

0
Stephen Hines On

You are using

buildToolsVersion '19.1.0'

which doesn't have headers that support "RS_KERNEL". Try replacing that with

__attribute__((kernel))