adabc1bac06d1362551897906d7087f4c3ebab49
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");
108 }
110 /**
111 * Drops any allocated memory.
112 */
113 static void
114 sp_feConvolveMatrix_release(SPObject *object)
115 {
116 if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
117 ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
118 }
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;
127 }
129 static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
130 if (!value) return 0; //duplicate is default
131 switch(value[0]){
132 case 'd':
133 if (strncmp(value, "duplicate", 9) == 0) return 0;
134 break;
135 case 'w':
136 if (strncmp(value, "wrap", 4) == 0) return 1;
137 break;
138 case 'n':
139 if (strncmp(value, "none", 4) == 0) return 2;
140 break;
141 }
142 return 0; //duplicate is default
143 }
145 /**
146 * Sets a specific value in the SPFeConvolveMatrix.
147 */
148 static void
149 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
150 {
151 SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
152 (void)feConvolveMatrix;
153 double read_num;
154 int read_int;
155 bool read_bool;
157 switch(key) {
158 /*DEAL WITH SETTING ATTRIBUTES HERE*/
159 case SP_ATTR_ORDER:
160 feConvolveMatrix->order.set(value);
161 //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
162 if (feConvolveMatrix->order.optNumIsSet() == false)
163 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
164 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
165 break;
166 case SP_ATTR_KERNELMATRIX:
167 feConvolveMatrix->kernelMatrix = read_kernel_matrix(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
168 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
169 break;
170 case SP_ATTR_DIVISOR:
171 read_num = helperfns_read_number(value);
172 if (read_num != feConvolveMatrix->divisor){
173 feConvolveMatrix->divisor = read_num;
174 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
175 }
176 break;
177 case SP_ATTR_BIAS:
178 read_num = helperfns_read_number(value);
179 if (read_num != feConvolveMatrix->bias){
180 feConvolveMatrix->bias = read_num;
181 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
182 }
183 break;
184 case SP_ATTR_TARGETX:
185 read_int = (int) helperfns_read_number(value);
186 if (read_int != feConvolveMatrix->targetX){
187 feConvolveMatrix->targetX = read_int;
188 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
189 }
190 break;
191 case SP_ATTR_TARGETY:
192 read_int = (int) helperfns_read_number(value);
193 if (read_int != feConvolveMatrix->targetY){
194 feConvolveMatrix->targetY = read_int;
195 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
196 }
197 break;
198 case SP_ATTR_EDGEMODE:
199 read_int = (int) sp_feConvolveMatrix_read_edgeMode(value);
200 if (read_int != feConvolveMatrix->edgeMode){
201 feConvolveMatrix->edgeMode = read_int;
202 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
203 }
204 break;
205 case SP_ATTR_KERNELUNITLENGTH:
206 feConvolveMatrix->kernelUnitLength.set(value);
207 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
208 break;
209 case SP_ATTR_PRESERVEALPHA:
210 read_bool = helperfns_read_bool(value, false);
211 if (read_bool != feConvolveMatrix->preserveAlpha){
212 feConvolveMatrix->preserveAlpha = read_bool;
213 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
214 }
215 break;
216 default:
217 if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
218 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
219 break;
220 }
222 }
224 /**
225 * Receives update notifications.
226 */
227 static void
228 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
229 {
230 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
231 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
233 /* do something to trigger redisplay, updates? */
235 }
237 if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
238 ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
239 }
240 }
242 /**
243 * Writes its settings to an incoming repr object, if any.
244 */
245 static Inkscape::XML::Node *
246 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
247 {
248 // Inkscape-only object, not copied during an "plain SVG" dump:
249 if (flags & SP_OBJECT_WRITE_EXT) {
250 if (repr) {
251 // is this sane?
252 repr->mergeFrom(SP_OBJECT_REPR(object), "id");
253 } else {
254 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
255 }
256 }
258 if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
259 ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
260 }
262 return repr;
263 }
265 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
266 g_assert(primitive != NULL);
267 g_assert(filter != NULL);
269 SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
271 int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
272 NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
273 NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
274 g_assert(nr_convolve != NULL);
276 sp_filter_primitive_renderer_common(primitive, nr_primitive);
278 nr_convolve->set_targetX(sp_convolve->targetX);
279 nr_convolve->set_targetY(sp_convolve->targetY);
280 nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
281 nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
282 nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
283 nr_convolve->set_divisor(sp_convolve->divisor);
284 nr_convolve->set_bias(sp_convolve->bias);
286 }
287 /*
288 Local Variables:
289 mode:c++
290 c-file-style:"stroustrup"
291 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
292 indent-tabs-mode:nil
293 fill-column:99
294 End:
295 */
296 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :