Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-81598

QImage::save/load functions don't retain QImage::Format after writing/reading data to/from IODevice

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.13.2
    • Image formats
    • None
    • GCC 9.2.0
      Linux 4.19.86
      Qt 5.13.2
    • Linux/X11

    Description

      Hello.

      I'd like to report a bug about a couple of QImage functions regarding serialization. When I create an QImage with Format_RGB888 and save it to a buffer the format doesn't seem to be saved properly, as when I load the image from the buffer and save the data to a different QImage, the format will become Format_RGB32. This also means, that a comparison of the source and result images will not return true (in case of this format at least). Moreover, if one relies heavily on the layout of the raw image data (e.g. using it with OpenCV's cv::Mat format), such an action will break things, because the change in format happens without notice.

      Because of the aforementioned problems, I consider this behavior to be a bug.

      Here's some code to reproduce:

       

      int main() {
          QByteArray byteArray, byteArray2;
          QBuffer buffer, buffer2;
          QDataStream dataStream;
          QImage sourceImg, destImg, destImg2;
      
          buffer.setBuffer(&byteArray);
          buffer2.setBuffer(&byteArray2);
          dataStream.setDevice(&buffer2);
          sourceImg = QImage(8, 14, QImage::Format_RGB888);
          sourceImg.fill(QColor(10, 87, 145));
      
          buffer.open(QIODevice::WriteOnly);
          sourceImg.save(&buffer, "PNG");
          buffer.close();
      
          buffer2.open(QIODevice::WriteOnly);
          dataStream << sourceImg;
          buffer2.close();
      
          buffer.open(QIODevice::ReadOnly);
          destImg.load(&buffer, "PNG");
         
          buffer2.open(QIODevice::ReadOnly);
          dataStream >> destImg2;
      
          qDebug() << sourceImg;
          qDebug() << destImg;
          qDebug() << destImg2;
      }
      

      The output will be the following:

      QImage(QSize(10,  5),format=QImage::Format_RGB888,depth=24,devicePixelRatio=1,bytesPerLine=32,sizeInBytes=160)
      QImage(QSize(10, 5),format=QImage::Format_RGB32,depth=32,devicePixelRatio=1,bytesPerLine=40,sizeInBytes=200)
      QImage(QSize(10, 5),format=QImage::Format_RGB32,depth=32,devicePixelRatio=1,bytesPerLine=40,sizeInBytes=200)
      

      As it is clearly visible above, not just the save and load functions are affected, but the input and output operators as well (I guess they use the same code internally?). It would be nice if any such serialization would retain the original format exactly, as a (compatible) replacement format might not be good enough under some circumstances.

       

      Thanks for reading and considering a patch.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vgt Eirik Aavitsland
            rpuskas Robert Puskas
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes