4

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.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
mimo
  • 2,469
  • 2
  • 28
  • 49

2 Answers2

3

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);
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
0

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};
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313