圖片灰化

BOOL CResourceCreator::CreateGrayPngFromColorPng(LPCTSTR lpColorPath, LPCTSTR lpGrayImagePath)
{
    if (NULL == lpColorPath || lpColorPath[0] == 0 || NULL == lpGrayImagePath)
    {
        return FALSE;
    }
    
    USES_CONVERSION;
    Gdiplus::Bitmap Bitmap(T2W(lpColorPath));
    Gdiplus::Status res = Bitmap.GetLastStatus();
    if (res != Gdiplus::Ok)
    {
        return FALSE;
    }
    Gdiplus::Color pixelColor;
    //修改R,G,B值
    int nCalHeight = Bitmap.GetHeight() ; 
    int nCalWidth = Bitmap.GetWidth() ; 
    for (int y = 0; y < nCalHeight; y++)
    {
        for (int x = 0; x < nCalWidth; x++)
        {
            Bitmap.GetPixel(x, y, &pixelColor);
            int lA = pixelColor.GetA();
            int lR = pixelColor.GetR();
            int lG = pixelColor.GetG();
            int lB = pixelColor.GetB();
            
            if (lA > 0)
            {
                int nAshValue = CaculateAshValue(lR, lG, lB);
                COLORREF crFill = RGB(nAshValue, nAshValue, nAshValue);
                Bitmap.SetPixel(x, y, Gdiplus::Color::MakeARGB(lA, nAshValue, nAshValue, nAshValue));	
            }				
        }
    }
    
    CLSID bmpClsid;
    if (-1 == GetEncoderClsid(L"image/png", &bmpClsid))
    {
        _ASSERT(FALSE);
        return FALSE;
    }
    
    Bitmap.Save(T2W(lpGrayImagePath), &bmpClsid, NULL);
    
    return TRUE;
}

int CResourceCreator::CaculateAshValue(int & nR, int & nG, int & nB)
{
 	return (nR*299+nG*587+nB*114+500)/1000;   //+500是爲了四捨五入
//     return (nR*117+nG*601+nB*306+502)>>10;
}

 
發佈了15 篇原創文章 · 獲贊 14 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章