使用GDI+保存图像为8bpp的灰度图像,GDI+真的有些特殊。。。。。
// Greyscale conversion
#define GREY(r, g, b) (BYTE)(((WORD)r * 77 + (WORD)g * 150 + (WORD)b * 29) >> 8) // .299R + .587G + .114B
//#define GREY(r, g, b) (BYTE)(((WORD)r * 169 + (WORD)g * 256 + (WORD)b * 87) >> 9) // .33R + 0.5G + .17B
// BOOL GDIPlusImage::SaveToFileWith8pp(TCHAR *pszPath)
// 功能:保存图片为8位的灰度图像
// 参数: pszPath要保存的文件名
BOOL GDIPlusImage::SaveToFileWith8pp(TCHAR *pszPath)
{Bitmap *ima = this->m_pBitmap; // GDIPlusImage的Bitmap对象if (!ima || !pszPath || !*pszPath){return FALSE;}int width = m_pBitmap->GetWidth();int height = m_pBitmap->GetHeight();int bitcount = 8; //1, 4, 8, 24, 32////Build bitmap headerBITMAPFILEHEADER bitmapFileHeader; BITMAPINFOHEADER bitmapInfoHeader; BYTE rgbquad[4]; // RGBQUADint index = 0;DWORD stride = ((bitcount*width + 31)/32)*4; //每行都是4的倍数,或者说是DWORD大小的倍数switch(bitcount) { case 1: index = 2; bitmapFileHeader.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 2*4); break; case 4: index = 16; bitmapFileHeader.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 16*4); break; case 8: index = 256; bitmapFileHeader.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)); break; case 24: case 32: index = 0; bitmapFileHeader.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)); break; default:break;} //构造Bitmap文件头BITMAPFILEHEADER bitmapFileHeader.bfType = 0x4d42; // 很重要的标志位 BM 标识bitmapFileHeader.bfSize = (DWORD)(bitmapFileHeader.bfOffBits + height * stride); //bmp文件长度 bitmapFileHeader.bfReserved1 = 0; bitmapFileHeader.bfReserved2 = 0; //构造Bitmap文件信息头BITMAPINFOHEADER bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER); bitmapInfoHeader.biWidth = width; bitmapInfoHeader.biHeight = height; bitmapInfoHeader.biPlanes = 1; bitmapInfoHeader.biBitCount = bitcount;bitmapInfoHeader.biCompression = BI_RGB; // 未压缩bitmapInfoHeader.biSizeImage = height * stride; bitmapInfoHeader.biXPelsPerMeter = 3780; bitmapInfoHeader.biYPelsPerMeter = 3780; bitmapInfoHeader.biClrUsed = 0; bitmapInfoHeader.biClrImportant = 0; //创建BMP内存映像,写入位图头部BYTE *pMyBmp = new BYTE[bitmapFileHeader.bfSize]; // 我的位图pMyBmpBYTE *curr = pMyBmp; // curr指针指示pMyBmp的位置memset(curr, 0, bitmapFileHeader.bfSize); //写入头信息 memcpy(curr, &bitmapFileHeader,sizeof(BITMAPFILEHEADER));curr = pMyBmp + sizeof(BITMAPFILEHEADER); memcpy(curr, &bitmapInfoHeader,sizeof(BITMAPINFOHEADER)); curr += sizeof(BITMAPINFOHEADER);//构造调色板 if(8 == bitcount) {rgbquad[3] = 0; //rgbReservedfor(int i = 0; i < 256; i++) { rgbquad[0] = rgbquad[1] = rgbquad[2] = i; memcpy(curr, rgbquad, sizeof(RGBQUAD)); curr += sizeof(RGBQUAD); } }else if (4 == bitcount){rgbquad[3] = 0;for (int i = 0; i < 16; i++){rgbquad[0] = rgbquad[1] = rgbquad[2] = i; memcpy(curr, rgbquad, sizeof(RGBQUAD)); curr += sizeof(RGBQUAD); }}else if(1 == bitcount) { rgbquad[3] = 0; //rgbReservedfor(int i = 0; i < 2; i++) { rgbquad[0] = rgbquad[1] = rgbquad[2] = (256 - i)%256; memcpy(curr, rgbquad, sizeof(RGBQUAD)); curr += sizeof(RGBQUAD); } }//用GDI+加载数据源,也可以是其他的,写入文件数据Rect rect(0,0,width,height); // Gdiplus+BitmapData bmData;Status iSucess = ima->LockBits(&rect,ImageLockModeRead,ima->GetPixelFormat() ,&bmData);BYTE *_pixels = (BYTE*)bmData.Scan0; //原图rect区域内存位置的起始指针,以BYTE作为单元类型BYTE *_pRow;int _strideoff8 = stride - width; //前面计算的索引图像的strideBYTE _grey;// build pixles, GDI+ (windows)的图像数据的原点在左下角,参考一下OpenCV的IplImage结构体switch(ima->GetPixelFormat()){case PixelFormat24bppRGB:{int _strideoff24 = stride - 3*width; //前面计算的索引图像的stride// 灰度化for (int i=height-1; i >= 0; i--){_pRow = _pixels + i*bmData.Stride; // 当前的行for (int j=width-1; j >= 0; j--){ BYTE* _pixels_b; //bBYTE* _pixels_g; //gBYTE* _pixels_r; //r_pixels_b = _pRow++; //blue_pixels_g = _pRow++; //green_pixels_r = _pRow++; //red_grey = GREY(*_pixels_r, *_pixels_g, *_pixels_b); *curr = _grey; //根据红绿蓝求出此像素的灰度值curr++;}curr += _strideoff8;}}break;case PixelFormat32bppARGB:{// 灰度化for (int i=height-1;i>=0;i--){_pRow = _pixels + i*bmData.Stride;for (int j=width-1;j>=0;j--){ BYTE* _pixels_b;BYTE* _pixels_g; BYTE* _pixels_r; _pixels_b = _pRow++; //blue_pixels_g = _pRow++; //green_pixels_r = _pRow++; //red_pRow++;_grey = GREY(*_pixels_r, *_pixels_g, *_pixels_b); *curr = _grey;curr++;}curr += _strideoff8;}}break;case PixelFormat8bppIndexed: // Gdi+只能保存成24位真彩色的图像{// 不能直接用memcpy复制,需要水平镜像,memcpy(curr, _pixels, height * stride);for (int i=height-1; i >= 0; i--){_pRow = _pixels + i*bmData.Stride;
// for (int j=width-1;j >= 0;j--)
// {
// _grey = *_pRow++;
// *curr = _grey;
// curr++;
// }memcpy(curr, _pRow, width);curr += stride;}}break;default:break;}// 保存图像 pMyBmp 到文件try{CFile f(pszPath, CFile::modeCreate | CFile::modeWrite );f.Write(pMyBmp, bitmapFileHeader.bfSize);f.Close();}catch( CFileException* e ){TCHAR szCause[255];e->GetErrorMessage(szCause, 255);CString msg;msg.Format(_T("file error: %s, m_cause:%d\n"),szCause, e->m_cause);TRACE1("%s",msg);AfxMessageBox(msg);e->Delete();}//clean:ima->UnlockBits(&bmData);delete[] pMyBmp;return TRUE;
}