Code

Filters. New custom predefined Drawing filter (experimental, and currently a bit...
[inkscape.git] / src / extension / internal / filter / experimental.h
1 #ifndef __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__
2 #define __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__
3 /* Change the 'EXPERIMENTAL' above to be your file name */
5 /*
6  * Copyright (C) 2010 Authors:
7  *   Ivan Louette (filters)
8  *   Nicolas Dufour (UI) <nicoduf@yahoo.fr>
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
12 /* ^^^ Change the copyright to be you and your e-mail address ^^^ */
14 #include "filter.h"
16 #include "extension/internal/clear-n_.h"
17 #include "extension/system.h"
18 #include "extension/extension.h"
20 namespace Inkscape {
21 namespace Extension {
22 namespace Internal {
23 namespace Filter {
25 /**
26     \brief    Custom predefined Drawing filter.
27     
28     Convert images to duochrome drawings.
30     Filter's parameters:
31     * Simplification (0.01->10, default 0.7) -> blur1 (stdDeviation)
32     * Lightness (0->50, default 5) -> convolve (kernelMatrix, central value -1000->-1050, default -1005)
33     * Smoothness (0.01->10, default 0.7) -> blur2 (stdDeviation)
34     * Dilatation (3->100, default 6) -> colormatrix3 (n-1th value)
36     * Blur (0.01->10., default 5.) -> blur3 (stdDeviation)
37     * Blur spread (3->20, default 6) -> colormatrix5 (n-1th value)
38     * Blur erosion (-2->0, default -2) -> colormatrix5 (nth value)
40     * Stroke color (guint, default 205,0,0) -> flood2 (flood-opacity, flood-color)
41     * Image on stroke (boolean, default false) -> composite1 (in="flood2" true-> in="SourceGraphic")
42     * Image on stroke opacity (0.->1., default 1) -> composite3 (k3)
43     * Fill color (guint, default 255,203,0) -> flood3 (flood-opacity, flood-color)
44     * Image on fill (boolean, default false) -> composite2 (in="flood3" true-> in="SourceGraphic")
45     * Image on fill opacity (0.->1., default 1) -> composite3 (k2)
46 */
48 class Drawing : public Inkscape::Extension::Internal::Filter::Filter {
49 protected:
50         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
52 public:
53         Drawing ( ) : Filter() { };
54         virtual ~Drawing ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
56         static void init (void) {
57                 Inkscape::Extension::build_from_mem(
58                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
59                                 "<name>" N_("Drawing, custom -EXP-") "</name>\n"
60                                 "<id>org.inkscape.effect.filter.Drawing</id>\n"
61                         "<param name=\"simply\" gui-text=\"" N_("Simplification:") "\" type=\"float\" min=\"0.01\" max=\"10\">0.7</param>\n"
62                         "<param name=\"light\" gui-text=\"" N_("Lightness:") "\" type=\"int\" min=\"0\" max=\"50\">5</param>\n"
63                         "<param name=\"smooth\" gui-text=\"" N_("Smoothness:") "\" type=\"float\" min=\"0.01\" max=\"10\">0.7</param>\n"
64                         "<param name=\"dilat\" gui-text=\"" N_("Dilatation:") "\" type=\"int\" min=\"3\" max=\"100\">6</param>\n"
65                         "<param name=\"blur\" gui-text=\"" N_("Blur:") "\" type=\"float\" min=\"0.01\" max=\"10\">5</param>\n"
66                         "<param name=\"spread\" gui-text=\"" N_("Spread:") "\" type=\"int\" min=\"3\" max=\"20\">6</param>\n"
67                         "<param name=\"erosion\" gui-text=\"" N_("Erosion:") "\" type=\"int\" min=\"-2\" max=\"0\">-2</param>\n"
68                         "<_param name=\"fillcolorheader\" type=\"groupheader\">Fill color</_param>\n"
69                                             "<param name=\"fcolor\" gui-text=\"" N_("Fill color") "\" type=\"color\">-3473153</param>\n"
70                             "<param name=\"iof\" gui-text=\"" N_("Image on fill") "\" type=\"boolean\" >false</param>\n"
71                             "<param name=\"iofo\" gui-text=\"" N_("Image on fill opacity:") "\" type=\"float\" min=\"0\" max=\"1\">1</param>\n"
72                         "<_param name=\"strokecolorheader\" type=\"groupheader\">Stroke color</_param>\n"
73                                             "<param name=\"scolor\" gui-text=\"" N_("Stroke color") "\" type=\"color\">-855637761</param>\n"
74                             "<param name=\"ios\" gui-text=\"" N_("Image on stroke") "\" type=\"boolean\" >false</param>\n"
75                             "<param name=\"ioso\" gui-text=\"" N_("Image on stroke opacity:") "\" type=\"float\" min=\"0\" max=\"1\">1</param>\n"
76                                 "<effect>\n"
77                                         "<object-type>all</object-type>\n"
78                                         "<effects-menu>\n"
79                                                 "<submenu name=\"" N_("Filters") "\">\n"
80                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
81                               "</submenu>\n"
82                                         "</effects-menu>\n"
83                                         "<menu-tip>" N_("Convert images to duochrome drawings") "</menu-tip>\n"
84                                 "</effect>\n"
85                         "</inkscape-extension>\n", new Drawing());
86         };
88 };
90 gchar const *
91 Drawing::get_filter_text (Inkscape::Extension::Extension * ext)
92 {
93         if (_filter != NULL) g_free((void *)_filter);
95     std::ostringstream simply;
96     std::ostringstream light;
97     std::ostringstream smooth;
98     std::ostringstream dilat;
99     std::ostringstream blur;
100     std::ostringstream spread;
101     std::ostringstream erosion;
102     std::ostringstream strokea;
103     std::ostringstream stroker;
104     std::ostringstream strokeg;
105     std::ostringstream strokeb;
106     std::ostringstream ios;
107     std::ostringstream ioso;
108     std::ostringstream filla;
109     std::ostringstream fillr;
110     std::ostringstream fillg;
111     std::ostringstream fillb;
112     std::ostringstream iof;
113     std::ostringstream iofo;
115     simply << ext->get_param_float("simply");
116     light << (-1000 - ext->get_param_int("light"));
117     smooth << ext->get_param_float("smooth");
118     dilat << ext->get_param_int("dilat");
120     blur << ext->get_param_float("blur");
121     spread << ext->get_param_int("spread");
122     erosion << ext->get_param_int("erosion");
124     guint32 fcolor = ext->get_param_color("fcolor");
125     fillr << ((fcolor >> 24) & 0xff);
126     fillg << ((fcolor >> 16) & 0xff);
127     fillb << ((fcolor >>  8) & 0xff);
128     filla << (fcolor & 0xff) / 255.0F;
129     if (ext->get_param_bool("iof"))
130         iof << "SourceGraphic";
131     else
132         iof << "flood3";
133     iofo << ext->get_param_float("iofo");
135     guint32 scolor = ext->get_param_color("scolor");
136     stroker << ((scolor >> 24) & 0xff);
137     strokeg << ((scolor >> 16) & 0xff);
138     strokeb << ((scolor >>  8) & 0xff);
139     strokea << (scolor & 0xff) / 255.0F;
140     if (ext->get_param_bool("ios"))
141         ios << "SourceGraphic";
142     else
143         ios << "flood2";
144     ioso << ext->get_param_float("ioso");
146         _filter = g_strdup_printf(
147                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Cross-smooth, custom -EXP-\">\n"
148         "<feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"%s\" result=\"blur1\" />\n"
149         "<feConvolveMatrix in=\"blur1\" order=\"3 3\" kernelMatrix=\"0 250 0 250 %s 250 0 250 0 \" divisor=\"1\" targetX=\"1\" targetY=\"1\" preserveAlpha=\"true\" bias=\"0\" stdDeviation=\"1\" result=\"convolve\" />\n"
150         "<feColorMatrix values=\"0 -100 0 0 1 0 -100 0 0 1 0 -100 0 0 1 0 0 0 1 0 \" result=\"colormatrix1\" />\n"
151         "<feColorMatrix in=\"colormatrix1\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.2125 -0.7154 -0.0721 1 0 \" result=\"colormatrix2\" />\n"
152         "<feGaussianBlur stdDeviation=\"%s\" result=\"blur2\" />\n"
153         "<feColorMatrix values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 %s -2 \" result=\"colormatrix3\" />\n"
154         "<feFlood flood-color=\"rgb(255,255,255)\" result=\"flood1\" />\n"
155         "<feBlend in2=\"colormatrix3\" blend=\"normal\" mode=\"multiply\" result=\"blend1\" />\n"
156         "<feComponentTransfer in=\"blend1\" result=\"component1\">\n"
157             "<feFuncR tableValues=\"0 1 1\" type=\"discrete\" />\n"
158             "<feFuncG tableValues=\"0 1 1\" type=\"discrete\" />\n"
159             "<feFuncB tableValues=\"0 1 1\" type=\"discrete\" />\n"
160         "</feComponentTransfer>\n"
161         "<feGaussianBlur stdDeviation=\"%s\" result=\"blur3\" />\n"
162         "<feColorMatrix in=\"blur3\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -0.2125 -0.7154 -0.0721 1 0 \" result=\"colormatrix4\" />\n"
163         "<feColorMatrix stdDeviation=\"3\" values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 %s %s \" result=\"colormatrix5\" />\n"
164         "<feColorMatrix in=\"colormatrix5\" type=\"saturate\" values=\"1\" result=\"colormatrix6\" />\n"
165         "<feFlood flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" stdDeviation=\"3\" result=\"flood2\" />\n"
166         "<feComposite in=\"%s\" in2=\"colormatrix6\" operator=\"in\" result=\"composite1\" />\n"
167         "<feFlood flood-opacity=\"%s\" in=\"colormatrix6\" flood-color=\"rgb(%s,%s,%s)\" result=\"flood3\" />\n"
168         "<feComposite in=\"%s\" in2=\"colormatrix6\" operator=\"out\" result=\"composite2\" />\n"
169         "<feComposite in2=\"composite1\" operator=\"arithmetic\" k2=\"%s\" k3=\"%s\" result=\"composite3\" />\n"
170         "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
171         "</filter>\n", simply.str().c_str(), light.str().c_str(), smooth.str().c_str(), dilat.str().c_str(), blur.str().c_str(), spread.str().c_str(), erosion.str().c_str(), strokea.str().c_str(), stroker.str().c_str(), strokeg.str().c_str(), strokeb.str().c_str(), ios.str().c_str(), filla.str().c_str(), fillr.str().c_str(), fillg.str().c_str(), fillb.str().c_str(), iof.str().c_str(), iofo.str().c_str(), ioso.str().c_str());
173         return _filter;
174 }; /* Drawing filter */
176 /**
177     \brief    Custom predefined Posterize filter.
178     
179     Poster and painting effects.
181     Filter's parameters:
182     * Type (enum, default "Normal") ->
183         Normal = feComponentTransfer
184         Dented = Normal + intermediate values
185     * Blur (0.01->10., default 5.) -> blur3 (stdDeviation)
187 */
188 class Posterize : public Inkscape::Extension::Internal::Filter::Filter {
189 protected:
190         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
192 public:
193         Posterize ( ) : Filter() { };
194         virtual ~Posterize ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
196         static void init (void) {
197                 Inkscape::Extension::build_from_mem(
198                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
199                                 "<name>" N_("Poster and painting, custom -EXP-") "</name>\n"
200                                 "<id>org.inkscape.effect.filter.Posterize</id>\n"
201                 "<param name=\"type\" gui-text=\"" N_("Effect type:") "\" type=\"enum\">\n"
202                     "<_item value=\"normal\">Normal</_item>\n"
203                     "<_item value=\"dented\">Dented</_item>\n"
204                 "</param>\n"
205                 "<param name=\"table\" gui-text=\"" N_("Transfer type:") "\" type=\"enum\">\n"
206                     "<_item value=\"discrete\">Poster</_item>\n"
207                     "<_item value=\"table\">Painting</_item>\n"
208                 "</param>\n"
209                 "<param name=\"levels\" gui-text=\"" N_("Levels:") "\" type=\"int\" min=\"1\" max=\"15\">5</param>\n"
210                 "<param name=\"blend\" gui-text=\"" N_("Blend mode:") "\" type=\"enum\">\n"
211                     "<_item value=\"lighten\">Lighten</_item>\n"
212                     "<_item value=\"normal\">Normal</_item>\n"
213                     "<_item value=\"darken\">Darken</_item>\n"
214                 "</param>\n"
215                 "<param name=\"blur1\" gui-text=\"" N_("Primary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">4.0</param>\n"
216                 "<param name=\"blur2\" gui-text=\"" N_("Secondary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">0.5</param>\n"
217                 "<param name=\"presaturation\" gui-text=\"" N_("Pre-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
218                 "<param name=\"postsaturation\" gui-text=\"" N_("Post-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
219                 "<param name=\"antialiasing\" gui-text=\"" N_("Simulate antialiasing") "\" type=\"boolean\">false</param>\n"
220                                 "<effect>\n"
221                                         "<object-type>all</object-type>\n"
222                                         "<effects-menu>\n"
223                                                 "<submenu name=\"" N_("Filters") "\">\n"
224                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
225                               "</submenu>\n"
226                                         "</effects-menu>\n"
227                                         "<menu-tip>" N_("Poster and painting effects") "</menu-tip>\n"
228                                 "</effect>\n"
229                         "</inkscape-extension>\n", new Posterize());
230         };
232 };
234 gchar const *
235 Posterize::get_filter_text (Inkscape::Extension::Extension * ext)
237         if (_filter != NULL) g_free((void *)_filter);
239     std::ostringstream table;
240     std::ostringstream blendmode;
241     std::ostringstream blur1;
242     std::ostringstream blur2;
243     std::ostringstream presat;
244     std::ostringstream postsat;
245     std::ostringstream transf;
246     std::ostringstream antialias;
247     
248     table << ext->get_param_enum("table");
249     blendmode << ext->get_param_enum("blend");
250     blur1 << ext->get_param_float("blur1") + 0.01;
251     blur2 << ext->get_param_float("blur2") + 0.01;
252     presat << ext->get_param_float("presaturation");
253     postsat << ext->get_param_float("postsaturation");
256     // TransfertComponent table values are calculated based on the poster type.
257     transf << "0";
258     int levels = ext->get_param_int("levels") + 1;
259     const gchar *effecttype =  ext->get_param_enum("type");
260     float val = 0.0;
261     for ( int step = 1 ; step <= levels ; step++ ) {
262         val = (float) step / levels;
263         transf << " " << val;
264         if((g_ascii_strcasecmp("dented", effecttype) == 0)) {
265             transf << " " << (val - ((float) 1 / (3 * levels))) << " " << (val + ((float) 1 / (2 * levels)));
266         }
267     }
268     transf << " 1";
269     
270     if (ext->get_param_bool("antialiasing"))
271         antialias << "0.5";
272     else
273         antialias << "0.01";
275     
276         _filter = g_strdup_printf(
277                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Poster and painting, custom -EXP-\">\n"
278             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
279             "<feGaussianBlur stdDeviation=\"%s\" result=\"Gaussian1\" />\n"
280             "<feGaussianBlur stdDeviation=\"%s\" in=\"Composite1\" />\n"
281             "<feBlend in2=\"Gaussian1\" mode=\"%s\" />\n"
282             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
283             "<feComponentTransfer>\n"
284                 "<feFuncR type=\"%s\" tableValues=\"%s\" />\n"
285                 "<feFuncG type=\"%s\" tableValues=\"%s\" />\n"
286                 "<feFuncB type=\"%s\" tableValues=\"%s\" />\n"
287             "</feComponentTransfer>\n"
288             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
289             "<feGaussianBlur stdDeviation=\"%s\" />\n"
290             "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
291         "</filter>\n", blur1.str().c_str(), blur2.str().c_str(), blendmode.str().c_str(), presat.str().c_str(), table.str().c_str(), transf.str().c_str(), table.str().c_str(), transf.str().c_str(), table.str().c_str(), transf.str().c_str(), postsat.str().c_str(), antialias.str().c_str());
293         return _filter;
294 }; /* Posterize filter */
297 class TestFilter : public Inkscape::Extension::Internal::Filter::Filter {
298 protected:
299         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
301 public:
302         TestFilter ( ) : Filter() { };
303         virtual ~TestFilter ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
305         static void init (void) {
306                 Inkscape::Extension::build_from_mem(
307                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
308                                 "<name>" N_("Test Filter -EXP-") "</name>\n"
309                                 "<id>org.inkscape.effect.filter.TestFilter</id>\n"
310                 "<_param name=\"header1\" type=\"groupheader\">Test filter</_param>\n"
311                                 "<effect>\n"
312                                         "<object-type>all</object-type>\n"
313                                         "<effects-menu>\n"
314                                                 "<submenu name=\"" N_("Filters") "\">\n"
315                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
316                               "</submenu>\n"
317                                         "</effects-menu>\n"
318                                         "<menu-tip>" N_("Change colors to a two colors palette") "</menu-tip>\n"
319                                 "</effect>\n"
320                         "</inkscape-extension>\n", new TestFilter());
321         };
323 };
325 gchar const *
326 TestFilter::get_filter_text (Inkscape::Extension::Extension * ext)
328         if (_filter != NULL) g_free((void *)_filter);
329     
330         _filter = g_strdup_printf(
331                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Test Filter -EXP-\">\n"
332             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
333             "<feGaussianBlur stdDeviation=\"4\" result=\"Gaussian1\" />\n"
334             "<feGaussianBlur stdDeviation=\"0.5\" in=\"Composite1\" />\n"
335             "<feBlend in2=\"Gaussian1\" mode=\"normal\" />\n"
336             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
337             "<feComponentTransfer>\n"
338                 "<feFuncR type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
339                 "<feFuncG type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
340                 "<feFuncB type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
341             "</feComponentTransfer>\n"
342             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
343             "<feGaussianBlur stdDeviation=\"0.05\" />\n"
344             "<feComposite in2=\"SourceGraphic\" operator=\"atop\" />\n"
345         "</filter>\n");
347         return _filter;
348 };
349 }; /* namespace Filter */
350 }; /* namespace Internal */
351 }; /* namespace Extension */
352 }; /* namespace Inkscape */
354 /* Change the 'COLOR' below to be your file name */
355 #endif /* __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__ */