Code

A simple layout document as to what, why and how is cppification.
[inkscape.git] / src / filters / colormatrix.cpp
1 #define __SP_FECOLORMATRIX_CPP__
3 /** \file
4  * SVG <feColorMatrix> implementation.
5  *
6  */
7 /*
8  * Authors:
9  *   Felipe Sanches <juca@members.fsf.org>
10  *   hugo Rodrigues <haa.rodrigues@gmail.com>
11  *
12  * Copyright (C) 2007 Felipe C. da S. Sanches
13  * Copyright (C) 2006 Hugo Rodrigues
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #include <string.h>
24 #include "attributes.h"
25 #include "svg/svg.h"
26 #include "colormatrix.h"
27 #include "xml/repr.h"
28 #include "helper-fns.h"
30 #include "display/nr-filter-colormatrix.h"
32 /* FeColorMatrix base class */
34 static void sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass);
35 static void sp_feColorMatrix_init(SPFeColorMatrix *feColorMatrix);
37 static void sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
38 static void sp_feColorMatrix_release(SPObject *object);
39 static void sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *value);
40 static void sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags);
41 static Inkscape::XML::Node *sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
42 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter);
44 static SPFilterPrimitiveClass *feColorMatrix_parent_class;
46 GType
47 sp_feColorMatrix_get_type()
48 {
49     static GType feColorMatrix_type = 0;
51     if (!feColorMatrix_type) {
52         GTypeInfo feColorMatrix_info = {
53             sizeof(SPFeColorMatrixClass),
54             NULL, NULL,
55             (GClassInitFunc) sp_feColorMatrix_class_init,
56             NULL, NULL,
57             sizeof(SPFeColorMatrix),
58             16,
59             (GInstanceInitFunc) sp_feColorMatrix_init,
60             NULL,    /* value_table */
61         };
62         feColorMatrix_type = g_type_register_static(SP_TYPE_FILTER_PRIMITIVE, "SPFeColorMatrix", &feColorMatrix_info, (GTypeFlags)0);
63     }
64     return feColorMatrix_type;
65 }
67 static void
68 sp_feColorMatrix_class_init(SPFeColorMatrixClass *klass)
69 {
70     SPObjectClass *sp_object_class = (SPObjectClass *)klass;
71     SPFilterPrimitiveClass *sp_primitive_class = (SPFilterPrimitiveClass *)klass;
73     feColorMatrix_parent_class = (SPFilterPrimitiveClass*)g_type_class_peek_parent(klass);
75     sp_object_class->build = sp_feColorMatrix_build;
76     sp_object_class->release = sp_feColorMatrix_release;
77     sp_object_class->write = sp_feColorMatrix_write;
78     sp_object_class->set = sp_feColorMatrix_set;
79     sp_object_class->update = sp_feColorMatrix_update;
80     sp_primitive_class->build_renderer = sp_feColorMatrix_build_renderer;
81 }
83 static void
84 sp_feColorMatrix_init(SPFeColorMatrix */*feColorMatrix*/)
85 {
86 }
88 /**
89  * Reads the Inkscape::XML::Node, and initializes SPFeColorMatrix variables.  For this to get called,
90  * our name must be associated with a repr via "sp_object_type_register".  Best done through
91  * sp-object-repr.cpp's repr_name_entries array.
92  */
93 static void
94 sp_feColorMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
95 {
96     if (((SPObjectClass *) feColorMatrix_parent_class)->build) {
97         ((SPObjectClass *) feColorMatrix_parent_class)->build(object, document, repr);
98     }
100     /*LOAD ATTRIBUTES FROM REPR HERE*/
101     object->readAttr( "type");
102     object->readAttr( "values");
105 /**
106  * Drops any allocated memory.
107  */
108 static void
109 sp_feColorMatrix_release(SPObject *object)
111     if (((SPObjectClass *) feColorMatrix_parent_class)->release)
112         ((SPObjectClass *) feColorMatrix_parent_class)->release(object);
115 static Inkscape::Filters::FilterColorMatrixType sp_feColorMatrix_read_type(gchar const *value){
116     if (!value) return Inkscape::Filters::COLORMATRIX_MATRIX; //matrix is default
117     switch(value[0]){
118         case 'm':
119             if (strcmp(value, "matrix") == 0) return Inkscape::Filters::COLORMATRIX_MATRIX;
120             break;
121         case 's':
122             if (strcmp(value, "saturate") == 0) return Inkscape::Filters::COLORMATRIX_SATURATE;
123             break;
124         case 'h':
125             if (strcmp(value, "hueRotate") == 0) return Inkscape::Filters::COLORMATRIX_HUEROTATE;
126             break;
127         case 'l':
128             if (strcmp(value, "luminanceToAlpha") == 0) return Inkscape::Filters::COLORMATRIX_LUMINANCETOALPHA;
129             break;
130     }
131     return Inkscape::Filters::COLORMATRIX_MATRIX; //matrix is default
134 /**
135  * Sets a specific value in the SPFeColorMatrix.
136  */
137 static void
138 sp_feColorMatrix_set(SPObject *object, unsigned int key, gchar const *str)
140     SPFeColorMatrix *feColorMatrix = SP_FECOLORMATRIX(object);
141     (void)feColorMatrix;
143     Inkscape::Filters::FilterColorMatrixType read_type;
144         /*DEAL WITH SETTING ATTRIBUTES HERE*/
145     switch(key) {
146         case SP_ATTR_TYPE:
147             read_type = sp_feColorMatrix_read_type(str);
148             if (feColorMatrix->type != read_type){
149                 feColorMatrix->type = read_type;
150                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
151             }
152             break;
153         case SP_ATTR_VALUES:
154             if (str){
155                 feColorMatrix->values = helperfns_read_vector(str, 20);
156                 feColorMatrix->value = helperfns_read_number(str, HELPERFNS_NO_WARNING);
157                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
158             }
159             break;
160         default:
161             if (((SPObjectClass *) feColorMatrix_parent_class)->set)
162                 ((SPObjectClass *) feColorMatrix_parent_class)->set(object, key, str);
163             break;
164     }
167 /**
168  * Receives update notifications.
169  */
170 static void
171 sp_feColorMatrix_update(SPObject *object, SPCtx *ctx, guint flags)
173     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
174                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
176         /* do something to trigger redisplay, updates? */
178     }
180     if (((SPObjectClass *) feColorMatrix_parent_class)->update) {
181         ((SPObjectClass *) feColorMatrix_parent_class)->update(object, ctx, flags);
182     }
185 /**
186  * Writes its settings to an incoming repr object, if any.
187  */
188 static Inkscape::XML::Node *
189 sp_feColorMatrix_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
191     /* TODO: Don't just clone, but create a new repr node and write all
192      * relevant values into it */
193     if (!repr) {
194         repr = SP_OBJECT_REPR(object)->duplicate(doc);
195     }
197     if (((SPObjectClass *) feColorMatrix_parent_class)->write) {
198         ((SPObjectClass *) feColorMatrix_parent_class)->write(object, doc, repr, flags);
199     }
201     return repr;
204 static void sp_feColorMatrix_build_renderer(SPFilterPrimitive *primitive, Inkscape::Filters::Filter *filter) {
205     g_assert(primitive != NULL);
206     g_assert(filter != NULL);
208     SPFeColorMatrix *sp_colormatrix = SP_FECOLORMATRIX(primitive);
210     int primitive_n = filter->add_primitive(Inkscape::Filters::NR_FILTER_COLORMATRIX);
211     Inkscape::Filters::FilterPrimitive *nr_primitive = filter->get_primitive(primitive_n);
212     Inkscape::Filters::FilterColorMatrix *nr_colormatrix = dynamic_cast<Inkscape::Filters::FilterColorMatrix*>(nr_primitive);
213     g_assert(nr_colormatrix != NULL);
215     sp_filter_primitive_renderer_common(primitive, nr_primitive);
216     nr_colormatrix->set_type(sp_colormatrix->type);
217     nr_colormatrix->set_value(sp_colormatrix->value);
218     nr_colormatrix->set_values(sp_colormatrix->values);
221 /*
222   Local Variables:
223   mode:c++
224   c-file-style:"stroustrup"
225   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
226   indent-tabs-mode:nil
227   fill-column:99
228   End:
229 */
230 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :