0

Can someone provide me some guidelines on how can I port this code to renderscript for better performance.

private void someMethod() {
 for (int i = 0; i < src.rows(); i++) {
  for (int j = 0; j < src.cols(); j++) {
   double hsv[] = src.get(i, j);
   double modifedHSV[] = this.modifyHSV(new Scalar(hsv), selectedRepaintColor, mean);
   res.put(i, j, modifedHSV);
  }
 }
}
private double[] modifyHSV(Scalar hsvImage, Scalar selectedHsv, Scalar mean) {

 Double h_final = hsvImage.val[0] - mean.val[0] + selectedHsv.val[0];
 Double s_final = hsvImage.val[1] - mean.val[1] + selectedHsv.val[1];
 Double v_final = hsvImage.val[2] - mean.val[2] + selectedHsv.val[2];

 h_final = (h_final <= 0) ? h_final + 180 : h_final;
 s_final = (s_final <= 0) ? 0 : s_final;
 v_final = (v_final <= 0) ? 0 : v_final;

 double[] final_hsv = new double[3];
 final_hsv[0] = h_final;
 final_hsv[1] = s_final;
 final_hsv[2] = v_final;
 return final_hsv;
}
Greeen Apple
  • 441
  • 1
  • 4
  • 12
  • You'll have to port this to RS with the appropriate `Allocation` objects and define your own kernel. If you are new to RS, you may find a talk such as [this one](https://youtu.be/3ynA92x8WQo) helpful. – Larry Schiefer Jul 12 '17 at 11:39

1 Answers1

1

You might try something like this:

hsv.rs:

#pragma rs_fp_relaxed
float3 mean;
float3 selectedHsv;
float3 RS_KERNEL process_hsv(float3 input) {
    float3 hsv_final = input - mean + selectedHsv;

    hsv_final.x = (hsv_final.x <= 0.f) ? hsv_final.x + 180.f : hsv_final.x;
    hsv_final.y = (hsv_final.y <= 0.f) ? 0.f : hsv_final.y;
    hsv_final.z = (hsv_final.z <= 0.f) ? 0.f : hsv_final.z;

    return hsv_final;
}

java:

ScriptC_hsv script;
void init() {
    script = new ScriptC_hsv(rs);
}
float[] process_hsv(float[] input_array, Float3 selectedHsv, Float3 mean) {
    script.set_mean(mean);
    script.set_selectedHsv(selectedHsv);
    output_array = new float[size];
    Type t = Type.createXY(rs, Element.F32_3(rs), columns, rows);
    Allocation input = Allocation.createTyped(rs, t, size, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, t, size, Allocation.USAGE_SCRIPT);
    input.copyFrom(input_array);
    script.forEach_process_hsv(input, output);
    output.copyTo(output_array);
    return output_array;
}
sakridge
  • 578
  • 3
  • 9