Code

87e737dfeaada7b616ba570d60713f286b35151f
[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 "helper-fns.h"
26 #include "xml/repr.h"
27 #include "display/nr-filter-convolve-matrix.h"
29 /* FeConvolveMatrix base class */
31 static void sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass);
32 static void sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix);
34 static void sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
35 static void sp_feConvolveMatrix_release(SPObject *object);
36 static void sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value);
37 static void sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
38 static Inkscape::XML::Node *sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
39 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
41 static SPFilterPrimitiveClass *feConvolveMatrix_parent_class;
43 GType
44 sp_feConvolveMatrix_get_type()
45 {
46     static GType feConvolveMatrix_type = 0;
48     if (!feConvolveMatrix_type) {
49         GTypeInfo feConvolveMatrix_info = {
50             sizeof(SPFeConvolveMatrixClass),
51             NULL, NULL,
52             (GClassInitFunc) sp_feConvolveMatrix_class_init,
53             NULL, NULL,
54             sizeof(SPFeConvolveMatrix),
55             16,
56             (GInstanceInitFunc) sp_feConvolveMatrix_init,
57             NULL,    /* value_table */
58         };
59         feConvolveMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeConvolveMatrix", &feConvolveMatrix_info, (GTypeFlags)0);
60     }
61     return feConvolveMatrix_type;
62 }
64 static void
65 sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass)
66 {
67     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
68     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
70     feConvolveMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
72     sp_object_class->build = sp_feConvolveMatrix_build;
73     sp_object_class->release = sp_feConvolveMatrix_release;
74     sp_object_class->write = sp_feConvolveMatrix_write;
75     sp_object_class->set = sp_feConvolveMatrix_set;
76     sp_object_class->update = sp_feConvolveMatrix_update;
78     sp_primitive_class->build_renderer = sp_feConvolveMatrix_build_renderer;
79 }
81 static void
82 sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix)
83 {}
85 /**
86  * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables.  For this to get called,
87  * our name must be associated with a repr via "sp_object_type_register".  Best done through
88  * sp-object-repr.cpp's repr_name_entries array.
89  */
90 static void
91 sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
92 {
93     if (((SPObjectClass *) feConvolveMatrix_parent_class)->build) {
94         ((SPObjectClass *) feConvolveMatrix_parent_class)->build(object, document, repr);
95     }
97     /*LOAD ATTRIBUTES FROM REPR HERE*/
98     sp_object_read_attr(object, "order");
99     sp_object_read_attr(object, "kernelMatrix");
100     sp_object_read_attr(object, "divisor");
101     sp_object_read_attr(object, "bias");
102     sp_object_read_attr(object, "targetX");
103     sp_object_read_attr(object, "targetY");
104     sp_object_read_attr(object, "edgeMode");
105     sp_object_read_attr(object, "kernelUnitLength");
106     sp_object_read_attr(object, "preserveAlpha");
110 /**
111  * Drops any allocated memory.
112  */
113 static void
114 sp_feConvolveMatrix_release(SPObject *object)
116     if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
117         ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
120 static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
121     if (!value) return 0; //duplicate is default
122     switch(value[0]){
123         case 'd':
124             if (strncmp(value, "duplicate", 9) == 0) return 0;
125             break;
126         case 'w':
127             if (strncmp(value, "wrap", 4) == 0) return 1;
128             break;
129         case 'n':
130             if (strncmp(value, "none", 4) == 0) return 2;
131             break;
132     }
133     return 0; //duplicate is default
136 /**
137  * Sets a specific value in the SPFeConvolveMatrix.
138  */
139 static void
140 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
142     SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
143     (void)feConvolveMatrix;
144     double read_num;
145     int read_int;
146     bool read_bool;
147    
148     switch(key) {
149         /*DEAL WITH SETTING ATTRIBUTES HERE*/
150         case SP_ATTR_ORDER:
151             feConvolveMatrix->order.set(value);
152             //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
153             if (feConvolveMatrix->order.optNumIsSet() == false)
154                 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
155             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
156             break;
157         case SP_ATTR_KERNELMATRIX:
158             feConvolveMatrix->kernelMatrix = helperfns_read_vector(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
159             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
160             break;
161         case SP_ATTR_DIVISOR:
162             read_num = helperfns_read_number(value);
163             if (read_num != feConvolveMatrix->divisor){
164                 feConvolveMatrix->divisor = read_num;
165                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
166             }
167             break;
168         case SP_ATTR_BIAS:
169             read_num = helperfns_read_number(value);
170             if (read_num != feConvolveMatrix->bias){
171                 feConvolveMatrix->bias = read_num;
172                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
173             }
174             break;
175         case SP_ATTR_TARGETX:
176             read_int = (int) helperfns_read_number(value);
177             if (read_int != feConvolveMatrix->targetX){
178                 feConvolveMatrix->targetX = read_int;
179                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
180             }
181             break;
182         case SP_ATTR_TARGETY:
183             read_int = (int) helperfns_read_number(value);
184             if (read_int != feConvolveMatrix->targetY){
185                 feConvolveMatrix->targetY = read_int;
186                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
187             }
188             break;
189         case SP_ATTR_EDGEMODE:
190             read_int = (int) sp_feConvolveMatrix_read_edgeMode(value);
191             if (read_int != feConvolveMatrix->edgeMode){
192                 feConvolveMatrix->edgeMode = read_int;
193                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
194             }
195             break;
196         case SP_ATTR_KERNELUNITLENGTH:
197             feConvolveMatrix->kernelUnitLength.set(value);
198             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
199             break;
200         case SP_ATTR_PRESERVEALPHA:
201             read_bool = helperfns_read_bool(value, false);
202             if (read_bool != feConvolveMatrix->preserveAlpha){
203                 feConvolveMatrix->preserveAlpha = read_bool;
204                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
205             }
206             break;
207         default:
208             if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
209                 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
210             break;
211     }
215 /**
216  * Receives update notifications.
217  */
218 static void
219 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
221     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
222                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
224         /* do something to trigger redisplay, updates? */
226     }
228     if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
229         ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
230     }
233 /**
234  * Writes its settings to an incoming repr object, if any.
235  */
236 static Inkscape::XML::Node *
237 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
239     // Inkscape-only object, not copied during an "plain SVG" dump:
240     if (flags & SP_OBJECT_WRITE_EXT) {
241         if (repr) {
242             // is this sane?
243             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
244         } else {
245             repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
246         }
247     }
249     if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
250         ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
251     }
253     return repr;
256 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
257     g_assert(primitive != NULL);
258     g_assert(filter != NULL);
260     SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
262     int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
263     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
264     NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
265     g_assert(nr_convolve != NULL);
267     sp_filter_primitive_renderer_common(primitive, nr_primitive);
269     nr_convolve->set_targetX(sp_convolve->targetX);
270     nr_convolve->set_targetY(sp_convolve->targetY);
271     nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
272     nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
273     nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
274     nr_convolve->set_divisor(sp_convolve->divisor);
275     nr_convolve->set_bias(sp_convolve->bias);
278 /*
279   Local Variables:
280   mode:c++
281   c-file-style:"stroustrup"
282   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
283   indent-tabs-mode:nil
284   fill-column:99
285   End:
286 */
287 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :