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