按照从网上查找的资料bmp格式的颜色表是不固定的,的确如此,我测试了几个bmp,颜色表各不相同。

不过由windows画图程序生成的256色的bmp试验时,发现颜色表一直相同,用来作为自己生成bmp文件的颜色表模版不错。

代码:

Code Snippet
  1. /*
  2. * =====================================================================================
  3. *       Filename:  PrintColorTable.cpp
  4. *    Description:  Print ColorTable Of Bmp Files
  5. *
  6. *        Version:  1.0
  7. *        Created:  03/09/2013 02:56:28 PM
  8. *
  9. *         Author:  zhy (), [email protected]
  10. * =====================================================================================
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include "Defs.h"
  17. using namespace std;
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21.     if (argc < 2)
  22.         return -1;
  23.  
  24.     FILE* fp = fopen(argv[1], "r");
  25.     if (!fp)
  26.         return -1;
  27.  
  28.     BitMapFileHeader bmfHeader;
  29.     fread(&bmfHeader, sizeof(bmfHeader), 1, fp);
  30.  
  31.     BitMapInfoHeader bmiHeader;
  32.     fread(&bmiHeader, sizeof(bmiHeader), 1, fp);
  33.  
  34.     RGBQuad* colorTable = NULL;
  35.     const int tableCount = pow(2, bmiHeader.biBitCount);
  36.     switch (bmiHeader.biBitCount)
  37.     {
  38.         case 1:
  39.         case 4:
  40.         case 8:
  41.             colorTable = (RGBQuad*)malloc(tableCount*sizeof(RGBQuad));
  42.             break;
  43.         case 24:
  44.             //no colorTable;
  45.             printf("No ColorTable For 24bits-bmp\n");
  46.             return 0;
  47.         default:
  48.             printf("unrecoginzed bitsCount\n");
  49.             return 0;
  50.     }
  51.  
  52.     if (!colorTable)
  53.     {
  54.         printf("malloc error!\n");
  55.         return -1;
  56.     }
  57.  
  58.     fread(colorTable, sizeof(RGBQuad), tableCount, fp);
  59.     for (int i=0; i<tableCount; ++i)
  60.     {
  61.         printf("%02x %02x %02x %02x\n", colorTable[i].rgbBlue, colorTable[i].rgbGreen, colorTable[i].rgbRed, colorTable[i].rgbReserved);
  62.     }
  63.  free(colorTable);
  64.     return 0;
  65. }

 

可以用来打印单色位图,16色位图,256色位图的颜色表,进一步比较相同位不同bmp的颜色表的区别。