0

In my Android code, I keep an array of colours I would like to use in my renderscript code as follows:

for(int col = 0; col < imgWidth; col++){
   const uchar4 colour = *(const uchar4*)rsGetElementAt(colours, col, 0);
     if (in.r == colour.r && in.g == colour.g && in.b == colour.b){
        in.r = 255;
        in.g = 0;
        in.b = 0;
        break;
    } else {
       in.r = 0;
       in.g = 255;
       in.b = 0;
    }

}
return in;

So basically, if the pixel is one of the colours in the array, turn it red. The problem is that I'm struggling to allocate the pixel array. In RS I have:

 rs_allocation colours;
 int imgWidth;

and in java I have:

Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap myBitmap = Bitmap.createBitmap(pickedPanels.size(), 1, conf);
myBitmap.setPixels(myInts, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),0);

final RenderScript rs = RenderScript.create(this);

// The input image.
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);

// The output image.
final Allocation output = Allocation.createTyped(rs, input.getType());
        final ScriptC_singlesource script = new 

ScriptC_singlesource(rs);

// The array of colours.
final Allocation pixels = Allocation.createFromBitmap(rs, myBitmap, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
script.set_image(pixels);
script.set_imgWidth(pickedPanels.size());
script.forEach_root(input, output);
// retrieve output
output.copyTo(bitmap);

        ImageView destim = (ImageView) findViewById (dest);
        destim.setDrawingCacheEnabled(true);
        destim.setImageBitmap(bitmap);

I've read that you cannot have more than 2 allocations. So, I'm guessing that's my problem. SO, I want to try and read the colours in as a uchar4* but I cannot find an example of how to do this.

Zee
  • 1,592
  • 12
  • 26
  • Code looks ok, can you describe what is going wrong is it just the output bitmap is not as you expect? Also, I recommend using rsGetElementAt_uchar4(colours, col, 0) which will be faster and add #pragma rs_fp_relaxed – sakridge Jun 27 '17 at 10:12
  • Hi there!! The problem was that the values were always 0. But, in the end I just broke it up into 3 int arrays, each containing r,g,b and use those and that works fine. – Zee Jun 27 '17 at 12:16

0 Answers0