4

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?
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
DEgITx
  • 960
  • 1
  • 13
  • 24
  • 1
    You don't need the memcpy. QImage has also a constructor that takes the image data as a parameter: http://qt-project.org/doc/qt-4.8/qimage.html#QImage-5 –  Jun 26 '12 at 16:27

2 Answers2

2

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."

hmuelner
  • 8,093
  • 1
  • 28
  • 39
  • pixmap.loadFromData(m_frameRGB->data[0], height * width * 3, "PPM"); // still doesn't work and return false – DEgITx Jun 26 '12 at 16:28
  • 2
    Obviously your data is not in PPM format so that doesn't work. Your data seems to be just raw pixel data without any jpg/ppm/png/... headers. So you need to create the QPixmap from QImage. –  Jun 26 '12 at 16:32
  • like it was before, i think... so there is no other more faster and optimized ways to map pixel data to widget (without opengl)? – DEgITx Jun 26 '12 at 16:46
  • 2
    I should be possible to add a PPM header in front of your buffer consisting of "P6 width_in_ascii height_in_ascii 255 ". See e.g. http://netpbm.sourceforge.net/doc/ppm.html – hmuelner Jun 27 '12 at 11:15
  • Thanks, it helped. It's working, and faster... little faster than load from QImage::fromImage() – DEgITx Jun 27 '12 at 14:55
  • @DEgITx any chance u could update the question with the correct way of doing it? Got the same issue here. – Dariusz Jan 15 '18 at 02:23
1

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());
denis
  • 11
  • 1