使用 C++ 和 Qt 如何将 16 位原始文件显示为图像

With C++ and Qt how do I display a 16-bit raw file as an image?

本文关键字:原始 文件 显示 图像 C++ Qt 使用      更新时间:2023-10-16

我希望在我的应用程序中将视频游戏《战地2》中的高度图显示为图像。
我是C++和Qt的新手,它可能很简单,但我遇到的问题是显示灰度16-bit 1025x1025 2101250字节图像。文件没有标头。我需要访问显示的像素(不必是像素完美的精度),以便我可以指向一个像素并获取其值。

我试过什么

我已经将二进制数据从 QFile 加载到QByteArray中,并尝试使用 QImage::fromData 函数制作图像,但我犯了很多错误,并且花费了很多时间没有走得太远。我希望在这里发帖能给我进步所需的线索。这是我的代码:

void LearningBinaryReader::setupReader()
{
    qDebug("Attempting to open file..");
    QFile file("HeightmapPrimary.raw");
    if (!file.open(QFile::ReadOnly))
    {
        qDebug("Could not open file");
        return;
    } else {
        qDebug() << file.fileName() << " opened";
    }
    QByteArray data = file.readAll();
    file.flush();
    file.close();
    qDebug() << data.count() << "bytes loaded.";
}

从这里我不知所措。我已经阅读了一些Qt文档,但是我是新手,我需要正确方向的指南来理解这个问题并获得解决方案。

请注意,我几乎是一个初学者,所以不要忽视我可能没有想到的简单解决方案。我想只使用Qt框架来做到这一点。

只是一个猜测。 也许尝试这样的事情?

#include <QColor>
...
QByteArray data=file.readAll();
// create an empty image of the right size.  We'll use 32-bit RGB for simplicity
QImage img(1025,1025, QImage::Format_RGB32);
// Access the image at low level.  From the manual, a 32-bit RGB image is just a
// vector of QRgb (which is really just some integer typedef)
QRgb *pixels=reinterpret_cast<QRgb*>(img.bits());
// Now copy our image data in.  We'll assume 16-bit LE format for the input data.
// Since we only have 8 bits of grayscale color resolution in a 32-bit RGB, we'll
// just chop off the most significant 8 bits (the second byte of each pair) and 
// make a pixel out of that.  If this doesn't work, our assumption might be off --
// perhaps assume 16-bit BE format.  In that case we'd want the first byte of each
// pair.
for (size_t i=0;2*i<data.size();++i)
{
    uchar pixel_msb=data[2*i+1]; // or maybe try =data[2*i+0]
    pixels[i]=qRgb(pixel_msb, pixel_msb, pixel_msb);
}
// (do something with the resulting 'img')

编辑:哎呀,QImage::Format_RGB32而不是QImage::Format_RGB

不能使用 loadFromData ,因为它不支持 raw(请参阅读取和写入图像文件)。

您找不到支持的格式的 16 位原始,因此我认为最好的解决方案是在图像加载和图像显示之间使用转换器。

创建一个新的 QImage,使用 qt 支持的格式。

QImage* image = new QImage(1025, 1025, QImage::Format_RGB888);

然后加载源图像并将其转换为RGB888。您的图像非常大,因此请避免使用readAll()加载所有图像。您可以使用这个简单的转换器(见下文)或现有库中的转换器(如Magick++)。

QFile file("HeightmapPrimary.raw");
if (!file.open(QFile::ReadOnly))
{
    qDebug("Could not open file");
    return;
}
uint16_t buf;
uchar* dst = image->bits();
while (readData(&buf, 2)) {
  dst[0] = buf / 256; /* from 16bit to 8bit */
  dst[1] = buf / 256;
  dst[2] = buf / 256;
  dst += 3; /* next pixel */
}

1.基本上你需要做的是在小部件中绘制像素。

因此,首先创建一个具有对话框的应用程序,然后使用以下建议在对话框上绘制:

Qt4 如何在小部件内部绘图?

对于 QPainter 文档:http://qt-project.org/doc/qt-4.8/QPainter.html

请注意,您需要在浏览像素时创建一个 QBrush:http://doc.qt.nokia.com/4.7/qbrush.html

这将非常缓慢:(

阿拉伯数字。另一个更高级的解决方案是创建一个QImageReader和一个关联的图像IO插件来读取您的格式(http://qt-project.org/doc/qt-4.8/qimagereader.html),然后使用图像阅读器(http://qt-project.org/doc/qt-4.8/qpixmap.html#fromImageReader)创建一个QPixMap,并使用给定的QPixmap创建一个QBrush并使用它来绘制:)