Bitmap文件格式+生成一个BMP文件

Posted uTank-木头

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bitmap文件格式+生成一个BMP文件相关的知识,希望对你有一定的参考价值。

 

Bitmap的文件格式:

 1 #define UINT16    unsigned short
 2 #define DWORD    unsigned int
 3 #define WORD    short
 4 #define LONG    int
 5 
 6 // Bitmap File Header ( 14 Bytes )
 7 typedef struct tagBITMAPFILEHEADER 
 8 {  
 9     UINT16 bfType;        // same as BM in ASCII.
10     DWORD bfSize;         // the size of the BMP file in bytes
11     UINT16 bfReserved1; 
12     UINT16 bfReserved2; 
13     DWORD bfOffBits;    // starting address, of the byte where the bitmap image data (Pixel Array) can be found.
14 } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
15 
16 // DIB头描述bitmap的信息,包括大小、压缩类型、颜色格式等,DIB头的大小根据版本不同,大小也不同。
17 typedef struct tagBITMAPINFOHEADER
18 {
19     DWORD biSize;             // 4, the size of this header (40 bytes)
20     LONG biWidth;             // 4, the bitmap width in pixels (signed integer).
21     LONG biHeight;             // 4, the bitmap height in pixels (signed integer).
22     WORD biPlanes;             // 2, the number of color planes being used. Must be set to 1.
23     WORD biBitCount;         // 2, the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
24     DWORD biCompression;     // 4, the compression method being used. 
25     DWORD biSizeImage;         // 4, the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
26     LONG biXPelsPerMeter;     // 4, the horizontal resolution of the image.
27     LONG biYPelsPerMeter;     // 4, the vertical resolution of the image. 
28     DWORD biClrUsed;         // 4, the number of colors in the color palette, or 0 to default to 2^n.
29     DWORD biClrImportant;    // 4, the number of important colors used, or 0 when every color is important; generally ignored.
30 } BITMAPINFOHEADER;

 

生成一个BMP文件:

  1 int bitmap_file_generate( char *name, int width, int height )
  2 {
  3     FILE *fp = NULL;
  4     unsigned char *pData;
  5     unsigned char *pp;
  6     int i, j;
  7     int iFileSize;
  8     
  9     unsigned char bfType1, bfType2;
 10     unsigned int bfSize;
 11     unsigned short bfReserved1, bfReserved2;
 12     unsigned int bfOffBits;
 13     unsigned int biSize;
 14     int biWidth, biHeight;
 15     unsigned short biPlanes;
 16     unsigned short biBitCount;
 17     unsigned int biCompression;
 18     unsigned int biSizeImage;
 19     unsigned int biXPelsPerMeter;
 20     unsigned int biYPelsPerMeter;
 21     unsigned int biClrUsed;
 22     unsigned int biClrImportant;
 23     
 24     // offset[0:1]
 25     bfType1   = B;
 26     bfType2   = M;
 27     // offset[2:5]
 28     bfSize    = width * height * 3 + 54;
 29     // offset[6:7]
 30     bfReserved1 = 0;
 31     // offsset[8:9]
 32     bfReserved2 = 0;
 33     // offset[10:13]
 34     bfOffBits = 54;
 35     // offset[14:17]
 36     biSize = 40;
 37     // offset[18:21]
 38     biWidth = width;
 39     // offset[22:25]
 40     biHeight = height;
 41     // offset[26:27]
 42     biPlanes = 1;
 43     // offset[28:29]
 44     biBitCount = 24;
 45     // offset[30:33]
 46     biCompression = 0;
 47     // offset[34:37]
 48     biSizeImage = width * height * 3;
 49     // offset[38:41]
 50     biXPelsPerMeter = 2835;
 51     // offset[42:45]
 52     biYPelsPerMeter = 2835;
 53     // offset[46:49]
 54     biClrUsed = 0;
 55     // offset[50:53]
 56     biClrImportant = 0;
 57     
 58     
 59     fp = fopen(name, "wb+");
 60     if( fp == NULL )
 61     {
 62         printf("create file err, fopen: %p\n", fp);
 63         return -1;    
 64     }
 65     
 66     pData = (unsigned char *)malloc(biWidth*biHeight*3+54);//malloc(iFileSize);
 67     if(pData == NULL)
 68     {
 69         printf("pData malloc err\n");
 70         return -2;
 71     }
 72     
 73     // same as BM in ASCII.
 74     pData[0] = bfType1;
 75     pData[1] = bfType2;
 76     
 77     // the size of the BMP file in bytes. bmp文件的实际大小
 78     pData[2] = (unsigned char)((bfSize>>0) & 0xff);
 79     pData[3] = (unsigned char)((bfSize>>8) & 0xff);
 80     pData[4] = (unsigned char)((bfSize>>16) & 0xff);    
 81     pData[5] = (unsigned char)((bfSize>>24) & 0xff);
 82     
 83     // reserved
 84     pData[6] = (unsigned char)((bfReserved1>>0) & 0xff);
 85     pData[7] = (unsigned char)((bfReserved1>>8) & 0xff);
 86     // reserved
 87     pData[8] = (unsigned char)((bfReserved2>>0) & 0xff);
 88     pData[9] = (unsigned char)((bfReserved2>>0) & 0xff);
 89     
 90     // the offset, i.e. starting address, of the byte where the bitmap image data (Pixel Array) can be found.
 91     // 颜色数据的偏移地址,实际为:54 = 14 + 40
 92     pData[10] = (unsigned char)((bfOffBits>>0) & 0xff);
 93     pData[11] = (unsigned char)((bfOffBits>>8) & 0xff);
 94     pData[12] = (unsigned char)((bfOffBits>>16) & 0xff);    
 95     pData[13] = (unsigned char)((bfOffBits>>24) & 0xff);
 96     
 97     // the size of this header (40 bytes)
 98     pData[14] = (unsigned char)((biSize>>0) & 0xff);
 99     pData[15] = (unsigned char)((biSize>>8) & 0xff);
100     pData[16] = (unsigned char)((biSize>>16) & 0xff);    
101     pData[17] = (unsigned char)((biSize>>24) & 0xff);
102     
103     // the bitmap width in pixels (signed integer).
104     pData[18] = (unsigned char)((biWidth>>0) & 0xff);
105     pData[19] = (unsigned char)((biWidth>>8) & 0xff);
106     pData[20] = (unsigned char)((biWidth>>16) & 0xff);
107     pData[21] = (unsigned char)((biWidth>>24) & 0xff);
108     
109     // the bitmap height in pixels (signed integer).
110     pData[22] = (unsigned char)((biHeight>>0) & 0xff);
111     pData[23] = (unsigned char)((biHeight>>8) & 0xff);
112     pData[24] = (unsigned char)((biHeight>>16) & 0xff);
113     pData[25] = (unsigned char)((biHeight>>24) & 0xff);
114     
115     // the number of color planes being used. Must be set to 1.
116     pData[26] = (unsigned char)((biPlanes>>0) & 0xff);
117     pData[27] = (unsigned char)((biPlanes>>8) & 0xff);
118     
119     // the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
120     pData[28] = (unsigned char)((biBitCount>>0) & 0xff);
121     pData[29] = (unsigned char)((biBitCount>>8) & 0xff);
122     
123     // the compression method being used. See the next table for a list of possible values.
124     pData[30] = (unsigned char)((biCompression>>0) & 0xff);
125     pData[31] = (unsigned char)((biCompression>>8) & 0xff);
126     pData[32] = (unsigned char)((biCompression>>16) & 0xff);
127     pData[33] = (unsigned char)((biCompression>>24) & 0xff);
128     
129     // the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
130     pData[34] = (unsigned char)((biSizeImage>>0) & 0xff);
131     pData[35] = (unsigned char)((biSizeImage>>8) & 0xff);
132     pData[36] = (unsigned char)((biSizeImage>>16) & 0xff);
133     pData[37] = (unsigned char)((biSizeImage>>24) & 0xff);
134     
135     // the horizontal resolution of the image. (pixel per meter, signed integer)
136     pData[38] = (unsigned char)((biXPelsPerMeter>>0) & 0xff);
137     pData[39] = (unsigned char)((biXPelsPerMeter>>8) & 0xff);
138     pData[40] = (unsigned char)((biXPelsPerMeter>>16) & 0xff);
139     pData[41] = (unsigned char)((biXPelsPerMeter>>24) & 0xff);
140     
141     // the vertical resolution of the image. (pixel per meter, signed integer)
142     pData[42] = (unsigned char)((biYPelsPerMeter>>0) & 0xff);
143     pData[43] = (unsigned char)((biYPelsPerMeter>>8) & 0xff);
144     pData[44] = (unsigned char)((biYPelsPerMeter>>16) & 0xff);
145     pData[45] = (unsigned char)((biYPelsPerMeter>>24) & 0xff);  
146     
147      // the number of colors in the color palette, or 0 to default to 2^n.
148     pData[46] = (unsigned char)((biClrUsed>>0) & 0xff);
149     pData[47] = (unsigned char)((biClrUsed>>8) & 0xff);
150     pData[48] = (unsigned char)((biClrUsed>>16) & 0xff);
151     pData[49] = (unsigned char)((biClrUsed>>24) & 0xff);  
152     
153      // the number of important colors used, or 0 when every color is important; generally ignored.
154     pData[50] = (unsigned char)((biClrImportant>>0) & 0xff);
155     pData[51] = (unsigned char)((biClrImportant>>8) & 0xff);
156     pData[52] = (unsigned char)((biClrImportant>>16) & 0xff);
157     pData[53] = (unsigned char)((biClrImportant>>24) & 0xff);     
158 
159 
160     pp = &pData[54];
161     for(i = 0; i < biHeight; i++)
162     {
163         for(j = 0; j < biWidth; j++)
164         {
165             set_color(pp, biWidth, biHeight, i, j, i+j, i, j);  // 随便填有点颜色
166         }
167     } 
168     
169     iFileSize = fwrite(pData, 1, bfSize, fp);
170     printf("generate file iFileSize: %d\n", iFileSize);
171     
172     free(pData);
173     
174     fclose(fp);
175     
176     return 0;
177 }

以上生成的BMP,坐标起始位置(0,0)为左下角,填充颜色:

1 void set_color( unsigned char *pp, int w, int h, 
2     int x, int y, unsigned char r, unsigned char g, unsigned char b )
3 {
4     pp[y*w*3+x*3+0] = (unsigned char)r;
5     pp[y*w*3+x*3+1] = (unsigned char)g;
6     pp[y*w*3+x*3+2] = (unsigned char)b;
7 }

生成一个,~~~~(>_<)~~~~:

 1 int main(int argc, char *argv[])
 2 {
 3     int ret;
 4     char *file_name;
 5     
 6     if( argc != 2 )
 7     {
 8         printf("please input bitmap file_name\n");
 9         printf("usage: %s bitmap_file_name\n", argv[0]);
10         return 0;
11     }
12     
13     file_name = argv[1];
14     
15     ret = bitmap_file_generate(file_name, 1024*10, 1024*10);
16     
17     printf("bitmap_file_generate, ret: %d\n", ret);
18 
19     return 0;    
20 }

 

以上是关于Bitmap文件格式+生成一个BMP文件的主要内容,如果未能解决你的问题,请参考以下文章

BMP文件格式分析

数字图像BMP文件格式详解

BMP文件格式具体解释

BMP文件格式详解

BMP文件格式详解

BMP格式详解