PDA

View Full Version : Sending image in blocks via datastream does not work for other block sizes??



ganapathi
15th October 2015, 07:22
Hi

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

******sender.cpp file**********


Sender::Sender(int no)
{
socket = new QUdpSocket( this );
a = 0;
img_block = 0;
block_size = 20;
num = no;


if(num == 1)
image = new QImage("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg" );
if( image->isNull() )
qDebug() << "Failed to open test.jpg" ;
QTimer *timer = new QTimer( this );

timer->isSingleShot();
QTimer::singleShot(1000, this, SLOT(broadcastLine()) );
connect( this, SIGNAL(done()), this, SLOT(broadcastLine()) );


}


void Sender::broadcastLine()
{

QByteArray buffer( block_size+3*image->width(), 0 );
QDataStream stream( &buffer, QIODevice::WriteOnly );
stream.setVersion( QDataStream::Qt_5_5 );

stream << (quint16)image->width() << (quint16)image->height();

stream << img_block;

for( int x=0; x<image->width(); ++x )
{
for(int y=block_size*a; y <block_size+(block_size*a);y++)
{

if( y < image->height())
{
QRgb rgb = image->pixel( x, y );
stream << (quint8)qRed( rgb ) << (quint8)qGreen( rgb ) << (quint8)qBlue( rgb );
}
else
{
img_block = y = a = 0;
img_block = -block_size;
}
}
}


if(num==1)
socket->writeDatagram( buffer, QHostAddress::Broadcast,9988 );
img_block = img_block + block_size;
a++;

emit done();

}




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

yeye_olive
15th October 2015, 08:48
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

img_block = y = a = 0;
img_block = -block_size;

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.

ganapathi
15th October 2015, 09:24
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

img_block = y = a = 0;
img_block = -block_size;

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:




#ifndef SENDER_H
#define SENDER_H
#include <QObject>
class QUdpSocket;
class QImage;

class Sender : public QObject
{
Q_OBJECT
public:
Sender(int no);

signals:
void done();

private slots:
void broadcastLine();

private:
QUdpSocket *socket;
QImage *image;
quint16 a;
quint16 img_block;
quint8 block_size;

// QUdpSocket *socket1;
int num;
};

#endif // SENDER_H



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





img_block = y = a = 0;
img_block = -block_size;




The above lines ensure to start the image sending from first again( because i send the image indefinitely). Yaa it can be modified like


y=a=0;
img_block = -block_size;





Does this help?

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



Regards