[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: [OT] Any plans to keep up with longhorn?
From:       Joachim Eibl <joachim.eibl () gmx ! de>
Date:       2003-11-07 21:09:21
[Download RAW message or body]

> > Before doing that, you might want to try to optimize the little code
> > snippet below.
>
> Why that when there's already QImage::smoothScale()?

Not so on my machine.
I've attached the result of a little testprogram.

The small pic (16x16) on the left is the original.
Then normal scale 100x100,
then smooth scale 100x100,
and finally my smoothResize 100x100.

At first the result of QImage::smoothScale() doesn't look smoother at all. You 
have to look quite closely. (Is this a bug?)

Cheers,
Joachim


/* Copyright (C) Joachim Eibl 2003
    Licence: GNU GPL version 2 */
QImage smoothResize( int xSize, int ySize, QImage& inputImg )
{
   if( inputImg.isNull() )
      return inputImg;

   QImage outputImg;
   outputImg.create(xSize, ySize, 32);

   int inWidth = inputImg.width();
   int inHeight = inputImg.height();

   double xFactor = (double)xSize/inWidth;
   double yFactor = (double)ySize/inHeight;
   for ( int x=0; x<xSize; ++x )
   {
      for ( int y=0; y<ySize; ++y )
      {
         double xOrig = x / xFactor;
         double yOrig = y / yFactor;
         double dx = xOrig - floor( xOrig );
         double dy = yOrig - floor( yOrig );
         double f1 = (1-dx)*(1-dy);   // Interpolation
         double f2 = dx * (1-dy);
         double f3 = (1-dx)*dy;
         double f4 = dx*dy;
         int xo = min(int(floor(xOrig)),inWidth-1);
         int yo = min(int(floor(yOrig)),inHeight-1);
         int xo1 = min(int(floor(xOrig))+1,inWidth-1);
         int yo1 = min(int(floor(yOrig))+1,inHeight-1);
         QRgb c1 = inputImg.pixel( xo, yo );
         QRgb c2 = inputImg.pixel( xo1, yo );
         QRgb c3 = inputImg.pixel( xo, yo1 );
         QRgb c4 = inputImg.pixel( xo1, yo1 );

         int r=int( f1*qRed(c1)+f2*qRed(c2)+f3*qRed(c3)+f4*qRed(c4) );
         int g=int( f1*qGreen(c1)+f2*qGreen(c2)+f3*qGreen(c3)+f4*qGreen(c4));
         int b=int( f1*qBlue(c1)+f2*qBlue(c2)+f3*qBlue(c3)+f4*qBlue(c4) );
         outputImg.setPixel( x,y, qRgb(r,g,b) );
      }
   }
   return outputImg;
}


void kscaletest::paintEvent(QPaintEvent*)
{
   QImage orig = QImage();
   orig.load("/opt/kdecvs/share/icons/hicolor/16x16/actions/reload_page.png");
   QImage scaled = orig.scale(100,100);
   QImage smoothScaled = orig.smoothScale(100,100);
   QImage smoothResized = smoothResize(100,100, orig);

   QPainter p(this);
   p.drawImage(0,0,orig);
   p.drawImage(20,0,scaled);
   p.drawImage(140,0,smoothScaled);
   p.drawImage(250,0,smoothResized);
}

["scaletest.png" (image/png)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic