用C++读取、修改和保存位图 该示例用C++读取、修改位图,通过它我们可以了解位图的文件结构,对图像处理的理解很有帮助。下面是全部的源码: #include #include #include /***********************变量的定义***********************************/ unsigned char* pBmpBuf; //读入图像数据的指针 int bmpWidth; //图像的宽度 int bmpHeight; //图像的高度 RGBQUAD* pColorTable; //颜色表指针 int biBitCount; //图像类型,像素位数 /**************************************************************************** 读取图像的位图数据、宽度、高度、颜色表及像素位数,并存放在全局变量中 *****************************************************************************/ bool readBmp(char* bmpName) { FILE* fp = fopen(bmpName,"rb"); //以二进制读的方式打开指定的图像文件 if(fp == 0) return 0; //跳过位图文件头 fseek(fp,sizeof(BITMAPFILEHEADER),0); //定义位图信息头结构变量,读取位图信息头进内存,存放在变量 infoHead 中 BITMAPINFOHEADER infoHead; fread(&infoHead,sizeof(BITMAPINFOHEADER),1,fp); bmpWidth = infoHead.biWidth; bmpHeight = infoHead.biHeight; biBitCount = infoHead.biBitCount; //定义变量,计算图像每行像素所占的字节数(必须为4的倍数) int lineByte=(bmpWidth * biBitCount/8 + 3)/4 * 4; //灰度图像有颜色表,且颜色表为256 if(biBitCount == 8) { //申请颜色表所需要的空间,读颜色表进内存 pColorTable = new RGBQUAD[256]; fread(pColorTable,sizeof(RGBQUAD),256,fp); } //申请位图数据所需要的空间,读位图数据进内存 pBmpBuf = new unsigned char[lineByte * bmpHeight]; fread(pBmpBuf,1,lineByte * bmpHeight,fp); fclose(fp); return 1; } /*************************************************************************************************** 将指定数据写到文件中 ***************************************************************************************************/ bool saveBmp(char* bmpName, unsigned char* imgBuf, int width, int height, int biBitCount, RGBQUAD* pColorTable) { //如果位图数据指针为0,...