Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Efficient serialization QImage
Forum Update on Monday, May 27th 2025

Efficient serialization QImage

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guess11
    wrote on 12 Aug 2016, 08:27 last edited by
    #1

    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);
    out.setVersion(QDataStream::Qt_4_0);
    // start time measure
    t.start();
    // reserve space 
    out << (quint32)0 ;
    // serialization image
    out << im;
    // Check size
    qDebug()<<"Data write byteArrray "<<block.size();
    // get back to start
    out.device()->seek(0);
    // write size
    out << (quint32)(block.size() - sizeof(quint32))
     qDebug("Time to serialize : %d ms", t.elapsed());
    // send 
     t.restart();
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
                    clientConnection, SLOT(deleteLater()));
    // write
    clientConnection->write(block);
    qDebug("Time  to write : %d ms", t.elapsed());
    clientConnection->disconnectFromHost();
    

    The client side looks like:

    QTime t;
    QDataStream in(tcpSocket);
    in.setVersion(QDataStream::Qt_4_0);  
    
    if (blockSize == 0) {
                if (tcpSocket->bytesAvailable() < sizeof(quint32))
                    return;
                //receive size of image
                in >> blockSize;
                qDebug()<<"The Block size "<<blockSize;
    }
            if (tcpSocket->bytesAvailable() < blockSize)
                return;
            QImage im;
            t.start();
             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.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 12 Aug 2016, 08:31 last edited by VRonin 8 Dec 2016, 08:42
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G 2 Replies Last reply 12 Aug 2016, 08:47
      1
      • V VRonin
        12 Aug 2016, 08:31

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

        G Offline
        G Offline
        Guess11
        wrote on 12 Aug 2016, 08:47 last edited by Guess11 8 Dec 2016, 08:48
        #3

        @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.

        1 Reply Last reply
        0
        • V VRonin
          12 Aug 2016, 08:31

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

          G Offline
          G Offline
          Guess11
          wrote on 12 Aug 2016, 10:06 last edited by
          #4

          @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);
          
          1 Reply Last reply
          0

          1/4

          12 Aug 2016, 08:27

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved