Code

Add CMakeLists for the filters directory.
[inkscape.git] / src / sp-gaussian-blur.cpp
1 #define __SP_GAUSSIANBLUR_CPP__
3 /** \file
4  * SVG <gaussianBlur> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   Hugo Rodrigues <haa.rodrigues@gmail.com>
10  *   Niko Kiirala <niko@kiirala.com>
11  *
12  * Copyright (C) 2006,2007 Authors
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 "attributes.h"
22 #include "svg/svg.h"
23 #include "sp-gaussian-blur.h"
24 #include "xml/repr.h"
26 #include "display/nr-filter.h"
27 #include "display/nr-filter-primitive.h"
28 #include "display/nr-filter-gaussian.h"
29 #include "display/nr-filter-types.h"
31 //#define SP_MACROS_SILENT
32 //#include "macros.h"
34 /* GaussianBlur base class */
36 static void sp_gaussianBlur_class_init(SPGaussianBlurClass *klass);
37 static void sp_gaussianBlur_init(SPGaussianBlur *gaussianBlur);
39 static void sp_gaussianBlur_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
40 static void sp_gaussianBlur_release(SPObject *object);
41 static void sp_gaussianBlur_set(SPObject *object, unsigned int key, gchar const *value);
42 static void sp_gaussianBlur_update(SPObject *object, SPCtx *ctx, guint flags);
43 static Inkscape::XML::Node *sp_gaussianBlur_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
44 static void sp_gaussianBlur_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
46 static SPFilterPrimitiveClass *gaussianBlur_parent_class;
48 GType
49 sp_gaussianBlur_get_type()
50 {
51     static GType gaussianBlur_type = 0;
53     if (!gaussianBlur_type) {
54         GTypeInfo gaussianBlur_info = {
55             sizeof(SPGaussianBlurClass),
56             NULL, NULL,
57             (GClassInitFunc) sp_gaussianBlur_class_init,
58             NULL, NULL,
59             sizeof(SPGaussianBlur),
60             16,
61             (GInstanceInitFunc) sp_gaussianBlur_init,
62             NULL,    /* value_table */
63         };
64         gaussianBlur_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPGaussianBlur", &gaussianBlur_info, (GTypeFlags)0);
65     }
66     return gaussianBlur_type;
67 }
69 static void
70 sp_gaussianBlur_class_init(SPGaussianBlurClass *klass)
71 {
72     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
73     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
75     gaussianBlur_parent_class = (SPFilterPrimitiveClass *)g_type_class_peek_parent(klass);
77     sp_object_class->build = sp_gaussianBlur_build;
78     sp_object_class->release = sp_gaussianBlur_release;
79     sp_object_class->write = sp_gaussianBlur_write;
80     sp_object_class->set = sp_gaussianBlur_set;
81     sp_object_class->update = sp_gaussianBlur_update;
83     sp_primitive_class->build_renderer = sp_gaussianBlur_build_renderer;
84 }
86 static void
87 sp_gaussianBlur_init(SPGaussianBlur */*gaussianBlur*/)
88 {
89 }
91 /**
92  * Reads the Inkscape::XML::Node, and initializes SPGaussianBlur variables.  For this to get called,
93  * our name must be associated with a repr via "sp_object_type_register".  Best done through
94  * sp-object-repr.cpp's repr_name_entries array.
95  */
96 static void
97 sp_gaussianBlur_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
98 {
99     if (((SPObjectClass *) gaussianBlur_parent_class)->build) {
100         ((SPObjectClass *) gaussianBlur_parent_class)->build(object, document, repr);
101     }
103     sp_object_read_attr(object, "stdDeviation");
107 /**
108  * Drops any allocated memory.
109  */
110 static void
111 sp_gaussianBlur_release(SPObject *object)
114     if (((SPObjectClass *) gaussianBlur_parent_class)->release)
115         ((SPObjectClass *) gaussianBlur_parent_class)->release(object);
118 /**
119  * Sets a specific value in the SPGaussianBlur.
120  */
121 static void
122 sp_gaussianBlur_set(SPObject *object, unsigned int key, gchar const *value)
124     SPGaussianBlur *gaussianBlur = SP_GAUSSIANBLUR(object);
126     switch(key) {
127         case SP_ATTR_STDDEVIATION:
128             gaussianBlur->stdDeviation.set(value);
129             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
130             break;
131         default:
132             if (((SPObjectClass *) gaussianBlur_parent_class)->set)
133                 ((SPObjectClass *) gaussianBlur_parent_class)->set(object, key, value);
134             break;
135     }
139 /**
140  * Receives update notifications.
141  */
142 static void
143 sp_gaussianBlur_update(SPObject *object, SPCtx *ctx, guint flags)
145     if (flags & SP_OBJECT_MODIFIED_FLAG) {
146         sp_object_read_attr(object, "stdDeviation");
147     }
149     if (((SPObjectClass *) gaussianBlur_parent_class)->update) {
150         ((SPObjectClass *) gaussianBlur_parent_class)->update(object, ctx, flags);
151     }
154 /**
155  * Writes its settings to an incoming repr object, if any.
156  */
157 static Inkscape::XML::Node *
158 sp_gaussianBlur_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
161     // Inkscape-only object, not copied during an "plain SVG" dump:
162     if (flags & SP_OBJECT_WRITE_EXT) {
163         if (repr) {
164             // is this sane?
165             // repr->mergeFrom(SP_OBJECT_REPR(object), "id");
166         } else {
167             repr = SP_OBJECT_REPR(object)->duplicate(doc); // FIXME
168         }
169     }
171     if (((SPObjectClass *) gaussianBlur_parent_class)->write) {
172         ((SPObjectClass *) gaussianBlur_parent_class)->write(object, doc, repr, flags);
173     }
175     return repr;
179 void  sp_gaussianBlur_setDeviation(SPGaussianBlur *blur, float num)
181     blur->stdDeviation.setNumber(num);
183 void  sp_gaussianBlur_setDeviation(SPGaussianBlur *blur, float num, float optnum)
185     blur->stdDeviation.setNumber(num);
186     blur->stdDeviation.setOptNumber(optnum);
189 static void sp_gaussianBlur_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
190     SPGaussianBlur *sp_blur = SP_GAUSSIANBLUR(primitive);
192     int handle = filter->add_primitive(Inkscape::Filters::NR_FILTER_GAUSSIANBLUR);
193     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(handle);
194     Inkscape::Filters::FilterGaussian *nr_blur = dynamic_cast<Inkscape::Filters::FilterGaussian*>(nr_primitive);
196     sp_filter_primitive_renderer_common(primitive, nr_primitive);
198     gfloat num = sp_blur->stdDeviation.getNumber();
199     if (num >= 0.0) {
200         gfloat optnum = sp_blur->stdDeviation.getOptNumber();
201         if(optnum >= 0.0)
202             nr_blur->set_deviation((double) num, (double) optnum);
203         else
204             nr_blur->set_deviation((double) num);
205     }
208 /*
209   Local Variables:
210   mode:c++
211   c-file-style:"stroustrup"
212   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
213   indent-tabs-mode:nil
214   fill-column:99
215   End:
216 */
217 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :