您的位置:首页 > 其它

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

2016-10-25 17:33 316 查看
Bitmap的文件格式:

#define UINT16    unsigned short
#define DWORD    unsigned int
#define WORD    short
#define LONG    int

// Bitmap File Header ( 14 Bytes )
typedef struct tagBITMAPFILEHEADER
{
UINT16 bfType;        // same as BM in ASCII.
DWORD bfSize;         // the size of the BMP file in bytes
UINT16 bfReserved1;
UINT16 bfReserved2;
DWORD bfOffBits;    // starting address, of the byte where the bitmap image data (Pixel Array) can be found.
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;

// DIB头描述bitmap的信息,包括大小、压缩类型、颜色格式等,DIB头的大小根据版本不同,大小也不同。
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize;             // 4, the size of this header (40 bytes)
LONG biWidth;             // 4, the bitmap width in pixels (signed integer).
LONG biHeight;             // 4, the bitmap height in pixels (signed integer).
WORD biPlanes;             // 2, the number of color planes being used. Must be set to 1.
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.
DWORD biCompression;     // 4, the compression method being used.
DWORD biSizeImage;         // 4, the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
LONG biXPelsPerMeter;     // 4, the horizontal resolution of the image.
LONG biYPelsPerMeter;     // 4, the vertical resolution of the image.
DWORD biClrUsed;         // 4, the number of colors in the color palette, or 0 to default to 2^n.
DWORD biClrImportant;    // 4, the number of important colors used, or 0 when every color is important; generally ignored.
} BITMAPINFOHEADER;


生成一个BMP文件:

int bitmap_file_generate( char *name, int width, int height )
{
FILE *fp = NULL;
unsigned char *pData;
unsigned char *pp;
int i, j;
int iFileSize;

unsigned char bfType1, bfType2;
unsigned int bfSize;
unsigned short bfReserved1, bfReserved2;
unsigned int bfOffBits;
unsigned int biSize;
int biWidth, biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;

// offset[0:1]
bfType1   = 'B';
bfType2   = 'M';
// offset[2:5]
bfSize    = width * height * 3 + 54;
// offset[6:7]
bfReserved1 = 0;
// offsset[8:9]
bfReserved2 = 0;
// offset[10:13]
bfOffBits = 54;
// offset[14:17]
biSize = 40;
// offset[18:21]
biWidth = width;
// offset[22:25]
biHeight = height;
// offset[26:27]
biPlanes = 1;
// offset[28:29]
biBitCount = 24;
// offset[30:33]
biCompression = 0;
// offset[34:37]
biSizeImage = width * height * 3;
// offset[38:41]
biXPelsPerMeter = 2835;
// offset[42:45]
biYPelsPerMeter = 2835;
// offset[46:49]
biClrUsed = 0;
// offset[50:53]
biClrImportant = 0;

fp = fopen(name, "wb+");
if( fp == NULL )
{
printf("create file err, fopen: %p\n", fp);
return -1;
}

pData = (unsigned char *)malloc(biWidth*biHeight*3+54);//malloc(iFileSize);
if(pData == NULL)
{
printf("pData malloc err\n");
return -2;
}

// same as BM in ASCII.
pData[0] = bfType1;
pData[1] = bfType2;

// the size of the BMP file in bytes. bmp文件的实际大小
pData[2] = (unsigned char)((bfSize>>0) & 0xff);
pData[3] = (unsigned char)((bfSize>>8) & 0xff);
pData[4] = (unsigned char)((bfSize>>16) & 0xff);
pData[5] = (unsigned char)((bfSize>>24) & 0xff);

// reserved
pData[6] = (unsigned char)((bfReserved1>>0) & 0xff);
pData[7] = (unsigned char)((bfReserved1>>8) & 0xff);
// reserved
pData[8] = (unsigned char)((bfReserved2>>0) & 0xff);
pData[9] = (unsigned char)((bfReserved2>>0) & 0xff);

// the offset, i.e. starting address, of the byte where the bitmap image data (Pixel Array) can be found.
// 颜色数据的偏移地址,实际为:54 = 14 + 40
pData[10] = (unsigned char)((bfOffBits>>0) & 0xff);
pData[11] = (unsigned char)((bfOffBits>>8) & 0xff);
pData[12] = (unsigned char)((bfOffBits>>16) & 0xff);
pData[13] = (unsigned char)((bfOffBits>>24) & 0xff);

// the size of this header (40 bytes)
pData[14] = (unsigned char)((biSize>>0) & 0xff);
pData[15] = (unsigned char)((biSize>>8) & 0xff);
pData[16] = (unsigned char)((biSize>>16) & 0xff);
pData[17] = (unsigned char)((biSize>>24) & 0xff);

// the bitmap width in pixels (signed integer).
pData[18] = (unsigned char)((biWidth>>0) & 0xff);
pData[19] = (unsigned char)((biWidth>>8) & 0xff);
pData[20] = (unsigned char)((biWidth>>16) & 0xff);
pData[21] = (unsigned char)((biWidth>>24) & 0xff);

// the bitmap height in pixels (signed integer).
pData[22] = (unsigned char)((biHeight>>0) & 0xff);
pData[23] = (unsigned char)((biHeight>>8) & 0xff);
pData[24] = (unsigned char)((biHeight>>16) & 0xff);
pData[25] = (unsigned char)((biHeight>>24) & 0xff);

// the number of color planes being used. Must be set to 1.
pData[26] = (unsigned char)((biPlanes>>0) & 0xff);
pData[27] = (unsigned char)((biPlanes>>8) & 0xff);

// the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.
pData[28] = (unsigned char)((biBitCount>>0) & 0xff);
pData[29] = (unsigned char)((biBitCount>>8) & 0xff);

// the compression method being used. See the next table for a list of possible values.
pData[30] = (unsigned char)((biCompression>>0) & 0xff);
pData[31] = (unsigned char)((biCompression>>8) & 0xff);
pData[32] = (unsigned char)((biCompression>>16) & 0xff);
pData[33] = (unsigned char)((biCompression>>24) & 0xff);

// the image size. This is the size of the raw bitmap data, and should not be confused with the file size.
pData[34] = (unsigned char)((biSizeImage>>0) & 0xff);
pData[35] = (unsigned char)((biSizeImage>>8) & 0xff);
pData[36] = (unsigned char)((biSizeImage>>16) & 0xff);
pData[37] = (unsigned char)((biSizeImage>>24) & 0xff);

// the horizontal resolution of the image. (pixel per meter, signed integer)
pData[38] = (unsigned char)((biXPelsPerMeter>>0) & 0xff);
pData[39] = (unsigned char)((biXPelsPerMeter>>8) & 0xff);
pData[40] = (unsigned char)((biXPelsPerMeter>>16) & 0xff);
pData[41] = (unsigned char)((biXPelsPerMeter>>24) & 0xff);

// the vertical resolution of the image. (pixel per meter, signed integer)
pData[42] = (unsigned char)((biYPelsPerMeter>>0) & 0xff);
pData[43] = (unsigned char)((biYPelsPerMeter>>8) & 0xff);
pData[44] = (unsigned char)((biYPelsPerMeter>>16) & 0xff);
pData[45] = (unsigned char)((biYPelsPerMeter>>24) & 0xff);

// the number of colors in the color palette, or 0 to default to 2^n.
pData[46] = (unsigned char)((biClrUsed>>0) & 0xff);
pData[47] = (unsigned char)((biClrUsed>>8) & 0xff);
pData[48] = (unsigned char)((biClrUsed>>16) & 0xff);
pData[49] = (unsigned char)((biClrUsed>>24) & 0xff);

// the number of important colors used, or 0 when every color is important; generally ignored.
pData[50] = (unsigned char)((biClrImportant>>0) & 0xff);
pData[51] = (unsigned char)((biClrImportant>>8) & 0xff);
pData[52] = (unsigned char)((biClrImportant>>16) & 0xff);
pData[53] = (unsigned char)((biClrImportant>>24) & 0xff);

pp = &pData[54];
for(i = 0; i < biHeight; i++)
{
for(j = 0; j < biWidth; j++)
{
set_color(pp, biWidth, biHeight, i, j, i+j, i, j);  // 随便填有点颜色
}
}

iFileSize = fwrite(pData, 1, bfSize, fp);
printf("generate file iFileSize: %d\n", iFileSize);

free(pData);

fclose(fp);

return 0;
}


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

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


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

int main(int argc, char *argv[])
{
int ret;
char *file_name;

if( argc != 2 )
{
printf("please input bitmap file_name\n");
printf("usage: %s bitmap_file_name\n", argv[0]);
return 0;
}

file_name = argv[1];

ret = bitmap_file_generate(file_name, 1024*10, 1024*10);

printf("bitmap_file_generate, ret: %d\n", ret);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: