Details
-
Bug
-
Resolution: Invalid
-
Not Evaluated
-
None
-
5.10.1
-
None
Description
I'm trying to convert an image to Grayscale8.
QHash<QRgb, qint32 (*)(QRgb)> colorFuncs { { Qt::red, qRed}, { Qt::green, qGreen}, { Qt::blue, qBlue}, { Qt::gray, qGray}, }; QImage color8(const QImage &image, int channel, QImage::Format outFormat) { QImage out(image.width(), image.height(), outFormat); int (*colorFunc)(QRgb rgb) = colorFuncs[channel]; if (QImage::Format_Indexed8 == out.format()) { QVector<QRgb> values; values.reserve(256); if (channel==Qt::gray) { for (int c=0; c<256; c++) values.append(qRgb(c,c,c)); } if (channel==Qt::red) { for (int c=0; c<256; c++) values.append(qRgb(c,0,0)); } if (channel==Qt::green){ for (int c=0; c<256; c++) values.append(qRgb(0,c,0)); } if (channel==Qt::blue) { for (int c=0; c<256; c++) values.append(qRgb(0,0,c)); } out.setColorTable(values); } for (int y=0; y < image.height(); y++) { for (int x=0; x < image.width(); x++) { out.setPixel(x,y, colorFunc(image.pixel(x,y))); } } return out; }
QImage gray8 = color8(image, Qt::gray, QImage::Format_Grayscale8); QImage index = color8(image, Qt::gray, QImage::Format_Indexed8); int differences = 0; int same = 0; for (int y=0; y < gray8.height(); y++) { for (int x=0; x < gray8.width(); x++) { //qDebug() << "gray" << gray8.pixel(x,y) << "index" << index.pixel(x,y); if (gray8.pixel(x,y) != index.pixel(x,y) ) differences++; else same++; } } qDebug() << "differences" << differences; qDebug() << "same" << same;
However every pixel in gray8 is 0xFF000000. So it seems that setPixel() does not work with Grayscale8?
Saving the image (to png) also results in a perfectly black image.