Code

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