Code

Makes sure a Gaussian filter is applied to premultiplied data.
[inkscape.git] / src / filters / flood.cpp
1 #define __SP_FEFLOOD_CPP__
3 /** \file
4  * SVG <feFlood> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   hugo Rodrigues <haa.rodrigues@gmail.com>
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 "attributes.h"
21 #include "svg/svg.h"
22 #include "flood.h"
23 #include "xml/repr.h"
24 #include "helper-fns.h"
25 #include "svg/svg-color.h"
27 /* FeFlood base class */
29 static void sp_feFlood_class_init(SPFeFloodClass *klass);
30 static void sp_feFlood_init(SPFeFlood *feFlood);
32 static void sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
33 static void sp_feFlood_release(SPObject *object);
34 static void sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value);
35 static void sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags);
36 static Inkscape::XML::Node *sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
37 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter);
39 static SPFilterPrimitiveClass *feFlood_parent_class;
41 GType
42 sp_feFlood_get_type()
43 {
44     static GType feFlood_type = 0;
46     if (!feFlood_type) {
47         GTypeInfo feFlood_info = {
48             sizeof(SPFeFloodClass),
49             NULL, NULL,
50             (GClassInitFunc) sp_feFlood_class_init,
51             NULL, NULL,
52             sizeof(SPFeFlood),
53             16,
54             (GInstanceInitFunc) sp_feFlood_init,
55             NULL,    /* value_table */
56         };
57         feFlood_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeFlood", &feFlood_info, (GTypeFlags)0);
58     }
59     return feFlood_type;
60 }
62 static void
63 sp_feFlood_class_init(SPFeFloodClass *klass)
64 {
65     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
66     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
68     feFlood_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
70     sp_object_class->build = sp_feFlood_build;
71     sp_object_class->release = sp_feFlood_release;
72     sp_object_class->write = sp_feFlood_write;
73     sp_object_class->set = sp_feFlood_set;
74     sp_object_class->update = sp_feFlood_update;
75     sp_primitive_class->build_renderer = sp_feFlood_build_renderer;
76 }
78 static void
79 sp_feFlood_init(SPFeFlood *feFlood)
80 {
81     feFlood->opacity = 1;
82 }
84 /**
85  * Reads the Inkscape::XML::Node, and initializes SPFeFlood variables.  For this to get called,
86  * our name must be associated with a repr via "sp_object_type_register".  Best done through
87  * sp-object-repr.cpp's repr_name_entries array.
88  */
89 static void
90 sp_feFlood_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
91 {
92     if (((SPObjectClass *) feFlood_parent_class)->build) {
93         ((SPObjectClass *) feFlood_parent_class)->build(object, document, repr);
94     }
96     /*LOAD ATTRIBUTES FROM REPR HERE*/
97     sp_object_read_attr(object, "flood-opacity");
98     sp_object_read_attr(object, "flood-color");
99 }
101 /**
102  * Drops any allocated memory.
103  */
104 static void
105 sp_feFlood_release(SPObject *object)
107     if (((SPObjectClass *) feFlood_parent_class)->release)
108         ((SPObjectClass *) feFlood_parent_class)->release(object);
111 /**
112  * Sets a specific value in the SPFeFlood.
113  */
114 static void
115 sp_feFlood_set(SPObject *object, unsigned int key, gchar const *value)
117     SPFeFlood *feFlood = SP_FEFLOOD(object);
118     (void)feFlood;
119     gchar const *cend_ptr = NULL;
120     gchar *end_ptr = NULL;
121     guint32 read_color;
122     double read_num;
123     
124     switch(key) {
125         /*DEAL WITH SETTING ATTRIBUTES HERE*/
126         case SP_PROP_FLOOD_COLOR:
127             cend_ptr = NULL;
128             read_color = sp_svg_read_color(value, &cend_ptr, 0xffffffff);
129             if (cend_ptr && read_color != feFlood->color){
130                 feFlood->color = read_color;
131                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
132             }
133             break;
134         case SP_PROP_FLOOD_OPACITY:
135             if (value) {
136                 read_num = g_ascii_strtod(value, &end_ptr);
137                 if (*end_ptr) {
138                     g_warning("Unable to convert \"%s\" to number", value);
139                     read_num = 1;
140                 }
141             }
142             else {
143                 read_num = 1;
144             }
145             if (read_num != feFlood->opacity){
146                 feFlood->opacity = read_num;
147                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
148             }
149             break;
150         default:
151             if (((SPObjectClass *) feFlood_parent_class)->set)
152                 ((SPObjectClass *) feFlood_parent_class)->set(object, key, value);
153             break;
154     }
158 /**
159  * Receives update notifications.
160  */
161 static void
162 sp_feFlood_update(SPObject *object, SPCtx *ctx, guint flags)
164     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
165                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
167         /* do something to trigger redisplay, updates? */
169     }
171     if (((SPObjectClass *) feFlood_parent_class)->update) {
172         ((SPObjectClass *) feFlood_parent_class)->update(object, ctx, flags);
173     }
176 /**
177  * Writes its settings to an incoming repr object, if any.
178  */
179 static Inkscape::XML::Node *
180 sp_feFlood_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
182     // Inkscape-only object, not copied during an "plain SVG" dump:
183     if (flags & SP_OBJECT_WRITE_EXT) {
184         if (repr) {
185             // is this sane?
186             //repr->mergeFrom(SP_OBJECT_REPR(object), "id");
187         } else {
188             repr = SP_OBJECT_REPR(object)->duplicate(doc);
189         }
190     }
192     if (((SPObjectClass *) feFlood_parent_class)->write) {
193         ((SPObjectClass *) feFlood_parent_class)->write(object, doc, repr, flags);
194     }
196     return repr;
199 static void sp_feFlood_build_renderer(SPFilterPrimitive *primitive, NR::Filter *filter) {
200     g_assert(primitive != NULL);
201     g_assert(filter != NULL);
203     SPFeFlood *sp_flood = SP_FEFLOOD(primitive);
204     (void)sp_flood;
206     int primitive_n = filter->add_primitive(NR::NR_FILTER_FLOOD);
207     NR::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
208     NR::FilterFlood *nr_flood = dynamic_cast<NR::FilterFlood*>(nr_primitive);
209     g_assert(nr_flood != NULL);
211     sp_filter_primitive_renderer_common(primitive, nr_primitive);
212     
213     nr_flood->set_opacity(sp_flood->opacity);
214     nr_flood->set_color(sp_flood->color);
218 /*
219   Local Variables:
220   mode:c++
221   c-file-style:"stroustrup"
222   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223   indent-tabs-mode:nil
224   fill-column:99
225   End:
226 */
227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :