2016-07-23 36 views
1

我正面臨處理RGB_565位圖的問題。我的代碼工作正常ARGB_8888: 這裏有一些代碼段我用ARGB_8888(工作正常):在NDK中處理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; 
...... 
.... 

我得到這樣 ARGB_8888 results結果。

但嘗試RGB_565格式時:

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

我得到以下結果:RGB_565 result

回答

0

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 
+0

謝謝,這樣可以解決重複問題。但結果的質量不如ARGB_8888格式。儘管我的程序從不使用alpha通道。無論如何,非常感謝:) – Thilleli

+0

@Thilleli不客氣:) – Sergio