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

Cannot save an indexed image from binary data as JPEG

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.5.1
    • Image formats
    • None
    • KUbuntu 14.04 32-bit

    Description

      Consider the following example:

      #include <QtCore>
      #include <QtGui>
      
      int main(int argc, char **argv)
      {
      #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
          QGuiApplication app(argc, argv);
      #else
          QApplication app(argc, argv);
      #endif
      
          QFile file("data.bin");
      
          if(!file.open(QFile::ReadOnly))
              qFatal("Cannot open file: %s", qPrintable(file.errorString()));
      
          QByteArray ba = file.readAll();
      
          QImage::Format format = QImage::Format_Indexed8;
          QVector<QRgb> table;
      
          for(int i = 0;i < 255;i++)
              table.append(qRgb(i, i, i));
      
          QImage image(reinterpret_cast<const uchar *>(ba.constData()),
                      850,
                      1400,
                      850,
                      format);
      
          if(image.isNull())
              qFatal("Image is null");
      
          image.setColorTable(table);
      
          //
          // Workaround:
          //
          // if(!image.convertToFormat(QImage::Format_RGB32).save("qt.jpg"))
          //    qFatal("Cannot save JPEG");
          //
      
          if(!image.save("qt.jpg"))
              qFatal("Cannot save JPEG");
      
          if(!image.save("qt.png"))
              qFatal("Cannot save PNG");
      
          qDebug("Success");
      
          return 0;
      }

      The sample code tries to save binary data as PNG and JPEG. PNG has been saved correctly. JPEG looks like a negative (see attach). To workaround the bug one needs to explicitly convert the image to the RGB format, then JPEG works fine too. Binary data is attached.

      Attachments

        1. qt.png
          144 kB
          Dmitry
        2. qt.jpg
          65 kB
          Dmitry
        3. data.bin
          1.13 MB
          Dmitry
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vgt Eirik Aavitsland
            dmitryrx Dmitry
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes