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