Results 1 to 3 of 3

Thread: Sending image in blocks via datastream does not work for other block sizes??

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Sending image in blocks via datastream does not work for other block sizes??

    Hi

    I am sending an image in bl via network as shown below:

    ******sender.cpp file**********
    Qt Code:
    1. Sender::Sender(int no)
    2. {
    3. socket = new QUdpSocket( this );
    4. a = 0;
    5. img_block = 0;
    6. block_size = 20;
    7. num = no;
    8.  
    9.  
    10. if(num == 1)
    11. image = new QImage("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg" );
    12. if( image->isNull() )
    13. qDebug() << "Failed to open test.jpg" ;
    14. QTimer *timer = new QTimer( this );
    15.  
    16. timer->isSingleShot();
    17. QTimer::singleShot(1000, this, SLOT(broadcastLine()) );
    18. connect( this, SIGNAL(done()), this, SLOT(broadcastLine()) );
    19.  
    20.  
    21. }
    22.  
    23.  
    24. void Sender::broadcastLine()
    25. {
    26.  
    27. QByteArray buffer( block_size+3*image->width(), 0 );
    28. QDataStream stream( &buffer, QIODevice::WriteOnly );
    29. stream.setVersion( QDataStream::Qt_5_5 );
    30.  
    31. stream << (quint16)image->width() << (quint16)image->height();
    32.  
    33. stream << img_block;
    34.  
    35. for( int x=0; x<image->width(); ++x )
    36. {
    37. for(int y=block_size*a; y <block_size+(block_size*a);y++)
    38. {
    39.  
    40. if( y < image->height())
    41. {
    42. QRgb rgb = image->pixel( x, y );
    43. stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
    44. }
    45. else
    46. {
    47. img_block = y = a = 0;
    48. img_block = -block_size;
    49. }
    50. }
    51. }
    52.  
    53.  
    54. if(num==1)
    55. socket->writeDatagram( buffer, QHostAddress::Broadcast,9988 );
    56. img_block = img_block + block_size;
    57. a++;
    58.  
    59. emit done();
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 



    The above program works for block_size< 20 but not for 32 which I am interested. I dont have even the slightest idea of whats happening and even how to debug this?

    Regards
    Last edited by anda_skoa; 15th October 2015 at 08:51. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending image in blocks via datastream does not work for other block sizes??

    The logic of your program is very hard to follow, with lots of side effects on global variables (by global, I mean non-local to a function; your code snippet does not tell us whether they are global or instance members of the Sender class). The two lines
    Qt Code:
    1. img_block = y = a = 0;
    2. img_block = -block_size;
    To copy to clipboard, switch view to plain text mode 
    are most likely buggy:
    • they overwrite the global variable a, which is used in the condition of the inner for loop;
    • they overwrite the index y of the inner for loop:
    • they overwrite (twice) the global variable img_block, which is never read in the remainder of the function; the effect could be observed if broadcastLine() were called again, but from your snippet we have no idea when/if that happens.

    Could you explain exactly what you are trying to achieve? On a side note, please wrap your code in CODE tags in your posts to make it much more readable.

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sending image in blocks via datastream does not work for other block sizes??

    Quote Originally Posted by yeye_olive View Post
    The logic of your program is very hard to follow, with lots of side effects on global variables (by global, I mean non-local to a function; your code snippet does not tell us whether they are global or instance members of the Sender class). The two lines
    Qt Code:
    1. img_block = y = a = 0;
    2. img_block = -block_size;
    To copy to clipboard, switch view to plain text mode 
    are most likely buggy:
    • they overwrite the global variable a, which is used in the condition of the inner for loop;
    • they overwrite the index y of the inner for loop:
    • they overwrite (twice) the global variable img_block, which is never read in the remainder of the function; the effect could be observed if broadcastLine() were called again, but from your snippet we have no idea when/if that happens.

    Could you explain exactly what you are trying to achieve? On a side note, please wrap your code in CODE tags in your posts to make it much more readable.

    I am trying to send the same image indefinitely which is ensured by one-shot timer to run Sender::broadcastLine() ; after this I emit the done() signal which has Sender::broadcastLine() as slot and thus indefinite sending of image.


    Here is my sender.h file:

    Qt Code:
    1. [CODE]
    2. #ifndef SENDER_H
    3. #define SENDER_H
    4. #include <QObject>
    5. class QUdpSocket;
    6. class QImage;
    7.  
    8. class Sender : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. Sender(int no);
    13.  
    14. signals:
    15. void done();
    16.  
    17. private slots:
    18. void broadcastLine();
    19.  
    20. private:
    21. QUdpSocket *socket;
    22. QImage *image;
    23. quint16 a;
    24. quint16 img_block;
    25. quint8 block_size;
    26.  
    27. // QUdpSocket *socket1;
    28. int num;
    29. };
    30.  
    31. #endif // SENDER_H
    To copy to clipboard, switch view to plain text mode 
    [/CODE]

    Yes, img_block ,y,a and block_size are not global but instance variables.



    Qt Code:
    1. img_block = y = a = 0;
    2. img_block = -block_size;
    To copy to clipboard, switch view to plain text mode 


    The above lines ensure to start the image sending from first again( because i send the image indefinitely). Yaa it can be modified like
    Qt Code:
    1. y=a=0;
    2. img_block = -block_size;
    To copy to clipboard, switch view to plain text mode 



    Does this help?

    why does it work for block_size = 20 but not 32?



    Regards

Similar Threads

  1. QHttpMultiPart - sending image to web serwer question
    By unnamed in forum Qt Programming
    Replies: 6
    Last Post: 20th January 2013, 19:59
  2. OpenGL Qt and image sizes
    By Atomic_Sheep in forum Newbie
    Replies: 2
    Last Post: 20th May 2012, 17:11
  3. Replies: 1
    Last Post: 28th January 2012, 13:59
  4. background-image on QLabel does NOT work
    By VireX in forum Newbie
    Replies: 2
    Last Post: 8th June 2007, 20:30
  5. Sending a image using QSocket & QServerSocket
    By machathu in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2006, 12:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.