Code

Work on filters. spFilterPrimitive structure added. Blur slider updated. Fixed sp...
[inkscape.git] / src / display / nr-filter.cpp
1 #define __NR_FILTER_CPP__
3 /*
4  * SVG filters rendering
5  *
6  * Author:
7  *   Niko Kiirala <niko@kiirala.com>
8  *
9  * Copyright (C) 2006 Niko Kiirala
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include <glib.h>
16 #include "display/nr-filter.h"
17 #include "display/nr-filter-primitive.h"
18 #include "display/nr-filter-gaussian.h"
19 #include "display/nr-filter-slot.h"
20 #include "display/nr-filter-types.h"
21 #include "display/pixblock-scaler.h"
23 #include "display/nr-arena-item.h"
24 #include "libnr/nr-pixblock.h"
25 #include "libnr/nr-blit.h"
26 #include "libnr/nr-matrix.h"
27 #include "libnr/nr-scale.h"
28 #include "svg/svg-length.h"
29 #include "sp-filter-units.h"
31 //#include "display/nr-arena-shape.h"
33 namespace NR {
35 Filter::Filter()
36 {
37     _primitive_count = 0;
38     _primitive_table_size = 1;
39     _primitive = new FilterPrimitive*[1];
40         _primitive[0] = NULL;
41     //_primitive_count = 1;
42     //_primitive[0] = new FilterGaussian;
43     _common_init();
44 }
46 Filter::Filter(int n)
47 {
48     _primitive_count = 0;
49     _primitive_table_size = n;
50     _primitive = new FilterPrimitive*[n];
51     for ( int i = 0 ; i < n ; i++ ) {
52         _primitive[i] = NULL;
53     }
54     _common_init();
55 }
57 void Filter::_common_init() {
58     _slot_count = 1;
59     // Having "not set" here as value means the output of last filter
60     // primitive will be used as output of this filter
61     _output_slot = NR_FILTER_SLOT_NOT_SET;
63     // These are the default values for filter region,
64     // as specified in SVG standard
65     // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
66     _region_x.set(SVGLength::PERCENT, -.10, 0);
67     _region_y.set(SVGLength::PERCENT, -.10, 0);
68     _region_width.set(SVGLength::PERCENT, 1.20, 0);
69     _region_height.set(SVGLength::PERCENT, 1.20, 0);
71     // Filter resolution, negative value here stands for "automatic"
72     _x_pixels = -1.0;
73     _y_pixels = -1.0;
75     _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
76     _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
77 }
79 Filter::~Filter()
80 {
81     clear_primitives();
82     delete[] _primitive;
83 }
86 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
87 {
88     Matrix trans = *item->ctm;
89     FilterSlot slot(_slot_count, item);
90     NRPixBlock *in = new NRPixBlock;
92     // First, if filter resolution is not set to automatic, we should
93     // scale the input image to correct resolution
94     if (_x_pixels >= 0) {
95         /* If filter resolution is zero, the object should not be rendered */
96         if (_x_pixels == 0 || _y_pixels == 0) {
97             int size = (pb->area.x1 - pb->area.x0)
98                 * (pb->area.y1 - pb->area.y0)
99                 * NR_PIXBLOCK_BPP(pb);
100             memset(NR_PIXBLOCK_PX(pb), 0, size);
101             return 0;
102         }
103         // Resolution is specified as pixel length of our internal buffer.
104         // Though, we might not be rendering the whole object at time,
105         // so we need to calculate the correct pixel size
106         int x_len = (int)round(((pb->area.x1 - pb->area.x0) * _x_pixels) / (item->bbox.x1 - item->bbox.x0));
107         if (x_len < 1) x_len = 1;
108         // If y-resolution is also set, count y-area in the same way as x-area
109         // Otherwise, make y-area so, that aspect ratio of input pixblock and
110         // internal pixblock are the same.
111         int y_len;
112         if (_y_pixels > 0) {
113             y_len = (int)round(((pb->area.y1 - pb->area.y0) * _y_pixels) / (item->bbox.y1 - item->bbox.y0));
114         } else {
115             y_len = (int)round((x_len * (pb->area.y1 - pb->area.y0)) / (double)(pb->area.x1 - pb->area.x0));
116         }
117         if (y_len < 1) y_len = 1;
118         nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true);
119         scale_bicubic(in, pb);
120         scale res_scaling(x_len / (double)(pb->area.x1 - pb->area.x0),
121                           y_len / (double)(pb->area.y1 - pb->area.y0));
122         trans *= res_scaling;
123     } else {
124         // If filter resolution is automatic, just make copy of input image
125         nr_pixblock_setup_fast(in, pb->mode,
126                                pb->area.x0, pb->area.y0,
127                                pb->area.x1, pb->area.y1, true);
128         nr_blit_pixblock_pixblock(in, pb);
129     }
130     slot.set(NR_FILTER_SOURCEGRAPHIC, in);
131     in = NULL; // in is now handled by FilterSlot, we should not touch it
133     // TODO: loop through the primitives and render them one at a time
134     _primitive[0]->render(slot, trans);
136     NRPixBlock *out = slot.get(_output_slot);
138     // Clear the pixblock, where the output will be put
139     // -> the original image does not show through
140     int size = (pb->area.x1 - pb->area.x0)
141         * (pb->area.y1 - pb->area.y0)
142         * NR_PIXBLOCK_BPP(pb);
143     memset(NR_PIXBLOCK_PX(pb), 0, size);
145     // If the filter resolution is automatic, just copy our final image
146     // to output pixblock, otherwise use bicubic scaling
147     if (_x_pixels < 0) {
148         nr_blit_pixblock_pixblock(pb, out);
149     } else {
150         scale_bicubic(pb, out);
151     }
153     // Take note of the amount of used image slots
154     // -> next time this filter is rendered, we can reserve enough slots
155     // immediately
156     _slot_count = slot.get_slot_count();
157     return 0;
160 int Filter::get_enlarge(Matrix const &m)
162     // Just sum the enlargement factor of all filter elements.
163     // TODO: this both sucks and blows for filters like feOffset
164     // -> ditch this method and design a better one...
165     int enlarge = 0;
166     for ( int i = 0 ; i < _primitive_count ; i++ ) {
167         if(_primitive[i]) enlarge += _primitive[i]->get_enlarge(m);
168     }
169     return enlarge;
172 void Filter::bbox_enlarge(NRRectL &bbox)
174     int len_x = bbox.x1 - bbox.x0;
175     int len_y = bbox.y1 - bbox.y0;
176     /* TODO: fetch somehow the object ex and em lengths */
177     _region_x.update(12, 6, len_x);
178     _region_y.update(12, 6, len_y);
179     _region_width.update(12, 6, len_x);
180     _region_height.update(12, 6, len_y);
181     if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
182         if (_region_x.unit == SVGLength::PERCENT) {
183             bbox.x0 += (ICoord)_region_x.computed;
184         } else {
185             bbox.x0 += (ICoord)(_region_x.computed * len_x);
186         }
187         if (_region_width.unit == SVGLength::PERCENT) {
188             bbox.x1 = bbox.x0 + (ICoord)_region_width.computed;
189         } else {
190             bbox.x1 = bbox.x0 + (ICoord)(_region_width.computed * len_x);
191         }
193         if (_region_y.unit == SVGLength::PERCENT) {
194             bbox.y0 += (ICoord)_region_y.computed;
195         } else {
196             bbox.y0 += (ICoord)(_region_y.computed * len_y);
197         }
198         if (_region_height.unit == SVGLength::PERCENT) {
199             bbox.y1 = bbox.y0 + (ICoord)_region_height.computed;
200         } else {
201             bbox.y1 = bbox.y0 + (ICoord)(_region_height.computed * len_y);
202         }
203     } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
204         /* TODO: make sure bbox and fe region are in same coordinate system */
205         bbox.x0 = (ICoord) _region_x.computed;
206         bbox.x1 = bbox.x0 + (ICoord) _region_width.computed;
207         bbox.y0 = (ICoord) _region_y.computed;
208         bbox.y1 = bbox.y0 + (ICoord) _region_height.computed;
209     } else {
210         g_warning("Error in NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
211     }
214 /* Constructor table holds pointers to static methods returning filter
215  * primitives. This table is indexed with FilterPrimitiveType, so that
216  * for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
217  * returns a filter object of type NR::FilterGaussian.
218  */
219 typedef FilterPrimitive*(*FilterConstructor)();
220 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
222 void Filter::_create_constructor_table()
224     // Constructor table won't change in run-time, so no need to recreate
225     static bool created = false;
226     if(created) return;
228     /* Filter effects not yet implemented are set to NULL */
229     _constructor[NR_FILTER_BLEND] = NULL;
230     _constructor[NR_FILTER_COLORMATRIX] = NULL;
231     _constructor[NR_FILTER_COMPONENTTRANSFER] = NULL;
232     _constructor[NR_FILTER_COMPOSITE] = NULL;
233     _constructor[NR_FILTER_CONVOLVEMATRIX] = NULL;
234     _constructor[NR_FILTER_DIFFUSELIGHTING] = NULL;
235     _constructor[NR_FILTER_DISPLACEMENTMAP] = NULL;
236     _constructor[NR_FILTER_FLOOD] = NULL;
237     _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
238     _constructor[NR_FILTER_IMAGE] = NULL;
239     _constructor[NR_FILTER_MERGE] = NULL;
240     _constructor[NR_FILTER_MORPHOLOGY] = NULL;
241     _constructor[NR_FILTER_OFFSET] = NULL;
242     _constructor[NR_FILTER_SPECULARLIGHTING] = NULL;
243     _constructor[NR_FILTER_TILE] = NULL;
244     _constructor[NR_FILTER_TURBULENCE] = NULL;
245     created = true;
248 /** Helper method for enlarging table of filter primitives. When new
249  * primitives are added, but we have no space for them, this function
250  * makes some more space.
251  */
252 void Filter::_enlarge_primitive_table() {
253     FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
254     for (int i = 0 ; i < _primitive_count ; i++) {
255         new_tbl[i] = _primitive[i];
256     }
257     _primitive_table_size *= 2;
258     for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
259         new_tbl[i] = NULL;
260     }
261     delete[] _primitive;
262     _primitive = new_tbl;
265 FilterPrimitive *Filter::add_primitive(FilterPrimitiveType type)
267     _create_constructor_table();
269     // Check that we can create a new filter of specified type
270     if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
271         return NULL;
272     if (!_constructor[type]) return NULL;
273     FilterPrimitive *created = _constructor[type]();
275     // If there is no space for new filter primitive, enlarge the table
276     if (_primitive_count >= _primitive_table_size) {
277         _enlarge_primitive_table();
278     }
280     _primitive[_primitive_count] = created;
281     return created;
284 FilterPrimitive *Filter::replace_primitive(FilterPrimitive *target, FilterPrimitiveType type)
286     _create_constructor_table();
288     // Check that target is valid primitive inside this filter
289     int place = -1;
290     for (int i = 0 ; i < _primitive_count ; i++) {
291         if (target == _primitive[i]) {
292             place = i;
293             break;
294         }
295     }
296     if (place < 0) return NULL;
298     // Check that we can create a new filter of specified type
299     if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
300         return NULL;
301     if (!_constructor[type]) return NULL;
302     FilterPrimitive *created = _constructor[type]();
304     // If there is no space for new filter primitive, enlarge the table
305     if (_primitive_count >= _primitive_table_size) {
306         _enlarge_primitive_table();
307     }
309     delete target;
310     _primitive[place] = created;
311     return created;
314 void Filter::clear_primitives()
316     for (int i = 0 ; i < _primitive_count ; i++) {
317         if (_primitive[i]) delete _primitive[i];
318     }
319     _primitive_count = 0;
322 void Filter::set_x(SVGLength &lenght)
323 { /*write me*/ }
324 void Filter::set_y(SVGLength &length)
325 { /*write me*/ }
326 void Filter::set_width(SVGLength &length)
327 { /*write me*/ }
328 void Filter::set_height(SVGLength &length)
329 { /*write me*/ }
331 } /* namespace NR */
333 /*
334   Local Variables:
335   mode:c++
336   c-file-style:"stroustrup"
337   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
338   indent-tabs-mode:nil
339   fill-column:99
340   End:
341 */
342 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :