#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <math.h>
#include <windows.h>
using namespace std;
//将位图转换为256色灰度图
void ToGray(const string& srcFile,const string& desFile)
{
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;
FILE *pFile;
if ((pFile = fopen(srcFile.c_str(),"rb")) == NULL)
{
printf("open bmp file error.");
exit(-1);
}
//读取文件和Bitmap头信息
fseek(pFile,0,SEEK_SET);
fread(&bmfHeader,sizeof(BITMAPFILEHEADER),1,pFile);
fread(&bmiHeader,sizeof(BITMAPINFOHEADER),1,pFile);
//先不支持16位位图
int bitCount = bmiHeader.biBitCount;
if (bitCount == 16)
{
exit(-1);
}
double byteCount = (double)bitCount / 8;
int nClr = 0;
if (bitCount < 16)
{
nClr = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bitCount;
if (nClr > 256)
nClr = 0;
}
//读取调色板
RGBQUAD *quad = NULL;
if (nClr > 0)
{
quad = new RGBQUAD[nClr];
fread(quad,sizeof(RGBQUAD) * nClr,1,pFile);
}
int srcW = bmiHeader.biWidth;
int srcH = bmiHeader.biHeight;
//原图像每一行去除偏移量的字节数
int lineSize = bitCount * srcW >> 3;
//偏移量,windows系统要求每个扫描行按四字节对齐
int alignBytes = (((bmiHeader.biWidth * bitCount + 31) & ~31) >> 3)
- ((bmiHeader.biWidth * bitCount) >> 3);
//原图像缓存
int srcBufSize = lineSize * srcH;
BYTE* srcBuf = new BYTE[srcBufSize];
int i,j;
//读取文件中数据
for (i = 0; i < srcH; i++)
{
fread(&srcBuf[lineSize * i],lineSize,1,pFile);
fseek(pFile,alignBytes,SEEK_CUR);
}
//256色位图调色板
RGBQUAD *quadDes = NULL;
quadDes = new RGBQUAD[256];
for (i = 0; i < 256; i++)
{
//灰度图的RGB值相等
quadDes[i].rgbBlue = quadDes[i].rgbGreen = quadDes[i].rgbRed = i;
}
//灰度图每个像素采用8位表示
int desBufSize = (((srcW * 8 + 31) & ~31) >> 3) * srcH;
BYTE *desBuf = new BYTE[desBufSize];
//每个扫描行占用字节数
int desLineSize = ((srcW * 8 + 31) >> 5) * 4;
for (i = 0; i < srcH; i++)
{
for (j = 0; j < srcW; j++)
{
//从调色板中读取RGB值
if (nClr > 0)
{
unsigned int pos = srcBuf[i * lineSize + int(j * byteCount)];
desBuf[i * desLineSize + j] = 0.3 * quad[pos].rgbRed
+ 0.59 * quad[pos].rgbGreen
+ 0.11 * quad[pos].rgbBlue;
}
else
desBuf[i * desLineSize + j] = 0.3 * srcBuf[i * lineSize + int(j * byteCount)]
+ 0.59 * srcBuf[i * lineSize + int(j * byteCount) + 1]
+ 0.11 * srcBuf[i * lineSize + int(j * byteCount) + 2];
}
}
//创建目标文件
HFILE hfile = _lcreat(desFile.c_str(),0);
//文件头信息
BITMAPFILEHEADER nbmfHeader;
nbmfHeader.bfType = 0x4D42;
nbmfHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ 256 * sizeof(RGBQUAD) + srcW * srcH;
nbmfHeader.bfReserved1 = 0;
nbmfHeader.bfReserved2 = 0;
nbmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
//Bitmap头信息
BITMAPINFOHEADER bmi;
bmi.biSize=sizeof(BITMAPINFOHEADER);
bmi.biWidth=srcW;
bmi.biHeight=srcH;
bmi.biPlanes=1;
bmi.biBitCount=8;
bmi.biCompression=BI_RGB;
bmi.biSizeImage=0;
bmi.biXPelsPerMeter=0;
bmi.biYPelsPerMeter=0;
bmi.biClrUsed= 256;
bmi.biClrImportant=0;
//写入文件头信息
_lwrite(hfile,(LPCSTR)&nbmfHeader,sizeof(BITMAPFILEHEADER));
//写入Bitmap头信息
_lwrite(hfile,(LPCSTR)&bmi,sizeof(BITMAPINFOHEADER));
if (quadDes)
{
_lwrite(hfile,(LPCSTR)quadDes,sizeof(RGBQUAD) * 256);
}
//写入图像数据
_lwrite(hfile,(LPCSTR)desBuf,desBufSize);
_lclose(hfile);
if (quad)
{
delete[] quad;
quad = NULL;
}
if (quadDes)
{
delete[] quadDes;
quadDes = NULL;
}
}
int main(int argc, char* argv[])
{
string srcFile("g://hangmu001.bmp");
//srcFile.erase(srcFile.length()-3,4);
string desFile(srcFile + "_gray.bmp");
ToGray(srcFile,desFile);
system("pause");
return 0;
}