[Qt-interest] QImage fade

Joshua Grauman jnfo-c at grauman.com
Sat May 7 02:41:44 CEST 2011


Hello all,

I've written a little function to fade two QImages to a 3rd. It works fine 
and the results are expected. I cycle through fadeval values to fade the 
two images. I was wondering if anyone had any tricks I'm missing to *speed 
it up* though...

FYI the function is dependant on all three QImages being the same size, 
and they all need to be QImage::Format_ARGB32...


static int fadeMax=50;

void fadeImages(QImage *dest, QImage *src1, QImage *src2, unsigned char fadeval)
{
   for(int y = 0; y < dest->height(); y++)
   {
     QRgb *destrow = (QRgb*)dest->scanLine(y);
     QRgb *src1row = (QRgb*)src1->scanLine(y);
     QRgb *src2row = (QRgb*)src2->scanLine(y);
     for(int x = 0; x < dest->width(); x++)
     {
       ((unsigned char*)&destrow[x])[0] = (((unsigned char*)&src1row[x])[0]*fadeval/fadeMax + ((unsigned char*)&src2row[x])[0]*(fadeMax-fadeval)/fadeMax);
       ((unsigned char*)&destrow[x])[1] = (((unsigned char*)&src1row[x])[1]*fadeval/fadeMax + ((unsigned char*)&src2row[x])[1]*(fadeMax-fadeval)/fadeMax);
       ((unsigned char*)&destrow[x])[2] = (((unsigned char*)&src1row[x])[2]*fadeval/fadeMax + ((unsigned char*)&src2row[x])[2]*(fadeMax-fadeval)/fadeMax);
       ((unsigned char*)&destrow[x])[3] = 255;
     }
   }
}


Josh



More information about the Qt-interest-old mailing list