使用 Format Indexed8 的 QImage 伪着色
QImage false coloring with Format Indexed8
我目前在使用我生成的颜色图对 QImage 进行假着色时遇到了一些麻烦。我已将代码精简为一些基本步骤(通常分布在多个 类)。
我可以用这段代码重现问题:
//fill color table
QVector< QRgb > colors;
for (unsigned int i = 0; i < 256; ++i) {
double fac = (double(i) / 255.) * 0.8;
QColor color = QColor::fromHslF(fac, 0.95, 0.5);
colors.push_front(color.rgba());
}
//load original
QImage origImg;
origImg.load("lena.jpg");
QImage::Format f = origImg.format(); //outputs Format_Grayscale8 (24)
origImg.save("out1.jpg");
//convert to pixmap
QPixmap pixmap;
pixmap.convertFromImage(origImg);
//convert back to Image
QImage tmp = pixmap.toImage();
tmp.save("out2.jpg");
//make false color version
QImage fc = QImage(tmp.bits(), tmp.width(), tmp.height(), QImage::Format_Indexed8);
fc.setColorTable(colors);
//save false color version
fc.save("fc.jpg");
我的临时结果 out1.jpg 和 out2.jpg 看起来不错(原件的副本)但最终结果已损坏。谁能告诉我出了什么问题?
原图:
fc.jpg
的输出
改变
QImage tmp = pixmap.toImage();
到
QImage tmp = pixmap.toImage().convertToFormat(QImage::Format_Grayscale8);
QPixmap 将图像存储为 RGB,因此当您从 QPixmap 转换为 QImage 时,格式将为 32 bpp (Format_RGB32
)。转换回 Format_Grayscale8
,将为您提供预期的 8 bpp 灰度格式。
我目前在使用我生成的颜色图对 QImage 进行假着色时遇到了一些麻烦。我已将代码精简为一些基本步骤(通常分布在多个 类)。
我可以用这段代码重现问题:
//fill color table
QVector< QRgb > colors;
for (unsigned int i = 0; i < 256; ++i) {
double fac = (double(i) / 255.) * 0.8;
QColor color = QColor::fromHslF(fac, 0.95, 0.5);
colors.push_front(color.rgba());
}
//load original
QImage origImg;
origImg.load("lena.jpg");
QImage::Format f = origImg.format(); //outputs Format_Grayscale8 (24)
origImg.save("out1.jpg");
//convert to pixmap
QPixmap pixmap;
pixmap.convertFromImage(origImg);
//convert back to Image
QImage tmp = pixmap.toImage();
tmp.save("out2.jpg");
//make false color version
QImage fc = QImage(tmp.bits(), tmp.width(), tmp.height(), QImage::Format_Indexed8);
fc.setColorTable(colors);
//save false color version
fc.save("fc.jpg");
我的临时结果 out1.jpg 和 out2.jpg 看起来不错(原件的副本)但最终结果已损坏。谁能告诉我出了什么问题?
原图:
fc.jpg
的输出改变
QImage tmp = pixmap.toImage();
到
QImage tmp = pixmap.toImage().convertToFormat(QImage::Format_Grayscale8);
QPixmap 将图像存储为 RGB,因此当您从 QPixmap 转换为 QImage 时,格式将为 32 bpp (Format_RGB32
)。转换回 Format_Grayscale8
,将为您提供预期的 8 bpp 灰度格式。