1.用VC++处理24位真彩转换成256级灰度怎么弄
用VC++处理24位真彩转换成256级灰度怎么弄
还是图片图片先熟悉了VC++吧!关于图像处理方面的转灰转灰formik源码内容建议看看:图像处理程序设计(杨淑莹)清晰版+源码
#include <windows.h>BOOL BMPto8(char *szSourceFile,char *szTargetFile);
int main(int argc,char* argv[])
{
//调用这个函数直接把位真彩色灰度化
BOOL stat=BMPto8("c://source.bmp","c://target.bmp");
return 0;
}
BOOL BMPto8(char *szSourceFile,char *szTargetFile)
{
HANDLE hSourceFile=INVALID_HANDLE_VALUE;
HANDLE hTargetFile=INVALID_HANDLE_VALUE;
DWORD dwSourceSize=0,dwTargetSize=0;
PBYTE pSource=NULL,pTarget=NULL;
hSourceFile=CreateFile(szSourceFile,GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hSourceFile==INVALID_HANDLE_VALUE)
return FALSE;
dwSourceSize=GetFileSize(hSourceFile,NULL);
pSource=(PBYTE)VirtualAlloc(NULL,dwSourceSize,MEM_COMMIT,PAGE_READWRITE);
//分配空间失败或者文件太小(BMP文件不可能小于个字节)
if(pSource==NULL||dwSourceSize<=)
{
CloseHandle(hSourceFile);
return FALSE;
}
DWORD dwTemp=0;
ReadFile(hSourceFile,pSource,dwSourceSize,&dwTemp,NULL);
BITMAPFILEHEADER *pSourceFileHeader=(BITMAPFILEHEADER*)pSource;
BITMAPINFOHEADER *pSourceInfoHeader=(BITMAPINFOHEADER*)(pSource+sizeof(BITMAPFILEHEADER));
//不是BMP文件或者不是位真彩色
if(pSourceFileHeader->bfType!=0x4d||pSourceInfoHeader->biBitCount!=)
{
CloseHandle(hSourceFile);
VirtualFree(pSource,NULL,MEM_RELEASE);
return FALSE;
}
CloseHandle(hSourceFile);
LONG nWidth=pSourceInfoHeader->biWidth;
LONG nHeight=pSourceInfoHeader->biHeight;
LONG nSourceWidth=nWidth*3;
//这里是因为BMP规定保存时长度和宽度必须是4的整数倍,如果不是则要补足
if(nSourceWidth%4)
nSourceWidth=(nSourceWidth/4+1)*4;
LONG nTargetWidth=nWidth;
if(nTargetWidth%4)
nTargetWidth=(nTargetWidth/4+1)*4;
dwTargetSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*+nHeight*nTargetWidth;
pTarget=(PBYTE)VirtualAlloc(NULL,dwTargetSize,MEM_COMMIT,PAGE_READWRITE);
memset(pTarget,0,dwTargetSize);
if(pTarget==NULL)
{
VirtualFree(pTarget,NULL,MEM_RELEASE);
return FALSE;
}
BITMAPFILEHEADER *pTargetFileHeader=(BITMAPFILEHEADER *)pTarget;
BITMAPINFOHEADER *pTargetInfoHeader =
(BITMAPINFOHEADER *)(pTarget+sizeof(BITMAPFILEHEADER));
pTargetFileHeader->bfType=pSourceFileHeader->bfType;
pTargetFileHeader->bfSize=dwTargetSize;
pTargetFileHeader->bfReserved1=0;
pTargetFileHeader->bfReserved2=0;
pTargetFileHeader->bfOffBits=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * ;
pTargetInfoHeader->biBitCount=8;
pTargetInfoHeader->biClrImportant=0;
pTargetInfoHeader->biClrUsed=;
pTargetInfoHeader->biCompression=BI_RGB;
pTargetInfoHeader->biHeight=pSourceInfoHeader->biHeight;
pTargetInfoHeader->biPlanes=1;
pTargetInfoHeader->biSize=sizeof(BITMAPINFOHEADER);
pTargetInfoHeader->biSizeImage=nHeight*nTargetWidth;
pTargetInfoHeader->biWidth=pSourceInfoHeader->biWidth;
pTargetInfoHeader->biXPelsPerMeter=pSourceInfoHeader->biXPelsPerMeter;
pTargetInfoHeader->biYPelsPerMeter=pSourceInfoHeader->biYPelsPerMeter;
RGBQUAD *pRgb;
for(int i=0;i<;i++)//初始化8位灰度图的调色板信息
{
pRgb = (RGBQUAD*)(pTarget+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+i*sizeof(RGBQUAD));
pRgb->rgbBlue=i;
pRgb->rgbGreen=i;
pRgb->rgbRed=i;
pRgb->rgbReserved=0;
}
for (int m=0;m<nHeight;m++)//转化真彩色图为灰度图
{
for(int n=0;n<nWidth;n++)
{
pTarget[pTargetFileHeader->bfOffBits+m*nTargetWidth+n] =
pSource[pSourceFileHeader->bfOffBits+m*nSourceWidth+n*3]*0.
+pSource[pSourceFileHeader->bfOffBits+m*nSourceWidth+n*3+1]*0.
+pSource[pSourceFileHeader->bfOffBits+m*nSourceWidth+n*3+2]*0.;
}
}
hTargetFile = CreateFile(szTargetFile,GENERIC_WRITE,FILE_SHARE_WRITE,
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
BOOL stat=WriteFile(hTargetFile,pTarget,dwTargetSize,&dwTemp,NULL);
CloseHandle(hTargetFile);
VirtualFree(pSource,NULL,MEM_RELEASE);
VirtualFree(pTarget,NULL,MEM_RELEASE);
return stat;
}