Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

opixmapeffect.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1998, 1999 Christian Tibirna <ctibirna@total.net>
00003               (C) 1998, 1999 Daniel M. Duley <mosfet@kde.org>
00004               (C) 1998, 1999 Dirk A. Mueller <mueller@kde.org>
00005 
00006 */
00007 
00008 // $Id: opixmapeffect.cpp,v 1.2 2004/03/13 19:51:49 zecke Exp $
00009 
00010 /* QT */
00011 
00012 #include <qimage.h>
00013 #include <qpainter.h>
00014 
00015 /* OPIE */
00016 
00017 #include <opie2/opixmapeffect.h>
00018 #include <opie2/oimageeffect.h>
00019 
00020 
00021 using namespace Opie::Ui;
00022 //======================================================================
00023 //
00024 // Gradient effects
00025 //
00026 //======================================================================
00027 
00028 
00029 OPixmap& OPixmapEffect::gradient(OPixmap &pixmap, const QColor &ca,
00030         const QColor &cb, GradientType eff, int ncols)
00031 {
00032     if(pixmap.depth() > 8 &&
00033        (eff == VerticalGradient || eff == HorizontalGradient)) {
00034 
00035         int rDiff, gDiff, bDiff;
00036         int rca, gca, bca /*, rcb, gcb, bcb*/;
00037 
00038         register int x, y;
00039 
00040         rDiff = (/*rcb = */ cb.red())   - (rca = ca.red());
00041         gDiff = (/*gcb = */ cb.green()) - (gca = ca.green());
00042         bDiff = (/*bcb = */ cb.blue())  - (bca = ca.blue());
00043 
00044         register int rl = rca << 16;
00045         register int gl = gca << 16;
00046         register int bl = bca << 16;
00047 
00048         int rcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * rDiff;
00049         int gcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * gDiff;
00050         int bcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * bDiff;
00051 
00052         QPainter p(&pixmap);
00053 
00054         // these for-loops could be merged, but the if's in the inner loop
00055         // would make it slow
00056         switch(eff) {
00057         case VerticalGradient:
00058             for ( y = 0; y < pixmap.height(); y++ ) {
00059                 rl += rcdelta;
00060                 gl += gcdelta;
00061                 bl += bcdelta;
00062 
00063                 p.setPen(QColor(rl>>16, gl>>16, bl>>16));
00064                 p.drawLine(0, y, pixmap.width()-1, y);
00065             }
00066             break;
00067         case HorizontalGradient:
00068             for( x = 0; x < pixmap.width(); x++) {
00069                 rl += rcdelta;
00070                 gl += gcdelta;
00071                 bl += bcdelta;
00072 
00073                 p.setPen(QColor(rl>>16, gl>>16, bl>>16));
00074                 p.drawLine(x, 0, x, pixmap.height()-1);
00075             }
00076             break;
00077         default:
00078             ;
00079         }
00080     }
00081     else {
00082         QImage image = OImageEffect::gradient(pixmap.size(), ca, cb,
00083                                               (OImageEffect::GradientType) eff, ncols);
00084         pixmap.convertFromImage(image);
00085     }
00086 
00087     return pixmap;
00088 }
00089 
00090 
00091 // -----------------------------------------------------------------------------
00092 
00093 OPixmap& OPixmapEffect::unbalancedGradient(OPixmap &pixmap, const QColor &ca,
00094          const QColor &cb, GradientType eff, int xfactor, int yfactor,
00095          int ncols)
00096 {
00097     QImage image = OImageEffect::unbalancedGradient(pixmap.size(), ca, cb,
00098                                  (OImageEffect::GradientType) eff,
00099                                  xfactor, yfactor, ncols);
00100     pixmap.convertFromImage(image);
00101 
00102     return pixmap;
00103 }
00104 
00105 
00106 //======================================================================
00107 //
00108 // Intensity effects
00109 //
00110 //======================================================================
00111 
00112 
00113 
00114 OPixmap& OPixmapEffect::intensity(OPixmap &pixmap, float percent)
00115 {
00116     QImage image = pixmap.convertToImage();
00117     OImageEffect::intensity(image, percent);
00118     pixmap.convertFromImage(image);
00119 
00120     return pixmap;
00121 }
00122 
00123 
00124 // -----------------------------------------------------------------------------
00125 
00126 OPixmap& OPixmapEffect::channelIntensity(OPixmap &pixmap, float percent,
00127                                      RGBComponent channel)
00128 {
00129     QImage image = pixmap.convertToImage();
00130     OImageEffect::channelIntensity(image, percent,
00131                    (OImageEffect::RGBComponent) channel);
00132     pixmap.convertFromImage(image);
00133 
00134     return pixmap;
00135 }
00136 
00137 
00138 //======================================================================
00139 //
00140 // Blend effects
00141 //
00142 //======================================================================
00143 
00144 
00145 OPixmap& OPixmapEffect::blend(OPixmap &pixmap, float initial_intensity,
00146                           const QColor &bgnd, GradientType eff,
00147                           bool anti_dir, int ncols)
00148 {
00149 
00150     QImage image = pixmap.convertToImage();
00151     if (image.depth() <=8)
00152         image = image.convertDepth(32); //Sloww..
00153 
00154     OImageEffect::blend(image, initial_intensity, bgnd,
00155                   (OImageEffect::GradientType) eff, anti_dir);
00156 
00157     unsigned int tmp;
00158 
00159     if(pixmap.depth() <= 8 ) {
00160         if ( ncols < 2 || ncols > 256 )
00161             ncols = 3;
00162         QColor *dPal = new QColor[ncols];
00163         for (int i=0; i<ncols; i++) {
00164             tmp = 0 + 255 * i / ( ncols - 1 );
00165             dPal[i].setRgb ( tmp, tmp, tmp );
00166         }
00167         OImageEffect::dither(image, dPal, ncols);
00168         pixmap.convertFromImage(image);
00169         delete [] dPal;
00170     }
00171     else
00172         pixmap.convertFromImage(image);
00173 
00174     return pixmap;
00175 }
00176 
00177 
00178 //======================================================================
00179 //
00180 // Hash effects
00181 //
00182 //======================================================================
00183 
00184 OPixmap& OPixmapEffect::hash(OPixmap &pixmap, Lighting lite,
00185                          unsigned int spacing, int ncols)
00186 {
00187     QImage image = pixmap.convertToImage();
00188     OImageEffect::hash(image, (OImageEffect::Lighting) lite, spacing);
00189 
00190     unsigned int tmp;
00191 
00192     if(pixmap.depth() <= 8 ) {
00193         if ( ncols < 2 || ncols > 256 )
00194             ncols = 3;
00195         QColor *dPal = new QColor[ncols];
00196         for (int i=0; i<ncols; i++) {
00197             tmp = 0 + 255 * i / ( ncols - 1 );
00198             dPal[i].setRgb ( tmp, tmp, tmp );
00199         }
00200         OImageEffect::dither(image, dPal, ncols);
00201         pixmap.convertFromImage(image);
00202         delete [] dPal;
00203     }
00204     else
00205         pixmap.convertFromImage(image);
00206 
00207     return pixmap;
00208 }
00209 
00210 
00211 //======================================================================
00212 //
00213 // Pattern effects
00214 //
00215 //======================================================================
00216 
00217 #if 0
00218 void OPixmapEffect::pattern(OPixmap &pixmap, const QColor &ca,
00219         const QColor &cb, unsigned pat[8])
00220 {
00221     QImage img = pattern(pixmap.size(), ca, cb, pat);
00222     pixmap.convertFromImage(img);
00223 }
00224 #endif
00225 
00226 // -----------------------------------------------------------------------------
00227 
00228 OPixmap OPixmapEffect::pattern(const OPixmap& pmtile, QSize size,
00229                        const QColor &ca, const QColor &cb, int ncols)
00230 {
00231     if (pmtile.depth() > 8)
00232         ncols = 0;
00233 
00234     QImage img = pmtile.convertToImage();
00235     OImageEffect::flatten(img, ca, cb, ncols);
00236     OPixmap pixmap;
00237     pixmap.convertFromImage(img);
00238 
00239     return OPixmapEffect::createTiled(pixmap, size);
00240 }
00241 
00242 
00243 // -----------------------------------------------------------------------------
00244 
00245 OPixmap OPixmapEffect::createTiled(const OPixmap& pixmap, QSize size)
00246 {
00247     OPixmap pix;
00248 
00249     QPainter p(&pix);
00250     p.drawTiledPixmap(0, 0, size.width(), size.height(), pixmap);
00251 
00252     return pix;
00253 }
00254 
00255 
00256 //======================================================================
00257 //
00258 // Fade effects
00259 //
00260 //======================================================================
00261 
00262 OPixmap& OPixmapEffect::fade(OPixmap &pixmap, double val, const QColor &color)
00263 {
00264     QImage img = pixmap.convertToImage();
00265     OImageEffect::fade(img, val, color);
00266     pixmap.convertFromImage(img);
00267 
00268     return pixmap;
00269 }
00270 
00271 
00272 // -----------------------------------------------------------------------------
00273 OPixmap& OPixmapEffect::toGray(OPixmap &pixmap, bool fast)
00274 {
00275     QImage img = pixmap.convertToImage();
00276     OImageEffect::toGray(img, fast);
00277     pixmap.convertFromImage(img);
00278 
00279     return pixmap;
00280 }
00281 
00282 // -----------------------------------------------------------------------------
00283 OPixmap& OPixmapEffect::desaturate(OPixmap &pixmap, float desat)
00284 {
00285     QImage img = pixmap.convertToImage();
00286     OImageEffect::desaturate(img, desat);
00287     pixmap.convertFromImage(img);
00288 
00289     return pixmap;
00290 }
00291 // -----------------------------------------------------------------------------
00292 OPixmap& OPixmapEffect::contrast(OPixmap &pixmap, int c)
00293 {
00294     QImage img = pixmap.convertToImage();
00295     OImageEffect::contrast(img, c);
00296     pixmap.convertFromImage(img);
00297 
00298     return pixmap;
00299 }
00300 
00301 //======================================================================
00302 //
00303 // Dither effects
00304 //
00305 //======================================================================
00306 
00307 // -----------------------------------------------------------------------------
00308 OPixmap& OPixmapEffect::dither(OPixmap &pixmap, const QColor *palette, int size)
00309 {
00310     QImage img = pixmap.convertToImage();
00311     OImageEffect::dither(img, palette, size);
00312     pixmap.convertFromImage(img);
00313 
00314     return pixmap;
00315 }
00316 
00317 //======================================================================
00318 //
00319 // Other effects
00320 //
00321 //======================================================================
00322 
00323 OPixmap OPixmapEffect::selectedPixmap( const OPixmap &pix, const QColor &col )
00324 {
00325     QImage img = pix.convertToImage();
00326     OImageEffect::selectedImage(img, col);
00327     OPixmap outPix;
00328     outPix.convertFromImage(img);
00329     return outPix;
00330 }

Generated on Sat Nov 5 16:16:26 2005 for OPIE by  doxygen 1.4.2