Code

af5a37b4516525ae206d4b618d14ef08f9ccf2c5
[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_("Posterize, custom -EXP-") "</name>\n"
37                                 "<id>org.inkscape.effect.filter.Posterize</id>\n"
38                 "<param name=\"type\" gui-text=\"" N_("Type:") "\" type=\"enum\">\n"
39                     "<_item value=\"normal\">Normal</_item>\n"
40                     "<_item value=\"contrasted\">Contrasted</_item>\n"
41                 "</param>\n"
42                 "<param name=\"level\" gui-text=\"" N_("Level:") "\" type=\"int\" min=\"1\" max=\"10\">3</param>\n"
43                                 "<effect>\n"
44                                         "<object-type>all</object-type>\n"
45                                         "<effects-menu>\n"
46                                                 "<submenu name=\"" N_("Filters") "\">\n"
47                                                 "<submenu name=\"" N_("Experimental") "\"/>\n"
48                               "</submenu>\n"
49                                         "</effects-menu>\n"
50                                         "<menu-tip>" N_("Change colors to a two colors palette") "</menu-tip>\n"
51                                 "</effect>\n"
52                         "</inkscape-extension>\n", new Posterize());
53         };
55 };
57 gchar const *
58 Posterize::get_filter_text (Inkscape::Extension::Extension * ext)
59 {
60         if (_filter != NULL) g_free((void *)_filter);
62     std::ostringstream transf;
64     int level = ext->get_param_int("level") + 1;
65     const gchar *type = ext->get_param_enum("type");
66     float val = 0.0;
67     transf << "0";
68     for ( int step = 1 ; step <= level ; step++ ) {
69         val = (float) step / level;
70         transf << " " << val;
71         if((g_ascii_strcasecmp("contrasted", type) == 0)) {
72             transf << " " << (val - ((float) 1 / (3 * level))) << " " << (val + ((float) 1 / (2 * level)));
73         }
74     }
75     transf << " 1";
77         _filter = g_strdup_printf(
78                 "<filter xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" color-interpolation-filters=\"sRGB\" height=\"1\" width=\"1\" y=\"0\" x=\"0\" inkscape:label=\"Posterize, custom -EXP-\">\n"
79             "<feGaussianBlur stdDeviation=\"1.5\" result=\"result3\" />\n"
80             "<feGaussianBlur stdDeviation=\"1.5\" in=\"SourceGraphic\" result=\"result4\" />\n"
81             "<feBlend in2=\"result3\" blend=\"normal\" mode=\"lighten\" />\n"
82             "<feComponentTransfer result=\"result1\">\n"
83                 "<feFuncR type=\"discrete\" tableValues=\"%s\" />\n"
84                 "<feFuncG type=\"discrete\" tableValues=\"%s\" />\n"
85                 "<feFuncB type=\"discrete\" tableValues=\"%s\" />\n"
86             "</feComponentTransfer>\n"
87             "<feColorMatrix type=\"saturate\" values=\"1\" />\n"
88             "<feGaussianBlur stdDeviation=\"0.01\" />\n"
89             "<feComposite in2=\"SourceGraphic\" operator=\"atop\" result=\"result2\" />\n"
90         "</filter>\n", transf.str().c_str(), transf.str().c_str(), transf.str().c_str());
92         return _filter;
93 };
94 }; /* namespace Filter */
95 }; /* namespace Internal */
96 }; /* namespace Extension */
97 }; /* namespace Inkscape */
99 /* Change the 'COLOR' below to be your file name */
100 #endif /* __INKSCAPE_EXTENSION_INTERNAL_FILTER_EXPERIMENTAL_H__ */