Better way to load QPixmap data

情到浓时终转凉″ 提交于 2019-12-23 19:41:07

问题


Better way to do it (without QImage)?:

QImage image(width, height, QImage::Format_RGB888);
memcpy(image.bits(), m_frameRGB->data[0], height * width * 3);
QPixmap pixmap = QPixmap::fromImage(image);

I don't see any reason to use QImage as intermediate buffer, but QPixmap::loadFromData don't load data with this context:

pixmap.loadFromData(m_frameRGB->data[0], height * width * 3); // Need pixmap resize?

回答1:


The documentation says: "If the format is not specified (which is the default), the loader probes the file for a header to guess the file format". You only provide the pointer to the raw image bytes, but you have to provide a header at the start of the buffer, e.g. for an uncompressed PPM format.

Edit: You could also test the suggestion of Roku to use the QImage constructor which takes the image data as a parameter, but see the remark in the documentation: "The buffer must remain valid throughout the life of the QImage."




回答2:


followed hmuelner hint and ... it's really little faster than QPixmap pixmap = QPixmap::fromImage(image);

QPixmap pixmap(height, width);
QByteArray pixData;
pixData.append(QString("P6 %1 %2 255 ").arg(width).arg(height));
pixData.append((char *)m_frameRGB->data[0], width * height * 3);
pixmap.loadFromData((uchar *)pixData.data(), pixData.size());


来源:https://stackoverflow.com/questions/11210884/better-way-to-load-qpixmap-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!