Loading DDS file to QImage from raw data

  • Hello,
    I would like to read a DDS file from an archive and show it using a QImage. I already have the code that is able to get the file contents from the zip file done, but for now I extracted the DDS file to a folder and am reading it from there.
    As I'm quite new to Qt, I'm using very simple code for now:

    int main(int argc, char *argv[])
    	QApplication app(argc, argv);
    	QFile file("helm.dds");
    	QByteArray bArray = file.readAll();
    	QImage image = QImage::fromData(bArray);//myImage.copy(0, 84, 102, 84).scaled(50, 50); //copying a part of the texture then reducing its size
    	QLabel label;
    	return app.exec();

    This code compiles, I don't have any issue with it.
    The problem comes from QImage::fromData. I'm not able to use "DDS" as the format as it's not supported, so I was wondering if there was any other alternative?
    Thank you!

  • Moderators

    Hi! You could you imagemagick to convert the DDS to PNG. You could do this either by calling the executable convert that comes with imagemagick or you could use imagemagick's C or C++ interface.

  • Hi, thank you for your reply!
    ImageMagick++ does not seem to do what I'm looking for unfortunately.
    I'd like to read / convert everything from memory instead of creating new files. So, read the DDS file in memory, (convert it to some other format if needed) load it into a QImage from a QByteArray (not necessarily a QByteArray though, but something that doesn't require me to create a new file).

  • Moderators

    Temporary files e.g. in /var/tmp aren't an option for you?

  • I've never used temporary files, but I did think of them as a last option if nothing else can help.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ? DDS is listed in the supported QImage formats

  • Moderators

    Yes, @SGaist is right; at least here with Qt 5.5.1 it works:

        const QString fileName("/home/pw/0.dds");
        const QPixmap pixmap(fileName);
        const QImage image = pixmap.toImage();
        qDebug() << (image.isNull() ? "QImage error" : "QImage ok");

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.