在NDK中处理RGB_565位图

时间:2016-07-23 12:41:59

标签: android c++ bitmap android-ndk java-native-interface

我正面临处理RGB_565位图的问题。我的代码适用于ARGB_8888: 以下是我用于ARGB_8888的一些代码片段(工作正常):

underlineColorAndroid: 'transparent'

我得到的结果是 ARGB_8888 results

但是在尝试RGB_565格式时:

typedef struct
{

    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} argb;
.....
.....        
void* pixelscolor;
    int ret;
    int y;
    int x;
    uint32_t *pixel;


    if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
        //return null;
    }

    if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
    }
    int width = infocolor.width;
    int height = infocolor.height;

    for (y = 0; y <  height; y++) {
        argb * line = (argb *) pixelscolor;
        for (int n = 0; n < width; n++) {
            int newValue = line[n].alpha+line[n].red+line[n].green+line[n].blue;
......
....

我得到以下结果:RGB_565 result

1 个答案:

答案 0 :(得分:1)

RGB_565每像素只使用2个字节,即16位:

 1         1
 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   red   |   green   |  blue   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

因此,要访问单独的颜色通道,您可以使用下一个代码:

uint16_t u16_pix;

red   = (u16_pix >> 11) & 0x1f;
green = (u16_pix >>  5) & 0x3f;
blue  = (u16_pix >>  0) & 0x1f;

设置它们:

u16_pix = (red << 11) | (green << 5) | (blue);

请注意,您必须确保颜色通道值必须符合其限制,即

red:   0 to 31
green: 0 to 63
blue:  0 to 31