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 struct NRArenaItem;
27 namespace Inkscape {
28 namespace Filters {
30 class Filter : public Inkscape::GC::Managed<> {
31 public:
32 int render(NRArenaItem const *item, NRPixBlock *pb);
34 /**
35 * Creates a new filter primitive under this filter object.
36 * New primitive is placed so that it will be executed after all filter
37 * primitives defined beforehand for this filter object.
38 * Should this filter not have enough space for a new primitive, the filter
39 * is enlarged to accomodate the new filter element. It may be enlarged by
40 * more that one element.
41 * Returns a handle (non-negative integer) to the filter primitive created.
42 * Returns -1, if type is not valid filter primitive type or filter
43 * primitive of such type cannot be created.
44 */
45 int add_primitive(FilterPrimitiveType type);
46 /**
47 * Removes all filter primitives from this filter.
48 * All pointers to filter primitives inside this filter should be
49 * considered invalid after calling this function.
50 */
51 void clear_primitives();
52 /**
53 * Replaces filter primitive pointed by 'target' with a new filter
54 * primitive of type 'type'
55 * If 'target' does not correspond to any primitive inside this filter OR
56 * 'type' is not a valid filter primitive type OR
57 * filter primitive of such type cannot be created,
58 * this function returns -1 and doesn't change the internal state of this
59 * filter.
60 * Otherwise, a new filter primitive is created. Any pointers to filter
61 * primitive 'target' should be considered invalid. A handle to the
62 * newly created primitive is returned.
63 */
64 int replace_primitive(int primitive, FilterPrimitiveType type);
66 /**
67 * Returns a pointer to the primitive, which the handle corrensponds to.
68 * If the handle is not valid, returns NULL.
69 */
70 FilterPrimitive *get_primitive(int handle);
72 /**
73 * Sets the slot number 'slot' to be used as result from this filter.
74 * If output is not set, the output from last filter primitive is used as
75 * output from the filter.
76 * It is an error to specify a pre-defined slot as 'slot'. Such call does
77 * not have any effect to the state of filter or its primitives.
78 */
79 void set_output(int slot);
81 void set_x(SVGLength const &length);
82 void set_y(SVGLength const &length);
83 void set_width(SVGLength const &length);
84 void set_height(SVGLength const &length);
86 /**
87 * Sets the filter effects region.
88 * Passing an unset length (length._set == false) as any of the parameters
89 * results in that parameter not being changed.
90 * Filter will not hold any references to the passed SVGLength object after
91 * function returns.
92 * If any of these parameters does not get set, the default value, as
93 * defined in SVG standard, for that parameter is used instead.
94 */
95 void set_region(SVGLength &x, SVGLength &y,
96 SVGLength &width, SVGLength &height);
98 /**
99 * Resets the filter effects region to its default value as defined
100 * in SVG standard.
101 */
102 void reset_region();
104 /**
105 * Sets the width of intermediate images in pixels. If not set, suitable
106 * resolution is determined automatically. If x_pixels is less than zero,
107 * calling this function results in no changes to filter state.
108 */
109 void set_resolution(double const x_pixels);
111 /**
112 * Sets the width and height of intermediate images in pixels. If not set,
113 * suitable resolution is determined automatically. If either parameter is
114 * less than zero, calling this function results in no changes to filter
115 * state.
116 */
117 void set_resolution(double const x_pixels, double const y_pixels);
119 /**
120 * Resets the filter resolution to its default value, i.e. automatically
121 * determined.
122 */
123 void reset_resolution();
125 /**
126 * Set the filterUnits-property. If not set, the default value of
127 * objectBoundingBox is used. If the parameter value is not a
128 * valid enumeration value from SPFilterUnits, no changes to filter state
129 * are made.
130 */
131 void set_filter_units(SPFilterUnits unit);
133 /**
134 * Set the primitiveUnits-properterty. If not set, the default value of
135 * userSpaceOnUse is used. If the parameter value is not a valid
136 * enumeration value from SPFilterUnits, no changes to filter state
137 * are made.
138 */
139 void set_primitive_units(SPFilterUnits unit);
141 /**
142 * Modifies the given area to accommodate for filters needing pixels
143 * outside the rendered area.
144 * When this function returns, area contains the area that needs
145 * to be rendered so that after filtering, the original area is
146 * drawn correctly.
147 */
148 void area_enlarge(NRRectL &area, NRArenaItem const *item) const;
149 /**
150 * Given an object bounding box, this function enlarges it so that
151 * it contains the filter effect area.
152 */
153 void bbox_enlarge(NRRectL &bbox);
154 /**
155 * Returns the filter effects area in user coordinate system.
156 * The given bounding box should be a bounding box as specified in
157 * SVG standard and in user coordinate system.
158 */
159 Geom::Rect filter_effect_area(Geom::Rect const &bbox);
161 /** Creates a new filter with space for one filter element */
162 Filter();
163 /**
164 * Creates a new filter with space for n filter elements. If number of
165 * filter elements is known beforehand, it's better to use this
166 * constructor.
167 */
168 Filter(int n);
169 /** Destroys the filter and all its primitives */
170 virtual ~Filter();
172 private:
173 int _primitive_count;
174 int _primitive_table_size;
176 /** Amount of image slots used, when this filter was rendered last time */
177 int _slot_count;
179 /** Image slot, from which filter output should be read.
180 * Negative values mean 'not set' */
181 int _output_slot;
183 SVGLength _region_x;
184 SVGLength _region_y;
185 SVGLength _region_width;
186 SVGLength _region_height;
188 /* x- and y-resolutions for filter rendering.
189 * Negative values mean 'not set'.
190 * If _y_pixels is set, _x_pixels should be set, too. */
191 double _x_pixels;
192 double _y_pixels;
194 SPFilterUnits _filter_units;
195 SPFilterUnits _primitive_units;
197 FilterPrimitive ** _primitive;
199 void _create_constructor_table();
200 void _enlarge_primitive_table();
201 void _common_init();
202 int _resolution_limit(FilterQuality const quality) const;
203 std::pair<double,double> _filter_resolution(Geom::Rect const &area,
204 Geom::Matrix const &trans,
205 FilterQuality const q) const;
206 };
209 } /* namespace Filters */
210 } /* namespace Inkscape */
213 #endif /* __NR_FILTER_H__ */
214 /*
215 Local Variables:
216 mode:c++
217 c-file-style:"stroustrup"
218 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
219 indent-tabs-mode:nil
220 fill-column:99
221 End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :