Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocation.copyTo(Bitmap) corrupting pixel values

I'm new to Renderscript, and am striking issues with my first script. As far as I can see (from debugging statements I've inserted) my code works fine, but the computed values are getting mangled when they are being copied back to the Bitmap by the Allocation.copyTo(Bitmap) method.

I was getting weird colours out, so eventually stripped down my script to this sample which shows the problem:

void root(const uchar4 *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y)
{
   *v_out = rsPackColorTo8888(1.f, 0.f, 0.f, 1.f);

   if (x==0 && y==0) {
      rsDebug("v_out ", v_out->x, v_out->y, v_out->z, v_out->w);
   }
}

Here we are just writing out an opaque red pixel. The debug line seems to print the right value (255 0 0 255) and indeed I get a red pixel in the bitmap.

However if I change the alpha on the red pixel slightly:

*v_out = rsPackColorTo8888(1.f, 0.f, 0.f, 0.998f);

The debug prints (255 0 0 254) which still seems correct, but the final pixel value ends up being (0 0 0 254) ie. black.

Obviously I suspected it was a premulltiplied alpha issue, but my understanding is that the Allocation routines to copy from and to Bitmaps is supposed to handle that for you. At least that's what Chet Haase suggests in this blog post: https://plus.google.com/u/0/+ChetHaase/posts/ef6Deey6xKA.

Also none of the example compute scripts out there seem to mention any issues with pre-multiplied alpha. My script was based on the HelloComputer example from the SDK.

If I am making a mistake, I would love an RS guru to point it out for me.

It's a shame that after 2+ years the documentation for Renderscript is still so poor.

PS. The Bitmaps I'm using are ARGB_888 and I am building and targetting on SDK18 (Android 4.3)

like image 239
Paul LeBeau Avatar asked Oct 22 '22 03:10

Paul LeBeau


1 Answers

The example works fine because the example does not modify alpha.

If you are going to modify alpha and then use the Allocation as a normal bitmap you should return (r*a, g*a, b*a, a).

However, if you were sending the Allocation to a GL surface which is not pre-multiplied, your code would work as-is.

like image 63
R. Jason Sams Avatar answered Oct 24 '22 05:10

R. Jason Sams