1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #ifndef SEEN_INKSCAPE_EXTENSION_INTERNAL_FILTER_DISTORT_H__
3 #define SEEN_INKSCAPE_EXTENSION_INTERNAL_FILTER_DISTORT_H__
4 /* Change the 'DISTORT' above to be your file name */
5 
6 /*
7  * Copyright (C) 2011 Authors:
8  *   Ivan Louette (filters)
9  *   Nicolas Dufour (UI) <nicoduf@yahoo.fr>
10  *
11  * Distort filters
12  *   Felt Feather
13  *   Roughen
14  *
15  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
16  */
17 /* ^^^ Change the copyright to be you and your e-mail address ^^^ */
18 
19 #include "filter.h"
20 
21 #include "extension/internal/clear-n_.h"
22 #include "extension/system.h"
23 #include "extension/extension.h"
24 
25 namespace Inkscape {
26 namespace Extension {
27 namespace Internal {
28 namespace Filter {
29 
30 /**
31     \brief    Custom predefined FeltFeather filter.
32 
33     Blur and displace edges of shapes and pictures
34 
35     Filter's parameters:
36     * Type (enum, default "In") ->
37         in = map (in="composite3")
38         out = map (in="blur")
39     * Horizontal blur (0.01->30., default 15) -> blur (stdDeviation)
40     * Vertical blur (0.01->30., default 15) -> blur (stdDeviation)
41     * Dilatation (n-1th value, 0.->100., default 1) -> colormatrix (matrix)
42     * Erosion (nth value, 0.->100., default 0) -> colormatrix (matrix)
43     * Stroke (enum, default "Normal") ->
44         Normal = composite4 (operator="atop")
45         Wide = composite4 (operator="over")
46         Narrow = composite4 (operator="in")
47         No fill = composite4 (operator="xor")
48     * Roughness (group)
49     * Turbulence type (enum, default fractalNoise else turbulence) -> turbulence (type)
50     * Horizontal frequency (0.001->1., default 0.05) -> turbulence (baseFrequency [/100])
51     * Vertical frequency (0.001->1., default 0.05) -> turbulence (baseFrequency [/100])
52     * Complexity (1->5, default 3) -> turbulence (numOctaves)
53     * Variation (0->100, default 0) -> turbulence (seed)
54     * Intensity (0.0->100., default 30) -> displacement (scale)
55 */
56 
57 class FeltFeather : public Inkscape::Extension::Internal::Filter::Filter {
58 protected:
59     gchar const * get_filter_text (Inkscape::Extension::Extension * ext) override;
60 
61 public:
FeltFeather()62     FeltFeather ( ) : Filter() { };
~FeltFeather()63     ~FeltFeather ( ) override { if (_filter != nullptr) g_free((void *)_filter); return; }
64 
init()65     static void init () {
66         // clang-format off
67         Inkscape::Extension::build_from_mem(
68             "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
69               "<name>" N_("Felt Feather") "</name>\n"
70               "<id>org.inkscape.effect.filter.FeltFeather</id>\n"
71               "<param name=\"type\" gui-text=\"" N_("Type:") "\" type=\"optiongroup\" appearance=\"combo\">\n"
72                 "<option value=\"in\">" N_("In") "</option>\n"
73                 "<option value=\"out\">" N_("Out") "</option>\n"
74               "</param>\n"
75               "<param name=\"hblur\" gui-text=\"" N_("Horizontal blur") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.01\" max=\"30.00\">15</param>\n"
76               "<param name=\"vblur\" gui-text=\"" N_("Vertical blur") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.01\" max=\"30.00\">15</param>\n"
77               "<param name=\"dilat\" gui-text=\"" N_("Dilatation") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0\" max=\"100\">1</param>\n"
78               "<param name=\"erosion\" gui-text=\"" N_("Erosion") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0\" max=\"100\">0</param>\n"
79               "<param name=\"stroke\" gui-text=\"" N_("Stroke:") "\" type=\"optiongroup\" appearance=\"combo\">\n"
80                 "<option value=\"atop\">" N_("Normal") "</option>\n"
81                 "<option value=\"over\">" N_("Wide") "</option>\n"
82                 "<option value=\"in\">" N_("Narrow") "</option>\n"
83                 "<option value=\"xor\">" N_("No fill") "</option>\n"
84               "</param>\n"
85               "<param name=\"turbulence\" indent=\"1\" gui-text=\"" N_("Turbulence:") "\" type=\"optiongroup\" appearance=\"combo\">\n"
86                 "<option value=\"fractalNoise\">" N_("Fractal noise") "</option>\n"
87                 "<option value=\"turbulence\">" N_("Turbulence") "</option>\n"
88               "</param>\n"
89               "<param name=\"hfreq\" gui-text=\"" N_("Horizontal frequency") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.01\" max=\"100.\">5</param>\n"
90               "<param name=\"vfreq\" gui-text=\"" N_("Vertical frequency") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.01\" max=\"100.\">5</param>\n"
91               "<param name=\"complexity\" gui-text=\"" N_("Complexity") "\" type=\"int\" appearance=\"full\" min=\"1\" max=\"5\">3</param>\n"
92               "<param name=\"variation\" gui-text=\"" N_("Variation") "\" type=\"int\" appearance=\"full\" min=\"1\" max=\"100\">0</param>\n"
93               "<param name=\"intensity\" gui-text=\"" N_("Intensity") "\" type=\"float\" appearance=\"full\" min=\"0.0\" max=\"100\">30</param>\n"
94               "<effect>\n"
95                 "<object-type>all</object-type>\n"
96                 "<effects-menu>\n"
97                   "<submenu name=\"" N_("Filters") "\">\n"
98                     "<submenu name=\"" N_("Distort") "\"/>\n"
99                   "</submenu>\n"
100                 "</effects-menu>\n"
101                 "<menu-tip>" N_("Blur and displace edges of shapes and pictures") "</menu-tip>\n"
102               "</effect>\n"
103             "</inkscape-extension>\n", new FeltFeather());
104         // clang-format on
105     };
106 
107 };
108 
109 gchar const *
get_filter_text(Inkscape::Extension::Extension * ext)110 FeltFeather::get_filter_text (Inkscape::Extension::Extension * ext)
111 {
112     if (_filter != nullptr) g_free((void *)_filter);
113 
114 
115     std::ostringstream hblur;
116     std::ostringstream vblur;
117     std::ostringstream dilat;
118     std::ostringstream erosion;
119 
120     std::ostringstream turbulence;
121     std::ostringstream hfreq;
122     std::ostringstream vfreq;
123     std::ostringstream complexity;
124     std::ostringstream variation;
125     std::ostringstream intensity;
126 
127     std::ostringstream map;
128     std::ostringstream stroke;
129 
130     hblur << ext->get_param_float("hblur");
131     vblur << ext->get_param_float("vblur");
132     dilat << ext->get_param_float("dilat");
133     erosion << -ext->get_param_float("erosion");
134 
135     turbulence << ext->get_param_optiongroup("turbulence");
136     hfreq << ext->get_param_float("hfreq") / 100;
137     vfreq << ext->get_param_float("vfreq") / 100;
138     complexity << ext->get_param_int("complexity");
139     variation << ext->get_param_int("variation");
140     intensity << ext->get_param_float("intensity");
141 
142     stroke << ext->get_param_optiongroup("stroke");
143 
144     const gchar *maptype = ext->get_param_optiongroup("type");
145     if (g_ascii_strcasecmp("in", maptype) == 0) {
146         map << "composite3";
147     } else {
148         map << "blur";
149     }
150 
151     // clang-format off
152     _filter = g_strdup_printf(
153         "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" x=\"-0.3\" width=\"1.6\" y=\"-0.3\" height=\"1.6\" style=\"color-interpolation-filters:sRGB;\" inkscape:label=\"Felt Feather\">\n"
154           "<feGaussianBlur stdDeviation=\"%s %s\" result=\"blur\" />\n"
155           "<feComposite in=\"SourceGraphic\" in2=\"blur\" operator=\"atop\" result=\"composite1\" />\n"
156           "<feComposite in2=\"composite1\" operator=\"in\" result=\"composite2\" />\n"
157           "<feComposite in2=\"composite2\" operator=\"in\" result=\"composite3\" />\n"
158           "<feTurbulence type=\"%s\" numOctaves=\"%s\" seed=\"%s\" baseFrequency=\"%s %s\" result=\"turbulence\" />\n"
159           "<feDisplacementMap in=\"%s\" in2=\"turbulence\" xChannelSelector=\"R\" scale=\"%s\" yChannelSelector=\"G\" result=\"map\" />\n"
160           "<feColorMatrix values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 %s %s \" result=\"colormatrix\" />\n"
161           "<feComposite in=\"composite3\" in2=\"colormatrix\" operator=\"%s\" result=\"composite4\" />\n"
162         "</filter>\n", hblur.str().c_str(), vblur.str().c_str(),
163                        turbulence.str().c_str(), complexity.str().c_str(), variation.str().c_str(), hfreq.str().c_str(), vfreq.str().c_str(),
164                        map.str().c_str(), intensity.str().c_str(), dilat.str().c_str(), erosion.str().c_str(), stroke.str().c_str() );
165     // clang-format on
166 
167     return _filter;
168 }; /* Felt feather filter */
169 
170 /**
171     \brief    Custom predefined Roughen filter.
172 
173     Small-scale roughening to edges and content
174 
175     Filter's parameters:
176     * Turbulence type (enum, default fractalNoise else turbulence) -> turbulence (type)
177     * Horizontal frequency (0.001->10., default 0.013) -> turbulence (baseFrequency [/100])
178     * Vertical frequency (0.001->10., default 0.013) -> turbulence (baseFrequency [/100])
179     * Complexity (1->5, default 5) -> turbulence (numOctaves)
180     * Variation (1->360, default 1) -> turbulence (seed)
181     * Intensity (0.0->50., default 6.6) -> displacement (scale)
182 */
183 
184 class Roughen : public Inkscape::Extension::Internal::Filter::Filter {
185 protected:
186     gchar const * get_filter_text (Inkscape::Extension::Extension * ext) override;
187 
188 public:
Roughen()189     Roughen ( ) : Filter() { };
~Roughen()190     ~Roughen ( ) override { if (_filter != nullptr) g_free((void *)_filter); return; }
191 
init()192     static void init () {
193         // clang-format off
194         Inkscape::Extension::build_from_mem(
195             "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
196               "<name>" N_("Roughen") "</name>\n"
197               "<id>org.inkscape.effect.filter.Roughen</id>\n"
198               "<param name=\"type\" gui-text=\"" N_("Turbulence type:") "\" type=\"optiongroup\" appearance=\"combo\">\n"
199                 "<option value=\"fractalNoise\">" N_("Fractal noise") "</option>\n"
200                 "<option value=\"turbulence\">" N_("Turbulence") "</option>\n"
201               "</param>\n"
202               "<param name=\"hfreq\" gui-text=\"" N_("Horizontal frequency") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.1\" max=\"1000.00\">1.3</param>\n"
203               "<param name=\"vfreq\" gui-text=\"" N_("Vertical frequency") "\" type=\"float\" appearance=\"full\" precision=\"2\" min=\"0.1\" max=\"1000.00\">1.3</param>\n"
204               "<param name=\"complexity\" gui-text=\"" N_("Complexity") "\" type=\"int\" appearance=\"full\" min=\"1\" max=\"5\">5</param>\n"
205               "<param name=\"variation\" gui-text=\"" N_("Variation") "\" type=\"int\" appearance=\"full\" min=\"1\" max=\"360\">0</param>\n"
206               "<param name=\"intensity\" gui-text=\"" N_("Intensity") "\" type=\"float\" appearance=\"full\" min=\"0.0\" max=\"50\">6.6</param>\n"
207               "<effect>\n"
208                 "<object-type>all</object-type>\n"
209                 "<effects-menu>\n"
210                   "<submenu name=\"" N_("Filters") "\">\n"
211                     "<submenu name=\"" N_("Distort") "\"/>\n"
212                   "</submenu>\n"
213                 "</effects-menu>\n"
214                 "<menu-tip>" N_("Small-scale roughening to edges and content") "</menu-tip>\n"
215               "</effect>\n"
216             "</inkscape-extension>\n", new Roughen());
217         // clang-format on
218     };
219 
220 };
221 
222 gchar const *
get_filter_text(Inkscape::Extension::Extension * ext)223 Roughen::get_filter_text (Inkscape::Extension::Extension * ext)
224 {
225     if (_filter != nullptr) g_free((void *)_filter);
226 
227     std::ostringstream type;
228     std::ostringstream hfreq;
229     std::ostringstream vfreq;
230     std::ostringstream complexity;
231     std::ostringstream variation;
232     std::ostringstream intensity;
233 
234     type << ext->get_param_optiongroup("type");
235     hfreq << ext->get_param_float("hfreq") / 100;
236     vfreq << ext->get_param_float("vfreq") / 100;
237     complexity << ext->get_param_int("complexity");
238     variation << ext->get_param_int("variation");
239     intensity << ext->get_param_float("intensity");
240 
241     // clang-format off
242     _filter = g_strdup_printf(
243         "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" style=\"color-interpolation-filters:sRGB;\" inkscape:label=\"Roughen\">\n"
244           "<feTurbulence  type=\"%s\" numOctaves=\"%s\" seed=\"%s\" baseFrequency=\"%s %s\" result=\"turbulence\" />\n"
245           "<feDisplacementMap in=\"SourceGraphic\" in2=\"turbulence\" scale=\"%s\" yChannelSelector=\"G\" xChannelSelector=\"R\" />\n"
246         "</filter>\n", type.str().c_str(), complexity.str().c_str(), variation.str().c_str(), hfreq.str().c_str(), vfreq.str().c_str(), intensity.str().c_str());
247     // clang-format on
248 
249     return _filter;
250 }; /* Roughen filter */
251 
252 }; /* namespace Filter */
253 }; /* namespace Internal */
254 }; /* namespace Extension */
255 }; /* namespace Inkscape */
256 
257 /* Change the 'DISTORT' below to be your file name */
258 #endif /* SEEN_INKSCAPE_EXTENSION_INTERNAL_FILTER_DISTORT_H__ */
259