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"
28 #include <math.h>
30 /* FeConvolveMatrix base class */
32 static void sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass);
33 static void sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix);
35 static void sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
36 static void sp_feConvolveMatrix_release(SPObject *object);
37 static void sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value);
38 static void sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
39 static Inkscape::XML::Node *sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
40 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
42 static SPFilterPrimitiveClass *feConvolveMatrix_parent_class;
44 GType
45 sp_feConvolveMatrix_get_type()
46 {
47 static GType feConvolveMatrix_type = 0;
49 if (!feConvolveMatrix_type) {
50 GTypeInfo feConvolveMatrix_info = {
51 sizeof(SPFeConvolveMatrixClass),
52 NULL, NULL,
53 (GClassInitFunc) sp_feConvolveMatrix_class_init,
54 NULL, NULL,
55 sizeof(SPFeConvolveMatrix),
56 16,
57 (GInstanceInitFunc) sp_feConvolveMatrix_init,
58 NULL, /* value_table */
59 };
60 feConvolveMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeConvolveMatrix", &feConvolveMatrix_info, (GTypeFlags)0);
61 }
62 return feConvolveMatrix_type;
63 }
65 static void
66 sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass)
67 {
68 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
69 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
71 feConvolveMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
73 sp_object_class->build = sp_feConvolveMatrix_build;
74 sp_object_class->release = sp_feConvolveMatrix_release;
75 sp_object_class->write = sp_feConvolveMatrix_write;
76 sp_object_class->set = sp_feConvolveMatrix_set;
77 sp_object_class->update = sp_feConvolveMatrix_update;
79 sp_primitive_class->build_renderer = sp_feConvolveMatrix_build_renderer;
80 }
82 static void
83 sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix)
84 {
85 //Setting default values:
86 feConvolveMatrix->order.set("3 3");
87 feConvolveMatrix->targetX = 1;
88 feConvolveMatrix->targetY = 1;
89 feConvolveMatrix->edgeMode = NR::CONVOLVEMATRIX_EDGEMODE_DUPLICATE;
90 feConvolveMatrix->preserveAlpha = false;
92 //some helper variables:
93 feConvolveMatrix->targetXIsSet = false;
94 feConvolveMatrix->targetYIsSet = false;
95 feConvolveMatrix->kernelMatrixIsSet = false;
96 }
98 /**
99 * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables. For this to get called,
100 * our name must be associated with a repr via "sp_object_type_register". Best done through
101 * sp-object-repr.cpp's repr_name_entries array.
102 */
103 static void
104 sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
105 {
106 if (((SPObjectClass *) feConvolveMatrix_parent_class)->build) {
107 ((SPObjectClass *) feConvolveMatrix_parent_class)->build(object, document, repr);
108 }
110 /*LOAD ATTRIBUTES FROM REPR HERE*/
111 sp_object_read_attr(object, "order");
112 sp_object_read_attr(object, "kernelMatrix");
113 sp_object_read_attr(object, "divisor");
114 sp_object_read_attr(object, "bias");
115 sp_object_read_attr(object, "targetX");
116 sp_object_read_attr(object, "targetY");
117 sp_object_read_attr(object, "edgeMode");
118 sp_object_read_attr(object, "kernelUnitLength");
119 sp_object_read_attr(object, "preserveAlpha");
120 }
122 /**
123 * Drops any allocated memory.
124 */
125 static void
126 sp_feConvolveMatrix_release(SPObject *object)
127 {
128 if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
129 ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
130 }
132 static NR::FilterConvolveMatrixEdgeMode sp_feConvolveMatrix_read_edgeMode(gchar const *value){
133 if (!value) return NR::CONVOLVEMATRIX_EDGEMODE_DUPLICATE; //duplicate is default
134 switch(value[0]){
135 case 'd':
136 if (strncmp(value, "duplicate", 9) == 0) return NR::CONVOLVEMATRIX_EDGEMODE_DUPLICATE;
137 break;
138 case 'w':
139 if (strncmp(value, "wrap", 4) == 0) return NR::CONVOLVEMATRIX_EDGEMODE_WRAP;
140 break;
141 case 'n':
142 if (strncmp(value, "none", 4) == 0) return NR::CONVOLVEMATRIX_EDGEMODE_NONE;
143 break;
144 }
145 return NR::CONVOLVEMATRIX_EDGEMODE_DUPLICATE; //duplicate is default
146 }
148 /**
149 * Sets a specific value in the SPFeConvolveMatrix.
150 */
151 static void
152 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
153 {
154 SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
155 (void)feConvolveMatrix;
156 double read_num;
157 int read_int;
158 bool read_bool;
159 NR::FilterConvolveMatrixEdgeMode read_mode;
161 switch(key) {
162 /*DEAL WITH SETTING ATTRIBUTES HERE*/
163 case SP_ATTR_ORDER:
164 feConvolveMatrix->order.set(value);
165 //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
166 if (feConvolveMatrix->order.optNumIsSet() == false)
167 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
168 if (feConvolveMatrix->targetXIsSet == false) feConvolveMatrix->targetX = (int) floor(feConvolveMatrix->order.getNumber()/2);
169 if (feConvolveMatrix->targetYIsSet == false) feConvolveMatrix->targetY = (int) floor(feConvolveMatrix->order.getOptNumber()/2);
170 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
171 break;
172 case SP_ATTR_KERNELMATRIX:
173 if (value){
174 feConvolveMatrix->kernelMatrixIsSet = true;
175 feConvolveMatrix->kernelMatrix = helperfns_read_vector(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
176 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
177 } else {
178 g_warning("You MUST pass a kernelMatrix parameter!");
179 }
180 break;
181 case SP_ATTR_DIVISOR:
182 if (!value){
183 read_num = 1;
184 } else {
185 read_num = helperfns_read_number(value);
186 if (read_num == 0) {
187 if (feConvolveMatrix->kernelMatrixIsSet){
188 g_warning("You shouldn't pass a divisor value equal to 0! Assuming the sum of all values in kernelMatrix as the default value.");
189 for (unsigned int i = 0; i< feConvolveMatrix->kernelMatrix.size(); i++)
190 read_num += feConvolveMatrix->kernelMatrix[i];
191 } else {
192 g_warning("You shouldn't pass a divisor value equal to 0! Assuming 1 as the default value.");
193 read_num = 1;
194 }
195 }
196 }
197 if (read_num != feConvolveMatrix->divisor){
198 feConvolveMatrix->divisor = read_num;
199 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
200 }
201 break;
202 case SP_ATTR_BIAS:
203 read_num = 0;
204 if (value) read_num = helperfns_read_number(value);
205 if (read_num != feConvolveMatrix->bias){
206 feConvolveMatrix->bias = read_num;
207 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
208 }
209 break;
210 case SP_ATTR_TARGETX:
211 read_int = (int) helperfns_read_number(value);
212 if (read_int < 0 || read_int > feConvolveMatrix->order.getNumber()){
213 g_warning("targetX must be a value between 0 and orderX! Assuming orderX as default value.");
214 read_int = (int) feConvolveMatrix->order.getNumber();
215 }
216 feConvolveMatrix->targetXIsSet = true;
217 if (read_int != feConvolveMatrix->targetX){
218 feConvolveMatrix->targetX = read_int;
219 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
220 }
221 break;
222 case SP_ATTR_TARGETY:
223 read_int = (int) helperfns_read_number(value);
224 if (read_int < 0 || read_int > feConvolveMatrix->order.getOptNumber()){
225 g_warning("targetY must be a value between 0 and orderY! Assuming orderY as default value.");
226 read_int = (int) feConvolveMatrix->order.getOptNumber();
227 }
228 feConvolveMatrix->targetYIsSet = true;
229 if (read_int != feConvolveMatrix->targetY){
230 feConvolveMatrix->targetY = read_int;
231 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
232 }
233 break;
234 case SP_ATTR_EDGEMODE:
235 read_mode = sp_feConvolveMatrix_read_edgeMode(value);
236 if (read_mode != feConvolveMatrix->edgeMode){
237 feConvolveMatrix->edgeMode = read_mode;
238 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
239 }
240 break;
241 case SP_ATTR_KERNELUNITLENGTH:
242 feConvolveMatrix->kernelUnitLength.set(value);
243 //From SVG spec: If the <dy> value is not specified, it defaults to the same value as <dx>.
244 if (feConvolveMatrix->kernelUnitLength.optNumIsSet() == false)
245 feConvolveMatrix->kernelUnitLength.setOptNumber(feConvolveMatrix->kernelUnitLength.getNumber());
246 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
247 break;
248 case SP_ATTR_PRESERVEALPHA:
249 read_bool = helperfns_read_bool(value, false);
250 if (read_bool != feConvolveMatrix->preserveAlpha){
251 feConvolveMatrix->preserveAlpha = read_bool;
252 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
253 }
254 break;
255 default:
256 if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
257 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
258 break;
259 }
261 }
263 /**
264 * Receives update notifications.
265 */
266 static void
267 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
268 {
269 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
270 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
272 /* do something to trigger redisplay, updates? */
274 }
276 if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
277 ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
278 }
279 }
281 /**
282 * Writes its settings to an incoming repr object, if any.
283 */
284 static Inkscape::XML::Node *
285 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
286 {
287 // Inkscape-only object, not copied during an "plain SVG" dump:
288 if (flags & SP_OBJECT_WRITE_EXT) {
289 if (repr) {
290 // is this sane?
291 //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
292 } else {
293 repr = SP_OBJECT_REPR(object)->duplicate(NULL); // FIXME
294 }
295 }
297 if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
298 ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, repr, flags);
299 }
301 return repr;
302 }
304 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
305 g_assert(primitive != NULL);
306 g_assert(filter != NULL);
308 SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
310 int primitive_n = filter->add_primitive(NR::NR_FILTER_CONVOLVEMATRIX);
311 NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
312 NR::FilterConvolveMatrix *nr_convolve = dynamic_cast<NR::FilterConvolveMatrix*>(nr_primitive);
313 g_assert(nr_convolve != NULL);
315 sp_filter_primitive_renderer_common(primitive, nr_primitive);
317 nr_convolve->set_targetX(sp_convolve->targetX);
318 nr_convolve->set_targetY(sp_convolve->targetY);
319 nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
320 nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
321 nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
322 nr_convolve->set_divisor(sp_convolve->divisor);
323 nr_convolve->set_bias(sp_convolve->bias);
324 nr_convolve->set_preserveAlpha(sp_convolve->preserveAlpha);
326 }
327 /*
328 Local Variables:
329 mode:c++
330 c-file-style:"stroustrup"
331 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
332 indent-tabs-mode:nil
333 fill-column:99
334 End:
335 */
336 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :