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