Code

Added a bunch of comments to filter effects rendering code
[inkscape.git] / src / display / nr-filter.h
1 #ifndef __NR_FILTER_H__
2 #define __NR_FILTER_H__
4 /*
5  * SVG filters rendering
6  *
7  * Author:
8  *   Niko Kiirala <niko@kiirala.com>
9  *
10  * Copyright (C) 2006 Niko Kiirala
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include "display/nr-arena-item.h"
16 #include "display/nr-filter-primitive.h"
17 #include "display/nr-filter-types.h"
18 #include "libnr/nr-pixblock.h"
19 #include "libnr/nr-matrix.h"
20 #include "libnr/nr-rect.h"
21 #include "svg/svg-length.h"
22 #include "sp-filter-units.h"
23 #include "gc-managed.h"
25 namespace NR {
27 class Filter : public Inkscape::GC::Managed<> {
28 public:
29     int render(NRArenaItem const *item, NRPixBlock *pb);
31     /**
32      * Creates a new filter primitive under this filter object.
33      * New primitive is placed so that it will be executed after all filter
34      * primitives defined beforehand for this filter object.
35      * Should this filter not have enough space for a new primitive, the filter
36      * is enlarged to accomodate the new filter element. It may be enlarged by
37      * more that one element.
38      * Returns a pointer to the filter primitive created.
39      * Returns NULL, if type is not valid filter primitive type or filter
40      * primitive of such type cannot be created.
41      */
42     FilterPrimitive *add_primitive(FilterPrimitiveType type);
43     /**
44      * Removes all filter primitives from this filter.
45      * All pointers to filter primitives inside this filter should be
46      * considered invalid after calling this function.
47      */
48     void clear_primitives();
49     /**
50      * Replaces filter primitive pointed by 'target' with a new filter
51      * primitive of type 'type'
52      * If 'target' does not correspond to any primitive inside this filter OR
53      * 'type' is not a valid filter primitive type OR
54      * filter primitive of such type cannot be created,
55      * this function returns NULL and doesn't change the internal state of this
56      * filter.
57      * Otherwise, a new filter primitive is created. Any pointers to filter
58      * primitive 'target' should be considered invalid. A pointer to the
59      * newly created primitive is returned.
60      */
61     FilterPrimitive *replace_primitive(FilterPrimitive *primitive,
62                                        FilterPrimitiveType type);
64     /**
65      * Sets the slot number 'slot' to be used as result from this filter.
66      * If output is not set, the output from last filter primitive is used as
67      * output from the filter.
68      * It is an error to specify a pre-defined slot as 'slot'. Such call does
69      * not have any effect to the state of filter or its primitives.
70      */
71     void set_output(int slot);
73     void set_x(SVGLength &lenght);
74     void set_y(SVGLength &length);
75     void set_width(SVGLength &length);
76     void set_height(SVGLength &length);
78     /**
79      * Sets the filter effects region.
80      * Passing an unset length (length._set == false) as any of the parameters
81      * results in that parameter not being changed.
82      * Filter will not hold any references to the passed SVGLength object after
83      * function returns.
84      * If any of these parameters does not get set, the default value, as
85      * defined in SVG standard, for that parameter is used instead.
86      */
87     void set_region(SVGLength &x, SVGLength &y,
88                     SVGLength &width, SVGLength &height);
90     /**
91      * Resets the filter effects region to its default value as defined
92      * in SVG standard.
93      */
94     void reset_region();
96     /**
97      * Sets the width of intermediate images in pixels. If not set, suitable
98      * resolution is determined automatically. If x_pixels is less than zero,
99      * calling this function results in no changes to filter state.
100      */
101     void set_resolution(double x_pixels);
103     /**
104      * Sets the width and height of intermediate images in pixels. If not set,
105      * suitable resolution is determined automatically. If either parameter is
106      * less than zero, calling this function results in no changes to filter
107      * state.
108      */
109     void set_resolution(double x_pixels, double y_pixels);
111     /**
112      * Resets the filter resolution to its default value, i.e. automatically
113      * determined.
114      */
115     void reset_resolution();
117     /**
118      * Set the filterUnits-property. If not set, the default value of 
119      * objectBoundingBox is used. If the parameter value is not a
120      * valid enumeration value from SPFilterUnits, no changes to filter state
121      * are made.
122      */
123     void set_filter_units(SPFilterUnits unit);
125     /**
126      * Set the primitiveUnits-properterty. If not set, the default value of
127      * userSpaceOnUseis used. If the parameter value is not a valid
128      * enumeration value from SPFilterUnits, no changes to filter state
129      * are made.
130      */
131     void set_primitive_units(SPFilterUnits unit);
133     /** 
134      * Returns the amount of pixels the rendering area should be enlarged
135      * to prevent visual artefacts when filter needs to read pixels that
136      * are outside its output area (e.g. gaussian blur)
137      */
138     int get_enlarge(Matrix const &m);
139     /**
140      * Given an object bounding box, this function enlarges it so that
141      * it contains the filter effect area.
142      */
143     void bbox_enlarge(NRRectL &bbox);
145     /** Creates a new filter with space for one filter element */
146     Filter();
147     /** 
148      * Creates a new filter with space for n filter elements. If number of
149      * filter elements is known beforehand, it's better to use this
150      * constructor.
151      */
152     Filter(int n);
153     /** Destroys the filter and all its primitives */
154     ~Filter();
156 private:
157     int _primitive_count;
158     int _primitive_table_size;
160     /** Amount of image slots used, when this filter was rendered last time */
161     int _slot_count;
163     /** Image slot, from which filter output should be read.
164      * Negative values mean 'not set' */
165     int _output_slot;
167     SVGLength _region_x;
168     SVGLength _region_y;
169     SVGLength _region_width;
170     SVGLength _region_height;
172     /* x- and y-resolutions for filter rendering.
173      * Negative values mean 'not set'.
174      * If _y_pixels is set, _x_pixels should be set, too. */
175     double _x_pixels;
176     double _y_pixels;
178     SPFilterUnits _filter_units;
179     SPFilterUnits _primitive_units;
181     FilterPrimitive ** _primitive;
183     void _create_constructor_table();
184     void _enlarge_primitive_table();
185     void _common_init();
186 };
189 } /* namespace NR */
194 #endif /* __NR_FILTER_H__ */
195 /*
196   Local Variables:
197   mode:c++
198   c-file-style:"stroustrup"
199   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200   indent-tabs-mode:nil
201   fill-column:99
202   End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :