关于bmp图像处理的MFC程序请教(寻找错误)
下面是主要的BMP图像处理程序。我的BMP图像为320*240BOOL WINAPI ConvertToGrayScale(LPSTR lpDIB)
{
LPSTR lpDIBBits;
LPSTR lpNewDIBBits;
LONG lLineBytes;
unsigned char *lpSrc;
unsigned char *lpdest;
unsigned char *ired,*igreen,*iblue;
long lWidth;
long lHeight;
long i,j;
lWidth=::DIBWidth(lpDIB);
lHeight=::DIBHeight(lpDIB);
RGBQUAD *lpRGBquad;
lpRGBquad=(RGBQUAD*)&lpDIB[sizeof(BITMAPINFOHEADER)];
if(::DIBNumColors(lpDIB)==256)
{
return TRUE;
}
if(::DIBNumColors(lpDIB)!=256)
{
lLineBytes=WIDTHBYTES(lWidth*8*3);
lpdest=new BYTE[lHeight*lWidth];
lpDIBBits=(LPSTR)lpDIB+sizeof(BITMAPINFOHEADER);
for(i=0;i<lHeight;i++)
for(j=0;j<lWidth*3;j+=3)
{
ired=(unsigned char*)lpDIBBits+lLineBytes*i+j+2;
igreen=(unsigned char*)lpDIBBits+lLineBytes*i+j+1;
iblue=(unsigned char*)lpDIBBits+lLineBytes*i+j;
lpdest[i*lWidth+j/3]=(unsigned char)(*ired)*0.299+(*igreen)*0.588+(*iblue)*0.114;
}//总是在这个地方出错,我调试了下发现*((unsigned char*)lpDIBBits+lWidth *3*( lHeight -1)+ lWidth *3-1);这个值就已报错,为什么?每个点都是3基色呀。。。
LPBITMAPINFOHEADER lpBI;
lpBI=(LPBITMAPINFOHEADER)lpDIB;
lpBI->biBitCount=8;
for(i=0;i<256;i++)
{
lpRGBquad[i].rgbRed=(unsigned char)i;
lpRGBquad[i].rgbGreen=(unsigned char)i;
lpRGBquad[i].rgbBlue=(unsigned char)i;
lpRGBquad[i].rgbReserved=0;
}
lpNewDIBBits=::FindDIBBits(lpDIB);
lLineBytes=WIDTHBYTES(lWidth*8);
for(i=0;i<lHeight;i++)
for(j=0;j<lWidth;j++)
{
lpSrc=(unsigned char*)lpNewDIBBits+lLineBytes*i+j;
*lpSrc=lpdest[i*lWidth+j];
}
delete lpdest;
}
return true;
}
在线求答案,跪求!!!!