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