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