Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QImage::loadFromData failing



  • I'm building an image viewer app for raw files (extracting the embedded jpeg). I am sending a list of all the image absolute paths to a function that:

    • extracts the embedded jpeg into a QByteArray

    • creates a QImage via loadFromData

    • creates a Pixmap via from Image

    • adds the Pixmap to QPixmapCache

      void ImageView::loadImageCache(QFileInfoList &imageList)
      {
          QPixmapCache::clear();
          QPixmapCache::setCacheLimit(10240000);   //10GB
          for (int i=0; i < imageList.size(); ++i) {
              QFileInfo fileInfo = imageList.at(i);
              QString imageFullPath = fileInfo.absoluteFilePath();
              QPixmap pm;
              loadPixmap(imageFullPath, pm);
              QPixmapCache::insert(imageFullPath, pm);
           }
      }
      
      void ImageView::loadPixmap(QString &imageFullPath, QPixmap &pm)
      {
          QImage image;
          QFileInfo fileInfo(imageFullPath);
          QFile imFile(imageFullPath);
          imFile.open(QIODevice::ReadOnly);       
          imFile.seek(mdCache->getOffsetFullJPG(imageFullPath));
          QByteArray buf = imFile.read(mdCache->getLengthFullJPG(imageFullPath));
          bool imLoaded = image.loadFromData(buf, "JPEG");  // errors out after multiple calls
          pm = QPixmap::fromImage(image);
      }
      

    When working on a folder with 36MP NEF files bool imLoaded becomes false and stays that way for all repeated executions for the remaining files in the folder after 5-6 files have been processed. When I send a folder of smaller jpegs I may get 30 files before imLoaded becomes false. The accumulated memory for the Pixmaps is usually around 1.2 - 1.4 GB when the image.loadFromData statement returns false. The memory estimate is based on (width * height * depth)/8 bytes. The QByteArray buf continues to work, no matter how many files I process.

    I'm hoping I am doing something obviously wrong. If not, I'm hoping someone can give me some advice on how to proceed to solve this.

    Running Qt 5.5 Win 10

    Thanks in advance
    Rory



  • From QPixmap Documentation:
    Note that the pixel data in a pixmap is internal and is managed by the underlying window system.

    So, for Windows:

    • Pixmaps are stored as GDI-Objects
    • Memory for GDI-Objects is taken from windows paged kernel memory
    • paged kernel memory is limited by windows (on my 12GB win7 its around 1GB)

    If you really need to have 10GB of Images in memory you need

    • use QImage for storing data in memory
    • write your own Cache handling those Images
    • use Pixmap::fromImage(...) to display the Images

    Hope that helps
    gerd



  • @Gerd

    Thanks Gerd. I'll start researching how to do that. Very much appreciated!

    Rory


Log in to reply