c++ - 如何在 Qt QImage 中使用颜色 LUT

标签 c++ qt qt4 lookup-tables qimage

我有一些遗留代码正在写入 NITF 文件以显示一些图像。在遗留代码中,看起来好像使用了 LUT,并且有一段代码一次向 NITF 文件写入一行,并且该行的值的计算如下:

// convert RGB to LUT values
unsigned char *lutData = new unsigned char[numBytes/3];
for (unsigned j = 0 ; j < numBytes/3 ; j++)
    lutData[j] = (unsigned char) stuff;

其中 data 是我的原始无符号字符数组。

所以现在我尝试获取该数据数组并将其输出到 GUI 中的 QImage 中。

在我看来,在 NITF 中,有一个 LUT 数据 block 的大小为“行 x 列”,对吗?所以我创建了一个 lut 数据数组:

unsigned char *lutData = new unsigned char[imwidth * imheight];
QImage *qi = new QImage(imwidth,imheight, QImage::Format_Indexed8);
for (int i = 0 ; i < imheight ; i++)
{
             #pragma omp parallel for
              for (int j = 0 ; j < imwidth ; j++)
              {
                     lutData[i*imwidth + j] = stuff;
              }
}

然后我尝试像这样填充 qimage:

   for (int i = 0 ; i < imheight ; i++)
   {
                #pragma omp parallel for
                 for (int j = 0 ; j < imwidth ; j++)
                 {
                     qi->setPixel(j,i,qRgb(lutData[i*imwidth + j],lutData[i*imwidth + j],lutData[i*imwidth + j]));
                }
   }

但是,这似乎或多或少只是给了我一个灰度图像,而不是我的实际数据。

我做错了什么?

谢谢!

最佳答案

qRgb 构造函数如下所示:

qRgb(int r, int g, int b)

您为所有三种颜色传递相同的值 (lutData[i*imwidth + j]),因此最终会得到灰度图像。

现在,qRgb 只是一个 typedefed unsigned int,所以如果您将颜色存储在 format 中(RGB32/ARGB32),您只需调用:

qi->setPixel(j, i, lutData[i*imwidth + j])

但是您可能想考虑使用 QImage 的 built-in lookup表(又名颜色表)支持 - 它最终可能会像这样简单:

QImage image(data, imwidth, imheight, QImage::Format_Indexed8);
QVector<QRgb> colorTable;
// Translate each color in lutData to a QRgb and push it onto colorTable;
image.setColorTable(colorTable);

希望这有帮助!

更新:出于引用目的,这里是我用来在索引颜色模式下尝试 QImage 的测试代码(使用 g++ 编译时不会出现警告 - 只需记住链接到 -lQtCore 和 -lQtGui):

#include <QtCore/QVector>
#include <QtGui/QApplication>
#include <QtGui/QImage>
#include <QtGui/QLabel>
#include <QtGui/QPixmap>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    unsigned char indices[1024];
    for(int i = 0; i < 1024; ++i)
    {
        indices[i] = qrand() & 0x0f;
    }

    QVector<QRgb> ctable;
    for(int i = 0; i < 16; ++i)
    {
        ctable.append(qRgb(qrand() & 0xff, qrand() & 0xff, qrand() & 0xff));
    }

    QImage image(indices, 32, 32, QImage::Format_Indexed8);
    image.setColorTable(ctable);

    QLabel label;
    label.setPixmap(QPixmap::fromImage(image));
    label.show();

    return app.exec();
} 

关于c++ - 如何在 Qt QImage 中使用颜色 LUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5504768/

相关文章:

c++ - 将字符串返回给主函数C++的函数

c++ - 如何在 C++ 中逻辑组织源文件

c++ - sigc::mem_fun中的C++ “no match for call”错误

c++ - 将二维数组整数数据从 C++ 发送到 qml

qt - QDockWidget 关闭按钮和 float 按钮的工具提示?

Qt QTableView如何有一个只有复选框的列

c++ - 在 Qt4 中隐藏 QMenuBar 的条目?

c++ - 从 MATLAB diag(sqrt(v)) 转换为 C++ 中的 Eigen

c++ - 检查焦点是否被鼠标在 mousePressEvent 中按下改变

python - 使用 QClipboard,如何复制富文本并将其降级为文本编辑器的纯文本?