Important: Please read the Qt Code of Conduct -

Raw pixel data to QPixmap without copying data

  • Hey there,

    Is there a way I can create a QPixmap directly out of raw pixel data without copying this data?

    QImage myImage = new QImage( ucharDataArray, w, h, w3, QImage::Format_RGB888);
    QPixmap *myPixmap = new QPixmap();
    *myPixmap = myPixmap->fromImage(*myImage);

    As I create the QImage out of the data, this works to pass the data and not copy it. But when I try to create a QPixmap out uf the QImage it starts to copy the data. As I'm working with images that are of huge sizes (>1GB) this is something I need to avoid.

    One way would be to write a known Image header like the one from "BMP" in front of the data array, so the QPixmap::loadFromData method would regocnize the data as valable image. But this seems to be quite a hack. Does anyone know a simpler way? Thx for help!

  • Maybe, you can load your data direclty into a QPixmap?

    @bool QPixmap::loadFromData(...)@

  • Yes I tried that, but therefore I need to have raw data in a defined image format. These are BMP, GIF, JPG, JPEG, PNG, PBM, PGM, PPM , XBM, XPM.
    If I don't have any header, but only raw pixel data, the method doesn't accept the data as valable image and thus doesn't load anything.

  • The QPixmap class is an off-screen image representation that can be used as a paint device.

    The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.

    Both class are quite different in terms of implementation, and i don't think it's currently possible to copy without data-copy...

    Why do you need a QPixmap?

  • That may be the reason, that the implementation of both classes are different. Although I wonder how a QPixmap is representet. Because I can't think of a more naked representation, than a plain array that contains every pixel with 3 rgb 8-bit values in a row and the width information so you know where to start a new row.

    I need a QPixmap because its way faster to show it on screen than a QImage. Tried it once with QImage and the performance differences with such huge images are enormous.

  • If this is about Qt 5 then the difference between QImage and QPixmap is negligible on most platforms given that a QPixmap will usually be backed by a QImage under the hood.

    Qt 4 was different in this respect since QPixmap was more likely to be backed by a platform-specific native image type there. This is not the case in 5.

  • So in terms of Qt5 this means that if I put a QImage into my QGraphicsScene, the performance should stay the same or that it should be no problem to pass a QImage to a QPixmap without copying data?

Log in to reply