在将所有文件转为32Bit位图后,就可以进行特效处理了,首先是文件旋转
/*********************************************************************************
* RotateImage(int nAngle)
* 按照90度的角度,顺时针或者逆时针旋转图片
* nAngle = 0 逆时针旋转
* nAngle = 1 顺时针旋转
**********************************************************************************/
void CImageEditor::RotateImage(int nAngle)
{
//原来的宽和高互换
unsigned int nImageWidthAfterRoate, nImageHeightAfterRoate;
nImageWidthAfterRoate = nImageHeightAfterRoate = 0;
unsigned int nImageWidthOri = m_pBitmapInfo->bmiHeader.biWidth; //旋转前的宽
unsigned int nImageHeightOri = abs(m_pBitmapInfo->bmiHeader.biHeight);
nImageWidthAfterRoate = abs(m_pBitmapInfo->bmiHeader.biHeight); //旋转后的宽
nImageHeightAfterRoate = m_pBitmapInfo->bmiHeader.biWidth;
unsigned int nBytesPerLine = ((nImageWidthAfterRoate * m_pBitmapInfo->bmiHeader.biBitCount + 31) / 32) * 4; //旋转后的每行的字节数
unsigned int nTotalImageBytesData = nBytesPerLine * nImageHeightAfterRoate; //旋转后的总字节数
unsigned int nBytesPerLineOri = ((nImageWidthOri * m_pBitmapInfo->bmiHeader.biBitCount + 31) / 32) * 4; //旋转前的每行的字节数
m_pBitmapInfo->bmiHeader.biSizeImage = nTotalImageBytesData;
BYTE *pImageBytesData = new BYTE[nTotalImageBytesData]; //旋转后的数据
ASSERT(pImageBytesData != NULL);
memset(pImageBytesData, 0, nTotalImageBytesData);
int nOffset = nBytesPerLine - nImageWidthAfterRoate * m_pBitmapInfo->bmiHeader.biBitCount / 8;
for(unsigned int nHeight = 0, int nHeightR =nImageHeightOri -1; nHeight < nImageHeightOri; nHeight++, nHeightR--)
{
for (unsigned int nWeidth = 0, int nWeidthR = nImageWidthOri -1; nWeidth < nImageWidthOri; nWeidth++, nWeidthR--)
{
int R, G, B, Alpha;
R = G = B = Alpha = 0;
Alpha = m_pImageBytesData[nBytesPerLineOri * nHeight + nWeidth * 4 + 3];
R = m_pImageBytesData[nBytesPerLineOri * nHeight + nWeidth * 4 + 2];
G = m_pImageBytesData[nBytesPerLineOri * nHeight + nWeidth * 4 + 1];
B = m_pImageBytesData[nBytesPerLineOri * nHeight + nWeidth * 4];
if (nAngle == 0)
{
//左转
pImageBytesData[nBytesPerLine * nWeidth + nHeightR * 4 +3] = Alpha;
pImageBytesData[nBytesPerLine * nWeidth + nHeightR * 4 +2] = R;
pImageBytesData[nBytesPerLine * nWeidth + nHeightR * 4 + 1] = G;
pImageBytesData[nBytesPerLine * nWeidth + nHeightR * 4] = B;
}
else
{
//右转
pImageBytesData[nBytesPerLine * nWeidthR + nHeight * 4 +3] = Alpha;
pImageBytesData[nBytesPerLine * nWeidthR + nHeight * 4 +2] = R;
pImageBytesData[nBytesPerLine * nWeidthR + nHeight * 4 + 1] = G;
pImageBytesData[nBytesPerLine * nWeidthR + nHeight * 4] = B;
}
}
}
//删除原来的图像数据
delete []m_pImageBytesData;
m_pImageBytesData = NULL;
m_pImageBytesData = new BYTE[nTotalImageBytesData];
memset(m_pImageBytesData, 0, nTotalImageBytesData);
memcpy(m_pImageBytesData, pImageBytesData, nTotalImageBytesData); //赋值旋转后数据给m_pImageBytesData
delete [] pImageBytesData;
pImageBytesData = NULL;
//在m_pBitmapInfo宽高互换
m_pBitmapInfo->bmiHeader.biWidth = nImageWidthAfterRoate;
m_pBitmapInfo->bmiHeader.biHeight = nImageHeightAfterRoate;
}
灰度图像处理。百度百科:把白色与黑色之间按对数关系分为若干等级,称为灰度。灰度分为256阶。用灰度表示的图像称作灰度图。灰度图的好处是每个像素的RGB分量都是一样的,如R=G=B =250, 虽然失去彩色,但是也能很直观的显示图像,对后期的边缘检测,二值化等实现起来会很方便。至于如何修改RGB分量的值,网上有很多算法,我只是取了其中一个。
/*****************************************************************************************
* ConvertToGrayBitmap()
* 将彩色图转为灰度图,灰度图的每个RGB分量都是一样的
* 转换公式:Gray = (R*30 + G*59 + B*11 + 50) / 100
*****************************************************************************************/
BOOL CImageEditor::ConvertToGrayBitmap()
{
if (m_bIsGrayImage)
return FALSE;
unsigned int nImageHeight = abs(m_pBitmapInfo->bmiHeader.biHeight);
unsigned int nImageWidth = m_pBitmapInfo->bmiHeader.biWidth;
unsigned int nBytesPerLine = ((nImageWidth * m_pBitmapInfo->bmiHeader.biBitCount + 31) / 32) * 4;
int nOffset = nBytesPerLine - nImageWidth * m_pBitmapInfo->bmiHeader.biBitCount / 8;
int R, G, B, Alpha, Gray;
R = G = B = Alpha = Gray = 0;
for(unsigned int nHeight = 0; nHeight < nImageHeight; nHeight++)
{
for (unsigned int nWidth = 0; nWidth < nImageWidth; nWidth++)
{
Alpha = m_pImageBytesData[nBytesPerLine * nHeight + nWidth * 4 + 3];
R = m_pImageBytesData[nBytesPerLine * nHeight + nWidth * 4 + 2];
G = m_pImageBytesData[nBytesPerLine * nHeight + nWidth * 4 + 1];
B = m_pImageBytesData[nBytesPerLine * nHeight + nWidth * 4];
Gray = (R*30 + G*59 + B*11 + 50) / 100;
m_pImageBytesData[nBytesPerLine * nHeight + nWidth * 4 + 3] = Alpha;
m_pImageBytesData[nBytesPerLine * nHeight + nWid