1 | #ifndef __BLITZ_H |
2 | #define __BLITZ_H |
3 | |
4 | /* |
5 | Copyright (C) 1998, 1999, 2001, 2002, 2004, 2005, 2007 |
6 | Daniel M. Duley <daniel.duley@verizon.net> |
7 | (C) 2004 Zack Rusin <zack@kde.org> |
8 | (C) 2000 Josef Weidendorfer <weidendo@in.tum.de> |
9 | (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl> |
10 | (C) 1998, 1999 Christian Tibirna <ctibirna@total.net> |
11 | (C) 1998, 1999 Dirk Mueller <mueller@kde.org> |
12 | |
13 | Redistribution and use in source and binary forms, with or without |
14 | modification, are permitted provided that the following conditions |
15 | are met: |
16 | |
17 | 1. Redistributions of source code must retain the above copyright |
18 | notice, this list of conditions and the following disclaimer. |
19 | 2. Redistributions in binary form must reproduce the above copyright |
20 | notice, this list of conditions and the following disclaimer in the |
21 | documentation and/or other materials provided with the distribution. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
24 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
25 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
26 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
27 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
28 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
32 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | |
34 | */ |
35 | |
36 | #include <QtGui/QImage> |
37 | #include "qimageblitz_export.h" |
38 | |
39 | class QIMAGEBLITZ_EXPORT Blitz |
40 | { |
41 | public: |
42 | enum GradientType {VerticalGradient=0, HorizontalGradient, DiagonalGradient, |
43 | CrossDiagonalGradient, PyramidGradient, RectangleGradient, |
44 | PipeCrossGradient, EllipticGradient}; |
45 | |
46 | enum RGBChannel{Grayscale=0, Brightness, Red, Green, Blue, Alpha, All}; |
47 | enum EffectQuality{Low=0, High}; |
48 | |
49 | enum ScaleFilterType{UndefinedFilter=0, PointFilter, BoxFilter, |
50 | TriangleFilter, HermiteFilter, HanningFilter, HammingFilter, |
51 | BlackmanFilter, GaussianFilter, QuadraticFilter, CubicFilter, |
52 | CatromFilter, MitchellFilter, LanczosFilter, BesselFilter, |
53 | SincFilter}; |
54 | |
55 | enum ModulationType{Intensity=0, Saturation, HueShift, Contrast}; |
56 | |
57 | /** |
58 | * Inverts an image. If the machine supports MMX this can do two pixels |
59 | * at a time, otherwise it's the same as QImage::invertPixels(). |
60 | * |
61 | * @param img The image to invert. |
62 | * @param mode If to invert the alpha channel or not. |
63 | * @author Daniel M. Duley (mosfet) |
64 | */ |
65 | static bool invert(QImage &img, QImage::InvertMode mode = |
66 | QImage::InvertRgb); |
67 | /** |
68 | * Grayscales an image. |
69 | * |
70 | * @param img The image to grayscale. |
71 | * @param reduceDepth If true the result will be an 8bit palette image. |
72 | * @author Daniel M. Duley (mosfet) |
73 | */ |
74 | static bool grayscale(QImage &img, bool reduceDepth=false); |
75 | /** |
76 | * A fast smoothscale method with MMX support. |
77 | * |
78 | * @param img The image to smoothscale. |
79 | * @param sz The size to scale to. |
80 | * @param aspectRatio What aspect ratio to use, if any. |
81 | * @return The processed image. The original is not changed. |
82 | * @author Daniel M. Duley (mosfet) |
83 | */ |
84 | static QImage smoothScale(QImage &img, const QSize &sz, |
85 | Qt::AspectRatioMode aspectRatio = |
86 | Qt::IgnoreAspectRatio); |
87 | static QImage smoothScale(QImage &img, int dw, int dh, |
88 | Qt::AspectRatioMode aspectRatio = |
89 | Qt::IgnoreAspectRatio); |
90 | /** |
91 | * Smoothscales an image using a high-quality filter. |
92 | * |
93 | * @param img The image to smoothscale. |
94 | * @param sz The size to scale to. |
95 | * @param blur A blur factor. Values greater than 1.0 blur while values |
96 | * less than 1.0 sharpen. |
97 | * @param filter The filter type. |
98 | * @param aspectRatio What aspect ratio to use, if any. |
99 | * @return The processed image. The original is not changed. |
100 | * @author Daniel M. Duley (mosfet) |
101 | */ |
102 | static QImage smoothScaleFilter(QImage &img, const QSize &sz, |
103 | float blur=1.0, |
104 | ScaleFilterType filter=BlackmanFilter, |
105 | Qt::AspectRatioMode aspectRatio = |
106 | Qt::IgnoreAspectRatio); |
107 | static QImage smoothScaleFilter(QImage &img, int dwX, int dwY, |
108 | float blur=1.0, |
109 | ScaleFilterType filter=BlackmanFilter, |
110 | Qt::AspectRatioMode aspectRatio = |
111 | Qt::IgnoreAspectRatio); |
112 | |
113 | /** |
114 | * Convolves an image using a convolution matrix, (kernel), and provides |
115 | * the basis for many different image filters and effects. |
116 | * |
117 | * @param img The image to process. |
118 | * @param matrix_size The size of one side of the convolution matrix, ie: |
119 | * a 3x3 matrix would be 3, not 9. |
120 | * @param matrix The convolution matrix. |
121 | * @return The processed image. The original is not changed. |
122 | * @author Daniel M. Duley (mosfet) |
123 | */ |
124 | static QImage convolve(QImage &img, int matrix_size, float *matrix); |
125 | /** |
126 | * Same as above but with a faster integer matrix. |
127 | */ |
128 | static QImage convolveInteger(QImage &img, int matrix_size, int *matrix, |
129 | int divisor=0); |
130 | /** |
131 | * A fast blur algorithm. |
132 | * |
133 | * @param img The image to process. |
134 | * @param radius The radius of the blur. Larger values blur more. |
135 | * @return The processed image. The original is not changed. |
136 | * @author Daniel M. Duley (mosfet) |
137 | */ |
138 | static QImage blur(QImage &img, int radius=3); |
139 | /** |
140 | * A high quality gaussian/convolve-based blur. |
141 | * |
142 | * @param img The image to process. |
143 | * @param radius The radius of the gaussian, not counting the center |
144 | * pixel. Use 0, (recommended), and an appropriate one will be used. |
145 | * @param sigma The standard deviation of the gaussian in pixels. Use |
146 | * 1.0, (recommended), for the default value. |
147 | * @return The processed image. The original is not changed. |
148 | * @author Daniel M. Duley (mosfet) |
149 | */ |
150 | static QImage gaussianBlur(QImage &img, float radius=0.0, float sigma=1.0); |
151 | /** |
152 | * Detects edges in an image using the Sobel algorithm. |
153 | * |
154 | * @param img The image to process. |
155 | * @return The processed image. The original is not changed. |
156 | * @author Daniel M. Duley (mosfet) |
157 | */ |
158 | static QImage edge(QImage &img); |
159 | /** |
160 | * Detects edges in an image using a convolution matrix. |
161 | * |
162 | * @param img The image to process. |
163 | * @param radius The radius of the gaussian, not counting the center |
164 | * pixel. Use 0, (recommended), and an appropriate one will be used. |
165 | * @param quality Determines if to use a small or large convolution matrix. |
166 | * @return The processed image. The original is not changed. |
167 | * @author Daniel M. Duley (mosfet) |
168 | */ |
169 | static QImage convolveEdge(QImage &img, float radius=0.0, |
170 | EffectQuality quality=High); |
171 | /** |
172 | * Sharpens an image. |
173 | * |
174 | * @param img The image to process. |
175 | * @param radius The radius of the gaussian, not counting the center |
176 | * pixel. Use 0, (recommended), and an appropriate one will be used. |
177 | * @param sigma The standard deviation of the gaussian in pixels. Use |
178 | * 1.0, (recommended), for the default value. |
179 | * @param quality Determines if to use a small or large convolution matrix. |
180 | * @return The processed image. The original is not changed. |
181 | * @author Daniel M. Duley (mosfet) |
182 | */ |
183 | static QImage gaussianSharpen(QImage &img, float radius=0.0, float sigma=1.0, |
184 | EffectQuality quality=High); |
185 | /** |
186 | * A cheap, fast sharpen. |
187 | * |
188 | * @param radius The radius. Larger values sharpen more. |
189 | * @return The processed image. The original is not changed. |
190 | * @author Daniel M. Duley (mosfet) |
191 | */ |
192 | static QImage sharpen(QImage &img, int radius=3); |
193 | /** |
194 | * Embosses an image. |
195 | * |
196 | * @param img The image to process. |
197 | * @param radius The radius of the gaussian, not counting the center |
198 | * pixel. Use 0, (recommended), and an appropriate one will be used. |
199 | * @param sigma The standard deviation of the gaussian in pixels. Use |
200 | * 1.0, (recommended), for the default value. |
201 | * @param quality Determines if to use a small or large convolution matrix. |
202 | * @return The processed image. The original is not changed. |
203 | * @author Daniel M. Duley (mosfet) |
204 | */ |
205 | static QImage emboss(QImage &img, float radius=0.0, float sigma=1.0, |
206 | EffectQuality quality=High); |
207 | /** |
208 | * Antialiases an image. |
209 | * |
210 | * @param img The image to process. |
211 | * @return The processed image. The original is not changed. |
212 | * @author Daniel M. Duley (mosfet) |
213 | */ |
214 | static QImage antialias(QImage &img); |
215 | /** |
216 | * Minimizes speckle noise in the source image using the 8 hull |
217 | * algorithm. |
218 | * |
219 | * @param img The image to process. |
220 | * @return A reference to the image for convenience. |
221 | * @author Daniel M. Duley (mosfet) |
222 | */ |
223 | static QImage& despeckle(QImage &img); |
224 | /** |
225 | * High quality, fast HSV contrast. |
226 | * |
227 | * @param img The image to process. |
228 | * @param sharpen If true sharpness is increase, (spiffed). Otherwise |
229 | * it is decreased, (dulled). |
230 | * @param weight How much to spiff or dull. |
231 | * @return A reference to the image for convenience. |
232 | * @author Daniel M. Duley (mosfet) |
233 | */ |
234 | static QImage& contrast(QImage &img, bool sharpen, int weight=3); |
235 | /** |
236 | * Performs histogram equalization. |
237 | * |
238 | * @param img The image to equalize. |
239 | * @author Daniel M. Duley (mosfet) |
240 | */ |
241 | static bool equalize(QImage &img); |
242 | /** |
243 | * Normalizes the pixel values to span the full range of color values. |
244 | * This is a contrast enhancement technique. |
245 | * |
246 | * @param img The image to normalize |
247 | * @author Daniel M. Duley (mosfet) |
248 | */ |
249 | static bool normalize(QImage &img); |
250 | /** |
251 | * Either brighten or dim the image by a specified percent. For example, |
252 | * 0.50 will modify the colors by 50%. |
253 | * |
254 | * @param img The image to process. |
255 | * @param percent The percent value. Use a negative value to dim. |
256 | * @return A reference to the image for convenience. |
257 | * @author Daniel M. Duley (mosfet) |
258 | * @author Benjamin Roe (ben@benroe.com) |
259 | */ |
260 | static QImage& intensity(QImage &img, float percent); |
261 | /** |
262 | * Modifies the intensity of an image's RGB channel. |
263 | * |
264 | * @param img The image to process. |
265 | * @param percent Percent value. Use a negative value to dim. |
266 | * @param channel Which channel(s) should be modified. Only Red, Green, |
267 | * and Blue are valid. |
268 | * @return A reference to the image for convenience. |
269 | * @author Daniel M. Duley (mosfet) |
270 | */ |
271 | static QImage& channelIntensity(QImage &img, float percent, |
272 | RGBChannel channel); |
273 | /** |
274 | * Desaturate an image evenly. |
275 | * |
276 | * @param img The image to process. |
277 | * @param desat A value between 0 and 1 setting the degree of desaturation |
278 | * @return A reference to the image for convenience. |
279 | * @author Daniel M. Duley (mosfet) |
280 | */ |
281 | static QImage& desaturate(QImage &img, float desat = 0.5); |
282 | /** |
283 | * Fade an image to a certain background color. The number of colors will |
284 | * not be changed. |
285 | * |
286 | * @param img The image to process. |
287 | * @param val The strength of the effect. 0 <= val <= 1. |
288 | * @param color The background color. |
289 | * @return A reference to the image for convenience. |
290 | */ |
291 | static QImage& fade(QImage &img, float val, const QColor &color); |
292 | |
293 | /** |
294 | * This recolors a pixmap. The most dark color will become color a, |
295 | * the most bright one color b, and in between. |
296 | * |
297 | * @param img The image to process. |
298 | * @param ca Color a |
299 | * @param cb Color b |
300 | */ |
301 | static QImage& flatten(QImage &img, const QColor &ca, const QColor &cb); |
302 | /** |
303 | * Thresholds an image based on a given channel and threshold value. |
304 | * |
305 | * @param img The image to process. |
306 | * @param thresholdValue The value that separates "on" colors from "off" |
307 | * ones. |
308 | * @param channel The channel to use when thresholding. |
309 | * @param aboveColor The color to use for values at or above the threshold. |
310 | * @param belowColor The color to use for values below the threshold. |
311 | * @return The processed image. The original is not changed. |
312 | * @author Daniel M. Duley (mosfet) |
313 | */ |
314 | static QImage threshold(QImage &img, unsigned char thresholdValue=127, |
315 | RGBChannel channel=Grayscale, |
316 | unsigned int aboveColor=qRgb(255, 255, 255), |
317 | unsigned int belowColor=qRgb(0, 0, 0)); |
318 | /** |
319 | * Swirls the image by a specified amount |
320 | * |
321 | * @param img The image to process. |
322 | * @param degrees The tightness of the swirl. |
323 | * @return The processed image. The original is not changed. |
324 | * @author Daniel M. Duley (mosfet) |
325 | */ |
326 | static QImage swirl(QImage &img, float degrees=60.0); |
327 | /** |
328 | * Implodes an image by a specified percent. |
329 | * |
330 | * @param img The QImage to process. |
331 | * @param amount The extent of the implosion. |
332 | * @return The processed image. The original is not changed. |
333 | * @author Daniel M. Duley (mosfet) |
334 | */ |
335 | static QImage implode(QImage &img, float amount=0.3); |
336 | /** |
337 | * Modifies the pixels along a sine wave. |
338 | * |
339 | * @param img The image to process. |
340 | * @param amplitude The amplitude of the sine wave. |
341 | * @param frequency The frequency of the sine wave. |
342 | * @param background An RGBA value to use for the background. After the |
343 | * effect some pixels may be "empty". This value is used for those pixels. |
344 | * @return The processed image. The original is not changed. |
345 | * @author Daniel M. Duley (mosfet) |
346 | */ |
347 | static QImage wave(QImage &img, float amplitude=25.0, float frequency=150.0, |
348 | unsigned int background=0); |
349 | /** |
350 | * Produces an oil painting effect. |
351 | * |
352 | * @param img The image to process. |
353 | * @param radius The radius of the gaussian, not counting the center |
354 | * pixel. Use 0, (recommended), and an appropriate one will be used. |
355 | * @param quality Determines if to use a small or large convolution matrix. |
356 | * @return The processed image. The original is not changed. |
357 | * @author Daniel M. Duley (mosfet) |
358 | */ |
359 | static QImage oilPaint(QImage &img, float radius=0.0, |
360 | EffectQuality quality=High); |
361 | /** |
362 | * Produces a "charcoal" image effect. |
363 | * |
364 | * @param img The image to process. |
365 | * @return The processed image. The original is not changed. |
366 | * @author Daniel M. Duley (mosfet) |
367 | */ |
368 | static QImage charcoal(QImage &img); |
369 | /** |
370 | * Create a gradient from color a to color b of the specified type. |
371 | * |
372 | * @param size The desired size of the gradient. |
373 | * @param ca Color a |
374 | * @param cb Color b |
375 | * @param type The type of gradient. |
376 | * @return The gradient. |
377 | */ |
378 | static QImage gradient(const QSize &size, const QColor &ca, |
379 | const QColor &cb, GradientType type); |
380 | /** |
381 | * Creates an 8bit grayscale gradient suitable for use as an alpha channel |
382 | * using QImage::setAlphaChannel(). |
383 | * |
384 | * @param size The desired size of the gradient. |
385 | * @param ca The grayscale start value. |
386 | * @param cb The grayscale end value. |
387 | * @param type The type of gradient. |
388 | * @return The gradient. |
389 | */ |
390 | static QImage grayGradient(const QSize &size, unsigned char ca, |
391 | unsigned char cb, GradientType type); |
392 | /** |
393 | * Create an unbalanced gradient. An unbalanced gradient is a gradient |
394 | * where the transition from color a to color b is not linear, but in this |
395 | * case exponential. |
396 | * |
397 | * @param size The desired size of the gradient. |
398 | * @param ca Color a |
399 | * @param cb Color b |
400 | * @param type The type of gradient. |
401 | * @param xfactor The x decay length. Use a value between -200 and 200. |
402 | * @param yfactor The y decay length. |
403 | * @return The gradient. |
404 | */ |
405 | static QImage unbalancedGradient(const QSize &size, const QColor &ca, |
406 | const QColor &cb, GradientType type, |
407 | int xfactor=100, int yfactor=100); |
408 | /** |
409 | * Creates an 8bit grayscale gradient suitable for use as an alpha channel |
410 | * using QImage::setAlphaChannel(). |
411 | * |
412 | * @param size The desired size of the gradient. |
413 | * @param type The type of gradient. |
414 | * @param ca The grayscale start value. |
415 | * @param cb The grayscale end value. |
416 | * @param xfactor The x decay length. Use a value between -200 and 200. |
417 | * @param yfactor The y decay length. |
418 | * @return The gradient. |
419 | */ |
420 | static QImage grayUnbalancedGradient(const QSize &size, unsigned char ca, |
421 | unsigned char cb, GradientType type, |
422 | int xfactor=100, int yfactor=100); |
423 | /** |
424 | * Modulate the image with a color channel of another image. |
425 | * |
426 | * @param img The QImage to modulate and result. |
427 | * @param modImg The QImage to use for modulation. |
428 | * @param reverse Invert the meaning of image/modImage; result is image! |
429 | * @param type The modulation Type to use. |
430 | * @param factor The modulation amplitude; with 0 no effect [-200;200]. |
431 | * @param channel The RBG channel of image2 to use for modulation. |
432 | * @return Returns the image(), provided for convenience. |
433 | */ |
434 | static QImage& modulate(QImage &img, QImage &modImg, bool reverse, |
435 | ModulationType type, int factor, |
436 | RGBChannel channel); |
437 | }; |
438 | |
439 | #endif |
440 | |
441 | |