2

I have a large display (about 1000x2000 pixels) and I'm doing the following to draw images to the screen:

QImage *pImage = GetImage(); // from wherever
QPainter painter(this);   
painter.drawImage((0,0), *pImage);  // this line takes over 100ms to complete.

The larger the screen is that I'm drawing to, the longer this paint takes. I guess the pImage is being memcpy'd and that's the difference. How can I reduce this overhead? I'm not trying to scale or anything here.

Thanks.

Morwenn
  • 21,684
  • 12
  • 93
  • 152
glutz
  • 1,889
  • 7
  • 29
  • 44
  • As noted below: I should also note that i'm using a QImage because i'm manipulating a raw image buffer in the QIMAGE::RGB16 format. Based on this, im not sure i can just use a QPixmap instead. Do you konw of a way to set a QPixmap to a raw image buffer of that format? – glutz Jun 29 '11 at 00:37
  • You can't "stuff" raw pixels into a QPixmap. QImage is the right way to go. And if the same image is not going to be drawn many times, there's not benefit to convert to QPixmap anyway. What platform is this on? I know the "accelerated" default painter backend on Mac is really slow. Try creating a painter from a QImage and see of the same operation is any faster. – Stephen Chu Jun 29 '11 at 03:29
  • @Stephen Chu. I am creating a painter from a QImage. Or do you mean something else? I'm on Ubuntu 10.10. – glutz Jun 29 '11 at 13:00
  • I thought it's a physical screen. On some platforms, Qt uses "accelerated" paint engines which are actually slower than the software engine. I think QImage backed painter is always software engine regardless of platform. I was suggesting to use the software engine. Since it doesn't work for you, maybe try a QPixmap backed painter? – Stephen Chu Jun 29 '11 at 13:47

2 Answers2

5

You're painting a QImage. Don't do that, try with a QPixmap instead.

From the QImage documentation:

QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen."

Depending on the platform, getting the QImage data into the format and location needed for painting, can be extremely expensive.

P.S.: There is no need to create QImages on the heap, as

QImage objects can be passed around by value since the QImage class uses implicit data sharing.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • I should also note that i'm using a QImage because i'm manipulating a raw image buffer in the QIMAGE::RGB16 format. Based on this, im not sure i can just use a QPixmap instead. Do you konw of a way to set a QPixmap to a raw image buffer of that format? – glutz Jun 28 '11 at 20:56
  • @glutz: You can create QPixmaps from raw data, but you can't change the data afterwards and have the QPixmap changed automatically. On X11, the pixmap is stored in the server address space, so getting it there from the client is expensive. Maybe try to not regenerate the image on every paint, but only when changes actually occurr. – Frank Osterfeld Jun 29 '11 at 09:50
0

One simple improvement you can make is to draw only the area that needs updated (if you can). The QPaintEvent contains a rect for the changed area, and the QPainter::drawImage has overloads that can take rects for the portion to draw.

You might also look at the ImageConversionFlags options for faster options.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49