1 /** \file
2 * SVG <feConvolveMatrix> implementation.
3 *
4 */
5 /*
6 * Authors:
7 * Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
8 * hugo Rodrigues <haa.rodrigues@gmail.com>
9 * Abhishek Sharma
10 *
11 * Copyright (C) 2006 Hugo Rodrigues
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include <string.h>
22 #include <vector>
23 #include "attributes.h"
24 #include "svg/svg.h"
25 #include "convolvematrix.h"
26 #include "helper-fns.h"
27 #include "xml/repr.h"
28 #include "display/nr-filter-convolve-matrix.h"
29 #include <math.h>
31 /* FeConvolveMatrix base class */
33 static void sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass);
34 static void sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix);
36 static void sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
37 static void sp_feConvolveMatrix_release(SPObject *object);
38 static void sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value);
39 static void sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
40 static Inkscape::XML::Node *sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
41 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
43 static SPFilterPrimitiveClass *feConvolveMatrix_parent_class;
45 GType
46 sp_feConvolveMatrix_get_type()
47 {
48 static GType feConvolveMatrix_type = 0;
50 if (!feConvolveMatrix_type) {
51 GTypeInfo feConvolveMatrix_info = {
52 sizeof(SPFeConvolveMatrixClass),
53 NULL, NULL,
54 (GClassInitFunc) sp_feConvolveMatrix_class_init,
55 NULL, NULL,
56 sizeof(SPFeConvolveMatrix),
57 16,
58 (GInstanceInitFunc) sp_feConvolveMatrix_init,
59 NULL, /* value_table */
60 };
61 feConvolveMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeConvolveMatrix", &feConvolveMatrix_info, (GTypeFlags)0);
62 }
63 return feConvolveMatrix_type;
64 }
66 static void
67 sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass)
68 {
69 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
70 SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
72 feConvolveMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
74 sp_object_class->build = sp_feConvolveMatrix_build;
75 sp_object_class->release = sp_feConvolveMatrix_release;
76 sp_object_class->write = sp_feConvolveMatrix_write;
77 sp_object_class->set = sp_feConvolveMatrix_set;
78 sp_object_class->update = sp_feConvolveMatrix_update;
80 sp_primitive_class->build_renderer = sp_feConvolveMatrix_build_renderer;
81 }
83 static void
84 sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix)
85 {
86 //Setting default values:
87 feConvolveMatrix->order.set("3 3");
88 feConvolveMatrix->targetX = 1;
89 feConvolveMatrix->targetY = 1;
90 feConvolveMatrix->edgeMode = Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_DUPLICATE;
91 feConvolveMatrix->preserveAlpha = false;
93 //some helper variables:
94 feConvolveMatrix->targetXIsSet = false;
95 feConvolveMatrix->targetYIsSet = false;
96 feConvolveMatrix->kernelMatrixIsSet = false;
97 }
99 /**
100 * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables. For this to get called,
101 * our name must be associated with a repr via "sp_object_type_register". Best done through
102 * sp-object-repr.cpp's repr_name_entries array.
103 */
104 static void
105 sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
106 {
107 if (((SPObjectClass *) feConvolveMatrix_parent_class)->build) {
108 ((SPObjectClass *) feConvolveMatrix_parent_class)->build(object, document, repr);
109 }
111 /*LOAD ATTRIBUTES FROM REPR HERE*/
112 object->readAttr( "order" );
113 object->readAttr( "kernelMatrix" );
114 object->readAttr( "divisor" );
115 object->readAttr( "bias" );
116 object->readAttr( "targetX" );
117 object->readAttr( "targetY" );
118 object->readAttr( "edgeMode" );
119 object->readAttr( "kernelUnitLength" );
120 object->readAttr( "preserveAlpha" );
121 }
123 /**
124 * Drops any allocated memory.
125 */
126 static void
127 sp_feConvolveMatrix_release(SPObject *object)
128 {
129 if (((SPObjectClass *) feConvolveMatrix_parent_class)->release)
130 ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
131 }
133 static Inkscape::Filters::FilterConvolveMatrixEdgeMode sp_feConvolveMatrix_read_edgeMode(gchar const *value){
134 if (!value) return Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_DUPLICATE; //duplicate is default
135 switch(value[0]){
136 case 'd':
137 if (strncmp(value, "duplicate", 9) == 0) return Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_DUPLICATE;
138 break;
139 case 'w':
140 if (strncmp(value, "wrap", 4) == 0) return Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_WRAP;
141 break;
142 case 'n':
143 if (strncmp(value, "none", 4) == 0) return Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_NONE;
144 break;
145 }
146 return Inkscape::Filters::CONVOLVEMATRIX_EDGEMODE_DUPLICATE; //duplicate is default
147 }
149 /**
150 * Sets a specific value in the SPFeConvolveMatrix.
151 */
152 static void
153 sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
154 {
155 SPFeConvolveMatrix *feConvolveMatrix = SP_FECONVOLVEMATRIX(object);
156 (void)feConvolveMatrix;
157 double read_num;
158 int read_int;
159 bool read_bool;
160 Inkscape::Filters::FilterConvolveMatrixEdgeMode read_mode;
162 switch(key) {
163 /*DEAL WITH SETTING ATTRIBUTES HERE*/
164 case SP_ATTR_ORDER:
165 feConvolveMatrix->order.set(value);
166 //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
167 if (feConvolveMatrix->order.optNumIsSet() == false)
168 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
169 if (feConvolveMatrix->targetXIsSet == false) feConvolveMatrix->targetX = (int) floor(feConvolveMatrix->order.getNumber()/2);
170 if (feConvolveMatrix->targetYIsSet == false) feConvolveMatrix->targetY = (int) floor(feConvolveMatrix->order.getOptNumber()/2);
171 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
172 break;
173 case SP_ATTR_KERNELMATRIX:
174 if (value){
175 feConvolveMatrix->kernelMatrixIsSet = true;
176 feConvolveMatrix->kernelMatrix = helperfns_read_vector(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
177 if (! feConvolveMatrix->divisorIsSet) {
178 feConvolveMatrix->divisor = 0;
179 for (unsigned int i = 0; i< feConvolveMatrix->kernelMatrix.size(); i++)
180 feConvolveMatrix->divisor += feConvolveMatrix->kernelMatrix[i];
181 if (feConvolveMatrix->divisor == 0) feConvolveMatrix->divisor = 1;
182 }
183 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
184 } else {
185 g_warning("For feConvolveMatrix you MUST pass a kernelMatrix parameter!");
186 }
187 break;
188 case SP_ATTR_DIVISOR:
189 if (value) {
190 read_num = helperfns_read_number(value);
191 if (read_num == 0) {
192 // This should actually be an error, but given our UI it is more useful to simply set divisor to the default.
193 if (feConvolveMatrix->kernelMatrixIsSet) {
194 for (unsigned int i = 0; i< feConvolveMatrix->kernelMatrix.size(); i++)
195 read_num += feConvolveMatrix->kernelMatrix[i];
196 }
197 if (read_num == 0) read_num = 1;
198 if (feConvolveMatrix->divisorIsSet || feConvolveMatrix->divisor!=read_num) {
199 feConvolveMatrix->divisorIsSet = false;
200 feConvolveMatrix->divisor = read_num;
201 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
202 }
203 } else if (!feConvolveMatrix->divisorIsSet || feConvolveMatrix->divisor!=read_num) {
204 feConvolveMatrix->divisorIsSet = true;
205 feConvolveMatrix->divisor = read_num;
206 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
207 }
208 }
209 break;
210 case SP_ATTR_BIAS:
211 read_num = 0;
212 if (value) read_num = helperfns_read_number(value);
213 if (read_num != feConvolveMatrix->bias){
214 feConvolveMatrix->bias = read_num;
215 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
216 }
217 break;
218 case SP_ATTR_TARGETX:
219 if (value) {
220 read_int = (int) helperfns_read_number(value);
221 if (read_int < 0 || read_int > feConvolveMatrix->order.getNumber()){
222 g_warning("targetX must be a value between 0 and orderX! Assuming floor(orderX/2) as default value.");
223 read_int = (int) floor(feConvolveMatrix->order.getNumber()/2.0);
224 }
225 feConvolveMatrix->targetXIsSet = true;
226 if (read_int != feConvolveMatrix->targetX){
227 feConvolveMatrix->targetX = read_int;
228 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
229 }
230 }
231 break;
232 case SP_ATTR_TARGETY:
233 if (value) {
234 read_int = (int) helperfns_read_number(value);
235 if (read_int < 0 || read_int > feConvolveMatrix->order.getOptNumber()){
236 g_warning("targetY must be a value between 0 and orderY! Assuming floor(orderY/2) as default value.");
237 read_int = (int) floor(feConvolveMatrix->order.getOptNumber()/2.0);
238 }
239 feConvolveMatrix->targetYIsSet = true;
240 if (read_int != feConvolveMatrix->targetY){
241 feConvolveMatrix->targetY = read_int;
242 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
243 }
244 }
245 break;
246 case SP_ATTR_EDGEMODE:
247 read_mode = sp_feConvolveMatrix_read_edgeMode(value);
248 if (read_mode != feConvolveMatrix->edgeMode){
249 feConvolveMatrix->edgeMode = read_mode;
250 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
251 }
252 break;
253 case SP_ATTR_KERNELUNITLENGTH:
254 feConvolveMatrix->kernelUnitLength.set(value);
255 //From SVG spec: If the <dy> value is not specified, it defaults to the same value as <dx>.
256 if (feConvolveMatrix->kernelUnitLength.optNumIsSet() == false)
257 feConvolveMatrix->kernelUnitLength.setOptNumber(feConvolveMatrix->kernelUnitLength.getNumber());
258 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
259 break;
260 case SP_ATTR_PRESERVEALPHA:
261 read_bool = helperfns_read_bool(value, false);
262 if (read_bool != feConvolveMatrix->preserveAlpha){
263 feConvolveMatrix->preserveAlpha = read_bool;
264 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
265 }
266 break;
267 default:
268 if (((SPObjectClass *) feConvolveMatrix_parent_class)->set)
269 ((SPObjectClass *) feConvolveMatrix_parent_class)->set(object, key, value);
270 break;
271 }
273 }
275 /**
276 * Receives update notifications.
277 */
278 static void
279 sp_feConvolveMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
280 {
281 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
282 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
284 /* do something to trigger redisplay, updates? */
286 }
288 if (((SPObjectClass *) feConvolveMatrix_parent_class)->update) {
289 ((SPObjectClass *) feConvolveMatrix_parent_class)->update(object, ctx, flags);
290 }
291 }
293 /**
294 * Writes its settings to an incoming repr object, if any.
295 */
296 static Inkscape::XML::Node *
297 sp_feConvolveMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
298 {
299 /* TODO: Don't just clone, but create a new repr node and write all
300 * relevant values into it */
301 if (!repr) {
302 repr = SP_OBJECT_REPR(object)->duplicate(doc);
303 }
306 if (((SPObjectClass *) feConvolveMatrix_parent_class)->write) {
307 ((SPObjectClass *) feConvolveMatrix_parent_class)->write(object, doc, repr, flags);
308 }
310 return repr;
311 }
313 static void sp_feConvolveMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
314 g_assert(primitive != NULL);
315 g_assert(filter != NULL);
317 SPFeConvolveMatrix *sp_convolve = SP_FECONVOLVEMATRIX(primitive);
319 int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_CONVOLVEMATRIX);
320 Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
321 Inkscape::Filters::FilterConvolveMatrix *nr_convolve = dynamic_cast<Inkscape::Filters::FilterConvolveMatrix*>(nr_primitive);
322 g_assert(nr_convolve != NULL);
324 sp_filter_primitive_renderer_common(primitive, nr_primitive);
326 nr_convolve->set_targetX(sp_convolve->targetX);
327 nr_convolve->set_targetY(sp_convolve->targetY);
328 nr_convolve->set_orderX( (int)sp_convolve->order.getNumber() );
329 nr_convolve->set_orderY( (int)sp_convolve->order.getOptNumber() );
330 nr_convolve->set_kernelMatrix(sp_convolve->kernelMatrix);
331 nr_convolve->set_divisor(sp_convolve->divisor);
332 nr_convolve->set_bias(sp_convolve->bias);
333 nr_convolve->set_preserveAlpha(sp_convolve->preserveAlpha);
335 }
336 /*
337 Local Variables:
338 mode:c++
339 c-file-style:"stroustrup"
340 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
341 indent-tabs-mode:nil
342 fill-column:99
343 End:
344 */
345 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :