Thilleli
Thilleli

Reputation: 13

Process RGB_565 bitmap in NDK

I'm facing problem processing RGB_565 bitmaps. My code works fine for ARGB_8888: Here are some code snippets I used for ARGB_8888(which works fine):

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;
......
....

I get a result like this ARGB_8888 results.

But when trying the RGB_565 format:

typedef struct
{

    uint8_t red;
    uint8_t green;
    uint8_t blue;

} rgb;
.....
.....        
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++) {
        rgb * line = (rgb *) pixelscolor;
        for (int n = 0; n < width; n++) {
            int newValue = line[n].red+line[n].green+line[n].blue;
......
....

I get the following result:RGB_565 result

Upvotes: 1

Views: 349

Answers (1)

Sergio
Sergio

Reputation: 8209

RGB_565 uses just 2 bytes per pixel i.e. 16 bits:

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

So to access separate color channels you can use next code:

uint16_t u16_pix;

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

To set them:

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

Note, that you must ensure that color channels values must fit into their limits, i.e

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

Upvotes: 1

Related Questions