`

获取位图RGB值的思路

阅读更多

    这是位图的文件结构,具体可查看BMP image format 。位图有文件头、图像的信息头、可选的调色表、数据区四部分组成。
    数据大小为:

              header=14 bytes

              info header=40 bytes

              option palette = 2的bbp次方 * 4

     其中,bpp是指每个像素占用的位数,bits per pixel。        

 

      查找RGB的思路是:根据数据区每个像素的值,直接索引调色表对应RGB即可

      其中,调色表的每一个元素的结构如下:

       unsigned char red;

       unsigned char green;

       unsigned char blue;

       unsigned char rev;    //预留字段

 

       步骤:

       1. 打开文件,读取头信息和文件信息中的planes(判断是否有调色表)、bpp值;

       2. malloc调色表使用空间,将调色表写入;

       3. 读取数据区,索引调色表,取出RGB值。

 

附件说明:

bitmap.rar:是bmp的定义实现,及使用demo。

       以上仅个人学习总结,供大家共同学习、改进!

  • 大小: 2.1 KB
分享到:
评论
1 楼 rogerer 2010-08-27  
        if ((int) chartolong(InfoHead.ciPlanes, 2) == 1) {
                int i, n=pow(2, ciBitCount);
                printf("number of color:%d, bpp is %d\n", n, ciBitCount);
                for (i =0; i < n;i++) {
                        PIXEL pix;
                        rc = fread((char *)&pix, 1, 4, fp);
                        printf("red=%d,green=%d,blue=%d\n", pix.red, pix.green, pix.blue);
                        sleep(2);
                }
        }

相关推荐

Global site tag (gtag.js) - Google Analytics