How can I modify an Android bitmap in the NDK so that I can use it on the Java side?

android, bitmap, c++, java, java-native-interface

Solution

It appears the problem is because of the premultiplied alpha.

Further testing revealed that the r,g,b values get scaled by the alpha so if I pass a bitmap to jni I need to multiply the r,g,b values by 255 and divide them by the alpha to have the real value that I can do alpha-operations with. When I pass them back I have to do the reverse.

For only copying and operations without alpha that is not necessary, as the alpha will be 255 for maximum opacity and * 255 / 255 negates itself.

Problem

I call a C++ function over JNI and pass a RGBA_8888 bitmap, lock it, change the values, unlock it, return and then display it in Java with this C++ code: ``` AndroidBitmap_getInfo(env, map, &info) < 0); AndroidBitmap_lockPixels(env, map, (void**)&pixel); for(i=info.width*info.height-1;i>=0;i--) { pixel[i] = 0xf1f1f1f1; } AndroidBitmap_unlockPixels(env, map); ``` The problem I have is that the bitmaps looks not as I expect it and the pixel values (verified with getPixel) are not the same when I check them in Java from what I set them in C++. When I set the bitmap values to 0xffffffff I get the correct value in Java, but for many others I don't. 0xf1f1f1f1 for example turns into 0xF1FFFFFF. What do I have to do to make it work ? PS: I am using Android 2.3.4

Original source