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

QByteArray dont working correctly for returned QImage



  • QByteArray don't working correctly when use Qimage returned from other function;

    we have simple ImageProvider class:

    ImageProvider::ImageProvider(QObject *parent) : QObject(parent)
    {
        image = QImage("D:/01.bmp"); // monochrome image
    }
    
    QImage ImageProvider::getImage()
    {
        return image;
    }
    

    and ImageConverter class:

    ImageConverter::ImageConverter(QObject *parent) : QObject(parent)
    {
        imgProvider = new ImageProvider(this);
    }
    
    void ImageConverter::convert()
    {
        QByteArray bytesArray= QByteArray::fromRawData(
                             (const char*)imgProvider->getImage().bits(),
                                          imgProvider->getImage().byteCount());
    
        QByteArray bytesStream;
        QDataStream ds(&bytesStream, QIODevice::ReadWrite);
        ds.writeRawData((const char*)imgProvider->getImage().bits(),
                                     imgProvider->getImage().byteCount());
    
        qDebug() << "length : " << bytesArray.length() << " = " << bytesStream.length();
        for (int i = 0; i < bytesArray.length(); ++i) {
            if (bytesArray.at(i) != bytesStream.at(i)) {
                qDebug() << i << " : " << (int)bytesArray.at(i) << " = "
                                       << (int)bytesStream.at(i) << " = "
                                       << (int)imgProvider->getImage().bits()[i];
            }
        }
    }
    

    Output of program:

    98304  -  98304
    0  :  -60  =  0  =  0
    2  :  -105  =  0  =  0
    3  :  58  =  0  =  0
    4  :  -32  =  0  =  0
    5  :  52  =  0  =  0
    6  :  -10  =  0  =  0
    7  :  59  =  0  =  0
    

    why QByteArray don't working correctly for some bytes ?

    • Now if change
    QImage ImageProvider::getImage()
    

    to const

    const QImage ImageProvider::getImage()
    

    OR

    • if in function
    void ImageConverter::convert()
    

    add

    QImage img = imgPro->getImage();
    

    and use img instead imgProvider->getImage()
    QByteArray working correctly.

    why QDataStream working correctly anyway ?



  • Because you are using a pointer to the internal data of a temporary variable.
    The correct way to do this is:

    QByteArray bytesArray
    QBuffer buff(&bytesArray);
    if(buff.open(QIODevice::WriteOnly))
        imgProvider->getImage().save(&buff,"PNG");
    

  • Lifetime Qt Champion

    Hi @AliPanahi2,

    citing the documentation of QByteArray::fromRawData():

    The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified.

    In your call, that is indeed not guaranteed, if you don't store the result of imgProvider->getImage() in a local variable.

    Regards


Log in to reply