mimo
mimo

Reputation: 2629

QImage: convert from grayscale to RGB with a color table

With Qt, I am trying to convert a Format_Indexed8 image to Format_RGB30 by using a custom conversion rule defined by a color table. I thought this would be simple, since QImage::convertToFormat can take a color table as an argument, but I can't make it work.

Here is my code:

QImage image = QImage(data, width, height, QImage::Format_Indexed8);
QVector<QRgb> colorTable(256);
for (int i = 0; i < 255; i++)
    colorTable[i] = qRgb(255 - i, i, i);
image = image.convertToFormat(QImage::Format_RGB30, colorTable);

This code just gives me an image that is in RGB format, but that looks identical to the eye to the grayscale image.

Upvotes: 4

Views: 3784

Answers (2)

This is not necessary since Qt 5.5 (released in July of 2015). You can now use QImage::Format_Grayscale8. Your code would be simply:

QImage image{data, width, height, QImage::Format_Grayscale8};

Upvotes: 0

p-a-o-l-o
p-a-o-l-o

Reputation: 10077

I think the color table argument in QImage::convertToFormat is required to convert from RGB to indexed, while you're converting the other way around.

I would try setting the color table directly in the indexed file (source), using QImage::setColorTable, then call convertToFormat passing the format argument only:

QImage image = QImage(data, width, height, QImage::Format_Indexed8);
image.setColorTable(colorTable);
image = image.convertToFormat(QImage::Format_RGB30);

Upvotes: 3

Related Questions