PDA

View Full Version : Best way to handle huge QImage setColorTable change



bruceariggs
24th September 2011, 21:02
I'm trying to find the fastest way to convert a bunch of QPixmaps from one color table to another color table. Here is how I'm currently doing it:

I have a huge QImage (10K pixel by 10K pixel or more) that I break up into tiles.

The tiles are then converted into QPixmaps and added to the QGraphicsScene.

I keep a backup of the original QImage.

When the user decides to change the color table, this is what I do



// Grab the table
QVector< QRgb > table;
table = colorTables.find( tableName ).value();

// Change the image now (chartImage is my entire QImage)
QImage tempImage = chartImage;

// Apply the color table
tempImage.setColorTable( table );

// Convert the changed image to pixmap
QPixmap tempPix;
tempPix = tempPix.fromImage( tempImage);

// For every tile...
// Update every tile
for( int ii = 0; ii < tiles.size(); ii++ )
{
// Update this tile's pixmap
tiles[ ii ]->setPixmap( tempPix.copy( tiles[ ii ]->getTopLeft().x(),
tiles[ ii ]->getTopLeft().y(), tileSize, tileSize ) );
}


This is what setPixmap does (inside the tiles vector)



void ChartTile::setPixmap( QPixmap pixmap )
{
// Keep a local copy of the QPixmap
image = pixmap;
// Set my QGraphicsPixmapItem* to point to this new QPixmap
// This QGraphicsPixmapItem* is already added to the QGraphicsScene
pixPoint->setPixmap( image );
}


But as you can imagine... due to the large size of this QImage, this takes anywhere from 16 - 40 seconds to complete.

How would you guys optimize something like this? I've tried having the QPixmaps in the tiles convert to smaller QImages, change those smaller QImages, then convert back to a QPixmap, but the result is the same. Slow.

Any input is appreciated. Constructive input, that is.

wysota
24th September 2011, 21:21
Keep your tiles as QImage instances and apply the new colormap to each small image and then convert it to a pixmap (keeping the original image). You can optimize the whole process by using memcpy (or qmemcpy) on QVector::data() to quickly switch the color table contents.

bruceariggs
24th September 2011, 22:47
Thanks, I'll give that a try and get back to you with the results. =)

Added after 1 17 minutes:

Awesome!

Now my image loads and cuts up into tiles a lot faster, but how exactly am I suppose to use the memcpy?

You're suggesting I memcpy the selected color table into the QImage? I must not have understood what's memcopying into what?