使用C ++和Qt,如何将16位原始文件显示为图像? [英] With C++ and Qt how do I display a 16-bit raw file as an image?
问题描述
我希望将视频游戏《战地风云2》中的高度图显示为应用程序中的图像.
我是C ++和Qt的新手,可能很简单,但是我遇到的麻烦是显示灰度16-bit 1025x1025 2101250
字节图像.该文件没有标题.
我需要访问显示的像素(不必达到像素的完美精度),所以我可以指向一个像素并获取其值.
I am looking to display heightmaps from the video game Battlefield 2 as images in my application.
I am new to C++ and Qt and it might be straight forward but what I am having trouble with is displaying a grayscale 16-bit 1025x1025 2101250
bytes image. There is no header to the file.
I need access to the displayed pixels (does not have to be pixel perfect precision) so I can point to a pixel and get its value.
我尝试过的事情
我已经将二进制数据从QFile加载到QByteArray
中,并且尝试使用QImage::fromData
函数制作图像,但是我犯了很多错误,并且花了很多时间并没有走得太远.我希望在这里发帖会给我一些我需要进步的线索.
这是我的代码:
I have loaded the binary data into a QByteArray
from a QFile and I have attempted to use the QImage::fromData
function to make the image but I am making a lot of mistakes and spending a lot of time not getting very far. It is my hope that posting here will give me the clue(s) that I need to progress.
Here is my code:
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文档,但是我是新手,我需要一个正确方向的指南以了解此问题并获得解决方案.
From here I am at a loss of what to do. I have read some of the Qt documentation, but I being new I need a guide in the right direction to understand this problem and get the solution.
请注意,我几乎是一个初学者,所以不要打折我可能没有想到的简单解决方案.我只想使用Qt框架来做到这一点.
Please note I am pretty much a beginner so don't discount easy solutions that I might not have thought of. I would like to do this just using the Qt framework.
推荐答案
只是一个猜测.也许尝试这样的事情?
Just a guess. Maybe try something like this?
#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
这篇关于使用C ++和Qt,如何将16位原始文件显示为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!