Fwd: My KImageEffect code

Richard Smith kde at metafoo.co.uk
Tue Nov 30 02:33:06 GMT 2004


On Tuesday 30 November 2004 02:02, Michael Pyne wrote:
> On Monday 29 November 2004 04:50 pm, Richard Smith wrote:
> > (especially since Qt seems to provide no way to blend two
> > pixmaps without destroying the alpha channel).
>
> Annoying as hell, isn't it?

Yup. Here's what I've got (works perfectly....):

static void blendOnLower( const QImage &upper_, QImage &lower )
{
        if ( upper_.width() <= 0 || upper_.height() <= 0 )
                return;
        if ( lower.width() <= 0 || lower.height() <= 0 )
                return;

        QImage upper = upper_;
        if ( upper.depth() != 32 )
                upper = upper.convertDepth( 32 );
        if ( lower.depth() != 32 )
                lower = lower.convertDepth( 32 );

        int cw = std::min( upper.width(), lower.width() ),
            ch = std::min( upper.height(), lower.height() );
        const int m = 255;

        for ( int j = 0; j < ch; j++ )
        {
                QRgb *u = (QRgb*)upper.scanLine(j);
                QRgb *l = (QRgb*)lower.scanLine(j);

                for( int k = cw; k; ++u, ++l, --k )
                {
                        int ua = qAlpha(*u);
                        if ( !ua )
                                continue;

                        int la = qAlpha(*l);

                        int   d =                       ua * m +              
la * (m - ua);
                        uchar r = uchar( (   qRed(*u) * ua * m +   qRed(*l) * 
la * (m - ua) ) / d );
                        uchar g = uchar( ( qGreen(*u) * ua * m + qGreen(*l) * 
la * (m - ua) ) / d );
                        uchar b = uchar( (  qBlue(*u) * ua * m +  qBlue(*l) * 
la * (m - ua) ) / d );
                        uchar a = uchar( (         ua * ua * m +         la * 
la * (m - ua) ) / d );
                        *l = qRgba( r, g, b, a );
                }
        }
}

> KIconEffect::overlay() I think would work for what you're talking about,
> but the images have to be the same size.

That sounds pretty much ideal. Thanks for the pointer.
-- 
Thanks,
Richard




More information about the kde-core-devel mailing list