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