如何在 QImage 中显示此数据缓冲区
我有一个图像试图在 QImage 中显示。
这是填充 rows*cols 图像的代码片段:
rgbMapped[row][col * 3] = red;
rgbMapped[row][col * 3 + 1] = green;
rgbMapped[row][col * 3 + 2] = blue;
如您所见,我的数据缓冲区是“rows-high”并且是“cols*3 width”
rgbMapped 是一个无符号 char** 数组。所以回到我的 QT 代码中,我有以下内容:
QImage *qi = new QImage(getWidth(), getHeight(), QImage::Format_RGB888);
for (int h = 0; h< getHeight(); h++){
memcpy(qi->scanLine(h), rgbMapped[h], getWidth()*3);
}
QPixmap p(QPixmap::fromImage(*qi,Qt::ColorOnly));
if(scene.items().contains(item)){
scene.removeItem(item);
}
item = new ImagePixmapItem(p);
scene.addItem(item);
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
ImagePixMapItem 是我创建的一个 QGraphicsPixmapItem,它允许我拦截一些鼠标事件,但我不会对任何绘画函数或任何东西做任何事情。
当我运行这段代码时,我的返回结果是一张看起来像我的图像的图像,除了有三个副本,一个具有绿色色调,一个看起来呈黄色,一个具有明显的紫色色调。
如果这三块数据……彼此重叠,这似乎可能是正确的图像?
I have an image I am trying to display in a QImage.
This is the snippet of code that populates the rows*cols image:
rgbMapped[row][col * 3] = red;
rgbMapped[row][col * 3 + 1] = green;
rgbMapped[row][col * 3 + 2] = blue;
As you can see, my data buffer is "rows-high" and is "cols*3 wide"
rgbMapped is an unsigned char** array. So back in my QT code I have the following:
QImage *qi = new QImage(getWidth(), getHeight(), QImage::Format_RGB888);
for (int h = 0; h< getHeight(); h++){
memcpy(qi->scanLine(h), rgbMapped[h], getWidth()*3);
}
QPixmap p(QPixmap::fromImage(*qi,Qt::ColorOnly));
if(scene.items().contains(item)){
scene.removeItem(item);
}
item = new ImagePixmapItem(p);
scene.addItem(item);
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();
ImagePixMapItem is a QGraphicsPixmapItem that I have created to allow me to intercept some mouse events, but I dindt do anyhting with any of the paint functions or anything.
When I run this code, my return comes back as an image that looks like my image, except there are three copies, one with a green tint, one looking yellow-ish and one with a noticeable purple tint.
It seems like maybe it would be the correct image if these three pieces of data were..overlayed on each other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是一个假设,但从您提到的(错误)颜色来看,我怀疑问题可能出在您关于 char **rgbMapped 变量的分配/初始化代码上。
您能发布这段代码吗?
我将尝试在下面编写一个可能正确的(?)初始化代码
只是为了给您一个可能有帮助的提示(我还没有编译代码,
因此,我对任何语法错误表示歉意)。
我使用 malloc() 但您也可以使用 new() 运算符。
重要的部分是行指针的初始化(您忘记了 *3 吗?)。
只是我的2c。
Just an assumption, but from the (wrong) colors you mentioned, I suspect the problem could be with your allocation/initialization code regarding the
char **rgbMapped
variable.Could you please post this code?
I will try to write bellow a possibly correct(?) initialization code
just to give you a hint which may help (I haven't compile the code,
therefore I apologize for any syntax errors).
I use malloc() but you can also use the new() operator.
The important part is the initialization of row pointers (did you forget the *3?).
Just my 2c.
您是否考虑了步幅?
每个扫描线必须从 4 字节边界开始。
而且它可能不是压缩像素格式,因此每个像素是 4 个字节而不是 3 个字节
Are you accounting for stride?
Each scanline must begin on a 4 byte boundary.
Also it may not be a packed pixel format, so each pixel is 4 bytes not 3