1 #define __NR_FILTER_CPP__
3 /*
4 * SVG filters rendering
5 *
6 * Author:
7 * Niko Kiirala <niko@kiirala.com>
8 *
9 * Copyright (C) 2006,2007 Niko Kiirala
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include <glib.h>
15 #include <cmath>
16 #include <cstring>
17 #include <string>
19 #include "display/nr-filter.h"
20 #include "display/nr-filter-primitive.h"
21 #include "display/nr-filter-slot.h"
22 #include "display/nr-filter-types.h"
23 #include "display/nr-filter-units.h"
25 #include "display/nr-filter-blend.h"
26 #include "display/nr-filter-composite.h"
27 #include "display/nr-filter-convolve-matrix.h"
28 #include "display/nr-filter-colormatrix.h"
29 #include "display/nr-filter-component-transfer.h"
30 #include "display/nr-filter-diffuselighting.h"
31 #include "display/nr-filter-displacement-map.h"
32 #include "display/nr-filter-flood.h"
33 #include "display/nr-filter-gaussian.h"
34 #include "display/nr-filter-image.h"
35 #include "display/nr-filter-merge.h"
36 #include "display/nr-filter-morphology.h"
37 #include "display/nr-filter-offset.h"
38 #include "display/nr-filter-specularlighting.h"
39 #include "display/nr-filter-tile.h"
40 #include "display/nr-filter-turbulence.h"
42 #include "display/nr-arena-item.h"
43 #include "libnr/nr-pixblock.h"
44 #include "libnr/nr-blit.h"
45 #include "libnr/nr-matrix.h"
46 #include "libnr/nr-scale.h"
47 #include "svg/svg-length.h"
48 #include "sp-filter-units.h"
49 #include "preferences.h"
51 #if defined (SOLARIS) && (SOLARIS == 8)
52 #include "round.h"
53 using Inkscape::round;
54 #endif
56 namespace NR {
58 Filter::Filter()
59 {
60 _primitive_count = 0;
61 _primitive_table_size = 1;
62 _primitive = new FilterPrimitive*[1];
63 _primitive[0] = NULL;
64 //_primitive_count = 1;
65 //_primitive[0] = new FilterGaussian;
66 _common_init();
67 }
69 Filter::Filter(int n)
70 {
71 _primitive_count = 0;
72 _primitive_table_size = n;
73 _primitive = new FilterPrimitive*[n];
74 for ( int i = 0 ; i < n ; i++ ) {
75 _primitive[i] = NULL;
76 }
77 _common_init();
78 }
80 void Filter::_common_init() {
81 _slot_count = 1;
82 // Having "not set" here as value means the output of last filter
83 // primitive will be used as output of this filter
84 _output_slot = NR_FILTER_SLOT_NOT_SET;
86 // These are the default values for filter region,
87 // as specified in SVG standard
88 // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
89 _region_x.set(SVGLength::PERCENT, -.10, 0);
90 _region_y.set(SVGLength::PERCENT, -.10, 0);
91 _region_width.set(SVGLength::PERCENT, 1.20, 0);
92 _region_height.set(SVGLength::PERCENT, 1.20, 0);
94 // Filter resolution, negative value here stands for "automatic"
95 _x_pixels = -1.0;
96 _y_pixels = -1.0;
98 _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
99 _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
100 }
102 Filter::~Filter()
103 {
104 clear_primitives();
105 delete[] _primitive;
106 }
109 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
110 {
111 if (!_primitive[0]) {
112 // TODO: Should clear the input buffer instead of just returning
113 return 1;
114 }
116 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
117 FilterQuality const filterquality = (FilterQuality)prefs->getInt("/options/filterquality/value");
119 Geom::Matrix trans = item->ctm;
120 FilterSlot slot(_slot_count, item);
121 slot.set_quality(filterquality);
123 Geom::Rect item_bbox;
124 if (item->item_bbox) {
125 item_bbox = *(item->item_bbox);
126 } else {
127 // Bounding box might not exist, so create a dummy one.
128 Geom::Point zero(0, 0);
129 item_bbox = Geom::Rect(zero, zero);
130 }
131 if (item_bbox.min()[X] > item_bbox.max()[X]
132 || item_bbox.min()[Y] > item_bbox.max()[Y])
133 {
134 // Code below assumes non-negative size.
135 return 1;
136 }
138 Geom::Rect filter_area = filter_effect_area(item_bbox);
139 if (item_bbox.area() == 0.0) {
140 // It's no use to try and filter an empty object.
141 return 1;
142 }
144 FilterUnits units(_filter_units, _primitive_units);
145 units.set_ctm(trans);
146 units.set_item_bbox(from_2geom(item_bbox));
147 units.set_filter_area(from_2geom(filter_area));
149 // TODO: with filterRes of 0x0 should return an empty image
150 if (_x_pixels > 0) {
151 double y_len;
152 if (_y_pixels > 0) {
153 y_len = _y_pixels;
154 } else {
155 y_len = (_x_pixels * (filter_area.max()[Y] - filter_area.min()[Y]))
156 / (filter_area.max()[X] - filter_area.min()[X]);
157 }
158 units.set_automatic_resolution(false);
159 units.set_resolution(_x_pixels, y_len);
160 } else {
161 Geom::Point origo = filter_area.min();
162 origo *= trans;
163 Geom::Point max_i(filter_area.max()[X], filter_area.min()[Y]);
164 max_i *= trans;
165 Geom::Point max_j(filter_area.min()[X], filter_area.max()[Y]);
166 max_j *= trans;
167 double i_len = sqrt((origo[X] - max_i[X]) * (origo[X] - max_i[X])
168 + (origo[Y] - max_i[Y]) * (origo[Y] - max_i[Y]));
169 double j_len = sqrt((origo[X] - max_j[X]) * (origo[X] - max_j[X])
170 + (origo[Y] - max_j[Y]) * (origo[Y] - max_j[Y]));
171 units.set_automatic_resolution(true);
172 double const divisor = _resolution_divisor(filterquality);
173 units.set_resolution(i_len / divisor, j_len / divisor);
174 }
176 units.set_paraller(false);
177 for (int i = 0 ; i < _primitive_count ; i++) {
178 if (_primitive[i]->get_input_traits() & TRAIT_PARALLER) {
179 units.set_paraller(true);
180 break;
181 }
182 }
184 slot.set_units(units);
186 NRPixBlock *in = new NRPixBlock;
187 nr_pixblock_setup_fast(in, pb->mode, pb->area.x0, pb->area.y0,
188 pb->area.x1, pb->area.y1, true);
189 if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) {
190 g_warning("NR::Filter::render: failed to reserve temporary buffer");
191 return 0;
192 }
193 nr_blit_pixblock_pixblock(in, pb);
194 in->empty = FALSE;
195 slot.set(NR_FILTER_SOURCEGRAPHIC, in);
197 // Check that we are rendering a non-empty area
198 in = slot.get(NR_FILTER_SOURCEGRAPHIC);
199 if (in->area.x1 - in->area.x0 <= 0 || in->area.y1 - in->area.y0 <= 0) {
200 if (in->area.x1 - in->area.x0 < 0 || in->area.y1 - in->area.y0 < 0) {
201 g_warning("NR::Filter::render: negative area! (%d, %d) (%d, %d)",
202 in->area.x0, in->area.y0, in->area.x1, in->area.y1);
203 }
204 return 0;
205 }
206 in = NULL; // in is now handled by FilterSlot, we should not touch it
208 for (int i = 0 ; i < _primitive_count ; i++) {
209 _primitive[i]->render(slot, units);
210 }
212 slot.get_final(_output_slot, pb);
214 // Take note of the amount of used image slots
215 // -> next time this filter is rendered, we can reserve enough slots
216 // immediately
217 _slot_count = slot.get_slot_count();
218 return 0;
219 }
221 void Filter::area_enlarge(NRRectL &bbox, Geom::Matrix const &m) {
222 for (int i = 0 ; i < _primitive_count ; i++) {
223 if (_primitive[i]) _primitive[i]->area_enlarge(bbox, m);
224 }
225 }
227 void Filter::bbox_enlarge(NRRectL &bbox) {
228 // Modifying empty bounding boxes confuses rest of the renderer, so
229 // let's not do that.
230 if (bbox.x0 > bbox.x1 || bbox.y0 > bbox.y1) return;
232 /* TODO: this is wrong. Should use bounding box in user coordinates
233 * and find its extents in display coordinates. */
234 Geom::Point min(bbox.x0, bbox.y0);
235 Geom::Point max(bbox.x1, bbox.y1);
236 Geom::Rect tmp_bbox(min, max);
238 Geom::Rect enlarged = filter_effect_area(tmp_bbox);
240 bbox.x0 = (ICoord)enlarged.min()[X];
241 bbox.y0 = (ICoord)enlarged.min()[Y];
242 bbox.x1 = (ICoord)enlarged.max()[X];
243 bbox.y1 = (ICoord)enlarged.max()[Y];
244 }
246 Geom::Rect Filter::filter_effect_area(Geom::Rect const &bbox)
247 {
248 Geom::Point minp, maxp;
249 double len_x = bbox.max()[X] - bbox.min()[X];
250 double len_y = bbox.max()[Y] - bbox.min()[Y];
251 /* TODO: fetch somehow the object ex and em lengths */
252 _region_x.update(12, 6, len_x);
253 _region_y.update(12, 6, len_y);
254 _region_width.update(12, 6, len_x);
255 _region_height.update(12, 6, len_y);
256 if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
257 if (_region_x.unit == SVGLength::PERCENT) {
258 minp[X] = bbox.min()[X] + _region_x.computed;
259 } else {
260 minp[X] = bbox.min()[X] + _region_x.computed * len_x;
261 }
262 if (_region_width.unit == SVGLength::PERCENT) {
263 maxp[X] = minp[X] + _region_width.computed;
264 } else {
265 maxp[X] = minp[X] + _region_width.computed * len_x;
266 }
268 if (_region_y.unit == SVGLength::PERCENT) {
269 minp[Y] = bbox.min()[Y] + _region_y.computed;
270 } else {
271 minp[Y] = bbox.min()[Y] + _region_y.computed * len_y;
272 }
273 if (_region_height.unit == SVGLength::PERCENT) {
274 maxp[Y] = minp[Y] + _region_height.computed;
275 } else {
276 maxp[Y] = minp[Y] + _region_height.computed * len_y;
277 }
278 } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
279 /* TODO: make sure bbox and fe region are in same coordinate system */
280 minp[X] = _region_x.computed;
281 maxp[X] = minp[X] + _region_width.computed;
282 minp[Y] = _region_y.computed;
283 maxp[Y] = minp[Y] + _region_height.computed;
284 } else {
285 g_warning("Error in NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
286 }
287 Geom::Rect area(minp, maxp);
288 return area;
289 }
291 /* Constructor table holds pointers to static methods returning filter
292 * primitives. This table is indexed with FilterPrimitiveType, so that
293 * for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
294 * returns a filter object of type NR::FilterGaussian.
295 */
296 typedef FilterPrimitive*(*FilterConstructor)();
297 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
299 void Filter::_create_constructor_table()
300 {
301 // Constructor table won't change in run-time, so no need to recreate
302 static bool created = false;
303 if(created) return;
305 /* Some filter classes are not implemented yet.
306 Some of them still have only boilerplate code.*/
307 _constructor[NR_FILTER_BLEND] = &FilterBlend::create;
308 _constructor[NR_FILTER_COLORMATRIX] = &FilterColorMatrix::create;
309 _constructor[NR_FILTER_COMPONENTTRANSFER] = &FilterComponentTransfer::create;
310 _constructor[NR_FILTER_COMPOSITE] = &FilterComposite::create;
311 _constructor[NR_FILTER_CONVOLVEMATRIX] = &FilterConvolveMatrix::create;
312 _constructor[NR_FILTER_DIFFUSELIGHTING] = &FilterDiffuseLighting::create;
313 _constructor[NR_FILTER_DISPLACEMENTMAP] = &FilterDisplacementMap::create;
314 _constructor[NR_FILTER_FLOOD] = &FilterFlood::create;
315 _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
316 _constructor[NR_FILTER_IMAGE] = &FilterImage::create;
317 _constructor[NR_FILTER_MERGE] = &FilterMerge::create;
318 _constructor[NR_FILTER_MORPHOLOGY] = &FilterMorphology::create;
319 _constructor[NR_FILTER_OFFSET] = &FilterOffset::create;
320 _constructor[NR_FILTER_SPECULARLIGHTING] = &FilterSpecularLighting::create;
321 _constructor[NR_FILTER_TILE] = &FilterTile::create;
322 _constructor[NR_FILTER_TURBULENCE] = &FilterTurbulence::create;
323 created = true;
324 }
326 /** Helper method for enlarging table of filter primitives. When new
327 * primitives are added, but we have no space for them, this function
328 * makes some more space.
329 */
330 void Filter::_enlarge_primitive_table() {
331 FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
332 for (int i = 0 ; i < _primitive_count ; i++) {
333 new_tbl[i] = _primitive[i];
334 }
335 _primitive_table_size *= 2;
336 for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
337 new_tbl[i] = NULL;
338 }
339 delete[] _primitive;
340 _primitive = new_tbl;
341 }
343 int Filter::add_primitive(FilterPrimitiveType type)
344 {
345 _create_constructor_table();
347 // Check that we can create a new filter of specified type
348 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
349 return -1;
350 if (!_constructor[type]) return -1;
351 FilterPrimitive *created = _constructor[type]();
353 // If there is no space for new filter primitive, enlarge the table
354 if (_primitive_count >= _primitive_table_size) {
355 _enlarge_primitive_table();
356 }
358 _primitive[_primitive_count] = created;
359 int handle = _primitive_count;
360 _primitive_count++;
361 return handle;
362 }
364 int Filter::replace_primitive(int target, FilterPrimitiveType type)
365 {
366 _create_constructor_table();
368 // Check that target is valid primitive inside this filter
369 if (target < 0) return -1;
370 if (target >= _primitive_count) return -1;
371 if (!_primitive[target]) return -1;
373 // Check that we can create a new filter of specified type
374 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
375 return -1;
376 if (!_constructor[type]) return -1;
377 FilterPrimitive *created = _constructor[type]();
379 // If there is no space for new filter primitive, enlarge the table
380 if (_primitive_count >= _primitive_table_size) {
381 _enlarge_primitive_table();
382 }
384 delete _primitive[target];
385 _primitive[target] = created;
386 return target;
387 }
389 FilterPrimitive *Filter::get_primitive(int handle) {
390 if (handle < 0 || handle >= _primitive_count) return NULL;
391 return _primitive[handle];
392 }
394 void Filter::clear_primitives()
395 {
396 for (int i = 0 ; i < _primitive_count ; i++) {
397 if (_primitive[i]) delete _primitive[i];
398 }
399 _primitive_count = 0;
400 }
402 void Filter::set_x(SVGLength const &length)
403 {
404 if (length._set)
405 _region_x = length;
406 }
407 void Filter::set_y(SVGLength const &length)
408 {
409 if (length._set)
410 _region_y = length;
411 }
412 void Filter::set_width(SVGLength const &length)
413 {
414 if (length._set)
415 _region_width = length;
416 }
417 void Filter::set_height(SVGLength const &length)
418 {
419 if (length._set)
420 _region_height = length;
421 }
423 void Filter::set_resolution(double const pixels) {
424 if (pixels > 0) {
425 _x_pixels = pixels;
426 _y_pixels = pixels;
427 }
428 }
430 void Filter::set_resolution(double const x_pixels, double const y_pixels) {
431 if (x_pixels >= 0 && y_pixels >= 0) {
432 _x_pixels = x_pixels;
433 _y_pixels = y_pixels;
434 }
435 }
437 void Filter::reset_resolution() {
438 _x_pixels = -1;
439 _y_pixels = -1;
440 }
442 double Filter::_resolution_divisor(FilterQuality const quality) const {
443 double divisor = 1;
444 switch (quality) {
445 case FILTER_QUALITY_WORST:
446 divisor = 8;
447 break;
448 case FILTER_QUALITY_WORSE:
449 divisor = 4;
450 break;
451 case FILTER_QUALITY_NORMAL:
452 divisor = 2;
453 break;
454 case FILTER_QUALITY_BETTER:
455 case FILTER_QUALITY_BEST:
456 default:
457 break;
458 }
459 return divisor;
460 }
462 } /* namespace NR */
464 /*
465 Local Variables:
466 mode:c++
467 c-file-style:"stroustrup"
468 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
469 indent-tabs-mode:nil
470 fill-column:99
471 End:
472 */
473 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :