PDA

View Full Version : Most Memory-Efficient Way to Add Images to QGraphicsScene?



datek2517
28th June 2011, 01:48
I have a series of six JPEG images that I'd like to display on the same QGraphicsScene. Each grayscale JPEG is approximately 4000x3400 pixels and just under 700 KB in file size.

I'm wondering about the most memory-efficient way to place these six JPEGs as items in a single graphics scene. I've looked into adding them as QGraphicsPixmapItems using the following code:


QGraphicsPixmapItem *pixItem = new QGraphicsPixmapItem();
QPixmap ncpPix(ncpPath);
pixItem ->setPixmap(ncpPix);
scene->addItem(ncpItem);

The problem is that the setPixmap() operation seems to consume more than 60 MB of memory for each new pixmap item I add to the scene. For six images, that's not really going to cut it. I've done some basic profiling of my application and the memory overhead of the graphics scene and graphics view is minimal - the problem is with setPixmap. What other options do I have for placing those JPEGs in the scene? Am I doing something terribly wrong? Are there straightforward steps I can take to decrease the memory footprint of displaying multiple JPEGs as items in a graphics scene?

NOTE: I'm using the stock Qt JPEG plugin to deal with the JPEG images.

SixDegrees
28th June 2011, 08:52
Your images may only be 700k on disk, but when uncompressed they contain roughly 13 megapixels. Each pixel takes up some space, depending on what sort of image your pixmap is set to receive.

The default is to assume a full color image with an alpha channel, so you wind up claiming roughly 55 megabytes per image. Setting the optional constructor flag 'format' to 'Qt::MonoOnly' will force the image to grayscale, which will only take about 13 megabytes.

That's the best you can do without reducing the image size/resolution. It's impossible to say whether this is an option without knowing more.

Rachol
28th June 2011, 09:11
If you describe what you are trying to achieve with your pixmaps in the scene, we might help with your app's memory consumption and/or performance.

datek2517
28th June 2011, 21:46
Thanks a lot for your help. I'm loading these in as image items so that users can look at the images side-by-side with synchronized zooming and panning capabilities. I have a single QGraphicsScene that contains all of the pixmap items and a series of QGraphicsViews centered on particular images.

I thought the graphics view framework made the most sense since we will also want to add layers of overlays, icons, and text over the images in the future. Also, though it's not needed for this particular application, I wanted to provide for the future ability to layer the images on top of one another within the same view and allow a user to move them around and hide certain images. So it's for more than just displaying the images.


Your images may only be 700k on disk, but when uncompressed they contain roughly 13 megapixels. Each pixel takes up some space, depending on what sort of image your pixmap is set to receive.

The default is to assume a full color image with an alpha channel, so you wind up claiming roughly 55 megabytes per image. Setting the optional constructor flag 'format' to 'Qt::MonoOnly' will force the image to grayscale, which will only take about 13 megabytes.

That's the best you can do without reducing the image size/resolution. It's impossible to say whether this is an option without knowing more.

Thanks for the insights. I tried using the MonoOnly optimization flag but didn't notice any difference with memory usage:


QPixmap ncpPix(imgPath, 0, Qt::MonoOnly);
pixItem->setPixmap(imgPix);