1

I'm working with a simple script in RenderScript. I have to modify the RGBA values on a pixel from a Bitmap. After many attempts I discovered that the Alpha channel doesn't get modified.

I did some researches and I found this old question but I don't understand how and why this happen. Is there a correctly way to modiphy Alpha channel on the script?

Here's a stripped down version of my code:

Java side:

Allocation img= Allocation.createFromBitmap(encodeRS, bmp,Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

RenderScript side:

uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t x, uint32_t y) {

    uchar r= (in.r) & 0xFC;
    uchar g= (in.g) & 0xFC;
    uchar b= (in.b) & 0xFC;
    uchar a= (in.a) & 0xFC;

    return (uchar4) {r,g,b,a};

}

I also tried with memory binding but results are the same:

void root(uchar4* in, uint32_t x, uint32_t y) {

    uchar r= (in->r) & 0xFC;
    uchar g= (in->g) & 0xFC;
    uchar b= (in->b) & 0xFC;
    uchar a= (in->a) & 0xFC;

    in->r= r;
    in->g= g;
    in->b= b;
    in->a= a;

}

Then I do the copyTo from the java side (after the forEach) but the alpha channel is setted automatically to 255.

img.copyTo(bmp);

Thanks anyway for the support.

- Update 1:

I forgot to mention that I get the Bitmap from a File with getAbsolutePath() like this:

Bitmap bmp= BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Community
  • 1
  • 1
gabrielication
  • 115
  • 1
  • 8

1 Answers1

2

Don't know, how your input Bitmap bmp was originally defined, but in order to ensure that the output Bitmap has an editable Alpha channel I would define it explicitely as:

 Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 Allocation img= Allocation.createFromBitmap(rs, outBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

and then, at the end:

img.copyTo(outBitmap);
Settembrini
  • 1,366
  • 3
  • 20
  • 32
  • You want to | Allocation.USAGE_SHARED as well, since it will reduce the number of copies on most bitmaps. – Stephen Hines Apr 29 '16 at 23:09
  • Thanks to both. I get the Bitmap from a path from the SDCARD, like this: Bitmap bmp= BitmapFactory.decodeFile(imgFile.getAbsolutePath()); I'll check with a generic Bitmap created with ARGB_8888 if this causes the issue. – gabrielication Apr 30 '16 at 09:20