QT 彩色图转灰度图

彩色图转灰度图

核心函数
QImage Widget::toGray(QImage initImg)
{
int width = initImg.width();
int height = initImg.height();
QImage img(width,height,initImg.format());
img.setColorCount(256);
switch(initImg.format())
{
case QImage::Format_Indexed8:
for(int i =0; i {
const uchar *pSrc = (uchar *)initImg.constScanLine(i);
uchar *pDes = (uchar *)img.scanLine(i);
memcpy(pDes,pSrc,width);
}
break;
case QImage::Format_ARGB32:
case QImage::Format_ARGB32_Premultiplied:
case QImage::Format_RGB32:
for(int i =0;i< height ; i++)
{
const QRgb *pSrc = (QRgb *)initImg.constScanLine(i);//注意此时类型是QRgb
QRgb *pDes = (QRgb *)img.scanLine(i);
for(int j =0;j {
int gray =qGray( pSrc[j]);
pDes[j] = qRgba(gray,gray,gray,qAlpha(pSrc[j]));//原始透明度
}
}
break;
}
return img;
}

你可能感兴趣的:(Qt,Qt,灰度图)