Code

4191cceecfae0a2cf3ce0c8b49d1e37cd827f07e
[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 /**
148  * Sets a specific value in the SPFeConvolveMatrix.
149  */
150 static void
151 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
153     SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
154     (void)feConvolveMatrix;
156     switch(key) {
157         /*DEAL WITH SETTING ATTRIBUTES HERE*/
158         case SP_ATTR_ORDER:
159             feConvolveMatrix->order.set(value);
160             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
161             break;
162         case SP_ATTR_KERNELMATRIX:
163             feConvolveMatrix->kernelMatrix = read_kernel_matrix(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
164             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
165             break;
166         case SP_ATTR_DIVISOR:
167             feConvolveMatrix->divisor = sp_feConvolveMatrix_read_number(value);
168             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
169             break;
170         case SP_ATTR_BIAS:
171             feConvolveMatrix->bias = sp_feConvolveMatrix_read_number(value);
172             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
173             break;
174         case SP_ATTR_TARGETX:
175             feConvolveMatrix->targetX = (int) sp_feConvolveMatrix_read_number(value);
176             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
177             break;
178         case SP_ATTR_TARGETY:
179             feConvolveMatrix->targetY = (int) sp_feConvolveMatrix_read_number(value);
180             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
181             break;
182         case SP_ATTR_EDGEMODE:
183                 //TODO
184             break;
185         case SP_ATTR_KERNELUNITLENGTH:
186             feConvolveMatrix->kernelUnitLength.set(value);
187             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
188             break;
189         case SP_ATTR_PRESERVEALPHA:
190                 //TODO
191             break;
193         default:
194             if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
195                 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
196             break;
197     }
201 /**
202  * Receives update notifications.
203  */
204 static void
205 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
207     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
208                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
210         /* do something to trigger redisplay, updates? */
212     }
214     if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
215         ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
216     }
219 /**
220  * Writes its settings to an incoming repr object, if any.
221  */
222 static Inkscape::XML::Node *
223 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
225     // Inkscape-only object, not copied during an "plain SVG" dump:
226     if (flags & SP_OBJECT_WRITE_EXT) {
227         if (repr) {
228             // is this sane?
229             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
230         } else {
231             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
232         }
233     }
235     if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
236         ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
237     }
239     return repr;
242 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
243     g_assert(primitive != NULL);
244     g_assert(filter != NULL);
246     SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
248     int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
249     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
250     NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
251     g_assert(nr_convolve != NULL);
253     sp_filter_primitive_renderer_common(primitive, nr_primitive);
255     nr_convolve->set_targetX(sp_convolve->targetX);
256     nr_convolve->set_targetY(sp_convolve->targetY);
257     nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
258     nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
259     nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
260     nr_convolve->set_divisor(sp_convolve->divisor);
261     nr_convolve->set_bias(sp_convolve->bias);
264 /*
265   Local Variables:
266   mode:c++
267   c-file-style:"stroustrup"
268   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
269   indent-tabs-mode:nil
270   fill-column:99
271   End:
272 */
273 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :