summaryrefslogtreecommitdiffstats
path: root/ksquirrel/fmt_filters.h
blob: 9f6cb5d9e4e2af302eebadec4d68a63d4c4d66cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (c) 2005 Dmitry Baryshev <ksquirrel.iv@gmail.com>
 */

/*
 * All methods (except redeye) in this namespace are ported from KDE 3.2.3.
 * All of them are copyrighted by their authors. See fmt_filters_README for more.
 */

/* This file is part of the KDE libraries
    Copyright (C) 1998, 1999, 2001, 2002 Daniel M. Duley <mosfet@kde.org>
    (C) 1998, 1999 Christian Tibirna <ctibirna@total.net>
    (C) 1998, 1999 Dirk A. Mueller <mueller@kde.org>
    (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
    (C) 2000 Josef Weidendorfer <weidendo@in.tum.de>
    (C) 2004 Zack Rusin <zack@kde.org>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

//
// ===================================================================
// Effects originally ported from ImageMagick for PixiePlus, plus a few
// new ones. (mosfet 05/26/2003)
// ===================================================================
//
/*
 Portions of this software are based on ImageMagick. Such portions are clearly
marked as being ported from ImageMagick. ImageMagick is copyrighted under the
following conditions:

Copyright (C) 2003 ImageMagick Studio, a non-profit organization dedicated to
making software imaging solutions freely available.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files ("ImageMagick"), to deal
in ImageMagick without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense,  and/or sell
copies of ImageMagick, and to permit persons to whom the ImageMagick is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of ImageMagick.

The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement.  In no event shall
ImageMagick Studio be liable for any claim, damages or other liability,
whether in an action of contract, tort or otherwise, arising from, out of or
in connection with ImageMagick or the use or other dealings in ImageMagick.

Except as contained in this notice, the name of the ImageMagick Studio shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in ImageMagick without prior written authorization from the
ImageMagick Studio.
*/

#ifndef FMT_FILTERS_H
#define FMT_FILTERS_H

//////////////////////////////////////////
//                                      //
//  Pass the image through some filter  //
//                                      //
//////////////////////////////////////////


namespace fmt_filters
{
    typedef char            s8;
    typedef unsigned char   u8;

    typedef short           s16;
    typedef unsigned short  u16;

    typedef int             s32;
    typedef unsigned int    u32;

	struct image
	{
	    image() : data(0), w(0), h(0), rw(0), rh(0)
	    {}

	    image(unsigned char *d, int _w, int _h) : data(d), w(_w), h(_h), rw(_w), rh(_h)
	    {}

	    image(unsigned char *d, int _w, int _h, int _rw, int _rh) : data(d), w(_w), h(_h), rw(_rw), rh(_rh)
	    {}

	    unsigned char *data;
	    int w;
	    int h;
            int rw;
            int rh;
	};
	
	struct rgb
	{
	    rgb() : r(0), g(0), b(0)
	    {}

	    rgb(int _r, int _g, int _b) : r(_r), g(_g), b(_b)
	    {}

	    unsigned char r;
	    unsigned char g;
	    unsigned char b;
	};

        struct rgba
        {
            rgba(int r1, int g1, int b1, int a1) : r(r1), g(g1), b(b1), a(a1)
            {}

            rgba() : r(0), g(0), b(0), a(0)
            {}

            unsigned char r;
            unsigned char g;
            unsigned char b;
            unsigned char a;
        };

        enum NoiseType
        {
                UniformNoise = 0,            // Uniform distribution
                GaussianNoise,               // Gaussian distribution
                MultiplicativeGaussianNoise, // Multiplicative Gaussian distribution
                ImpulseNoise,                // Impulse distribution
                LaplacianNoise,              // Laplacian distribution
                PoissonNoise                 // Poisson distribution
        };


	bool checkImage(const image &im);

	// colorize the image, which is w x h, left alpha channel unchanged.
	//
	// it just adds to each pixel in the image
	// aproproriate value.
	void colorize(const image &im, int red, int green, int blue);

	// change brightness of the image
	void brightness(const image &im, int bn);

	// change gamma
	// gamma should be  0.0 <= L <= 6.0
	//
	// it is no problem to set L to 8.0 or 9.0, but the resulting
	// image won't have much difference from 6.0
	void gamma(const image &im, double L);

	// change contrast with Photoshop-like method
	// contrast should be  -255 <= contrast <= 255
	void contrast(const image &im, int contrast);

	enum swapRGBtype { GBR = 0, BRG = 1 };

	// negative
	void negative(const image &im);

	// swap RGB values
	void swapRGB(const image &im, int type);

        //
        // All the following filters are ported from KDE's
        // KImageEffect. See tdelibs/tdefx/kimageeffect.cpp
        // for more.
        //

	// blend
        // opacity = [0.0; 1.0]
	void blend(const image &im, const rgb &rgb, float opacity);

        // val = [0.0; 1.0]
        void fade(const image &im, const rgb &rgb, float val);

        void gray(const image &im);

        // desat = [0.0; 1.0]
        void desaturate(const image &im, float desat);

        // threshold = [0; 255]
        void threshold(const image &im, unsigned int threshold);

        // factor = [0.0; 50.0]
        void solarize(const image &im, double factor);

        // amount = [1; 10]
        void spread(const image &im, unsigned int amount);

        // degrees = [-720.0; 720.0]
        void swirl(const image &im, double degrees, const rgba &background);

        void noise(const image &im, NoiseType noise_type);

        void flatten(const image &im, const rgb &ca, const rgb &cb);

        // azimuth = [0.0; 90.0], elevation = [0.0; 90.0]
        void shade(const image &im, bool color, double azimuth, double elevation);

        void equalize(const image &im);

        // radius = [0.01; 90.0], sigma = [0.01; 50.0]
        void blur(const image &im, double radius, double sigma);

        void despeckle(const image &im);

        // factor = [0; 100]
        void implode(const image &im, double factor, const rgba &background);

        // radius = [0.01; 30.0]
        void edge(image &im, double radius);

        // radius = [0.01; 99.9], sigma = [0.01; 99.9]
        void emboss(image &im, double radius, double sigma);

        // radius = [0.01; 99.9], sigma = [0.01; 30.0]
        void sharpen(image &im, double radius, double sigma);

        // radius = [1.0; 5.0]
        void oil(const image &im, double radius);

/*
 * Red-eye removal was taken from "redeye" plugin for GIMP
 */

/* redeye.c: redeye remover plugin code
 *
 * Copyright (C) 2004  Robert Merkel <robert.merkel@benambra.org> (the "Author").
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the Author of the
 * Software shall not be used in advertising or otherwise to promote the
 * sale, use or other dealings in this Software without prior written
 * authorization from the Author.
1;3B */

        // red-eye removal.
        // th = [0; 255]
        void redeye(const image &im, const int w, const int h,
                            const int x, const int y,
                            int th);

        // Do we need some color definitions ?
        static const rgba white       = rgba(255, 255, 255, 255);
        static const rgba black       = rgba(0,   0,   0,   255);
        static const rgba red         = rgba(255, 0,   0,   255);
        static const rgba green       = rgba(0,   255, 0,   255);
        static const rgba blue        = rgba(0,   0,   255, 255);
        static const rgba cyan        = rgba(0,   255, 255, 255);
        static const rgba magenta     = rgba(255, 0,   255, 255);
        static const rgba yellow      = rgba(255, 255, 0,   255);
        static const rgba mediumgray  = rgba(128, 128, 128, 255);
        static const rgba lightgray   = rgba(160, 160, 164, 255);
        static const rgba normalgray  = rgba(192, 192, 192, 255);
        static const rgba darkred     = rgba(128, 0,   0,   255);
        static const rgba darkgreen   = rgba(0,   128, 0,   255);
        static const rgba darkblue    = rgba(0,   0,   128, 255);
        static const rgba darkcyan    = rgba(0,   128, 128, 255);
        static const rgba darkmagenta = rgba(128, 0,   128, 255);
        static const rgba darkyellow  = rgba(128, 128, 0,   255);
}

#endif