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 #if defined (SOLARIS) && (SOLARIS == 8)
50 #include "round.h"
51 using Inkscape::round;
52 #endif
54 namespace NR {
56 Filter::Filter()
57 {
58 _primitive_count = 0;
59 _primitive_table_size = 1;
60 _primitive = new FilterPrimitive*[1];
61 _primitive[0] = NULL;
62 //_primitive_count = 1;
63 //_primitive[0] = new FilterGaussian;
64 _common_init();
65 }
67 Filter::Filter(int n)
68 {
69 _primitive_count = 0;
70 _primitive_table_size = n;
71 _primitive = new FilterPrimitive*[n];
72 for ( int i = 0 ; i < n ; i++ ) {
73 _primitive[i] = NULL;
74 }
75 _common_init();
76 }
78 void Filter::_common_init() {
79 _slot_count = 1;
80 // Having "not set" here as value means the output of last filter
81 // primitive will be used as output of this filter
82 _output_slot = NR_FILTER_SLOT_NOT_SET;
84 // These are the default values for filter region,
85 // as specified in SVG standard
86 // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
87 _region_x.set(SVGLength::PERCENT, -.10, 0);
88 _region_y.set(SVGLength::PERCENT, -.10, 0);
89 _region_width.set(SVGLength::PERCENT, 1.20, 0);
90 _region_height.set(SVGLength::PERCENT, 1.20, 0);
92 // Filter resolution, negative value here stands for "automatic"
93 _x_pixels = -1.0;
94 _y_pixels = -1.0;
96 _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
97 _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
98 }
100 Filter::~Filter()
101 {
102 clear_primitives();
103 delete[] _primitive;
104 }
107 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
108 {
109 if (!_primitive[0]) {
110 // TODO: Should clear the input buffer instead of just returning
111 return 1;
112 }
114 Matrix trans = item->ctm;
115 FilterSlot slot(_slot_count, item);
117 Rect item_bbox;
118 if (item->item_bbox) {
119 item_bbox = *(item->item_bbox);
120 } else {
121 // Bounding box might not exist, so create a dummy one.
122 Point zero(0, 0);
123 item_bbox = Rect(zero, zero);
124 }
125 if (item_bbox.min()[X] > item_bbox.max()[X]
126 || item_bbox.min()[Y] > item_bbox.max()[Y])
127 {
128 // Code below assumes non-negative size.
129 return 1;
130 }
132 Rect filter_area = filter_effect_area(item_bbox);
133 if (item_bbox.isEmpty()) {
134 // It's no use to try and filter an empty object.
135 return 1;
136 }
138 FilterUnits units(_filter_units, _primitive_units);
139 units.set_ctm(trans);
140 units.set_item_bbox(item_bbox);
141 units.set_filter_area(filter_area);
143 // TODO: with filterRes of 0x0 should return an empty image
144 if (_x_pixels > 0) {
145 double y_len;
146 if (_y_pixels > 0) {
147 y_len = _y_pixels;
148 } else {
149 y_len = (_x_pixels * (filter_area.max()[Y] - filter_area.min()[Y]))
150 / (filter_area.max()[X] - filter_area.min()[X]);
151 }
152 units.set_automatic_resolution(false);
153 units.set_resolution(_x_pixels, y_len);
154 } else {
155 Point origo = filter_area.min();
156 origo *= trans;
157 Point max_i(filter_area.max()[X], filter_area.min()[Y]);
158 max_i *= trans;
159 Point max_j(filter_area.min()[X], filter_area.max()[Y]);
160 max_j *= trans;
161 double i_len = sqrt((origo[X] - max_i[X]) * (origo[X] - max_i[X])
162 + (origo[Y] - max_i[Y]) * (origo[Y] - max_i[Y]));
163 double j_len = sqrt((origo[X] - max_j[X]) * (origo[X] - max_j[X])
164 + (origo[Y] - max_j[Y]) * (origo[Y] - max_j[Y]));
165 units.set_automatic_resolution(true);
166 units.set_resolution(i_len, j_len);
167 }
169 units.set_paraller(false);
170 for (int i = 0 ; i < _primitive_count ; i++) {
171 if (_primitive[i]->get_input_traits() & TRAIT_PARALLER) {
172 units.set_paraller(true);
173 break;
174 }
175 }
177 slot.set_units(units);
179 NRPixBlock *in = new NRPixBlock;
180 nr_pixblock_setup_fast(in, pb->mode, pb->area.x0, pb->area.y0,
181 pb->area.x1, pb->area.y1, true);
182 if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) {
183 g_warning("NR::Filter::render: failed to reserve temporary buffer");
184 return 0;
185 }
186 nr_blit_pixblock_pixblock(in, pb);
187 in->empty = FALSE;
188 slot.set(NR_FILTER_SOURCEGRAPHIC, in);
190 // Check that we are rendering a non-empty area
191 in = slot.get(NR_FILTER_SOURCEGRAPHIC);
192 if (in->area.x1 - in->area.x0 <= 0 || in->area.y1 - in->area.y0 <= 0) {
193 if (in->area.x1 - in->area.x0 < 0 || in->area.y1 - in->area.y0 < 0) {
194 g_warning("NR::Filter::render: negative area! (%d, %d) (%d, %d)",
195 in->area.x0, in->area.y0, in->area.x1, in->area.y1);
196 }
197 return 0;
198 }
199 in = NULL; // in is now handled by FilterSlot, we should not touch it
201 for (int i = 0 ; i < _primitive_count ; i++) {
202 _primitive[i]->render(slot, units);
203 }
205 slot.get_final(_output_slot, pb);
207 // Take note of the amount of used image slots
208 // -> next time this filter is rendered, we can reserve enough slots
209 // immediately
210 _slot_count = slot.get_slot_count();
211 return 0;
212 }
214 void Filter::area_enlarge(NRRectL &bbox, Matrix const &m) {
215 for (int i = 0 ; i < _primitive_count ; i++) {
216 if (_primitive[i]) _primitive[i]->area_enlarge(bbox, m);
217 }
218 }
220 void Filter::bbox_enlarge(NRRectL &bbox) {
221 // Modifying empty bounding boxes confuses rest of the renderer, so
222 // let's not do that.
223 if (bbox.x0 > bbox.x1 || bbox.y0 > bbox.y1) return;
225 /* TODO: this is wrong. Should use bounding box in user coordinates
226 * and find its extents in display coordinates. */
227 Point min(bbox.x0, bbox.y0);
228 Point max(bbox.x1, bbox.y1);
229 Rect tmp_bbox(min, max);
231 Rect enlarged = filter_effect_area(tmp_bbox);
233 bbox.x0 = (ICoord)enlarged.min()[X];
234 bbox.y0 = (ICoord)enlarged.min()[Y];
235 bbox.x1 = (ICoord)enlarged.max()[X];
236 bbox.y1 = (ICoord)enlarged.max()[Y];
237 }
239 Rect Filter::filter_effect_area(Rect const &bbox)
240 {
241 Point minp, maxp;
242 double len_x = bbox.max()[X] - bbox.min()[X];
243 double len_y = bbox.max()[Y] - bbox.min()[Y];
244 /* TODO: fetch somehow the object ex and em lengths */
245 _region_x.update(12, 6, len_x);
246 _region_y.update(12, 6, len_y);
247 _region_width.update(12, 6, len_x);
248 _region_height.update(12, 6, len_y);
249 if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
250 if (_region_x.unit == SVGLength::PERCENT) {
251 minp[X] = bbox.min()[X] + _region_x.computed;
252 } else {
253 minp[X] = bbox.min()[X] + _region_x.computed * len_x;
254 }
255 if (_region_width.unit == SVGLength::PERCENT) {
256 maxp[X] = minp[X] + _region_width.computed;
257 } else {
258 maxp[X] = minp[X] + _region_width.computed * len_x;
259 }
261 if (_region_y.unit == SVGLength::PERCENT) {
262 minp[Y] = bbox.min()[Y] + _region_y.computed;
263 } else {
264 minp[Y] = bbox.min()[Y] + _region_y.computed * len_y;
265 }
266 if (_region_height.unit == SVGLength::PERCENT) {
267 maxp[Y] = minp[Y] + _region_height.computed;
268 } else {
269 maxp[Y] = minp[Y] + _region_height.computed * len_y;
270 }
271 } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
272 /* TODO: make sure bbox and fe region are in same coordinate system */
273 minp[X] = _region_x.computed;
274 maxp[X] = minp[X] + _region_width.computed;
275 minp[Y] = _region_y.computed;
276 maxp[Y] = minp[Y] + _region_height.computed;
277 } else {
278 g_warning("Error in NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
279 }
280 Rect area(minp, maxp);
281 return area;
282 }
284 /* Constructor table holds pointers to static methods returning filter
285 * primitives. This table is indexed with FilterPrimitiveType, so that
286 * for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
287 * returns a filter object of type NR::FilterGaussian.
288 */
289 typedef FilterPrimitive*(*FilterConstructor)();
290 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
292 void Filter::_create_constructor_table()
293 {
294 // Constructor table won't change in run-time, so no need to recreate
295 static bool created = false;
296 if(created) return;
298 /* Some filter classes are not implemented yet.
299 Some of them still have only boilerplate code.*/
300 _constructor[NR_FILTER_BLEND] = &FilterBlend::create;
301 _constructor[NR_FILTER_COLORMATRIX] = &FilterColorMatrix::create;
302 _constructor[NR_FILTER_COMPONENTTRANSFER] = &FilterComponentTransfer::create;
303 _constructor[NR_FILTER_COMPOSITE] = &FilterComposite::create;
304 _constructor[NR_FILTER_CONVOLVEMATRIX] = &FilterConvolveMatrix::create;
305 _constructor[NR_FILTER_DIFFUSELIGHTING] = &FilterDiffuseLighting::create;
306 _constructor[NR_FILTER_DISPLACEMENTMAP] = &FilterDisplacementMap::create;
307 _constructor[NR_FILTER_FLOOD] = &FilterFlood::create;
308 _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
309 _constructor[NR_FILTER_IMAGE] = &FilterImage::create;
310 _constructor[NR_FILTER_MERGE] = &FilterMerge::create;
311 _constructor[NR_FILTER_MORPHOLOGY] = &FilterMorphology::create;
312 _constructor[NR_FILTER_OFFSET] = &FilterOffset::create;
313 _constructor[NR_FILTER_SPECULARLIGHTING] = &FilterSpecularLighting::create;
314 _constructor[NR_FILTER_TILE] = &FilterTile::create;
315 _constructor[NR_FILTER_TURBULENCE] = &FilterTurbulence::create;
316 created = true;
317 }
319 /** Helper method for enlarging table of filter primitives. When new
320 * primitives are added, but we have no space for them, this function
321 * makes some more space.
322 */
323 void Filter::_enlarge_primitive_table() {
324 FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
325 for (int i = 0 ; i < _primitive_count ; i++) {
326 new_tbl[i] = _primitive[i];
327 }
328 _primitive_table_size *= 2;
329 for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
330 new_tbl[i] = NULL;
331 }
332 delete[] _primitive;
333 _primitive = new_tbl;
334 }
336 int Filter::add_primitive(FilterPrimitiveType type)
337 {
338 _create_constructor_table();
340 // Check that we can create a new filter of specified type
341 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
342 return -1;
343 if (!_constructor[type]) return -1;
344 FilterPrimitive *created = _constructor[type]();
346 // If there is no space for new filter primitive, enlarge the table
347 if (_primitive_count >= _primitive_table_size) {
348 _enlarge_primitive_table();
349 }
351 _primitive[_primitive_count] = created;
352 int handle = _primitive_count;
353 _primitive_count++;
354 return handle;
355 }
357 int Filter::replace_primitive(int target, FilterPrimitiveType type)
358 {
359 _create_constructor_table();
361 // Check that target is valid primitive inside this filter
362 if (target < 0) return -1;
363 if (target >= _primitive_count) return -1;
364 if (!_primitive[target]) return -1;
366 // Check that we can create a new filter of specified type
367 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
368 return -1;
369 if (!_constructor[type]) return -1;
370 FilterPrimitive *created = _constructor[type]();
372 // If there is no space for new filter primitive, enlarge the table
373 if (_primitive_count >= _primitive_table_size) {
374 _enlarge_primitive_table();
375 }
377 delete _primitive[target];
378 _primitive[target] = created;
379 return target;
380 }
382 FilterPrimitive *Filter::get_primitive(int handle) {
383 if (handle < 0 || handle >= _primitive_count) return NULL;
384 return _primitive[handle];
385 }
387 void Filter::clear_primitives()
388 {
389 for (int i = 0 ; i < _primitive_count ; i++) {
390 if (_primitive[i]) delete _primitive[i];
391 }
392 _primitive_count = 0;
393 }
395 void Filter::set_x(SVGLength const &length)
396 {
397 if (length._set)
398 _region_x = length;
399 }
400 void Filter::set_y(SVGLength const &length)
401 {
402 if (length._set)
403 _region_y = length;
404 }
405 void Filter::set_width(SVGLength const &length)
406 {
407 if (length._set)
408 _region_width = length;
409 }
410 void Filter::set_height(SVGLength const &length)
411 {
412 if (length._set)
413 _region_height = length;
414 }
416 void Filter::set_resolution(double const pixels) {
417 if (pixels > 0) {
418 _x_pixels = pixels;
419 _y_pixels = pixels;
420 }
421 }
423 void Filter::set_resolution(double const x_pixels, double const y_pixels) {
424 if (x_pixels >= 0 && y_pixels >= 0) {
425 _x_pixels = x_pixels;
426 _y_pixels = y_pixels;
427 }
428 }
430 void Filter::reset_resolution() {
431 _x_pixels = -1;
432 _y_pixels = -1;
433 }
435 } /* namespace NR */
437 /*
438 Local Variables:
439 mode:c++
440 c-file-style:"stroustrup"
441 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
442 indent-tabs-mode:nil
443 fill-column:99
444 End:
445 */
446 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :