基本所有图像美化app都有该特效,有的app叫晕影效果,有的app叫暗角效果,其实就是以图像为中心,提高图像四角的亮度或者降低亮度。为图像四角提高亮度的不多,更多的还是给图像增加暗角效果,以突出图像主体。 尤其是很多lomo滤镜,基本都会加一个暗角。那么暗角是怎么实现的呢,看似很简单,但真做到ps般色调过渡自然、平滑,看着舒服,还是需要一番计算与调试的。算法总体上没有复杂原理与计算,看代码基本就明白了。
下面是相关示例代码(亮角: strength范围为[0.0f, 1.0f], 暗角: strength范围为[-1.0f, 0.0f]):
void ImageVignetting(BMPINFO *pSrcBitmap, float strength)
{
int width = pSrcBitmap->lWidth;
int height = pSrcBitmap->lHeight;
float radio = 0.0;
float maxLen = (float)MAX(width, height);
float cx = maxLen * 0.5f;
float cy = maxLen * 0.5f;
float maxDist = cx*cx + cy*cy;
uchar* pSrcData = pSrcBitmap->pPlane[0];
if (strength > 0.0f)
{
// 亮角
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++, pSrcData += 4)
{
float nx = (float)maxLen * j / width;
float ny = (float)maxLen * i / height;
float curDist = (nx - cx)*(nx - cx) + (ny - cy)*(ny - cy);
radio = curDist / maxDist;
// radio *= radio;
radio *= strength;
pSrcData[BLUE] = (uchar)CLAMP0255(pSrcData[BLUE]*(1.0f - radio) + SCREEN_XY(pSrcData[BLUE], 230)*radio);
pSrcData[GREEN] = (uchar)CLAMP0255(pSrcData[GREEN]*(1.0f - radio) + SCREEN_XY(pSrcData[GREEN], 230)*radio);
pSrcData[RED] = (uchar)CLAMP0255(pSrcData[RED]*(1.0f - radio) + SCREEN_XY(pSrcData[RED], 230)*radio);
}
}
}
else
{
// 暗角
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++, pSrcData += 4)
{
float nx = (float)maxLen * j / width;
float ny = (float)maxLen * i / height;
float curDist = (nx - cx)*(nx - cx) + (ny - cy)*(ny - cy);
radio = curDist / maxDist;
// radio *= radio;
radio *= strength;
radio = 1.0f + radio;
pSrcData[BLUE] = (uchar)CLAMP0255(pSrcData[BLUE]*radio);
pSrcData[GREEN] = (uchar)CLAMP0255(pSrcData[GREEN]*radio);
pSrcData[RED] = (uchar)CLAMP0255(pSrcData[RED]*radio);
}
}
}
}
下面是效果图: