⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Efficient serialization QImage

  • Hi I am trying to implement fortune server - client example with one little difference that it sending images. Everythigs works but in pretty slow way. So the main difference between fortune examples and my code in sending and receiving parts.

    Server side looks like:

    QPixmap myPixmap = loadPixmap(...);
    QImage im;
    im = myPixmap.toImage();
    // time measure
    QTime t;
    // init block array
    QByteArray block;
    // init datastream  
    QDataStream out(&block, QIODevice::WriteOnly);
    // start time measure
    // reserve space 
    out << (quint32)0 ;
    // serialization image
    out << im;
    // Check size
    qDebug()<<"Data write byteArrray "<<block.size();
    // get back to start
    // write size
    out << (quint32)(block.size() - sizeof(quint32))
     qDebug("Time to serialize : %d ms", t.elapsed());
    // send 
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
                    clientConnection, SLOT(deleteLater()));
    // write
    qDebug("Time  to write : %d ms", t.elapsed());

    The client side looks like:

    QTime t;
    QDataStream in(tcpSocket);
    if (blockSize == 0) {
                if (tcpSocket->bytesAvailable() < sizeof(quint32))
                //receive size of image
                in >> blockSize;
                qDebug()<<"The Block size "<<blockSize;
            if (tcpSocket->bytesAvailable() < blockSize)
            QImage im;
             in >> im;
            qDebug("Time to read : %d ms", t.elapsed());

    Everything works the client side gets an image, but the problem is that time to serialize image is about 1 sec( on prety new computer) and to deserialize and read( on localhost) is about 500 ms. Are there any methods to do it in more efficiently way? I will be glad to any help.

  • did you try im.save(out.device(),"png"); instead of out << im; and im.load(in.device(),"png");instead of in >> im;

  • @VRonin Yes I tried that, this works but almoustly with the same speed.

    On the server side I change:

        //out << im;
        qDebug()<<"In save im "<<im.save(out.device(),"PNG",-1);

    On the client:

    // in>>im
    qDebug()<<"Load "<<im.load(in.device(),"PNG");

    Both return true. But save takes 1198 ms (QImage size(1920 x 1080) and ByteArray.size() == 2510190) and to load 287 ms.

  • @VRonin Thank you for help. The idea to save and load to the device is greate. It seems that when Qt serialize the image it serialize it as png and for my aims I can use just JPG. So after some editing I changed it. Whats more that I acttually don't need Qimage so acctuly can. In the end my Client side look:

    qDebug()<<"Load "<<im.load(in.device(),"JPG");

    and my server side:

    myPixmap = loadPixmap(...)
    qDebug()<<"In save im "<<myPixmap.save(out.device(),"JPG",-1);

Log in to reply