Code

a8879720bdfc8cfe71012423d6aef07ef6f894b1
[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  * Experimental filters (no assigned menu)
11  *   Chromolitho
12  *   Drawing
13  *   Posterize
14  *   Test filter (should no be used...)
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
18 /* ^^^ Change the copyright to be you and your e-mail address ^^^ */
20 #include "filter.h"
22 #include "extension/internal/clear-n_.h"
23 #include "extension/system.h"
24 #include "extension/extension.h"
26 namespace Inkscape {
27 namespace Extension {
28 namespace Internal {
29 namespace Filter {
31 /**
32     \brief    Custom predefined Chromolitho filter.
33     
34     Chromo effect with customizable edge drawing and graininess
36     Filter's parameters:
37     * Drawing (boolean, default checked) -> Checked = blend1 (in="convolve1"), unchecked = blend1 (in="composite1")
38     * Transparent (boolean, default unchecked) -> Checked = colormatrix5 (in="colormatrix4"), Unchecked = colormatrix5 (in="component1")
39     * Invert (boolean, default false) -> component1 (tableValues) [adds a trailing 0]
40     * Dented (boolean, default false) -> component1 (tableValues) [adds intermediate 0s]
41     * Expand white (0->5, default 1) -> component1 (tableValues) [0="0 1", 5="0 1 1 1 1 1 1"]
42     * Lightness (0->10, default 0) -> composite1 (k1)
43     * Saturation (0.->1., default 1.) -> colormatrix3 (values)
44     * Noise reduction (1->1000, default 20) -> convolve (kernelMatrix, central value -1001->-2000, default -1020)
45     * Drawing blend (enum, default Normal) -> blend1 (mode)
46     * Smoothness (0.01->10, default 1) -> blur1 (stdDeviation)
47     * Grain (boolean, default unchecked) -> Checked = blend2 (in="colormatrix2"), Unchecked = blend2 (in="blur1")
48         * Grain x frequency (0.->100, default 100) -> turbulence1 (baseFrequency, first value)
49         * Grain y frequency (0.->100, default 100) -> turbulence1 (baseFrequency, second value)
50         * Grain complexity (1->5, default 1) -> turbulence1 (numOctaves)
51         * Grain variation (0->1000, default 0) -> turbulence1 (seed)
52         * Grain expansion (1.->50., default 1.) -> colormatrix1 (n-1 value)
53         * Grain erosion (0.->40., default 0.) -> colormatrix1 (nth value) [inverted]
54         * Grain color (boolean, default true) -> colormatrix2 (values)
55         * Grain blend (enum, default Normal) -> blend2 (mode)
56 */
57 class Chromolitho : public Inkscape::Extension::Internal::Filter::Filter {
58 protected:
59         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
61 public:
62         Chromolitho ( ) : Filter() { };
63         virtual ~Chromolitho ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
65         static void init (void) {
66                 Inkscape::Extension::build_from_mem(
67                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
68                                 "<name>" N_("Chromolitho, custom -EXP-") "</name>\n"
69                                 "<id>org.inkscape.effect.filter.Chromolitho</id>\n"
70                     "<param name=\"tab\" type=\"notebook\">\n"
71                     "<page name=\"optionstab\" _gui-text=\"Options\">\n"
72                         "<param name=\"drawing\" gui-text=\"" N_("Drawing mode") "\" type=\"boolean\" >true</param>\n"
73                         "<param name=\"dblend\" gui-text=\"" N_("Drawing blend:") "\" type=\"enum\">\n"
74                             "<_item value=\"normal\">Normal</_item>\n"
75                             "<_item value=\"multiply\">Multiply</_item>\n"
76                             "<_item value=\"screen\">Screen</_item>\n"
77                             "<_item value=\"lighten\">Lighten</_item>\n"
78                             "<_item value=\"darken\">Darken</_item>\n"
79                         "</param>\n"
80                         "<param name=\"transparent\" gui-text=\"" N_("Transparent") "\" type=\"boolean\" >false</param>\n"
81                         "<param name=\"dented\" gui-text=\"" N_("Dented") "\" type=\"boolean\" >false</param>\n"
82                         "<param name=\"inverted\" gui-text=\"" N_("Inverted") "\" type=\"boolean\" >false</param>\n"
83                         "<param name=\"light\" gui-text=\"" N_("Lightness:") "\" type=\"int\" min=\"0\" max=\"10\">0</param>\n"
84                         "<param name=\"saturation\" gui-text=\"" N_("Saturation:") "\" type=\"float\" min=\"0\" max=\"1\">1</param>\n"
85                         "<param name=\"noise\" gui-text=\"" N_("Noise reduction:") "\" type=\"int\" min=\"1\" max=\"1000\">20</param>\n"
86                         "<param name=\"smooth\" gui-text=\"" N_("Smoothness:") "\" type=\"float\" min=\"0.01\" max=\"10\">1</param>\n"
87                     "</page>\n"
88                     "<page name=\"graintab\" _gui-text=\"Grain\">\n"
89                         "<param name=\"grain\" gui-text=\"" N_("Grain mode") "\" type=\"boolean\" >true</param>\n"
90                         "<param name=\"grainxf\" gui-text=\"" N_("X frequency:") "\" type=\"float\" min=\"0\" max=\"100\">100</param>\n"
91                         "<param name=\"grainyf\" gui-text=\"" N_("Y frequency:") "\" type=\"float\" min=\"0\" max=\"100\">100</param>\n"
92                         "<param name=\"grainc\" gui-text=\"" N_("Complexity:") "\" type=\"int\" min=\"1\" max=\"5\">1</param>\n"
93                         "<param name=\"grainv\" gui-text=\"" N_("Variation:") "\" type=\"int\" min=\"0\" max=\"1000\">0</param>\n"
94                         "<param name=\"grainexp\" gui-text=\"" N_("Expansion:") "\" type=\"float\" min=\"1\" max=\"50\">1</param>\n"
95                         "<param name=\"grainero\" gui-text=\"" N_("Erosion:") "\" type=\"float\" min=\"0\" max=\"40\">0</param>\n"
96                         "<param name=\"graincol\" gui-text=\"" N_("Color") "\" type=\"boolean\" >true</param>\n"
97                         "<param name=\"gblend\" gui-text=\"" N_("Grain blend:") "\" type=\"enum\">\n"
98                             "<_item value=\"normal\">Normal</_item>\n"
99                             "<_item value=\"multiply\">Multiply</_item>\n"
100                             "<_item value=\"screen\">Screen</_item>\n"
101                             "<_item value=\"lighten\">Lighten</_item>\n"
102                             "<_item value=\"darken\">Darken</_item>\n"
103                         "</param>\n"
104                     "</page>\n"
105                 "</param>\n"
106                                 "<effect>\n"
107                                         "<object-type>all</object-type>\n"
108                                         "<effects-menu>\n"
109                                                 "<submenu name=\"" N_("Filters") "\">\n"
110                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
111                               "</submenu>\n"
112                                         "</effects-menu>\n"
113                                         "<menu-tip>" N_("Chromo effect with customizable edge drawing and graininess") "</menu-tip>\n"
114                                 "</effect>\n"
115                         "</inkscape-extension>\n", new Chromolitho());
116         };
117 };
119 gchar const *
120 Chromolitho::get_filter_text (Inkscape::Extension::Extension * ext)
122         if (_filter != NULL) g_free((void *)_filter);
123     
124     std::ostringstream b1in;
125     std::ostringstream b2in;
126     std::ostringstream col3in;
127     std::ostringstream transf;
128     std::ostringstream light;
129     std::ostringstream saturation;
130     std::ostringstream noise;
131     std::ostringstream dblend;
132     std::ostringstream smooth;
133     std::ostringstream grain;
134     std::ostringstream grainxf;
135     std::ostringstream grainyf;
136     std::ostringstream grainc;
137     std::ostringstream grainv;
138     std::ostringstream gblend;
139     std::ostringstream grainexp;
140     std::ostringstream grainero;
141     std::ostringstream graincol;
143     if (ext->get_param_bool("drawing"))
144         b1in << "convolve1";
145     else
146         b1in << "composite1";
148     if (ext->get_param_bool("transparent"))
149         col3in << "colormatrix4";
150     else
151         col3in << "component1";
152     light << ext->get_param_int("light");
153     saturation << ext->get_param_float("saturation");
154     noise << (-1000 - ext->get_param_int("noise"));
155     dblend << ext->get_param_enum("dblend");
156     smooth << ext->get_param_float("smooth");
158     if (ext->get_param_bool("dented")) {
159         transf << "0 1 0 1";
160     } else {
161         transf << "0 1 1";
162     }
163     if (ext->get_param_bool("inverted"))
164         transf << " 0";
166     if (ext->get_param_bool("grain"))
167         b2in << "colormatrix2";
168     else
169         b2in << "blur1";
170     grainxf << (ext->get_param_float("grainxf") / 100);
171     grainyf << (ext->get_param_float("grainyf") / 100);
172     grainc << ext->get_param_int("grainc");
173     grainv << ext->get_param_int("grainv");
174     gblend << ext->get_param_enum("gblend");
175     grainexp << ext->get_param_float("grainexp");
176     grainero << (-ext->get_param_float("grainero"));
177     if (ext->get_param_bool("graincol"))
178         graincol << "1";
179     else
180         graincol << "0";
182         _filter = g_strdup_printf(
183                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Chromolitho, custom -EXP-\">\n"
185         "<feComposite stdDeviation=\"1\" in=\"SourceGraphic\" in2=\"SourceGraphic\" operator=\"arithmetic\" k1=\"%s\" k2=\"1\" result=\"composite1\" />\n"
186         "<feConvolveMatrix in=\"composite1\" kernelMatrix=\"0 250 0 250 %s 250 0 250 0 \" order=\"3 3\" stdDeviation=\"1\" result=\"convolve1\" />\n"
187         "<feBlend in=\"%s\" in2=\"composite1\" mode=\"%s\" blend=\"normal\" stdDeviation=\"1\" result=\"blend1\" />\n"
188         "<feGaussianBlur in=\"blend1\" stdDeviation=\"%s\" result=\"blur1\" />\n"
189         "<feTurbulence baseFrequency=\"%s %s\" numOctaves=\"%s\" seed=\"%s\" type=\"fractalNoise\" result=\"turbulence1\" />\n"
190         "<feColorMatrix values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 %s %s \" result=\"colormatrix1\" />\n"
191         "<feColorMatrix type=\"saturate\" stdDeviation=\"3\" values=\"%s\" result=\"colormatrix2\" />\n"
192         "<feBlend in=\"%s\" in2=\"blur1\" stdDeviation=\"1\" blend=\"normal\" mode=\"%s\" result=\"blend2\" />\n"
193         "<feColorMatrix in=\"blend2\" type=\"saturate\" values=\"%s\" result=\"colormatrix3\" />\n"
194         "<feComponentTransfer in=\"colormatrix3\" stdDeviation=\"2\" result=\"component1\">\n"
195             "<feFuncR type=\"discrete\" tableValues=\"%s\" />\n"
196             "<feFuncG type=\"discrete\" tableValues=\"%s\" />\n"
197             "<feFuncB type=\"discrete\" tableValues=\"%s\" />\n"
198         "</feComponentTransfer>\n"
199         "<feColorMatrix values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 -0.2125 -0.7154 -0.0721 1 0 \" result=\"colormatrix4\" />\n"
200         "<feColorMatrix in=\"%s\" values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 15 0 \" result=\"colormatrix5\" />\n"
201         "<feComposite in2=\"SourceGraphic\" operator=\"in\" result=\"composite2\" />\n"
202         "</filter>\n", light.str().c_str(), noise.str().c_str(), b1in.str().c_str(), dblend.str().c_str(), smooth.str().c_str(), grainxf.str().c_str(), grainyf.str().c_str(), grainc.str().c_str(), grainv.str().c_str(), grainexp.str().c_str(), grainero.str().c_str(), graincol.str().c_str(), b2in.str().c_str(), gblend.str().c_str(), saturation.str().c_str(), transf.str().c_str(), transf.str().c_str(), transf.str().c_str(), col3in.str().c_str());
204         return _filter;
205 }; /* Chromolitho filter */
207 /**
208     \brief    Custom predefined Drawing filter.
209     
210     Convert images to duochrome drawings.
212     Filter's parameters:
213     * Simplification (0.01->10, default 0.7) -> blur1 (stdDeviation)
214     * Lightness (0->50, default 5) -> convolve (kernelMatrix, central value -1000->-1050, default -1005)
215     * Smoothness (0.01->10, default 0.7) -> blur2 (stdDeviation)
216     * Dilatation (3->100, default 6) -> colormatrix3 (n-1th value)
218     * Blur (0.01->10., default 1.) -> blur3 (stdDeviation)
219     * Blur spread (3->20, default 6) -> colormatrix5 (n-1th value)
220     * Blur erosion (-2->0, default -2) -> colormatrix5 (nth value)
222     * Stroke color (guint, default 205,0,0) -> flood2 (flood-opacity, flood-color)
223     * Image on stroke (boolean, default false) -> composite1 (in="flood2" true-> in="SourceGraphic")
224     * Image on stroke opacity (0.->1., default 1) -> composite3 (k3)
225     * Fill color (guint, default 255,203,0) -> flood3 (flood-opacity, flood-color)
226     * Image on fill (boolean, default false) -> composite2 (in="flood3" true-> in="SourceGraphic")
227     * Image on fill opacity (0.->1., default 1) -> composite3 (k2)
228 */
230 class Drawing : public Inkscape::Extension::Internal::Filter::Filter {
231 protected:
232         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
234 public:
235         Drawing ( ) : Filter() { };
236         virtual ~Drawing ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
238         static void init (void) {
239                 Inkscape::Extension::build_from_mem(
240                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
241                                 "<name>" N_("Drawing, custom -EXP-") "</name>\n"
242                                 "<id>org.inkscape.effect.filter.Drawing</id>\n"
243                     "<param name=\"tab\" type=\"notebook\">\n"
244                     "<page name=\"optionstab\" _gui-text=\"Options\">\n"
245                         "<param name=\"simply\" gui-text=\"" N_("Simplification:") "\" type=\"float\" min=\"0.01\" max=\"10\">0.7</param>\n"
246                         "<param name=\"light\" gui-text=\"" N_("Lightness:") "\" type=\"int\" min=\"0\" max=\"50\">5</param>\n"
247                         "<param name=\"smooth\" gui-text=\"" N_("Smoothness:") "\" type=\"float\" min=\"0.01\" max=\"10\">0.7</param>\n"
248                         "<param name=\"dilat\" gui-text=\"" N_("Dilatation:") "\" type=\"int\" min=\"3\" max=\"100\">6</param>\n"
249                         "<_param name=\"blurheader\" type=\"groupheader\">Blur</_param>\n"
250                             "<param name=\"blur\" gui-text=\"" N_("Level:") "\" type=\"float\" min=\"0.01\" max=\"10\">1</param>\n"
251                             "<param name=\"spread\" gui-text=\"" N_("Spread:") "\" type=\"int\" min=\"3\" max=\"20\">6</param>\n"
252                             "<param name=\"erosion\" gui-text=\"" N_("Erosion:") "\" type=\"int\" min=\"-2\" max=\"0\">-2</param>\n"
253                     "</page>\n"
254                     "<page name=\"co11tab\" _gui-text=\"Fill color\">\n"
255                                     "<param name=\"fcolor\" gui-text=\"" N_("Fill color") "\" type=\"color\">-3473153</param>\n"
256                         "<param name=\"iof\" gui-text=\"" N_("Image on fill") "\" type=\"boolean\" >false</param>\n"
257                         "<param name=\"iofo\" gui-text=\"" N_("Image on fill opacity:") "\" type=\"float\" min=\"0\" max=\"1\">1</param>\n"
258                     "</page>\n"
259                     "<page name=\"co12tab\" _gui-text=\"Stroke color\">\n"
260                                     "<param name=\"scolor\" gui-text=\"" N_("Stroke color") "\" type=\"color\">-855637761</param>\n"
261                         "<param name=\"ios\" gui-text=\"" N_("Image on stroke") "\" type=\"boolean\" >false</param>\n"
262                         "<param name=\"ioso\" gui-text=\"" N_("Image on stroke opacity:") "\" type=\"float\" min=\"0\" max=\"1\">1</param>\n"
263                     "</page>\n"
264                 "</param>\n"
265                                 "<effect>\n"
266                                         "<object-type>all</object-type>\n"
267                                         "<effects-menu>\n"
268                                                 "<submenu name=\"" N_("Filters") "\">\n"
269                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
270                               "</submenu>\n"
271                                         "</effects-menu>\n"
272                                         "<menu-tip>" N_("Convert images to duochrome drawings") "</menu-tip>\n"
273                                 "</effect>\n"
274                         "</inkscape-extension>\n", new Drawing());
275         };
276 };
278 gchar const *
279 Drawing::get_filter_text (Inkscape::Extension::Extension * ext)
281         if (_filter != NULL) g_free((void *)_filter);
283     std::ostringstream simply;
284     std::ostringstream light;
285     std::ostringstream smooth;
286     std::ostringstream dilat;
287     std::ostringstream blur;
288     std::ostringstream spread;
289     std::ostringstream erosion;
290     std::ostringstream strokea;
291     std::ostringstream stroker;
292     std::ostringstream strokeg;
293     std::ostringstream strokeb;
294     std::ostringstream ios;
295     std::ostringstream ioso;
296     std::ostringstream filla;
297     std::ostringstream fillr;
298     std::ostringstream fillg;
299     std::ostringstream fillb;
300     std::ostringstream iof;
301     std::ostringstream iofo;
303     simply << ext->get_param_float("simply");
304     light << (-1000 - ext->get_param_int("light"));
305     smooth << ext->get_param_float("smooth");
306     dilat << ext->get_param_int("dilat");
308     blur << ext->get_param_float("blur");
309     spread << ext->get_param_int("spread");
310     erosion << ext->get_param_int("erosion");
312     guint32 fcolor = ext->get_param_color("fcolor");
313     fillr << ((fcolor >> 24) & 0xff);
314     fillg << ((fcolor >> 16) & 0xff);
315     fillb << ((fcolor >>  8) & 0xff);
316     filla << (fcolor & 0xff) / 255.0F;
317     if (ext->get_param_bool("iof"))
318         iof << "SourceGraphic";
319     else
320         iof << "flood3";
321     iofo << ext->get_param_float("iofo");
323     guint32 scolor = ext->get_param_color("scolor");
324     stroker << ((scolor >> 24) & 0xff);
325     strokeg << ((scolor >> 16) & 0xff);
326     strokeb << ((scolor >>  8) & 0xff);
327     strokea << (scolor & 0xff) / 255.0F;
328     if (ext->get_param_bool("ios"))
329         ios << "SourceGraphic";
330     else
331         ios << "flood2";
332     ioso << ext->get_param_float("ioso");
334         _filter = g_strdup_printf(
335                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Drawing, custom -EXP-\">\n"
336         "<feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"%s\" result=\"blur1\" />\n"
337         "<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"
338         "<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"
339         "<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"
340         "<feGaussianBlur stdDeviation=\"%s\" result=\"blur2\" />\n"
341         "<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"
342         "<feFlood flood-color=\"rgb(255,255,255)\" result=\"flood1\" />\n"
343         "<feBlend in2=\"colormatrix3\" blend=\"normal\" mode=\"multiply\" result=\"blend1\" />\n"
344         "<feComponentTransfer in=\"blend1\" result=\"component1\">\n"
345             "<feFuncR tableValues=\"0 1 1\" type=\"discrete\" />\n"
346             "<feFuncG tableValues=\"0 1 1\" type=\"discrete\" />\n"
347             "<feFuncB tableValues=\"0 1 1\" type=\"discrete\" />\n"
348         "</feComponentTransfer>\n"
349         "<feGaussianBlur stdDeviation=\"%s\" result=\"blur3\" />\n"
350         "<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"
351         "<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"
352         "<feColorMatrix in=\"colormatrix5\" type=\"saturate\" values=\"1\" result=\"colormatrix6\" />\n"
353         "<feFlood flood-opacity=\"%s\" flood-color=\"rgb(%s,%s,%s)\" stdDeviation=\"3\" result=\"flood2\" />\n"
354         "<feComposite in=\"%s\" in2=\"colormatrix6\" operator=\"in\" result=\"composite1\" />\n"
355         "<feFlood flood-opacity=\"%s\" in=\"colormatrix6\" flood-color=\"rgb(%s,%s,%s)\" result=\"flood3\" />\n"
356         "<feComposite in=\"%s\" in2=\"colormatrix6\" operator=\"out\" result=\"composite2\" />\n"
357         "<feComposite in2=\"composite1\" operator=\"arithmetic\" k2=\"%s\" k3=\"%s\" result=\"composite3\" />\n"
358         "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
359         "</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());
361         return _filter;
362 }; /* Drawing filter */
364 /**
365     \brief    Custom predefined Posterize filter.
366     
367     Poster and painting effects.
369     Filter's parameters (not finished yet):
370     * Effect type (enum, default "Normal") ->
371         Normal = feComponentTransfer
372         Dented = Normal + intermediate values
373     * Blur (0.01->10., default 5.) -> blur3 (stdDeviation)
375 */
376 class Posterize : public Inkscape::Extension::Internal::Filter::Filter {
377 protected:
378         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
380 public:
381         Posterize ( ) : Filter() { };
382         virtual ~Posterize ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
384         static void init (void) {
385                 Inkscape::Extension::build_from_mem(
386                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
387                                 "<name>" N_("Poster and painting, custom -EXP-") "</name>\n"
388                                 "<id>org.inkscape.effect.filter.Posterize</id>\n"
389                 "<param name=\"type\" gui-text=\"" N_("Effect type:") "\" type=\"enum\">\n"
390                     "<_item value=\"normal\">Normal</_item>\n"
391                     "<_item value=\"dented\">Dented</_item>\n"
392                 "</param>\n"
393                 "<param name=\"table\" gui-text=\"" N_("Transfer type:") "\" type=\"enum\">\n"
394                     "<_item value=\"discrete\">Poster</_item>\n"
395                     "<_item value=\"table\">Painting</_item>\n"
396                 "</param>\n"
397                 "<param name=\"levels\" gui-text=\"" N_("Levels:") "\" type=\"int\" min=\"1\" max=\"15\">5</param>\n"
398                 "<param name=\"blend\" gui-text=\"" N_("Blend mode:") "\" type=\"enum\">\n"
399                     "<_item value=\"lighten\">Lighten</_item>\n"
400                     "<_item value=\"normal\">Normal</_item>\n"
401                     "<_item value=\"darken\">Darken</_item>\n"
402                 "</param>\n"
403                 "<param name=\"blur1\" gui-text=\"" N_("Primary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">4.0</param>\n"
404                 "<param name=\"blur2\" gui-text=\"" N_("Secondary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">0.5</param>\n"
405                 "<param name=\"presaturation\" gui-text=\"" N_("Pre-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
406                 "<param name=\"postsaturation\" gui-text=\"" N_("Post-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
407                 "<param name=\"antialiasing\" gui-text=\"" N_("Simulate antialiasing") "\" type=\"boolean\">false</param>\n"
408                                 "<effect>\n"
409                                         "<object-type>all</object-type>\n"
410                                         "<effects-menu>\n"
411                                                 "<submenu name=\"" N_("Filters") "\">\n"
412                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
413                               "</submenu>\n"
414                                         "</effects-menu>\n"
415                                         "<menu-tip>" N_("Poster and painting effects") "</menu-tip>\n"
416                                 "</effect>\n"
417                         "</inkscape-extension>\n", new Posterize());
418         };
419 };
421 gchar const *
422 Posterize::get_filter_text (Inkscape::Extension::Extension * ext)
424         if (_filter != NULL) g_free((void *)_filter);
426     std::ostringstream table;
427     std::ostringstream blendmode;
428     std::ostringstream blur1;
429     std::ostringstream blur2;
430     std::ostringstream presat;
431     std::ostringstream postsat;
432     std::ostringstream transf;
433     std::ostringstream antialias;
434     
435     table << ext->get_param_enum("table");
436     blendmode << ext->get_param_enum("blend");
437     blur1 << ext->get_param_float("blur1") + 0.01;
438     blur2 << ext->get_param_float("blur2") + 0.01;
439     presat << ext->get_param_float("presaturation");
440     postsat << ext->get_param_float("postsaturation");
442     // TransfertComponent table values are calculated based on the poster type.
443     transf << "0";
444     int levels = ext->get_param_int("levels") + 1;
445     const gchar *effecttype =  ext->get_param_enum("type");
446     float val = 0.0;
447     for ( int step = 1 ; step <= levels ; step++ ) {
448         val = (float) step / levels;
449         transf << " " << val;
450         if((g_ascii_strcasecmp("dented", effecttype) == 0)) {
451             transf << " " << (val - ((float) 1 / (3 * levels))) << " " << (val + ((float) 1 / (2 * levels)));
452         }
453     }
454     transf << " 1";
455     
456     if (ext->get_param_bool("antialiasing"))
457         antialias << "0.5";
458     else
459         antialias << "0.01";
460     
461         _filter = g_strdup_printf(
462                 "<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"
463             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
464             "<feGaussianBlur stdDeviation=\"%s\" result=\"Gaussian1\" />\n"
465             "<feGaussianBlur stdDeviation=\"%s\" in=\"Composite1\" />\n"
466             "<feBlend in2=\"Gaussian1\" mode=\"%s\" />\n"
467             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
468             "<feComponentTransfer>\n"
469                 "<feFuncR type=\"%s\" tableValues=\"%s\" />\n"
470                 "<feFuncG type=\"%s\" tableValues=\"%s\" />\n"
471                 "<feFuncB type=\"%s\" tableValues=\"%s\" />\n"
472             "</feComponentTransfer>\n"
473             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
474             "<feGaussianBlur stdDeviation=\"%s\" />\n"
475             "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
476         "</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());
478         return _filter;
479 }; /* Posterize filter */
482 class TestFilter : public Inkscape::Extension::Internal::Filter::Filter {
483 protected:
484         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
486 public:
487         TestFilter ( ) : Filter() { };
488         virtual ~TestFilter ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
490         static void init (void) {
491                 Inkscape::Extension::build_from_mem(
492                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
493                                 "<name>" N_("Test Filter -EXP-") "</name>\n"
494                                 "<id>org.inkscape.effect.filter.TestFilter</id>\n"
495                 "<_param name=\"header1\" type=\"groupheader\">Test filter</_param>\n"
496                                 "<effect>\n"
497                                         "<object-type>all</object-type>\n"
498                                         "<effects-menu>\n"
499                                                 "<submenu name=\"" N_("Filters") "\">\n"
500                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
501                               "</submenu>\n"
502                                         "</effects-menu>\n"
503                                         "<menu-tip>" N_("Change colors to a two colors palette") "</menu-tip>\n"
504                                 "</effect>\n"
505                         "</inkscape-extension>\n", new TestFilter());
506         };
507 };
509 gchar const *
510 TestFilter::get_filter_text (Inkscape::Extension::Extension * ext)
512         if (_filter != NULL) g_free((void *)_filter);
513     
514         _filter = g_strdup_printf(
515                 "<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"
516             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
517             "<feGaussianBlur stdDeviation=\"4\" result=\"Gaussian1\" />\n"
518             "<feGaussianBlur stdDeviation=\"0.5\" in=\"Composite1\" />\n"
519             "<feBlend in2=\"Gaussian1\" mode=\"normal\" />\n"
520             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
521             "<feComponentTransfer>\n"
522                 "<feFuncR type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
523                 "<feFuncG type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
524                 "<feFuncB type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
525             "</feComponentTransfer>\n"
526             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
527             "<feGaussianBlur stdDeviation=\"0.05\" />\n"
528             "<feComposite in2=\"SourceGraphic\" operator=\"atop\" />\n"
529         "</filter>\n");
531         return _filter;
532 }; /* Test filter */
534 }; /* namespace Filter */
535 }; /* namespace Internal */
536 }; /* namespace Extension */
537 }; /* namespace Inkscape */
539 /* Change the 'COLOR' below to be your file name */
540 #endif /* __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__ */