Code

edd093499883324052eaea98c5e79a14b1bf5c9b
[inkscape.git] / src / sp-feconvolvematrix.cpp
1 #define __SP_FECONVOLVEMATRIX_CPP__
3 /** \file
4  * SVG <feConvolveMatrix> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
10  *   hugo Rodrigues <haa.rodrigues@gmail.com>
11  *
12  * Copyright (C) 2006 Hugo Rodrigues
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <vector>
22 #include "attributes.h"
23 #include "svg/svg.h"
24 #include "sp-feconvolvematrix.h"
25 #include "xml/repr.h"
26 #include "display/nr-filter-convolve-matrix.h"
28 /* FeConvolveMatrix base class */
30 static void sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass);
31 static void sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix);
33 static void sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
34 static void sp_feConvolveMatrix_release(SPObject *object);
35 static void sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value);
36 static void sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
37 static Inkscape::XML::Node *sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
38 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
40 static SPFilterPrimitiveClass *feConvolveMatrix_parent_class;
42 GType
43 sp_feConvolveMatrix_get_type()
44 {
45     static GType feConvolveMatrix_type = 0;
47     if (!feConvolveMatrix_type) {
48         GTypeInfo feConvolveMatrix_info = {
49             sizeof(SPFeConvolveMatrixClass),
50             NULL, NULL,
51             (GClassInitFunc) sp_feConvolveMatrix_class_init,
52             NULL, NULL,
53             sizeof(SPFeConvolveMatrix),
54             16,
55             (GInstanceInitFunc) sp_feConvolveMatrix_init,
56             NULL,    /* value_table */
57         };
58         feConvolveMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeConvolveMatrix", &feConvolveMatrix_info, (GTypeFlags)0);
59     }
60     return feConvolveMatrix_type;
61 }
63 static void
64 sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass)
65 {
66     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
67     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
69     feConvolveMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
71     sp_object_class->build = sp_feConvolveMatrix_build;
72     sp_object_class->release = sp_feConvolveMatrix_release;
73     sp_object_class->write = sp_feConvolveMatrix_write;
74     sp_object_class->set = sp_feConvolveMatrix_set;
75     sp_object_class->update = sp_feConvolveMatrix_update;
77     sp_primitive_class->build_renderer = sp_feConvolveMatrix_build_renderer;
78 }
80 static void
81 sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix)
82 {}
84 /**
85  * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables.  For this to get called,
86  * our name must be associated with a repr via "sp_object_type_register".  Best done through
87  * sp-object-repr.cpp's repr_name_entries array.
88  */
89 static void
90 sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
91 {
92     if (((SPObjectClass *) feConvolveMatrix_parent_class)->build) {
93         ((SPObjectClass *) feConvolveMatrix_parent_class)->build(object, document, repr);
94     }
96     /*LOAD ATTRIBUTES FROM REPR HERE*/
97     sp_object_read_attr(object, "order");
98     sp_object_read_attr(object, "kernelMatrix");
99     sp_object_read_attr(object, "divisor");
100     sp_object_read_attr(object, "bias");
101     sp_object_read_attr(object, "targetX");
102     sp_object_read_attr(object, "targetY");
103     sp_object_read_attr(object, "edgeMode");
104     sp_object_read_attr(object, "kernelUnitLength");
105     sp_object_read_attr(object, "preserveAlpha");
109 /**
110  * Drops any allocated memory.
111  */
112 static void
113 sp_feConvolveMatrix_release(SPObject *object)
115     if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
116         ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
120 static std::vector<gdouble> read_kernel_matrix(const gchar* value, int size){
121         std::vector<gdouble> v(size, (gdouble) 0);
122         int i;
123         gchar** values = g_strsplit(value , " ", size);
124         for (i=0;i<size;i++)
125                 v[i] = g_ascii_strtod(values[i], NULL);
126         return v;
129 static double
130 sp_feConvolveMatrix_read_number(gchar const *value) {
131     if (!value) return 0;
132     char *end;
133     double ret = g_ascii_strtod(value, &end);
134     if (*end) {
135         g_warning("Unable to convert \"%s\" to number", value);
136         // We could leave this out, too. If strtod can't convert
137         // anything, it will return zero.
138         ret = 0;
139     }
140     return ret;
143 static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
144     if (!value) return 0; //duplicate is default
145     switch(value[0]){
146         case 'd':
147             if (strncmp(value, "duplicate", 9) == 0) return 0;
148             break;
149         case 'w':
150             if (strncmp(value, "wrap", 4) == 0) return 1;
151             break;
152         case 'n':
153             if (strncmp(value, "none", 4) == 0) return 2;
154             break;
155     }
156     return 0; //duplicate is default
160 static bool sp_feConvolveMatrix_read_bool(gchar const *value){
161     if (!value) return false; //false is default
162     switch(value[0]){
163         case 't':
164             if (strncmp(value, "true", 4) == 0) return true;
165             break;
166         case 'f':
167             if (strncmp(value, "false", 5) == 0) return false;
168             break;
169     }
170     return false; //false is default
173 /**
174  * Sets a specific value in the SPFeConvolveMatrix.
175  */
176 static void
177 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
179     SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
180     (void)feConvolveMatrix;
181     double read_num;
182     int read_int;
183     bool read_bool;
184    
185     switch(key) {
186         /*DEAL WITH SETTING ATTRIBUTES HERE*/
187         case SP_ATTR_ORDER:
188             feConvolveMatrix->order.set(value);
189             //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
190             if (feConvolveMatrix->order.optNumIsSet() == false)
191                 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
192             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
193             break;
194         case SP_ATTR_KERNELMATRIX:
195             feConvolveMatrix->kernelMatrix = read_kernel_matrix(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
196             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
197             break;
198         case SP_ATTR_DIVISOR:
199             read_num = sp_feConvolveMatrix_read_number(value);
200             if (read_num != feConvolveMatrix->divisor){
201                 feConvolveMatrix->divisor = read_num;
202                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
203             }
204             break;
205         case SP_ATTR_BIAS:
206             read_num = sp_feConvolveMatrix_read_number(value);
207             if (read_num != feConvolveMatrix->bias){
208                 feConvolveMatrix->bias = read_num;
209                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
210             }
211             break;
212         case SP_ATTR_TARGETX:
213             read_int = (int) sp_feConvolveMatrix_read_number(value);
214             if (read_int != feConvolveMatrix->targetX){
215                 feConvolveMatrix->targetX = read_int;
216                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
217             }
218             break;
219         case SP_ATTR_TARGETY:
220             read_int = (int) sp_feConvolveMatrix_read_number(value);
221             if (read_int != feConvolveMatrix->targetY){
222                 feConvolveMatrix->targetY = read_int;
223                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
224             }
225             break;
226         case SP_ATTR_EDGEMODE:
227             read_int = (int) sp_feConvolveMatrix_read_edgeMode(value);
228             if (read_int != feConvolveMatrix->edgeMode){
229                 feConvolveMatrix->edgeMode = read_int;
230                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
231             }
232             break;
233         case SP_ATTR_KERNELUNITLENGTH:
234             feConvolveMatrix->kernelUnitLength.set(value);
235             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
236             break;
237         case SP_ATTR_PRESERVEALPHA:
238             read_bool = sp_feConvolveMatrix_read_bool(value);
239             if (read_bool != feConvolveMatrix->preserveAlpha){
240                 feConvolveMatrix->preserveAlpha = read_bool;
241                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
242             }
243             break;
244         default:
245             if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
246                 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
247             break;
248     }
252 /**
253  * Receives update notifications.
254  */
255 static void
256 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
258     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
259                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
261         /* do something to trigger redisplay, updates? */
263     }
265     if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
266         ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
267     }
270 /**
271  * Writes its settings to an incoming repr object, if any.
272  */
273 static Inkscape::XML::Node *
274 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
276     // Inkscape-only object, not copied during an "plain SVG" dump:
277     if (flags & SP_OBJECT_WRITE_EXT) {
278         if (repr) {
279             // is this sane?
280             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
281         } else {
282             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
283         }
284     }
286     if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
287         ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
288     }
290     return repr;
293 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
294     g_assert(primitive != NULL);
295     g_assert(filter != NULL);
297     SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
299     int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
300     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
301     NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
302     g_assert(nr_convolve != NULL);
304     sp_filter_primitive_renderer_common(primitive, nr_primitive);
306     nr_convolve->set_targetX(sp_convolve->targetX);
307     nr_convolve->set_targetY(sp_convolve->targetY);
308     nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
309     nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
310     nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
311     nr_convolve->set_divisor(sp_convolve->divisor);
312     nr_convolve->set_bias(sp_convolve->bias);
315 /*
316   Local Variables:
317   mode:c++
318   c-file-style:"stroustrup"
319   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
320   indent-tabs-mode:nil
321   fill-column:99
322   End:
323 */
324 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :