我期待显示高度地图从视频游戏战场2作为图像在我的应用程序。
我是C++和Qt的新手,它可能是直接的,但我遇到的问题是显示一个灰度16-bit 1025x1025 2101250
字节图像。文件没有头文件。我需要访问显示的像素(不一定是像素的完美精度),所以我可以指向一个像素并得到它的值。
我已经尝试过的
我已经将二进制数据从一个QByteArray
中加载到一个QFile中,并且我尝试使用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框架来完成这个任务。
发布于 2012-06-28 06:53:48
只是猜一下。也许可以试试这样的东西?
#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')
编辑:oops,QImage::Format_RGB32
而不是QImage::Format_RGB
发布于 2012-06-28 07:06:15
您不能使用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 */
}
发布于 2012-06-28 06:49:18
因此,首先创建一个具有对话框的应用程序,然后使用以下建议引用该对话框:
对于QPainter文档:http://qt-project.org/doc/qt-4.8/QPainter.html
注意,您需要在浏览像素时创建一个QBrush:http://doc.qt.nokia.com/4.7/qbrush.html
这会很慢的:
https://stackoverflow.com/questions/11239203
复制相似问题