Code

Filters. New custom predefined filters (Colorize and Quadritone fantasy) and some...
[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 class Posterize : public Inkscape::Extension::Internal::Filter::Filter {
26 protected:
27         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
29 public:
30         Posterize ( ) : Filter() { };
31         virtual ~Posterize ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
33         static void init (void) {
34                 Inkscape::Extension::build_from_mem(
35                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
36                                 "<name>" N_("Poster and painting, custom -EXP-") "</name>\n"
37                                 "<id>org.inkscape.effect.filter.Posterize</id>\n"
38                 "<param name=\"type\" gui-text=\"" N_("Effect type:") "\" type=\"enum\">\n"
39                     "<_item value=\"normal\">Normal</_item>\n"
40                     "<_item value=\"dented\">Dented</_item>\n"
41                 "</param>\n"
42                 "<param name=\"table\" gui-text=\"" N_("Transfer type:") "\" type=\"enum\">\n"
43                     "<_item value=\"discrete\">Poster</_item>\n"
44                     "<_item value=\"table\">Painting</_item>\n"
45                 "</param>\n"
46                 "<param name=\"levels\" gui-text=\"" N_("Levels:") "\" type=\"int\" min=\"1\" max=\"15\">5</param>\n"
47                 "<param name=\"blend\" gui-text=\"" N_("Blend mode:") "\" type=\"enum\">\n"
48                     "<_item value=\"lighten\">Lighten</_item>\n"
49                     "<_item value=\"normal\">Normal</_item>\n"
50                     "<_item value=\"darken\">Darken</_item>\n"
51                 "</param>\n"
52                 "<param name=\"blur1\" gui-text=\"" N_("Primary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">4.0</param>\n"
53                 "<param name=\"blur2\" gui-text=\"" N_("Secondary blur:") "\" type=\"float\" min=\"0.0\" max=\"100.0\">0.5</param>\n"
54                 "<param name=\"presaturation\" gui-text=\"" N_("Pre-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
55                 "<param name=\"postsaturation\" gui-text=\"" N_("Post-saturation:") "\" type=\"float\" min=\"0.00\" max=\"1.00\">1.00</param>\n"
56                 "<param name=\"antialiasing\" gui-text=\"" N_("Simulate antialiasing") "\" type=\"boolean\">false</param>\n"
57                                 "<effect>\n"
58                                         "<object-type>all</object-type>\n"
59                                         "<effects-menu>\n"
60                                                 "<submenu name=\"" N_("Filters") "\">\n"
61                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
62                               "</submenu>\n"
63                                         "</effects-menu>\n"
64                                         "<menu-tip>" N_("Poster and painting effects") "</menu-tip>\n"
65                                 "</effect>\n"
66                         "</inkscape-extension>\n", new Posterize());
67         };
69 };
71 gchar const *
72 Posterize::get_filter_text (Inkscape::Extension::Extension * ext)
73 {
74         if (_filter != NULL) g_free((void *)_filter);
76     std::ostringstream table;
77     std::ostringstream blendmode;
78     std::ostringstream blur1;
79     std::ostringstream blur2;
80     std::ostringstream presat;
81     std::ostringstream postsat;
82     std::ostringstream transf;
83     std::ostringstream antialias;
84     
85     table << ext->get_param_enum("table");
86     blendmode << ext->get_param_enum("blend");
87     blur1 << ext->get_param_float("blur1") + 0.01;
88     blur2 << ext->get_param_float("blur2") + 0.01;
89     presat << ext->get_param_float("presaturation");
90     postsat << ext->get_param_float("postsaturation");
93     // TransfertComponenet table values are calculated based on the poster type.
94     transf << "0";
95     int levels = ext->get_param_int("levels") + 1;
96     const gchar *effecttype =  ext->get_param_enum("type");
97     float val = 0.0;
98     for ( int step = 1 ; step <= levels ; step++ ) {
99         val = (float) step / levels;
100         transf << " " << val;
101         if((g_ascii_strcasecmp("dented", effecttype) == 0)) {
102             transf << " " << (val - ((float) 1 / (3 * levels))) << " " << (val + ((float) 1 / (2 * levels)));
103         }
104     }
105     transf << " 1";
106     
107     if (ext->get_param_bool("antialiasing"))
108         antialias << "0.5";
109     else
110         antialias << "0.01";
112     
113         _filter = g_strdup_printf(
114                 "<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"
115             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
116             "<feGaussianBlur stdDeviation=\"%s\" result=\"Gaussian1\" />\n"
117             "<feGaussianBlur stdDeviation=\"%s\" in=\"Composite1\" />\n"
118             "<feBlend in2=\"Gaussian1\" mode=\"%s\" />\n"
119             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
120             "<feComponentTransfer>\n"
121                 "<feFuncR type=\"%s\" tableValues=\"%s\" />\n"
122                 "<feFuncG type=\"%s\" tableValues=\"%s\" />\n"
123                 "<feFuncB type=\"%s\" tableValues=\"%s\" />\n"
124             "</feComponentTransfer>\n"
125             "<feColorMatrix type=\"saturate\" values=\"%s\" />\n"
126             "<feGaussianBlur stdDeviation=\"%s\" />\n"
127             "<feComposite in2=\"SourceGraphic\" operator=\"in\" />\n"
128         "</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());
130         return _filter;
131 };
133 class TestFilter : public Inkscape::Extension::Internal::Filter::Filter {
134 protected:
135         virtual gchar const * get_filter_text (Inkscape::Extension::Extension * ext);
137 public:
138         TestFilter ( ) : Filter() { };
139         virtual ~TestFilter ( ) { if (_filter != NULL) g_free((void *)_filter); return; }
141         static void init (void) {
142                 Inkscape::Extension::build_from_mem(
143                         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
144                                 "<name>" N_("Test Filter -EXP-") "</name>\n"
145                                 "<id>org.inkscape.effect.filter.TestFilter</id>\n"
146                 "<_param name=\"header1\" type=\"groupheader\">Test filter</_param>\n"
147                                 "<effect>\n"
148                                         "<object-type>all</object-type>\n"
149                                         "<effects-menu>\n"
150                                                 "<submenu name=\"" N_("Filters") "\">\n"
151                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
152                               "</submenu>\n"
153                                         "</effects-menu>\n"
154                                         "<menu-tip>" N_("Change colors to a two colors palette") "</menu-tip>\n"
155                                 "</effect>\n"
156                         "</inkscape-extension>\n", new TestFilter());
157         };
159 };
161 gchar const *
162 TestFilter::get_filter_text (Inkscape::Extension::Extension * ext)
164         if (_filter != NULL) g_free((void *)_filter);
165     
166         _filter = g_strdup_printf(
167                 "<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"
168             "<feComposite result=\"Composite1\" operator=\"arithmetic\" k2=\"1\" />\n"
169             "<feGaussianBlur stdDeviation=\"4\" result=\"Gaussian1\" />\n"
170             "<feGaussianBlur stdDeviation=\"0.5\" in=\"Composite1\" />\n"
171             "<feBlend in2=\"Gaussian1\" mode=\"normal\" />\n"
172             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
173             "<feComponentTransfer>\n"
174                 "<feFuncR type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
175                 "<feFuncG type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
176                 "<feFuncB type=\"discrete\" tableValues=\"0 0.25 0.5 0.75 1 1\" />\n"
177             "</feComponentTransfer>\n"
178             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
179             "<feGaussianBlur stdDeviation=\"0.05\" />\n"
180             "<feComposite in2=\"SourceGraphic\" operator=\"atop\" />\n"
181         "</filter>\n");
183         return _filter;
184 };
185 }; /* namespace Filter */
186 }; /* namespace Internal */
187 }; /* namespace Extension */
188 }; /* namespace Inkscape */
190 /* Change the 'COLOR' below to be your file name */
191 #endif /* __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__ */