[Qt-interest] how to speed up code?
Srdjan Todorovic
todorovic.s at googlemail.com
Thu Jul 9 10:32:52 CEST 2009
Hi,
I'll just start with saying that I don't really know much about image
manipulation, but I will see if I can suggest anything that could be
useful. Please correct me if I'm wrong...
On 09/07/2009, Igor Sobinov <sluge at mail.ru> wrote:
> hello all
>
> I develop application that get images from web camera and show it in Qt
> widget.
>
> My code is:
>
> //if we need to convert image formatsd, let's convert it
> if(m_destFmt != m_srcFmt)
> {
Do you often have to convert the image?
> const int ret = v4lconvert_convert(m_convertData, &m_srcFmt,
> &m_destFmt, (uchar*)m_buffers[buf.index].m_address,
> m_buffers[buf.index].m_len,
> dst_buf, m_destFmt.fmt.pix.sizeimage);
>
> if(ERR_RET == ret)
> {
> qDebug("Error conversion libv4l");
> return;
> }
> }
> else//no conversion is needed
Semicolon or { missing for the else?
> memcpy(dst_buf, (uchar*)m_buffers[buf.index].m_address,
> m_buffers[buf.index].m_len);
> //creating QImage
> QImage img(m_destFmt.fmt.pix.width, m_destFmt.fmt.pix.height, 32);
> //Convert from 24 to 32 bbp
> uchar* bits = img.bits();
bits() is such a misleading name for a method, as it actually returns bytes...
> const uint len = img.width()*img.height();
> bzero(bits, len);
> //very slow loop
> for (int i = 0; i < len; i++)
> {
> bits[4*i ] = dst_buf[3*i];
> bits[4*i+1] = dst_buf[3*i+1];
> bits[4*i+2] = dst_buf[3*i+12;
> }
Do you have an optimising compiler? It should make that nice and fast.
Perhaps change the code to do pointer addition to speed it up?
> QPixmap pix;
> //very slow convertion
> if(!pix.convertFromImage(img))
Could not see QPixmap::convertFromImage() - what version of Qt are you using?
> qDebug("Error convertiong pixmap");
> //send image to display in widget
> emit setImage(pix);
> //fps calculation
> m_fpsCount++;
>
>
> actually camera sends 25 frames per second, but because of very slow image
> converion operation real FPS is 1-2 ;(
>
> Is any way to improve perfomance of this code?
I wonder
- would it be faster to do the conversion in another thread?
- what about keeping a list of QPixmap objects and then buffering up
the data before displaying? Perhaps you may get a lag of a few seconds
but the pictures may display faster.
It seems a bit annoying that you have to use a QImage for manipulation
and then convert to QPixmap for display...
Am I wrong? Anyone else have any suggestions?
Good luck
Srdjan
More information about the Qt-interest-old
mailing list