本文主要是介绍[开源]Qt图片调整之饱和度调节,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原理较简单不作详述
QImage AdjustSaturation(QImage Img, int iSaturateValue)
{int red, green, blue, nRed, nGreen, nBlue;int pixels = Img.width() * Img.height();unsigned int *data = (unsigned int *)Img.bits();float Increment = iSaturateValue/100.0;float delta = 0;float minVal, maxVal;float L, S;float alpha;for (int i = 0; i < pixels; ++i){nRed = qRed(data[i]);nGreen = qGreen(data[i]);nBlue = qBlue(data[i]);minVal = std::min(std::min(nRed, nGreen), nBlue);maxVal = std::max(std::max(nRed, nGreen), nBlue);delta = (maxVal - minVal) / 255.0;L = 0.5*(maxVal + minVal) / 255.0;S = std::max(0.5*delta / L, 0.5*delta / (1 - L));if (Increment > 0){alpha = std::max(S, 1 - Increment);alpha = 1.0 / alpha - 1;red = nRed + (nRed - L*255.0)*alpha;red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;green = nGreen + (nGreen - L*255.0)*alpha;green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;blue = nBlue + (nBlue - L*255.0)*alpha;blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue;}else{alpha = Increment;red = L*255.0 + (nRed - L * 255.0)*(1+alpha);red = (red < 0x00) ? 0x00 : (red > 0xff) ? 0xff : red;green = L*255.0 + (nGreen - L * 255.0)*(1+alpha);green = (green < 0x00) ? 0x00 : (green > 0xff) ? 0xff : green;blue = L*255.0 + (nBlue - L * 255.0)*(1+alpha);blue = (blue < 0x00) ? 0x00 : (blue > 0xff) ? 0xff : blue;}data[i] = qRgba(red, green, blue, qAlpha(data[i]));}return Img;
}
这篇关于[开源]Qt图片调整之饱和度调节的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!