Diffstat (limited to 'libopie2/opieui/opixmapeffect.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opieui/opixmapeffect.cpp | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/libopie2/opieui/opixmapeffect.cpp b/libopie2/opieui/opixmapeffect.cpp new file mode 100644 index 0000000..05f851d --- a/dev/null +++ b/libopie2/opieui/opixmapeffect.cpp | |||
@@ -0,0 +1,328 @@ | |||
1 | /* This file is part of the KDE libraries | ||
2 | Copyright (C) 1998, 1999 Christian Tibirna <ctibirna@total.net> | ||
3 | (C) 1998, 1999 Daniel M. Duley <mosfet@kde.org> | ||
4 | (C) 1998, 1999 Dirk A. Mueller <mueller@kde.org> | ||
5 | |||
6 | */ | ||
7 | |||
8 | // $Id$ | ||
9 | |||
10 | /* QT */ | ||
11 | |||
12 | #include <qimage.h> | ||
13 | #include <qpainter.h> | ||
14 | |||
15 | /* OPIE */ | ||
16 | |||
17 | #include <opie2/opixmapeffect.h> | ||
18 | #include <opie2/oimageeffect.h> | ||
19 | |||
20 | //====================================================================== | ||
21 | // | ||
22 | // Gradient effects | ||
23 | // | ||
24 | //====================================================================== | ||
25 | |||
26 | |||
27 | OPixmap& OPixmapEffect::gradient(OPixmap &pixmap, const QColor &ca, | ||
28 | const QColor &cb, GradientType eff, int ncols) | ||
29 | { | ||
30 | if(pixmap.depth() > 8 && | ||
31 | (eff == VerticalGradient || eff == HorizontalGradient)) { | ||
32 | |||
33 | int rDiff, gDiff, bDiff; | ||
34 | int rca, gca, bca /*, rcb, gcb, bcb*/; | ||
35 | |||
36 | register int x, y; | ||
37 | |||
38 | rDiff = (/*rcb = */ cb.red()) - (rca = ca.red()); | ||
39 | gDiff = (/*gcb = */ cb.green()) - (gca = ca.green()); | ||
40 | bDiff = (/*bcb = */ cb.blue()) - (bca = ca.blue()); | ||
41 | |||
42 | register int rl = rca << 16; | ||
43 | register int gl = gca << 16; | ||
44 | register int bl = bca << 16; | ||
45 | |||
46 | int rcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * rDiff; | ||
47 | int gcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * gDiff; | ||
48 | int bcdelta = ((1<<16) / (eff == VerticalGradient ? pixmap.height() : pixmap.width())) * bDiff; | ||
49 | |||
50 | QPainter p(&pixmap); | ||
51 | |||
52 | // these for-loops could be merged, but the if's in the inner loop | ||
53 | // would make it slow | ||
54 | switch(eff) { | ||
55 | case VerticalGradient: | ||
56 | for ( y = 0; y < pixmap.height(); y++ ) { | ||
57 | rl += rcdelta; | ||
58 | gl += gcdelta; | ||
59 | bl += bcdelta; | ||
60 | |||
61 | p.setPen(QColor(rl>>16, gl>>16, bl>>16)); | ||
62 | p.drawLine(0, y, pixmap.width()-1, y); | ||
63 | } | ||
64 | break; | ||
65 | case HorizontalGradient: | ||
66 | for( x = 0; x < pixmap.width(); x++) { | ||
67 | rl += rcdelta; | ||
68 | gl += gcdelta; | ||
69 | bl += bcdelta; | ||
70 | |||
71 | p.setPen(QColor(rl>>16, gl>>16, bl>>16)); | ||
72 | p.drawLine(x, 0, x, pixmap.height()-1); | ||
73 | } | ||
74 | break; | ||
75 | default: | ||
76 | ; | ||
77 | } | ||
78 | } | ||
79 | else { | ||
80 | QImage image = OImageEffect::gradient(pixmap.size(), ca, cb, | ||
81 | (OImageEffect::GradientType) eff, ncols); | ||
82 | pixmap.convertFromImage(image); | ||
83 | } | ||
84 | |||
85 | return pixmap; | ||
86 | } | ||
87 | |||
88 | |||
89 | // ----------------------------------------------------------------------------- | ||
90 | |||
91 | OPixmap& OPixmapEffect::unbalancedGradient(OPixmap &pixmap, const QColor &ca, | ||
92 | const QColor &cb, GradientType eff, int xfactor, int yfactor, | ||
93 | int ncols) | ||
94 | { | ||
95 | QImage image = OImageEffect::unbalancedGradient(pixmap.size(), ca, cb, | ||
96 | (OImageEffect::GradientType) eff, | ||
97 | xfactor, yfactor, ncols); | ||
98 | pixmap.convertFromImage(image); | ||
99 | |||
100 | return pixmap; | ||
101 | } | ||
102 | |||
103 | |||
104 | //====================================================================== | ||
105 | // | ||
106 | // Intensity effects | ||
107 | // | ||
108 | //====================================================================== | ||
109 | |||
110 | |||
111 | |||
112 | OPixmap& OPixmapEffect::intensity(OPixmap &pixmap, float percent) | ||
113 | { | ||
114 | QImage image = pixmap.convertToImage(); | ||
115 | OImageEffect::intensity(image, percent); | ||
116 | pixmap.convertFromImage(image); | ||
117 | |||
118 | return pixmap; | ||
119 | } | ||
120 | |||
121 | |||
122 | // ----------------------------------------------------------------------------- | ||
123 | |||
124 | OPixmap& OPixmapEffect::channelIntensity(OPixmap &pixmap, float percent, | ||
125 | RGBComponent channel) | ||
126 | { | ||
127 | QImage image = pixmap.convertToImage(); | ||
128 | OImageEffect::channelIntensity(image, percent, | ||
129 | (OImageEffect::RGBComponent) channel); | ||
130 | pixmap.convertFromImage(image); | ||
131 | |||
132 | return pixmap; | ||
133 | } | ||
134 | |||
135 | |||
136 | //====================================================================== | ||
137 | // | ||
138 | // Blend effects | ||
139 | // | ||
140 | //====================================================================== | ||
141 | |||
142 | |||
143 | OPixmap& OPixmapEffect::blend(OPixmap &pixmap, float initial_intensity, | ||
144 | const QColor &bgnd, GradientType eff, | ||
145 | bool anti_dir, int ncols) | ||
146 | { | ||
147 | |||
148 | QImage image = pixmap.convertToImage(); | ||
149 | if (image.depth() <=8) | ||
150 | image = image.convertDepth(32); //Sloww.. | ||
151 | |||
152 | OImageEffect::blend(image, initial_intensity, bgnd, | ||
153 | (OImageEffect::GradientType) eff, anti_dir); | ||
154 | |||
155 | unsigned int tmp; | ||
156 | |||
157 | if(pixmap.depth() <= 8 ) { | ||
158 | if ( ncols < 2 || ncols > 256 ) | ||
159 | ncols = 3; | ||
160 | QColor *dPal = new QColor[ncols]; | ||
161 | for (int i=0; i<ncols; i++) { | ||
162 | tmp = 0 + 255 * i / ( ncols - 1 ); | ||
163 | dPal[i].setRgb ( tmp, tmp, tmp ); | ||
164 | } | ||
165 | OImageEffect::dither(image, dPal, ncols); | ||
166 | pixmap.convertFromImage(image); | ||
167 | delete [] dPal; | ||
168 | } | ||
169 | else | ||
170 | pixmap.convertFromImage(image); | ||
171 | |||
172 | return pixmap; | ||
173 | } | ||
174 | |||
175 | |||
176 | //====================================================================== | ||
177 | // | ||
178 | // Hash effects | ||
179 | // | ||
180 | //====================================================================== | ||
181 | |||
182 | OPixmap& OPixmapEffect::hash(OPixmap &pixmap, Lighting lite, | ||
183 | unsigned int spacing, int ncols) | ||
184 | { | ||
185 | QImage image = pixmap.convertToImage(); | ||
186 | OImageEffect::hash(image, (OImageEffect::Lighting) lite, spacing); | ||
187 | |||
188 | unsigned int tmp; | ||
189 | |||
190 | if(pixmap.depth() <= 8 ) { | ||
191 | if ( ncols < 2 || ncols > 256 ) | ||
192 | ncols = 3; | ||
193 | QColor *dPal = new QColor[ncols]; | ||
194 | for (int i=0; i<ncols; i++) { | ||
195 | tmp = 0 + 255 * i / ( ncols - 1 ); | ||
196 | dPal[i].setRgb ( tmp, tmp, tmp ); | ||
197 | } | ||
198 | OImageEffect::dither(image, dPal, ncols); | ||
199 | pixmap.convertFromImage(image); | ||
200 | delete [] dPal; | ||
201 | } | ||
202 | else | ||
203 | pixmap.convertFromImage(image); | ||
204 | |||
205 | return pixmap; | ||
206 | } | ||
207 | |||
208 | |||
209 | //====================================================================== | ||
210 | // | ||
211 | // Pattern effects | ||
212 | // | ||
213 | //====================================================================== | ||
214 | |||
215 | #if 0 | ||
216 | void OPixmapEffect::pattern(OPixmap &pixmap, const QColor &ca, | ||
217 | const QColor &cb, unsigned pat[8]) | ||
218 | { | ||
219 | QImage img = pattern(pixmap.size(), ca, cb, pat); | ||
220 | pixmap.convertFromImage(img); | ||
221 | } | ||
222 | #endif | ||
223 | |||
224 | // ----------------------------------------------------------------------------- | ||
225 | |||
226 | OPixmap OPixmapEffect::pattern(const OPixmap& pmtile, QSize size, | ||
227 | const QColor &ca, const QColor &cb, int ncols) | ||
228 | { | ||
229 | if (pmtile.depth() > 8) | ||
230 | ncols = 0; | ||
231 | |||
232 | QImage img = pmtile.convertToImage(); | ||
233 | OImageEffect::flatten(img, ca, cb, ncols); | ||
234 | OPixmap pixmap; | ||
235 | pixmap.convertFromImage(img); | ||
236 | |||
237 | return OPixmapEffect::createTiled(pixmap, size); | ||
238 | } | ||
239 | |||
240 | |||
241 | // ----------------------------------------------------------------------------- | ||
242 | |||
243 | OPixmap OPixmapEffect::createTiled(const OPixmap& pixmap, QSize size) | ||
244 | { | ||
245 | OPixmap pix; | ||
246 | |||
247 | QPainter p(&pix); | ||
248 | p.drawTiledPixmap(0, 0, size.width(), size.height(), pixmap); | ||
249 | |||
250 | return pix; | ||
251 | } | ||
252 | |||
253 | |||
254 | //====================================================================== | ||
255 | // | ||
256 | // Fade effects | ||
257 | // | ||
258 | //====================================================================== | ||
259 | |||
260 | OPixmap& OPixmapEffect::fade(OPixmap &pixmap, double val, const QColor &color) | ||
261 | { | ||
262 | QImage img = pixmap.convertToImage(); | ||
263 | OImageEffect::fade(img, val, color); | ||
264 | pixmap.convertFromImage(img); | ||
265 | |||
266 | return pixmap; | ||
267 | } | ||
268 | |||
269 | |||
270 | // ----------------------------------------------------------------------------- | ||
271 | OPixmap& OPixmapEffect::toGray(OPixmap &pixmap, bool fast) | ||
272 | { | ||
273 | QImage img = pixmap.convertToImage(); | ||
274 | OImageEffect::toGray(img, fast); | ||
275 | pixmap.convertFromImage(img); | ||
276 | |||
277 | return pixmap; | ||
278 | } | ||
279 | |||
280 | // ----------------------------------------------------------------------------- | ||
281 | OPixmap& OPixmapEffect::desaturate(OPixmap &pixmap, float desat) | ||
282 | { | ||
283 | QImage img = pixmap.convertToImage(); | ||
284 | OImageEffect::desaturate(img, desat); | ||
285 | pixmap.convertFromImage(img); | ||
286 | |||
287 | return pixmap; | ||
288 | } | ||
289 | // ----------------------------------------------------------------------------- | ||
290 | OPixmap& OPixmapEffect::contrast(OPixmap &pixmap, int c) | ||
291 | { | ||
292 | QImage img = pixmap.convertToImage(); | ||
293 | OImageEffect::contrast(img, c); | ||
294 | pixmap.convertFromImage(img); | ||
295 | |||
296 | return pixmap; | ||
297 | } | ||
298 | |||
299 | //====================================================================== | ||
300 | // | ||
301 | // Dither effects | ||
302 | // | ||
303 | //====================================================================== | ||
304 | |||
305 | // ----------------------------------------------------------------------------- | ||
306 | OPixmap& OPixmapEffect::dither(OPixmap &pixmap, const QColor *palette, int size) | ||
307 | { | ||
308 | QImage img = pixmap.convertToImage(); | ||
309 | OImageEffect::dither(img, palette, size); | ||
310 | pixmap.convertFromImage(img); | ||
311 | |||
312 | return pixmap; | ||
313 | } | ||
314 | |||
315 | //====================================================================== | ||
316 | // | ||
317 | // Other effects | ||
318 | // | ||
319 | //====================================================================== | ||
320 | |||
321 | OPixmap OPixmapEffect::selectedPixmap( const OPixmap &pix, const QColor &col ) | ||
322 | { | ||
323 | QImage img = pix.convertToImage(); | ||
324 | OImageEffect::selectedImage(img, col); | ||
325 | OPixmap outPix; | ||
326 | outPix.convertFromImage(img); | ||
327 | return outPix; | ||
328 | } | ||