2012-08-04 63 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