I have been trying to display a gray-scale image using Qt. The image data is loaded from a .txt file that contains 256x256 float data. There is no header involved for the im
How are you loading the QImage with the grey image data ?
QT doesn't have a greyscale image type, only a bilevel type. You can either create an RGB image with R=G=B=grey. Or more preferably use QImage::Format_Indexed8
and create a colour table where each entry has the same value as the index. i.e.
QImage *qi = new QImage(data_ptr, width, height, QImage::Format_Indexed8);
QVector<QRgb> my_table;
for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
qi->setColorTable(my_table);
QImage img = AImage;
if (!AImage.isNull())
{
int pixels = img.width() * img.height();
if (pixels*(int)sizeof(QRgb) <= img.byteCount())
{
QRgb *data = (QRgb *)img.bits();
for (int i = 0; i < pixels; i++)
{
int val = qGray(data[i]);
data[i] = qRgba(val, val, val, qAlpha(data[i]));
}
}
}
return img;
Use RGBA for good grayscale.