2012-08-04 61 views
2

我正在嘗試使用androids ndk做一些簡單的圖像過濾,並且似乎在獲取和設置位圖的rgb值時遇到了一些問題。Android NDK設置RGB位圖像素

我已經將所有實際的處理都去掉了,我只是試圖將位圖的每個像素都設置爲紅色,但是我最終得到了藍色圖像。我認爲有一些簡單的,我忽略了,但任何幫助表示讚賞。

static void changeIt(AndroidBitmapInfo* info, void* pixels){ 
int x, y, red, green, blue; 

for (y=0;y<info->height;y++) { 


    uint32_t * line = (uint32_t *)pixels; 
     for (x=0;x<info->width;x++) { 

      //get the values 
      red = (int) ((line[x] & 0xFF0000) >> 16); 
      green = (int)((line[x] & 0x00FF00) >> 8); 
      blue = (int) (line[x] & 0x0000FF); 

      //just set it to all be red for testing 
      red = 255; 
      green = 0; 
      blue = 0; 

      //why is the image totally blue?? 
      line[x] = 
       ((red << 16) & 0xFF0000) | 
       ((green << 8) & 0x00FF00) | 
       (blue & 0x0000FF); 
     } 

     pixels = (char *)pixels + info->stride; 
    } 
} 

我應該如何得到,然後設置每個像素的rgb值?

更新與答案
正如指出的下面似乎小端使用,所以在我的原代碼,我不得不切換紅色和藍色變量:

static void changeIt(AndroidBitmapInfo* info, void* pixels){ 
int x, y, red, green, blue; 

for (y=0;y<info->height;y++) { 


    uint32_t * line = (uint32_t *)pixels; 
     for (x=0;x<info->width;x++) { 

      //get the values 
      blue = (int) ((line[x] & 0xFF0000) >> 16); 
      green = (int)((line[x] & 0x00FF00) >> 8); 
      red = (int) (line[x] & 0x0000FF); 

      //just set it to all be red for testing 
      red = 255; 
      green = 0; 
      blue = 0; 

      //why is the image totally blue?? 
      line[x] = 
       ((blue<< 16) & 0xFF0000) | 
       ((green << 8) & 0x00FF00) | 
       (red & 0x0000FF); 
     } 

     pixels = (char *)pixels + info->stride; 
    } 
} 

回答

2

這取決於像素格式。推測你的位圖是在RGBA中。因此,0x00FF0000對應於字節序列0x00,0x00,0xFF,0x00(little endian),即透明度爲0的藍色。

我不是Android開發人員,所以我不知道是否有輔助函數可以獲取/設置顏色組件,或者如果你必須自己做,基於AndroidBitmapInfo.format字段。你必須閱讀API文檔。

+0

除非我誤會,否則我認爲位圖是ARGB。上面的代碼實際上是C代碼,因爲它是NDK(本地開發工具包)而不是SDK。 – 2012-08-05 02:07:28