Code

059475e5fc40ae8487e28c75d6ada2bd95cf5a76
[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 {
83         feConvolveMatrix->divisor=0;
84         feConvolveMatrix->preserveAlpha = false;
85         feConvolveMatrix->order.set("3 3");
86 }
88 /**
89  * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables.  For this to get called,
90  * our name must be associated with a repr via "sp_object_type_register".  Best done through
91  * sp-object-repr.cpp's repr_name_entries array.
92  */
93 static void
94 sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
95 {
96     if (((SPObjectClass *) feConvolveMatrix_parent_class)->build) {
97         ((SPObjectClass *) feConvolveMatrix_parent_class)->build(object, document, repr);
98     }
100     /*LOAD ATTRIBUTES FROM REPR HERE*/
101     sp_object_read_attr(object, "order");
102     sp_object_read_attr(object, "kernelMatrix");
103     sp_object_read_attr(object, "divisor");
104     sp_object_read_attr(object, "bias");
105     sp_object_read_attr(object, "targetX");
106     sp_object_read_attr(object, "targetY");
107     sp_object_read_attr(object, "edgeMode");
108     sp_object_read_attr(object, "kernelUnitLength");
109     sp_object_read_attr(object, "preserveAlpha");
113 /**
114  * Drops any allocated memory.
115  */
116 static void
117 sp_feConvolveMatrix_release(SPObject *object)
119     if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
120         ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
124 static std::vector<gdouble> read_kernel_matrix(const gchar* value, int size){
125         std::vector<gdouble> v(size, (gdouble) 0);
126         int i;
127         gchar** values = g_strsplit(value , " ", size);
128         for (i=0;i<size;i++)
129                 v[i] = g_ascii_strtod(values[i], NULL);
130         return v;
133 static double
134 sp_feConvolveMatrix_read_number(gchar const *value) {
135     if (!value) return 0;
136     char *end;
137     double ret = g_ascii_strtod(value, &end);
138     if (*end) {
139         g_warning("Unable to convert \"%s\" to number", value);
140         // We could leave this out, too. If strtod can't convert
141         // anything, it will return zero.
142         ret = 0;
143     }
144     return ret;
147 static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
148     if (!value) return 0; //duplicate is default
149     switch(value[0]){
150         case 'd':
151             if (strncmp(value, "duplicate", 9) == 0) return 0;
152             break;
153         case 'w':
154             if (strncmp(value, "wrap", 4) == 0) return 1;
155             break;
156         case 'n':
157             if (strncmp(value, "none", 4) == 0) return 2;
158             break;
159     }
160     return 0; //duplicate is default
164 static bool sp_feConvolveMatrix_read_bool(gchar const *value){
165     if (!value) return false; //false is default
166     switch(value[0]){
167         case 't':
168             if (strncmp(value, "true", 4) == 0) return true;
169             break;
170         case 'f':
171             if (strncmp(value, "false", 5) == 0) return false;
172             break;
173     }
174     return false; //false is default
177 /**
178  * Sets a specific value in the SPFeConvolveMatrix.
179  */
180 static void
181 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
183     SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
184     (void)feConvolveMatrix;
185     double read_num;
186     int read_int;
187     bool read_bool;
188    
189     switch(key) {
190         /*DEAL WITH SETTING ATTRIBUTES HERE*/
191         case SP_ATTR_ORDER:
192             feConvolveMatrix->order.set(value);
193             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
194             break;
195         case SP_ATTR_KERNELMATRIX:
196             feConvolveMatrix->kernelMatrix = read_kernel_matrix(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
197             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
198             break;
199         case SP_ATTR_DIVISOR:
200             read_num = sp_feConvolveMatrix_read_number(value);
201             if (read_num != feConvolveMatrix->divisor){
202                 feConvolveMatrix->divisor = read_num;
203                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
204             }
205             break;
206         case SP_ATTR_BIAS:
207             read_num = sp_feConvolveMatrix_read_number(value);
208             if (read_num != feConvolveMatrix->bias){
209                 feConvolveMatrix->bias = read_num;
210                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
211             }
212             break;
213         case SP_ATTR_TARGETX:
214             read_int = (int) sp_feConvolveMatrix_read_number(value);
215             if (read_int != feConvolveMatrix->targetX){
216                 feConvolveMatrix->targetX = read_int;
217                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
218             }
219             break;
220         case SP_ATTR_TARGETY:
221             read_int = (int) sp_feConvolveMatrix_read_number(value);
222             if (read_int != feConvolveMatrix->targetY){
223                 feConvolveMatrix->targetY = read_int;
224                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
225             }
226             break;
227         case SP_ATTR_EDGEMODE:
228             read_int = (int) sp_feConvolveMatrix_read_edgeMode(value);
229             if (read_int != feConvolveMatrix->edgeMode){
230                 feConvolveMatrix->edgeMode = read_int;
231                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
232             }
233             break;
234         case SP_ATTR_KERNELUNITLENGTH:
235             feConvolveMatrix->kernelUnitLength.set(value);
236             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
237             break;
238         case SP_ATTR_PRESERVEALPHA:
239             read_bool = sp_feConvolveMatrix_read_bool(value);
240             if (read_bool != feConvolveMatrix->preserveAlpha){
241                 feConvolveMatrix->preserveAlpha = read_bool;
242                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
243             }
244             break;
245         default:
246             if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
247                 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
248             break;
249     }
253 /**
254  * Receives update notifications.
255  */
256 static void
257 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
259     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
260                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
262         /* do something to trigger redisplay, updates? */
264     }
266     if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
267         ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
268     }
271 /**
272  * Writes its settings to an incoming repr object, if any.
273  */
274 static Inkscape::XML::Node *
275 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
277     // Inkscape-only object, not copied during an "plain SVG" dump:
278     if (flags & SP_OBJECT_WRITE_EXT) {
279         if (repr) {
280             // is this sane?
281             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
282         } else {
283             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
284         }
285     }
287     if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
288         ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
289     }
291     return repr;
294 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
295     g_assert(primitive != NULL);
296     g_assert(filter != NULL);
298     SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
300     int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
301     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
302     NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
303     g_assert(nr_convolve != NULL);
305     sp_filter_primitive_renderer_common(primitive, nr_primitive);
307     nr_convolve->set_targetX(sp_convolve->targetX);
308     nr_convolve->set_targetY(sp_convolve->targetY);
309     nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
310     nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
311     nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
312     nr_convolve->set_divisor(sp_convolve->divisor);
313     nr_convolve->set_bias(sp_convolve->bias);
316 /*
317   Local Variables:
318   mode:c++
319   c-file-style:"stroustrup"
320   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
321   indent-tabs-mode:nil
322   fill-column:99
323   End:
324 */
325 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :