-rw-r--r-- | noncore/styles/theme/ogfxeffect.cpp | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/noncore/styles/theme/ogfxeffect.cpp b/noncore/styles/theme/ogfxeffect.cpp index cc5bbcd..2071a67 100644 --- a/noncore/styles/theme/ogfxeffect.cpp +++ b/noncore/styles/theme/ogfxeffect.cpp | |||
@@ -1,507 +1,509 @@ | |||
1 | /* This file is part of the KDE libraries | 1 | /* This file is part of the KDE libraries |
2 | Copyright (C) 1998, 1999 Christian Tibirna <ctibirna@total.net> | 2 | Copyright (C) 1998, 1999 Christian Tibirna <ctibirna@total.net> |
3 | (C) 1998, 1999 Daniel M. Duley <mosfet@kde.org> | 3 | (C) 1998, 1999 Daniel M. Duley <mosfet@kde.org> |
4 | (C) 1998, 1999 Dirk A. Mueller <mueller@kde.org> | 4 | (C) 1998, 1999 Dirk A. Mueller <mueller@kde.org> |
5 | 5 | ||
6 | */ | 6 | */ |
7 | 7 | ||
8 | // $Id$ | 8 | // $Id$ |
9 | 9 | ||
10 | #include <qimage.h> | 10 | #include <qimage.h> |
11 | #include <qpainter.h> | 11 | #include <qpainter.h> |
12 | 12 | ||
13 | #include <qpe/qmath.h> | 13 | #include <qpe/qmath.h> |
14 | 14 | ||
15 | #include "ogfxeffect.h" | 15 | #include "ogfxeffect.h" |
16 | 16 | ||
17 | #include <cstdlib> | ||
18 | #include <cmath> | ||
17 | 19 | ||
18 | //====================================================================== | 20 | //====================================================================== |
19 | // | 21 | // |
20 | // Gradient effects | 22 | // Gradient effects |
21 | // | 23 | // |
22 | //====================================================================== | 24 | //====================================================================== |
23 | 25 | ||
24 | 26 | ||
25 | QPixmap& OGfxEffect::gradient(QPixmap &pixmap, const QColor &ca, | 27 | QPixmap& OGfxEffect::gradient(QPixmap &pixmap, const QColor &ca, |
26 | const QColor &cb, GradientType eff, int ncols) | 28 | const QColor &cb, GradientType eff, int ncols) |
27 | { | 29 | { |
28 | QImage image = gradient(pixmap.size(), ca, cb, eff, ncols); | 30 | QImage image = gradient(pixmap.size(), ca, cb, eff, ncols); |
29 | pixmap.convertFromImage(image); | 31 | pixmap.convertFromImage(image); |
30 | 32 | ||
31 | return pixmap; | 33 | return pixmap; |
32 | } | 34 | } |
33 | 35 | ||
34 | QImage OGfxEffect::gradient(const QSize &size, const QColor &ca, | 36 | QImage OGfxEffect::gradient(const QSize &size, const QColor &ca, |
35 | const QColor &cb, GradientType eff, int /*ncols*/) | 37 | const QColor &cb, GradientType eff, int /*ncols*/) |
36 | { | 38 | { |
37 | int rDiff, gDiff, bDiff; | 39 | int rDiff, gDiff, bDiff; |
38 | int rca, gca, bca, rcb, gcb, bcb; | 40 | int rca, gca, bca, rcb, gcb, bcb; |
39 | 41 | ||
40 | QImage image(size, 32); | 42 | QImage image(size, 32); |
41 | 43 | ||
42 | if (size.width() == 0 || size.height() == 0) { | 44 | if (size.width() == 0 || size.height() == 0) { |
43 | qDebug ( "WARNING: OGfxEffect::gradient: invalid image" ); | 45 | qDebug ( "WARNING: OGfxEffect::gradient: invalid image" ); |
44 | return image; | 46 | return image; |
45 | } | 47 | } |
46 | 48 | ||
47 | register int x, y; | 49 | register int x, y; |
48 | 50 | ||
49 | rDiff = (rcb = cb.red()) - (rca = ca.red()); | 51 | rDiff = (rcb = cb.red()) - (rca = ca.red()); |
50 | gDiff = (gcb = cb.green()) - (gca = ca.green()); | 52 | gDiff = (gcb = cb.green()) - (gca = ca.green()); |
51 | bDiff = (bcb = cb.blue()) - (bca = ca.blue()); | 53 | bDiff = (bcb = cb.blue()) - (bca = ca.blue()); |
52 | 54 | ||
53 | if( eff == VerticalGradient || eff == HorizontalGradient ){ | 55 | if( eff == VerticalGradient || eff == HorizontalGradient ){ |
54 | 56 | ||
55 | uint *p; | 57 | uint *p; |
56 | uint rgb; | 58 | uint rgb; |
57 | 59 | ||
58 | register int rl = rca << 16; | 60 | register int rl = rca << 16; |
59 | register int gl = gca << 16; | 61 | register int gl = gca << 16; |
60 | register int bl = bca << 16; | 62 | register int bl = bca << 16; |
61 | 63 | ||
62 | if( eff == VerticalGradient ) { | 64 | if( eff == VerticalGradient ) { |
63 | 65 | ||
64 | int rcdelta = ((1<<16) / size.height()) * rDiff; | 66 | int rcdelta = ((1<<16) / size.height()) * rDiff; |
65 | int gcdelta = ((1<<16) / size.height()) * gDiff; | 67 | int gcdelta = ((1<<16) / size.height()) * gDiff; |
66 | int bcdelta = ((1<<16) / size.height()) * bDiff; | 68 | int bcdelta = ((1<<16) / size.height()) * bDiff; |
67 | 69 | ||
68 | for ( y = 0; y < size.height(); y++ ) { | 70 | for ( y = 0; y < size.height(); y++ ) { |
69 | p = (uint *) image.scanLine(y); | 71 | p = (uint *) image.scanLine(y); |
70 | 72 | ||
71 | rl += rcdelta; | 73 | rl += rcdelta; |
72 | gl += gcdelta; | 74 | gl += gcdelta; |
73 | bl += bcdelta; | 75 | bl += bcdelta; |
74 | 76 | ||
75 | rgb = qRgb( (rl>>16), (gl>>16), (bl>>16) ); | 77 | rgb = qRgb( (rl>>16), (gl>>16), (bl>>16) ); |
76 | 78 | ||
77 | for( x = 0; x < size.width(); x++ ) { | 79 | for( x = 0; x < size.width(); x++ ) { |
78 | *p = rgb; | 80 | *p = rgb; |
79 | p++; | 81 | p++; |
80 | } | 82 | } |
81 | } | 83 | } |
82 | 84 | ||
83 | } | 85 | } |
84 | else { // must be HorizontalGradient | 86 | else { // must be HorizontalGradient |
85 | 87 | ||
86 | unsigned int *o_src = (unsigned int *)image.scanLine(0); | 88 | unsigned int *o_src = (unsigned int *)image.scanLine(0); |
87 | unsigned int *src = o_src; | 89 | unsigned int *src = o_src; |
88 | 90 | ||
89 | int rcdelta = ((1<<16) / size.width()) * rDiff; | 91 | int rcdelta = ((1<<16) / size.width()) * rDiff; |
90 | int gcdelta = ((1<<16) / size.width()) * gDiff; | 92 | int gcdelta = ((1<<16) / size.width()) * gDiff; |
91 | int bcdelta = ((1<<16) / size.width()) * bDiff; | 93 | int bcdelta = ((1<<16) / size.width()) * bDiff; |
92 | 94 | ||
93 | for( x = 0; x < size.width(); x++) { | 95 | for( x = 0; x < size.width(); x++) { |
94 | 96 | ||
95 | rl += rcdelta; | 97 | rl += rcdelta; |
96 | gl += gcdelta; | 98 | gl += gcdelta; |
97 | bl += bcdelta; | 99 | bl += bcdelta; |
98 | 100 | ||
99 | *src++ = qRgb( (rl>>16), (gl>>16), (bl>>16)); | 101 | *src++ = qRgb( (rl>>16), (gl>>16), (bl>>16)); |
100 | } | 102 | } |
101 | 103 | ||
102 | src = o_src; | 104 | src = o_src; |
103 | 105 | ||
104 | // Believe it or not, manually copying in a for loop is faster | 106 | // Believe it or not, manually copying in a for loop is faster |
105 | // than calling memcpy for each scanline (on the order of ms...). | 107 | // than calling memcpy for each scanline (on the order of ms...). |
106 | // I think this is due to the function call overhead (mosfet). | 108 | // I think this is due to the function call overhead (mosfet). |
107 | 109 | ||
108 | for (y = 1; y < size.height(); ++y) { | 110 | for (y = 1; y < size.height(); ++y) { |
109 | 111 | ||
110 | p = (unsigned int *)image.scanLine(y); | 112 | p = (unsigned int *)image.scanLine(y); |
111 | src = o_src; | 113 | src = o_src; |
112 | for(x=0; x < size.width(); ++x) | 114 | for(x=0; x < size.width(); ++x) |
113 | *p++ = *src++; | 115 | *p++ = *src++; |
114 | } | 116 | } |
115 | } | 117 | } |
116 | } | 118 | } |
117 | 119 | ||
118 | else { | 120 | else { |
119 | 121 | ||
120 | float rfd, gfd, bfd; | 122 | float rfd, gfd, bfd; |
121 | float rd = rca, gd = gca, bd = bca; | 123 | float rd = rca, gd = gca, bd = bca; |
122 | 124 | ||
123 | unsigned char *xtable[3]; | 125 | unsigned char *xtable[3]; |
124 | unsigned char *ytable[3]; | 126 | unsigned char *ytable[3]; |
125 | 127 | ||
126 | unsigned int w = size.width(), h = size.height(); | 128 | unsigned int w = size.width(), h = size.height(); |
127 | xtable[0] = new unsigned char[w]; | 129 | xtable[0] = new unsigned char[w]; |
128 | xtable[1] = new unsigned char[w]; | 130 | xtable[1] = new unsigned char[w]; |
129 | xtable[2] = new unsigned char[w]; | 131 | xtable[2] = new unsigned char[w]; |
130 | ytable[0] = new unsigned char[h]; | 132 | ytable[0] = new unsigned char[h]; |
131 | ytable[1] = new unsigned char[h]; | 133 | ytable[1] = new unsigned char[h]; |
132 | ytable[2] = new unsigned char[h]; | 134 | ytable[2] = new unsigned char[h]; |
133 | w*=2, h*=2; | 135 | w*=2, h*=2; |
134 | 136 | ||
135 | if ( eff == DiagonalGradient || eff == CrossDiagonalGradient) { | 137 | if ( eff == DiagonalGradient || eff == CrossDiagonalGradient) { |
136 | // Diagonal dgradient code inspired by BlackBox (mosfet) | 138 | // Diagonal dgradient code inspired by BlackBox (mosfet) |
137 | // BlackBox dgradient is (C) Brad Hughes, <bhughes@tcac.net> and | 139 | // BlackBox dgradient is (C) Brad Hughes, <bhughes@tcac.net> and |
138 | // Mike Cole <mike@mydot.com>. | 140 | // Mike Cole <mike@mydot.com>. |
139 | 141 | ||
140 | rfd = (float)rDiff/w; | 142 | rfd = (float)rDiff/w; |
141 | gfd = (float)gDiff/w; | 143 | gfd = (float)gDiff/w; |
142 | bfd = (float)bDiff/w; | 144 | bfd = (float)bDiff/w; |
143 | 145 | ||
144 | int dir; | 146 | int dir; |
145 | for (x = 0; x < size.width(); x++, rd+=rfd, gd+=gfd, bd+=bfd) { | 147 | for (x = 0; x < size.width(); x++, rd+=rfd, gd+=gfd, bd+=bfd) { |
146 | dir = eff == DiagonalGradient? x : size.width() - x - 1; | 148 | dir = eff == DiagonalGradient? x : size.width() - x - 1; |
147 | xtable[0][dir] = (unsigned char) rd; | 149 | xtable[0][dir] = (unsigned char) rd; |
148 | xtable[1][dir] = (unsigned char) gd; | 150 | xtable[1][dir] = (unsigned char) gd; |
149 | xtable[2][dir] = (unsigned char) bd; | 151 | xtable[2][dir] = (unsigned char) bd; |
150 | } | 152 | } |
151 | rfd = (float)rDiff/h; | 153 | rfd = (float)rDiff/h; |
152 | gfd = (float)gDiff/h; | 154 | gfd = (float)gDiff/h; |
153 | bfd = (float)bDiff/h; | 155 | bfd = (float)bDiff/h; |
154 | rd = gd = bd = 0; | 156 | rd = gd = bd = 0; |
155 | for (y = 0; y < size.height(); y++, rd+=rfd, gd+=gfd, bd+=bfd) { | 157 | for (y = 0; y < size.height(); y++, rd+=rfd, gd+=gfd, bd+=bfd) { |
156 | ytable[0][y] = (unsigned char) rd; | 158 | ytable[0][y] = (unsigned char) rd; |
157 | ytable[1][y] = (unsigned char) gd; | 159 | ytable[1][y] = (unsigned char) gd; |
158 | ytable[2][y] = (unsigned char) bd; | 160 | ytable[2][y] = (unsigned char) bd; |
159 | } | 161 | } |
160 | 162 | ||
161 | for (y = 0; y < size.height(); y++) { | 163 | for (y = 0; y < size.height(); y++) { |
162 | unsigned int *scanline = (unsigned int *)image.scanLine(y); | 164 | unsigned int *scanline = (unsigned int *)image.scanLine(y); |
163 | for (x = 0; x < size.width(); x++) { | 165 | for (x = 0; x < size.width(); x++) { |
164 | scanline[x] = qRgb(xtable[0][x] + ytable[0][y], | 166 | scanline[x] = qRgb(xtable[0][x] + ytable[0][y], |
165 | xtable[1][x] + ytable[1][y], | 167 | xtable[1][x] + ytable[1][y], |
166 | xtable[2][x] + ytable[2][y]); | 168 | xtable[2][x] + ytable[2][y]); |
167 | } | 169 | } |
168 | } | 170 | } |
169 | } | 171 | } |
170 | 172 | ||
171 | else if (eff == RectangleGradient || | 173 | else if (eff == RectangleGradient || |
172 | eff == PyramidGradient || | 174 | eff == PyramidGradient || |
173 | eff == PipeCrossGradient || | 175 | eff == PipeCrossGradient || |
174 | eff == EllipticGradient) | 176 | eff == EllipticGradient) |
175 | { | 177 | { |
176 | int rSign = rDiff>0? 1: -1; | 178 | int rSign = rDiff>0? 1: -1; |
177 | int gSign = gDiff>0? 1: -1; | 179 | int gSign = gDiff>0? 1: -1; |
178 | int bSign = bDiff>0? 1: -1; | 180 | int bSign = bDiff>0? 1: -1; |
179 | 181 | ||
180 | rfd = (float)rDiff / size.width(); | 182 | rfd = (float)rDiff / size.width(); |
181 | gfd = (float)gDiff / size.width(); | 183 | gfd = (float)gDiff / size.width(); |
182 | bfd = (float)bDiff / size.width(); | 184 | bfd = (float)bDiff / size.width(); |
183 | 185 | ||
184 | rd = (float)rDiff/2; | 186 | rd = (float)rDiff/2; |
185 | gd = (float)gDiff/2; | 187 | gd = (float)gDiff/2; |
186 | bd = (float)bDiff/2; | 188 | bd = (float)bDiff/2; |
187 | 189 | ||
188 | for (x = 0; x < size.width(); x++, rd-=rfd, gd-=gfd, bd-=bfd) | 190 | for (x = 0; x < size.width(); x++, rd-=rfd, gd-=gfd, bd-=bfd) |
189 | { | 191 | { |
190 | xtable[0][x] = (unsigned char) abs((int)rd); | 192 | xtable[0][x] = (unsigned char) abs((int)rd); |
191 | xtable[1][x] = (unsigned char) abs((int)gd); | 193 | xtable[1][x] = (unsigned char) abs((int)gd); |
192 | xtable[2][x] = (unsigned char) abs((int)bd); | 194 | xtable[2][x] = (unsigned char) abs((int)bd); |
193 | } | 195 | } |
194 | 196 | ||
195 | rfd = (float)rDiff/size.height(); | 197 | rfd = (float)rDiff/size.height(); |
196 | gfd = (float)gDiff/size.height(); | 198 | gfd = (float)gDiff/size.height(); |
197 | bfd = (float)bDiff/size.height(); | 199 | bfd = (float)bDiff/size.height(); |
198 | 200 | ||
199 | rd = (float)rDiff/2; | 201 | rd = (float)rDiff/2; |
200 | gd = (float)gDiff/2; | 202 | gd = (float)gDiff/2; |
201 | bd = (float)bDiff/2; | 203 | bd = (float)bDiff/2; |
202 | 204 | ||
203 | for (y = 0; y < size.height(); y++, rd-=rfd, gd-=gfd, bd-=bfd) | 205 | for (y = 0; y < size.height(); y++, rd-=rfd, gd-=gfd, bd-=bfd) |
204 | { | 206 | { |
205 | ytable[0][y] = (unsigned char) abs((int)rd); | 207 | ytable[0][y] = (unsigned char) abs((int)rd); |
206 | ytable[1][y] = (unsigned char) abs((int)gd); | 208 | ytable[1][y] = (unsigned char) abs((int)gd); |
207 | ytable[2][y] = (unsigned char) abs((int)bd); | 209 | ytable[2][y] = (unsigned char) abs((int)bd); |
208 | } | 210 | } |
209 | unsigned int rgb; | 211 | unsigned int rgb; |
210 | int h = (size.height()+1)>>1; | 212 | int h = (size.height()+1)>>1; |
211 | for (y = 0; y < h; y++) { | 213 | for (y = 0; y < h; y++) { |
212 | unsigned int *sl1 = (unsigned int *)image.scanLine(y); | 214 | unsigned int *sl1 = (unsigned int *)image.scanLine(y); |
213 | unsigned int *sl2 = (unsigned int *)image.scanLine(QMAX(size.height()-y-1, y)); | 215 | unsigned int *sl2 = (unsigned int *)image.scanLine(QMAX(size.height()-y-1, y)); |
214 | 216 | ||
215 | int w = (size.width()+1)>>1; | 217 | int w = (size.width()+1)>>1; |
216 | int x2 = size.width()-1; | 218 | int x2 = size.width()-1; |
217 | 219 | ||
218 | for (x = 0; x < w; x++, x2--) { | 220 | for (x = 0; x < w; x++, x2--) { |
219 | rgb = 0; | 221 | rgb = 0; |
220 | if (eff == PyramidGradient) { | 222 | if (eff == PyramidGradient) { |
221 | rgb = qRgb(rcb-rSign*(xtable[0][x]+ytable[0][y]), | 223 | rgb = qRgb(rcb-rSign*(xtable[0][x]+ytable[0][y]), |
222 | gcb-gSign*(xtable[1][x]+ytable[1][y]), | 224 | gcb-gSign*(xtable[1][x]+ytable[1][y]), |
223 | bcb-bSign*(xtable[2][x]+ytable[2][y])); | 225 | bcb-bSign*(xtable[2][x]+ytable[2][y])); |
224 | } | 226 | } |
225 | if (eff == RectangleGradient) { | 227 | if (eff == RectangleGradient) { |
226 | rgb = qRgb(rcb - rSign * | 228 | rgb = qRgb(rcb - rSign * |
227 | QMAX(xtable[0][x], ytable[0][y]) * 2, | 229 | QMAX(xtable[0][x], ytable[0][y]) * 2, |
228 | gcb - gSign * | 230 | gcb - gSign * |
229 | QMAX(xtable[1][x], ytable[1][y]) * 2, | 231 | QMAX(xtable[1][x], ytable[1][y]) * 2, |
230 | bcb - bSign * | 232 | bcb - bSign * |
231 | QMAX(xtable[2][x], ytable[2][y]) * 2); | 233 | QMAX(xtable[2][x], ytable[2][y]) * 2); |
232 | } | 234 | } |
233 | if (eff == PipeCrossGradient) { | 235 | if (eff == PipeCrossGradient) { |
234 | rgb = qRgb(rcb - rSign * | 236 | rgb = qRgb(rcb - rSign * |
235 | QMIN(xtable[0][x], ytable[0][y]) * 2, | 237 | QMIN(xtable[0][x], ytable[0][y]) * 2, |
236 | gcb - gSign * | 238 | gcb - gSign * |
237 | QMIN(xtable[1][x], ytable[1][y]) * 2, | 239 | QMIN(xtable[1][x], ytable[1][y]) * 2, |
238 | bcb - bSign * | 240 | bcb - bSign * |
239 | QMIN(xtable[2][x], ytable[2][y]) * 2); | 241 | QMIN(xtable[2][x], ytable[2][y]) * 2); |
240 | } | 242 | } |
241 | if (eff == EllipticGradient) { | 243 | if (eff == EllipticGradient) { |
242 | rgb = qRgb(rcb - rSign * | 244 | rgb = qRgb(rcb - rSign * |
243 | (int)sqrt((xtable[0][x]*xtable[0][x] + | 245 | (int)sqrt((xtable[0][x]*xtable[0][x] + |
244 | ytable[0][y]*ytable[0][y])*2.0), | 246 | ytable[0][y]*ytable[0][y])*2.0), |
245 | gcb - gSign * | 247 | gcb - gSign * |
246 | (int)sqrt((xtable[1][x]*xtable[1][x] + | 248 | (int)sqrt((xtable[1][x]*xtable[1][x] + |
247 | ytable[1][y]*ytable[1][y])*2.0), | 249 | ytable[1][y]*ytable[1][y])*2.0), |
248 | bcb - bSign * | 250 | bcb - bSign * |
249 | (int)sqrt((xtable[2][x]*xtable[2][x] + | 251 | (int)sqrt((xtable[2][x]*xtable[2][x] + |
250 | ytable[2][y]*ytable[2][y])*2.0)); | 252 | ytable[2][y]*ytable[2][y])*2.0)); |
251 | } | 253 | } |
252 | 254 | ||
253 | sl1[x] = sl2[x] = rgb; | 255 | sl1[x] = sl2[x] = rgb; |
254 | sl1[x2] = sl2[x2] = rgb; | 256 | sl1[x2] = sl2[x2] = rgb; |
255 | } | 257 | } |
256 | } | 258 | } |
257 | } | 259 | } |
258 | 260 | ||
259 | delete [] xtable[0]; | 261 | delete [] xtable[0]; |
260 | delete [] xtable[1]; | 262 | delete [] xtable[1]; |
261 | delete [] xtable[2]; | 263 | delete [] xtable[2]; |
262 | delete [] ytable[0]; | 264 | delete [] ytable[0]; |
263 | delete [] ytable[1]; | 265 | delete [] ytable[1]; |
264 | delete [] ytable[2]; | 266 | delete [] ytable[2]; |
265 | } | 267 | } |
266 | return image; | 268 | return image; |
267 | } | 269 | } |
268 | 270 | ||
269 | 271 | ||
270 | //====================================================================== | 272 | //====================================================================== |
271 | // | 273 | // |
272 | // Blend effects | 274 | // Blend effects |
273 | // | 275 | // |
274 | //====================================================================== | 276 | //====================================================================== |
275 | 277 | ||
276 | 278 | ||
277 | QPixmap& OGfxEffect::blend(QPixmap &pixmap, float initial_intensity, | 279 | QPixmap& OGfxEffect::blend(QPixmap &pixmap, float initial_intensity, |
278 | const QColor &bgnd, GradientType eff, | 280 | const QColor &bgnd, GradientType eff, |
279 | bool anti_dir, int /*ncols*/) | 281 | bool anti_dir, int /*ncols*/) |
280 | { | 282 | { |
281 | QImage image = pixmap.convertToImage(); | 283 | QImage image = pixmap.convertToImage(); |
282 | OGfxEffect::blend(image, initial_intensity, bgnd, eff, anti_dir); | 284 | OGfxEffect::blend(image, initial_intensity, bgnd, eff, anti_dir); |
283 | 285 | ||
284 | if ( pixmap. depth ( ) <= 8 ) | 286 | if ( pixmap. depth ( ) <= 8 ) |
285 | image. convertDepth ( pixmap. depth ( )); | 287 | image. convertDepth ( pixmap. depth ( )); |
286 | 288 | ||
287 | pixmap.convertFromImage(image); | 289 | pixmap.convertFromImage(image); |
288 | 290 | ||
289 | return pixmap; | 291 | return pixmap; |
290 | } | 292 | } |
291 | 293 | ||
292 | 294 | ||
293 | QImage& OGfxEffect::blend(QImage &image, float initial_intensity, | 295 | QImage& OGfxEffect::blend(QImage &image, float initial_intensity, |
294 | const QColor &bgnd, GradientType eff, | 296 | const QColor &bgnd, GradientType eff, |
295 | bool anti_dir) | 297 | bool anti_dir) |
296 | { | 298 | { |
297 | if (image.width() == 0 || image.height() == 0) { | 299 | if (image.width() == 0 || image.height() == 0) { |
298 | qDebug ( "Invalid image\n" ); | 300 | qDebug ( "Invalid image\n" ); |
299 | return image; | 301 | return image; |
300 | } | 302 | } |
301 | 303 | ||
302 | int r_bgnd = bgnd.red(), g_bgnd = bgnd.green(), b_bgnd = bgnd.blue(); | 304 | int r_bgnd = bgnd.red(), g_bgnd = bgnd.green(), b_bgnd = bgnd.blue(); |
303 | int r, g, b; | 305 | int r, g, b; |
304 | int ind; | 306 | int ind; |
305 | 307 | ||
306 | unsigned int xi, xf, yi, yf; | 308 | unsigned int xi, xf, yi, yf; |
307 | unsigned int a; | 309 | unsigned int a; |
308 | 310 | ||
309 | // check the boundaries of the initial intesity param | 311 | // check the boundaries of the initial intesity param |
310 | float unaffected = 1; | 312 | float unaffected = 1; |
311 | if (initial_intensity > 1) initial_intensity = 1; | 313 | if (initial_intensity > 1) initial_intensity = 1; |
312 | if (initial_intensity < -1) initial_intensity = -1; | 314 | if (initial_intensity < -1) initial_intensity = -1; |
313 | if (initial_intensity < 0) { | 315 | if (initial_intensity < 0) { |
314 | unaffected = 1. + initial_intensity; | 316 | unaffected = 1. + initial_intensity; |
315 | initial_intensity = 0; | 317 | initial_intensity = 0; |
316 | } | 318 | } |
317 | 319 | ||
318 | 320 | ||
319 | float intensity = initial_intensity; | 321 | float intensity = initial_intensity; |
320 | float var = 1. - initial_intensity; | 322 | float var = 1. - initial_intensity; |
321 | 323 | ||
322 | if (anti_dir) { | 324 | if (anti_dir) { |
323 | initial_intensity = intensity = 1.; | 325 | initial_intensity = intensity = 1.; |
324 | var = -var; | 326 | var = -var; |
325 | } | 327 | } |
326 | 328 | ||
327 | register int x, y; | 329 | register int x, y; |
328 | 330 | ||
329 | unsigned int *data = (unsigned int *)image.bits(); | 331 | unsigned int *data = (unsigned int *)image.bits(); |
330 | 332 | ||
331 | if( eff == VerticalGradient || eff == HorizontalGradient ) { | 333 | if( eff == VerticalGradient || eff == HorizontalGradient ) { |
332 | 334 | ||
333 | // set the image domain to apply the effect to | 335 | // set the image domain to apply the effect to |
334 | xi = 0, xf = image.width(); | 336 | xi = 0, xf = image.width(); |
335 | yi = 0, yf = image.height(); | 337 | yi = 0, yf = image.height(); |
336 | if (eff == VerticalGradient) { | 338 | if (eff == VerticalGradient) { |
337 | if (anti_dir) yf = (int)(image.height() * unaffected); | 339 | if (anti_dir) yf = (int)(image.height() * unaffected); |
338 | else yi = (int)(image.height() * (1 - unaffected)); | 340 | else yi = (int)(image.height() * (1 - unaffected)); |
339 | } | 341 | } |
340 | else { | 342 | else { |
341 | if (anti_dir) xf = (int)(image.width() * unaffected); | 343 | if (anti_dir) xf = (int)(image.width() * unaffected); |
342 | else xi = (int)(image.height() * (1 - unaffected)); | 344 | else xi = (int)(image.height() * (1 - unaffected)); |
343 | } | 345 | } |
344 | 346 | ||
345 | var /= (eff == VerticalGradient?yf-yi:xf-xi); | 347 | var /= (eff == VerticalGradient?yf-yi:xf-xi); |
346 | 348 | ||
347 | for (y = yi; y < (int)yf; y++) { | 349 | for (y = yi; y < (int)yf; y++) { |
348 | intensity = eff == VerticalGradient? intensity + var : | 350 | intensity = eff == VerticalGradient? intensity + var : |
349 | initial_intensity; | 351 | initial_intensity; |
350 | for (x = xi; x < (int)xf ; x++) { | 352 | for (x = xi; x < (int)xf ; x++) { |
351 | if (eff == HorizontalGradient) intensity += var; | 353 | if (eff == HorizontalGradient) intensity += var; |
352 | ind = x + image.width() * y ; | 354 | ind = x + image.width() * y ; |
353 | r = qRed (data[ind]) + (int)(intensity * | 355 | r = qRed (data[ind]) + (int)(intensity * |
354 | (r_bgnd - qRed (data[ind]))); | 356 | (r_bgnd - qRed (data[ind]))); |
355 | g = qGreen(data[ind]) + (int)(intensity * | 357 | g = qGreen(data[ind]) + (int)(intensity * |
356 | (g_bgnd - qGreen(data[ind]))); | 358 | (g_bgnd - qGreen(data[ind]))); |
357 | b = qBlue (data[ind]) + (int)(intensity * | 359 | b = qBlue (data[ind]) + (int)(intensity * |
358 | (b_bgnd - qBlue (data[ind]))); | 360 | (b_bgnd - qBlue (data[ind]))); |
359 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 361 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
360 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 362 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
361 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 363 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
362 | a = qAlpha(data[ind]); | 364 | a = qAlpha(data[ind]); |
363 | data[ind] = qRgba(r, g, b, a); | 365 | data[ind] = qRgba(r, g, b, a); |
364 | } | 366 | } |
365 | } | 367 | } |
366 | } | 368 | } |
367 | else if (eff == DiagonalGradient || eff == CrossDiagonalGradient) { | 369 | else if (eff == DiagonalGradient || eff == CrossDiagonalGradient) { |
368 | float xvar = var / 2 / image.width(); // / unaffected; | 370 | float xvar = var / 2 / image.width(); // / unaffected; |
369 | float yvar = var / 2 / image.height(); // / unaffected; | 371 | float yvar = var / 2 / image.height(); // / unaffected; |
370 | float tmp; | 372 | float tmp; |
371 | 373 | ||
372 | for (x = 0; x < image.width() ; x++) { | 374 | for (x = 0; x < image.width() ; x++) { |
373 | tmp = xvar * (eff == DiagonalGradient? x : image.width()-x-1); | 375 | tmp = xvar * (eff == DiagonalGradient? x : image.width()-x-1); |
374 | for (y = 0; y < image.height() ; y++) { | 376 | for (y = 0; y < image.height() ; y++) { |
375 | intensity = initial_intensity + tmp + yvar * y; | 377 | intensity = initial_intensity + tmp + yvar * y; |
376 | ind = x + image.width() * y ; | 378 | ind = x + image.width() * y ; |
377 | r = qRed (data[ind]) + (int)(intensity * | 379 | r = qRed (data[ind]) + (int)(intensity * |
378 | (r_bgnd - qRed (data[ind]))); | 380 | (r_bgnd - qRed (data[ind]))); |
379 | g = qGreen(data[ind]) + (int)(intensity * | 381 | g = qGreen(data[ind]) + (int)(intensity * |
380 | (g_bgnd - qGreen(data[ind]))); | 382 | (g_bgnd - qGreen(data[ind]))); |
381 | b = qBlue (data[ind]) + (int)(intensity * | 383 | b = qBlue (data[ind]) + (int)(intensity * |
382 | (b_bgnd - qBlue (data[ind]))); | 384 | (b_bgnd - qBlue (data[ind]))); |
383 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 385 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
384 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 386 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
385 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 387 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
386 | a = qAlpha(data[ind]); | 388 | a = qAlpha(data[ind]); |
387 | data[ind] = qRgba(r, g, b, a); | 389 | data[ind] = qRgba(r, g, b, a); |
388 | } | 390 | } |
389 | } | 391 | } |
390 | } | 392 | } |
391 | 393 | ||
392 | else if (eff == RectangleGradient || eff == EllipticGradient) { | 394 | else if (eff == RectangleGradient || eff == EllipticGradient) { |
393 | float xvar; | 395 | float xvar; |
394 | float yvar; | 396 | float yvar; |
395 | 397 | ||
396 | for (x = 0; x < image.width() / 2 + image.width() % 2; x++) { | 398 | for (x = 0; x < image.width() / 2 + image.width() % 2; x++) { |
397 | xvar = var / image.width() * (image.width() - x*2/unaffected-1); | 399 | xvar = var / image.width() * (image.width() - x*2/unaffected-1); |
398 | for (y = 0; y < image.height() / 2 + image.height() % 2; y++) { | 400 | for (y = 0; y < image.height() / 2 + image.height() % 2; y++) { |
399 | yvar = var / image.height() * (image.height() - y*2/unaffected -1); | 401 | yvar = var / image.height() * (image.height() - y*2/unaffected -1); |
400 | 402 | ||
401 | if (eff == RectangleGradient) | 403 | if (eff == RectangleGradient) |
402 | intensity = initial_intensity + QMAX(xvar, yvar); | 404 | intensity = initial_intensity + QMAX(xvar, yvar); |
403 | else | 405 | else |
404 | intensity = initial_intensity + qSqrt(xvar * xvar + yvar * yvar); | 406 | intensity = initial_intensity + qSqrt(xvar * xvar + yvar * yvar); |
405 | if (intensity > 1) intensity = 1; | 407 | if (intensity > 1) intensity = 1; |
406 | if (intensity < 0) intensity = 0; | 408 | if (intensity < 0) intensity = 0; |
407 | 409 | ||
408 | //NW | 410 | //NW |
409 | ind = x + image.width() * y ; | 411 | ind = x + image.width() * y ; |
410 | r = qRed (data[ind]) + (int)(intensity * | 412 | r = qRed (data[ind]) + (int)(intensity * |
411 | (r_bgnd - qRed (data[ind]))); | 413 | (r_bgnd - qRed (data[ind]))); |
412 | g = qGreen(data[ind]) + (int)(intensity * | 414 | g = qGreen(data[ind]) + (int)(intensity * |
413 | (g_bgnd - qGreen(data[ind]))); | 415 | (g_bgnd - qGreen(data[ind]))); |
414 | b = qBlue (data[ind]) + (int)(intensity * | 416 | b = qBlue (data[ind]) + (int)(intensity * |
415 | (b_bgnd - qBlue (data[ind]))); | 417 | (b_bgnd - qBlue (data[ind]))); |
416 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 418 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
417 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 419 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
418 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 420 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
419 | a = qAlpha(data[ind]); | 421 | a = qAlpha(data[ind]); |
420 | data[ind] = qRgba(r, g, b, a); | 422 | data[ind] = qRgba(r, g, b, a); |
421 | 423 | ||
422 | //NE | 424 | //NE |
423 | ind = image.width() - x - 1 + image.width() * y ; | 425 | ind = image.width() - x - 1 + image.width() * y ; |
424 | r = qRed (data[ind]) + (int)(intensity * | 426 | r = qRed (data[ind]) + (int)(intensity * |
425 | (r_bgnd - qRed (data[ind]))); | 427 | (r_bgnd - qRed (data[ind]))); |
426 | g = qGreen(data[ind]) + (int)(intensity * | 428 | g = qGreen(data[ind]) + (int)(intensity * |
427 | (g_bgnd - qGreen(data[ind]))); | 429 | (g_bgnd - qGreen(data[ind]))); |
428 | b = qBlue (data[ind]) + (int)(intensity * | 430 | b = qBlue (data[ind]) + (int)(intensity * |
429 | (b_bgnd - qBlue (data[ind]))); | 431 | (b_bgnd - qBlue (data[ind]))); |
430 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 432 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
431 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 433 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
432 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 434 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
433 | a = qAlpha(data[ind]); | 435 | a = qAlpha(data[ind]); |
434 | data[ind] = qRgba(r, g, b, a); | 436 | data[ind] = qRgba(r, g, b, a); |
435 | } | 437 | } |
436 | } | 438 | } |
437 | 439 | ||
438 | //CT loop is doubled because of stupid central row/column issue. | 440 | //CT loop is doubled because of stupid central row/column issue. |
439 | // other solution? | 441 | // other solution? |
440 | for (x = 0; x < image.width() / 2; x++) { | 442 | for (x = 0; x < image.width() / 2; x++) { |
441 | xvar = var / image.width() * (image.width() - x*2/unaffected-1); | 443 | xvar = var / image.width() * (image.width() - x*2/unaffected-1); |
442 | for (y = 0; y < image.height() / 2; y++) { | 444 | for (y = 0; y < image.height() / 2; y++) { |
443 | yvar = var / image.height() * (image.height() - y*2/unaffected -1); | 445 | yvar = var / image.height() * (image.height() - y*2/unaffected -1); |
444 | 446 | ||
445 | if (eff == RectangleGradient) | 447 | if (eff == RectangleGradient) |
446 | intensity = initial_intensity + QMAX(xvar, yvar); | 448 | intensity = initial_intensity + QMAX(xvar, yvar); |
447 | else | 449 | else |
448 | intensity = initial_intensity + qSqrt(xvar * xvar + yvar * yvar); | 450 | intensity = initial_intensity + qSqrt(xvar * xvar + yvar * yvar); |
449 | if (intensity > 1) intensity = 1; | 451 | if (intensity > 1) intensity = 1; |
450 | if (intensity < 0) intensity = 0; | 452 | if (intensity < 0) intensity = 0; |
451 | 453 | ||
452 | //SW | 454 | //SW |
453 | ind = x + image.width() * (image.height() - y -1) ; | 455 | ind = x + image.width() * (image.height() - y -1) ; |
454 | r = qRed (data[ind]) + (int)(intensity * | 456 | r = qRed (data[ind]) + (int)(intensity * |
455 | (r_bgnd - qRed (data[ind]))); | 457 | (r_bgnd - qRed (data[ind]))); |
456 | g = qGreen(data[ind]) + (int)(intensity * | 458 | g = qGreen(data[ind]) + (int)(intensity * |
457 | (g_bgnd - qGreen(data[ind]))); | 459 | (g_bgnd - qGreen(data[ind]))); |
458 | b = qBlue (data[ind]) + (int)(intensity * | 460 | b = qBlue (data[ind]) + (int)(intensity * |
459 | (b_bgnd - qBlue (data[ind]))); | 461 | (b_bgnd - qBlue (data[ind]))); |
460 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 462 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
461 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 463 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
462 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 464 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
463 | a = qAlpha(data[ind]); | 465 | a = qAlpha(data[ind]); |
464 | data[ind] = qRgba(r, g, b, a); | 466 | data[ind] = qRgba(r, g, b, a); |
465 | 467 | ||
466 | //SE | 468 | //SE |
467 | ind = image.width()-x-1 + image.width() * (image.height() - y - 1) ; | 469 | ind = image.width()-x-1 + image.width() * (image.height() - y - 1) ; |
468 | r = qRed (data[ind]) + (int)(intensity * | 470 | r = qRed (data[ind]) + (int)(intensity * |
469 | (r_bgnd - qRed (data[ind]))); | 471 | (r_bgnd - qRed (data[ind]))); |
470 | g = qGreen(data[ind]) + (int)(intensity * | 472 | g = qGreen(data[ind]) + (int)(intensity * |
471 | (g_bgnd - qGreen(data[ind]))); | 473 | (g_bgnd - qGreen(data[ind]))); |
472 | b = qBlue (data[ind]) + (int)(intensity * | 474 | b = qBlue (data[ind]) + (int)(intensity * |
473 | (b_bgnd - qBlue (data[ind]))); | 475 | (b_bgnd - qBlue (data[ind]))); |
474 | if (r > 255) r = 255; if (r < 0 ) r = 0; | 476 | if (r > 255) r = 255; if (r < 0 ) r = 0; |
475 | if (g > 255) g = 255; if (g < 0 ) g = 0; | 477 | if (g > 255) g = 255; if (g < 0 ) g = 0; |
476 | if (b > 255) b = 255; if (b < 0 ) b = 0; | 478 | if (b > 255) b = 255; if (b < 0 ) b = 0; |
477 | a = qAlpha(data[ind]); | 479 | a = qAlpha(data[ind]); |
478 | data[ind] = qRgba(r, g, b, a); | 480 | data[ind] = qRgba(r, g, b, a); |
479 | } | 481 | } |
480 | } | 482 | } |
481 | } | 483 | } |
482 | 484 | ||
483 | else | 485 | else |
484 | qDebug ( "not implemented\n" ); | 486 | qDebug ( "not implemented\n" ); |
485 | 487 | ||
486 | return image; | 488 | return image; |
487 | } | 489 | } |
488 | 490 | ||
489 | #if 0 | 491 | #if 0 |
490 | // Not very efficient as we create a third big image... | 492 | // Not very efficient as we create a third big image... |
491 | // | 493 | // |
492 | QImage& KQGfxEffect::blend(QImage &image1, QImage &image2, | 494 | QImage& KQGfxEffect::blend(QImage &image1, QImage &image2, |
493 | GradientType gt, int xf, int yf) | 495 | GradientType gt, int xf, int yf) |
494 | { | 496 | { |
495 | if (image1.width() == 0 || image1.height() == 0 || | 497 | if (image1.width() == 0 || image1.height() == 0 || |
496 | image2.width() == 0 || image2.height() == 0) | 498 | image2.width() == 0 || image2.height() == 0) |
497 | return image1; | 499 | return image1; |
498 | 500 | ||
499 | QImage image3; | 501 | QImage image3; |
500 | 502 | ||
501 | image3 = KQGfxEffect::unbalancedGradient(image1.size(), | 503 | image3 = KQGfxEffect::unbalancedGradient(image1.size(), |
502 | QColor(0,0,0), QColor(255,255,255), | 504 | QColor(0,0,0), QColor(255,255,255), |
503 | gt, xf, yf, 0); | 505 | gt, xf, yf, 0); |
504 | 506 | ||
505 | return blend(image1,image2,image3, Red); // Channel to use is arbitrary | 507 | return blend(image1,image2,image3, Red); // Channel to use is arbitrary |
506 | } | 508 | } |
507 | #endif | 509 | #endif |