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