Code

more unreffing temporary styles properly
[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>
15 #include <cmath>
17 #include "display/nr-filter.h"
18 #include "display/nr-filter-primitive.h"
19 #include "display/nr-filter-slot.h"
20 #include "display/nr-filter-types.h"
21 #include "display/pixblock-scaler.h"
22 #include "display/pixblock-transform.h"
23 #include "display/nr-filter-gaussian.h"
24 #include "display/nr-filter-blend.h"
25 #include "display/nr-filter-offset.h"
27 #include "display/nr-arena-item.h"
28 #include "libnr/nr-pixblock.h"
29 #include "libnr/nr-blit.h"
30 #include "libnr/nr-matrix.h"
31 #include "libnr/nr-scale.h"
32 #include "svg/svg-length.h"
33 #include "sp-filter-units.h"
34 #if defined (SOLARIS_2_8)
35 #include "round.h"
36 using Inkscape::round;
37 #endif 
39 __attribute__ ((const))
40 inline static int _max4(const double a, const double b,
41                         const double c, const double d) {
42     double ret = a;
43     if (b > ret) ret = b;
44     if (c > ret) ret = c;
45     if (d > ret) ret = d;
46     return (int)round(ret);
47 }
49 __attribute__ ((const))
50 inline static int _min4(const double a, const double b,
51                         const double c, const double d) {
52     double ret = a;
53     if (b < ret) ret = b;
54     if (c < ret) ret = c;
55     if (d < ret) ret = d;
56     return (int)round(ret);
57 }
59 namespace NR {
61 Filter::Filter()
62 {
63     _primitive_count = 0;
64     _primitive_table_size = 1;
65     _primitive = new FilterPrimitive*[1];
66     _primitive[0] = NULL;
67     //_primitive_count = 1;
68     //_primitive[0] = new FilterGaussian;
69     _common_init();
70 }
72 Filter::Filter(int n)
73 {
74     _primitive_count = 0;
75     _primitive_table_size = n;
76     _primitive = new FilterPrimitive*[n];
77     for ( int i = 0 ; i < n ; i++ ) {
78         _primitive[i] = NULL;
79     }
80     _common_init();
81 }
83 void Filter::_common_init() {
84     _slot_count = 1;
85     // Having "not set" here as value means the output of last filter
86     // primitive will be used as output of this filter
87     _output_slot = NR_FILTER_SLOT_NOT_SET;
89     // These are the default values for filter region,
90     // as specified in SVG standard
91     // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
92     _region_x.set(SVGLength::PERCENT, -.10, 0);
93     _region_y.set(SVGLength::PERCENT, -.10, 0);
94     _region_width.set(SVGLength::PERCENT, 1.20, 0);
95     _region_height.set(SVGLength::PERCENT, 1.20, 0);
97     // Filter resolution, negative value here stands for "automatic"
98     _x_pixels = -1.0;
99     _y_pixels = -1.0;
101     _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
102     _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
105 Filter::~Filter()
107     clear_primitives();
108     delete[] _primitive;
112 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
114     if(!_primitive[0]) { // if there are no primitives, do nothing
115        return 0; 
116     }
118     Matrix trans = *item->ctm;
119     Matrix paraller_trans = trans;
120     bool notparaller = false;
121     FilterSlot slot(_slot_count, item);
122     NRPixBlock *in = new NRPixBlock;
124     // If filter effects region is not paraller to viewport,
125     // we must first undo the rotation / shear.
126     // It will be redone after filtering.
127     // If there is only rotation and uniform scaling (zoom), let's skip this,
128     // as it will not make a difference with gaussian blur.
129     if ((fabs(trans[1]) > 1e-6 || fabs(trans[2]) > 1e-6) &&
130         !(fabs(trans[0] - trans[3]) < 1e-6 && fabs(trans[1] + trans[2]) < 1e-6)) {
131         notparaller = true;
133         // TODO: if filter resolution is specified, scaling should be set
134         // according to that
135         double scaling_factor = sqrt(trans.expansionX() * trans.expansionX() +
136                                      trans.expansionY() * trans.expansionY());
137         scale scaling(scaling_factor, scaling_factor);
138         scale scaling_inv(1.0 / scaling_factor, 1.0 / scaling_factor);
139         trans *= scaling_inv;
140         paraller_trans.set_identity();
141         paraller_trans *= scaling;
143         Matrix itrans = trans.inverse();
144         int x0 = pb->area.x0;
145         int y0 = pb->area.y0;
146         int x1 = pb->area.x1;
147         int y1 = pb->area.y1;
148         int min_x = _min4(itrans[0] * x0 + itrans[2] * y0 + itrans[4],
149                           itrans[0] * x0 + itrans[2] * y1 + itrans[4],
150                           itrans[0] * x1 + itrans[2] * y0 + itrans[4],
151                           itrans[0] * x1 + itrans[2] * y1 + itrans[4]);
152         int max_x = _max4(itrans[0] * x0 + itrans[2] * y0 + itrans[4],
153                           itrans[0] * x0 + itrans[2] * y1 + itrans[4],
154                           itrans[0] * x1 + itrans[2] * y0 + itrans[4],
155                           itrans[0] * x1 + itrans[2] * y1 + itrans[4]);
156         int min_y = _min4(itrans[1] * x0 + itrans[3] * y0 + itrans[5],
157                           itrans[1] * x0 + itrans[3] * y1 + itrans[5],
158                           itrans[1] * x1 + itrans[3] * y0 + itrans[5],
159                           itrans[1] * x1 + itrans[3] * y1 + itrans[5]);
160         int max_y = _max4(itrans[1] * x0 + itrans[3] * y0 + itrans[5],
161                           itrans[1] * x0 + itrans[3] * y1 + itrans[5],
162                           itrans[1] * x1 + itrans[3] * y0 + itrans[5],
163                           itrans[1] * x1 + itrans[3] * y1 + itrans[5]);
164         
165         nr_pixblock_setup_fast(in, pb->mode,
166                                min_x, min_y,
167                                max_x, max_y, true);
168         if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) // memory allocation failed
169             return 0;
170         transform_nearest(in, pb, itrans);
171     } else if (_x_pixels >= 0) {
172         // If filter resolution is not set to automatic, we should
173         // scale the input image to correct resolution
174         /* If filter resolution is zero, the object should not be rendered */
175         if (_x_pixels == 0 || _y_pixels == 0) {
176             int size = (pb->area.x1 - pb->area.x0)
177                 * (pb->area.y1 - pb->area.y0)
178                 * NR_PIXBLOCK_BPP(pb);
179             memset(NR_PIXBLOCK_PX(pb), 0, size);
180             return 0;
181         }
182         // Resolution is specified as pixel length of our internal buffer.
183         // Though, we might not be rendering the whole object at time,
184         // so we need to calculate the correct pixel size
185         int x_len = (int)round(((pb->area.x1 - pb->area.x0) * _x_pixels) / (item->bbox.x1 - item->bbox.x0));
186         if (x_len < 1) x_len = 1;
187         // If y-resolution is also set, count y-area in the same way as x-area
188         // Otherwise, make y-area so, that aspect ratio of input pixblock and
189         // internal pixblock are the same.
190         int y_len;
191         if (_y_pixels > 0) {
192             y_len = (int)round(((pb->area.y1 - pb->area.y0) * _y_pixels) / (item->bbox.y1 - item->bbox.y0));
193         } else {
194             y_len = (int)round((x_len * (pb->area.y1 - pb->area.y0)) / (double)(pb->area.x1 - pb->area.x0));
195         }
196         if (y_len < 1) y_len = 1;
197         nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true);
198         if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) // memory allocation failed
199             return 0;
200         scale_bicubic(in, pb);
201         scale res_scaling(x_len / (double)(pb->area.x1 - pb->area.x0),
202                           y_len / (double)(pb->area.y1 - pb->area.y0));
203         paraller_trans *= res_scaling;
204     } else {
205         // If filter resolution is automatic, just make copy of input image
206         nr_pixblock_setup_fast(in, pb->mode,
207                                pb->area.x0, pb->area.y0,
208                                pb->area.x1, pb->area.y1, true);
209         if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) // memory allocation failed
210             return 0;
211         nr_blit_pixblock_pixblock(in, pb);
212     }
213     in->empty = FALSE;
214     slot.set(NR_FILTER_SOURCEGRAPHIC, in);
215     in = NULL; // in is now handled by FilterSlot, we should not touch it
217     for (int i = 0 ; i < _primitive_count ; i++) {
218         _primitive[i]->render(slot, paraller_trans);
219     }
220     NRPixBlock *out = slot.get(_output_slot);
222     // Clear the pixblock, where the output will be put
223     // -> the original image does not show through
224     int size = (pb->area.x1 - pb->area.x0)
225         * (pb->area.y1 - pb->area.y0)
226         * NR_PIXBLOCK_BPP(pb);
227     memset(NR_PIXBLOCK_PX(pb), 0, size);
229     if (notparaller) {
230         transform_nearest(pb, out, trans);
231     } else if (_x_pixels < 0) {
232         // If the filter resolution is automatic, just copy our final image
233         // to output pixblock, otherwise use bicubic scaling
234         nr_blit_pixblock_pixblock(pb, out);
235     } else {
236         scale_bicubic(pb, out);
237     }
239     // Take note of the amount of used image slots
240     // -> next time this filter is rendered, we can reserve enough slots
241     // immediately
242     _slot_count = slot.get_slot_count();
243     return 0;
246 void Filter::area_enlarge(NRRectL &bbox, Matrix const &m) {
247     for (int i = 0 ; i < _primitive_count ; i++) {
248         if (_primitive[i]) _primitive[i]->area_enlarge(bbox, m);
249     }
252 void Filter::bbox_enlarge(NRRectL &bbox)
254     int len_x = bbox.x1 - bbox.x0;
255     int len_y = bbox.y1 - bbox.y0;
256     /* TODO: fetch somehow the object ex and em lengths */
257     _region_x.update(12, 6, len_x);
258     _region_y.update(12, 6, len_y);
259     _region_width.update(12, 6, len_x);
260     _region_height.update(12, 6, len_y);
261     if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
262         if (_region_x.unit == SVGLength::PERCENT) {
263             bbox.x0 += (ICoord)_region_x.computed;
264         } else {
265             bbox.x0 += (ICoord)(_region_x.computed * len_x);
266         }
267         if (_region_width.unit == SVGLength::PERCENT) {
268             bbox.x1 = bbox.x0 + (ICoord)_region_width.computed;
269         } else {
270             bbox.x1 = bbox.x0 + (ICoord)(_region_width.computed * len_x);
271         }
273         if (_region_y.unit == SVGLength::PERCENT) {
274             bbox.y0 += (ICoord)_region_y.computed;
275         } else {
276             bbox.y0 += (ICoord)(_region_y.computed * len_y);
277         }
278         if (_region_height.unit == SVGLength::PERCENT) {
279             bbox.y1 = bbox.y0 + (ICoord)_region_height.computed;
280         } else {
281             bbox.y1 = bbox.y0 + (ICoord)(_region_height.computed * len_y);
282         }
283     } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
284         /* TODO: make sure bbox and fe region are in same coordinate system */
285         bbox.x0 = (ICoord) _region_x.computed;
286         bbox.x1 = bbox.x0 + (ICoord) _region_width.computed;
287         bbox.y0 = (ICoord) _region_y.computed;
288         bbox.y1 = bbox.y0 + (ICoord) _region_height.computed;
289     } else {
290         g_warning("Error in NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
291     }
294 /* Constructor table holds pointers to static methods returning filter
295  * primitives. This table is indexed with FilterPrimitiveType, so that
296  * for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
297  * returns a filter object of type NR::FilterGaussian.
298  */
299 typedef FilterPrimitive*(*FilterConstructor)();
300 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
302 void Filter::_create_constructor_table()
304     // Constructor table won't change in run-time, so no need to recreate
305     static bool created = false;
306     if(created) return;
308     /* Filter effects not yet implemented are set to NULL */
309     _constructor[NR_FILTER_BLEND] = &FilterBlend::create;
310     _constructor[NR_FILTER_COLORMATRIX] = NULL;
311     _constructor[NR_FILTER_COMPONENTTRANSFER] = NULL;
312     _constructor[NR_FILTER_COMPOSITE] = NULL;
313     _constructor[NR_FILTER_CONVOLVEMATRIX] = NULL;
314     _constructor[NR_FILTER_DIFFUSELIGHTING] = NULL;
315     _constructor[NR_FILTER_DISPLACEMENTMAP] = NULL;
316     _constructor[NR_FILTER_FLOOD] = NULL;
317     _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
318     _constructor[NR_FILTER_IMAGE] = NULL;
319     _constructor[NR_FILTER_MERGE] = NULL;
320     _constructor[NR_FILTER_MORPHOLOGY] = NULL;
321     _constructor[NR_FILTER_OFFSET] = &FilterOffset::create;
322     _constructor[NR_FILTER_SPECULARLIGHTING] = NULL;
323     _constructor[NR_FILTER_TILE] = NULL;
324     _constructor[NR_FILTER_TURBULENCE] = NULL;
325     created = true;
328 /** Helper method for enlarging table of filter primitives. When new
329  * primitives are added, but we have no space for them, this function
330  * makes some more space.
331  */
332 void Filter::_enlarge_primitive_table() {
333     FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
334     for (int i = 0 ; i < _primitive_count ; i++) {
335         new_tbl[i] = _primitive[i];
336     }
337     _primitive_table_size *= 2;
338     for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
339         new_tbl[i] = NULL;
340     }
341     delete[] _primitive;
342     _primitive = new_tbl;
345 int Filter::add_primitive(FilterPrimitiveType type)
347     _create_constructor_table();
349     // Check that we can create a new filter of specified type
350     if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
351         return -1;
352     if (!_constructor[type]) return -1;
353     FilterPrimitive *created = _constructor[type]();
355     // If there is no space for new filter primitive, enlarge the table
356     if (_primitive_count >= _primitive_table_size) {
357         _enlarge_primitive_table();
358     }
360     _primitive[_primitive_count] = created;
361     int handle = _primitive_count;
362     _primitive_count++;
363     return handle;
366 int Filter::replace_primitive(int target, FilterPrimitiveType type)
368     _create_constructor_table();
370     // Check that target is valid primitive inside this filter
371     if (target < 0) return -1;
372     if (target >= _primitive_count) return -1;
373     if (!_primitive[target]) return -1;
375     // Check that we can create a new filter of specified type
376     if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
377         return -1;
378     if (!_constructor[type]) return -1;
379     FilterPrimitive *created = _constructor[type]();
381     // If there is no space for new filter primitive, enlarge the table
382     if (_primitive_count >= _primitive_table_size) {
383         _enlarge_primitive_table();
384     }
386     delete _primitive[target];
387     _primitive[target] = created;
388     return target;
391 FilterPrimitive *Filter::get_primitive(int handle) {
392     if (handle < 0 || handle >= _primitive_count) return NULL;
393     return _primitive[handle];
396 void Filter::clear_primitives()
398     for (int i = 0 ; i < _primitive_count ; i++) {
399         if (_primitive[i]) delete _primitive[i];
400     }
401     _primitive_count = 0;
404 void Filter::set_x(SVGLength &length)
405
406   if (length._set)
407       _region_x = length;
409 void Filter::set_y(SVGLength &length)
411   if (length._set)
412       _region_y = length;
414 void Filter::set_width(SVGLength &length)
416   if (length._set)
417       _region_width = length;
419 void Filter::set_height(SVGLength &length)
420
421   if (length._set)
422       _region_height = length;
425 } /* namespace NR */
427 /*
428   Local Variables:
429   mode:c++
430   c-file-style:"stroustrup"
431   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
432   indent-tabs-mode:nil
433   fill-column:99
434   End:
435 */
436 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :