/**
BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),
是Windows系统中广泛使用的图像文件格式。由于它可以不作任何变换地保存图像像素域的数据,
因此成为我们取得RAW数据的重要来源。
Windows的图形用户界面(graphical user interfaces)也在它的内建图像子系统GDI中对BMP格式提供了支持。
**/
/*一套BMP文字的图片,想把BMP转化为字库,需要去掉BMP的头和BMP信息,只取数据部分,并存为数组*/
#include
#include
#include
#include
#include
using namespace std;
int Save8bitBmp(const char* FileName, unsigned char* img, int width, int height, int lineByte, RGBQUAD* rgb);
int Load8bitBmp(const char* FileName, int &width, int &height)
{
FILE* fp = fopen(FileName, "rb");
if(fp == NULL)
{
printf("open file error\n");
return 0;
}
BITMAPFILEHEADER FileHeader;
//fread函数的原型是size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ; 从文件中一次最多读count项,每个项size字节,保存到buffer中
if(fread(&FileHeader, sizeof(BITMAPFILEHEADER), 1, fp) != 1)
{
printf("read file error\n");
return 0;
}
if (FileHeader.bfType != 0x4d42)
{
printf("no bmp file\n");
return 0;
}
BITMAPINFOHEADER InfoHeader;
if(fread(&InfoHeader, sizeof(BITMAPINFOHEADER), 1, fp) != 1)
{
printf("read file2 error\n");
return 0;
}
if(InfoHeader.biBitCount != 8)
{
printf("this is not 8 bit bmp file\n");
return 0;
}
if(InfoHeader.biCompression != 0)
{
printf("compression mode\n");
return 0;
}
RGBQUAD rgb[256];
int i;
for(i = 0; i < 256; i++)
{
rgb[i].rgbBlue = rgb[i].rgbGreen = rgb[i].rgbRed = i;
rgb[i].rgbReserved = 0;
}
width = InfoHeader.biWidth;
height = InfoHeader.biHeight;
unsigned char* img = (unsigned char *) malloc(width * height);
//SEEK_SET:文件开头 int fseek(FILE *stream, long offset, int fromwhere); 把指针移动到离fromwhere处的offset字节处
fseek(fp, FileHeader.bfOffBits, SEEK_SET);
for(int i = height-1; i>=0 ;i--)
{
if(fread(img+i*width,width, 1, fp) != 1)
{
printf("read file error\n");
return 0;
}
if(width %4 != 0)
fseek(fp, 4-width % 4, SEEK_CUR);
}
Save8bitBmp("E:\\newcopy.bmp",img, width, height, 0, rgb);
free(img);
fclose(fp);
printf("success Load8bitBmp\n");
return 1;
}
int Save8bitBmp(const char* FileName, unsigned char* img, int width, int height, int lineByte, RGBQUAD* rgb)
{
FILE* fp = fopen(FileName, "wb");
if(fp == NULL)
{
printf("open file error\n");
return 0;
}
DWORD bfSize = (width +3) /4 * 4 * height +54 + 256 * 4; //bmp文件的大小
BITMAPFILEHEADER FileHeader = { 0x4d42, bfSize, 0, 0, 54 + 256 * 4 };
if(fwrite(&FileHeader, sizeof(BITMAPFILEHEADER), 1, fp) != 1)
{
printf("write filehead error\n");
return 0;
}
BITMAPINFOHEADER InfoHeader =
{ 40, width , height, 1, 8, 0, 0, 0, 0, 0, 0 };
if (fwrite(&InfoHeader, sizeof(BITMAPINFOHEADER), 1, fp) != 1)
{
printf("write file error\n");
return 0;
}
if (fwrite(rgb, sizeof(RGBQUAD), 256, fp) != 256)
{
printf("read rgb error\n");
return 0;
}
BYTE blank[3] = { 0,0,0 };
for (int i = height - 1; i >= 0; i--)
{
if (fwrite(img + i * width, width, 1, fp) != 1)
{
cout << "write file error" << endl;
return 0;
}
if (width % 4 != 0)
fwrite(blank, 4 - width % 4, 1, fp);
}
system("pause");
fclose(fp);
printf("success Save8bitBmp\n");
return 1;
}
int main()
{
int width = 0;
int height = 0;
Load8bitBmp("E:\\woman.bmp", width, height);
}
我使用的图片是