592623c495e0855a741be4b342e3aaced2bfd146
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>
16 #include "display/nr-filter.h"
17 #include "display/nr-filter-primitive.h"
18 #include "display/nr-filter-gaussian.h"
19 #include "display/nr-filter-slot.h"
20 #include "display/nr-filter-types.h"
21 #include "display/pixblock-scaler.h"
23 #include "display/nr-arena-item.h"
24 #include "libnr/nr-pixblock.h"
25 #include "libnr/nr-blit.h"
26 #include "libnr/nr-matrix.h"
27 #include "libnr/nr-scale.h"
28 #include "svg/svg-length.h"
29 #include "sp-filter-units.h"
31 //#include "display/nr-arena-shape.h"
33 namespace NR {
35 Filter::Filter()
36 {
37 _primitive_count = 0;
38 _primitive_table_size = 1;
39 _primitive = new FilterPrimitive*[1];
40 _primitive[0] = NULL;
41 //_primitive_count = 1;
42 //_primitive[0] = new FilterGaussian;
43 _common_init();
44 }
46 Filter::Filter(int n)
47 {
48 _primitive_count = 0;
49 _primitive_table_size = n;
50 _primitive = new FilterPrimitive*[n];
51 for ( int i = 0 ; i < n ; i++ ) {
52 _primitive[i] = NULL;
53 }
54 _common_init();
55 }
57 void Filter::_common_init() {
58 _slot_count = 1;
59 // Having "not set" here as value means the output of last filter
60 // primitive will be used as output of this filter
61 _output_slot = NR_FILTER_SLOT_NOT_SET;
63 // These are the default values for filter region,
64 // as specified in SVG standard
65 // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
66 _region_x.set(SVGLength::PERCENT, -.10, 0);
67 _region_y.set(SVGLength::PERCENT, -.10, 0);
68 _region_width.set(SVGLength::PERCENT, 1.20, 0);
69 _region_height.set(SVGLength::PERCENT, 1.20, 0);
71 // Filter resolution, negative value here stands for "automatic"
72 _x_pixels = -1.0;
73 _y_pixels = -1.0;
75 _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
76 _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
77 }
79 Filter::~Filter()
80 {
81 clear_primitives();
82 delete[] _primitive;
83 }
86 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
87 {
88 if(!_primitive[0]) { // if there are no primitives, do nothing
89 return 0;
90 }
92 Matrix trans = *item->ctm;
93 FilterSlot slot(_slot_count, item);
94 NRPixBlock *in = new NRPixBlock;
96 // First, if filter resolution is not set to automatic, we should
97 // scale the input image to correct resolution
98 if (_x_pixels >= 0) {
99 /* If filter resolution is zero, the object should not be rendered */
100 if (_x_pixels == 0 || _y_pixels == 0) {
101 int size = (pb->area.x1 - pb->area.x0)
102 * (pb->area.y1 - pb->area.y0)
103 * NR_PIXBLOCK_BPP(pb);
104 memset(NR_PIXBLOCK_PX(pb), 0, size);
105 return 0;
106 }
107 // Resolution is specified as pixel length of our internal buffer.
108 // Though, we might not be rendering the whole object at time,
109 // so we need to calculate the correct pixel size
110 int x_len = (int)round(((pb->area.x1 - pb->area.x0) * _x_pixels) / (item->bbox.x1 - item->bbox.x0));
111 if (x_len < 1) x_len = 1;
112 // If y-resolution is also set, count y-area in the same way as x-area
113 // Otherwise, make y-area so, that aspect ratio of input pixblock and
114 // internal pixblock are the same.
115 int y_len;
116 if (_y_pixels > 0) {
117 y_len = (int)round(((pb->area.y1 - pb->area.y0) * _y_pixels) / (item->bbox.y1 - item->bbox.y0));
118 } else {
119 y_len = (int)round((x_len * (pb->area.y1 - pb->area.y0)) / (double)(pb->area.x1 - pb->area.x0));
120 }
121 if (y_len < 1) y_len = 1;
122 nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true);
123 if (in->data.px == NULL) // memory allocation failed
124 return 0;
125 scale_bicubic(in, pb);
126 scale res_scaling(x_len / (double)(pb->area.x1 - pb->area.x0),
127 y_len / (double)(pb->area.y1 - pb->area.y0));
128 trans *= res_scaling;
129 } else {
130 // If filter resolution is automatic, just make copy of input image
131 nr_pixblock_setup_fast(in, pb->mode,
132 pb->area.x0, pb->area.y0,
133 pb->area.x1, pb->area.y1, true);
134 if (in->data.px == NULL) // memory allocation failed
135 return 0;
136 nr_blit_pixblock_pixblock(in, pb);
137 }
138 slot.set(NR_FILTER_SOURCEGRAPHIC, in);
139 in = NULL; // in is now handled by FilterSlot, we should not touch it
141 // TODO: loop through ALL the primitives and render them one at a time
142 _primitive[0]->render(slot, trans);
143 NRPixBlock *out = slot.get(_output_slot);
145 // Clear the pixblock, where the output will be put
146 // -> the original image does not show through
147 int size = (pb->area.x1 - pb->area.x0)
148 * (pb->area.y1 - pb->area.y0)
149 * NR_PIXBLOCK_BPP(pb);
150 memset(NR_PIXBLOCK_PX(pb), 0, size);
152 // If the filter resolution is automatic, just copy our final image
153 // to output pixblock, otherwise use bicubic scaling
154 if (_x_pixels < 0) {
155 nr_blit_pixblock_pixblock(pb, out);
156 } else {
157 scale_bicubic(pb, out);
158 }
160 // Take note of the amount of used image slots
161 // -> next time this filter is rendered, we can reserve enough slots
162 // immediately
163 _slot_count = slot.get_slot_count();
164 return 0;
165 }
167 int Filter::get_enlarge(Matrix const &m)
168 {
169 // Just sum the enlargement factor of all filter elements.
170 // TODO: this both sucks and blows for filters like feOffset
171 // -> ditch this method and design a better one...
172 int enlarge = 0;
173 for ( int i = 0 ; i < _primitive_count ; i++ ) {
174 if(_primitive[i]) enlarge += _primitive[i]->get_enlarge(m);
175 }
176 return enlarge;
177 }
179 void Filter::bbox_enlarge(NRRectL &bbox)
180 {
181 int len_x = bbox.x1 - bbox.x0;
182 int len_y = bbox.y1 - bbox.y0;
183 /* TODO: fetch somehow the object ex and em lengths */
184 _region_x.update(12, 6, len_x);
185 _region_y.update(12, 6, len_y);
186 _region_width.update(12, 6, len_x);
187 _region_height.update(12, 6, len_y);
188 if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
189 if (_region_x.unit == SVGLength::PERCENT) {
190 bbox.x0 += (ICoord)_region_x.computed;
191 } else {
192 bbox.x0 += (ICoord)(_region_x.computed * len_x);
193 }
194 if (_region_width.unit == SVGLength::PERCENT) {
195 bbox.x1 = bbox.x0 + (ICoord)_region_width.computed;
196 } else {
197 bbox.x1 = bbox.x0 + (ICoord)(_region_width.computed * len_x);
198 }
200 if (_region_y.unit == SVGLength::PERCENT) {
201 bbox.y0 += (ICoord)_region_y.computed;
202 } else {
203 bbox.y0 += (ICoord)(_region_y.computed * len_y);
204 }
205 if (_region_height.unit == SVGLength::PERCENT) {
206 bbox.y1 = bbox.y0 + (ICoord)_region_height.computed;
207 } else {
208 bbox.y1 = bbox.y0 + (ICoord)(_region_height.computed * len_y);
209 }
210 } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
211 /* TODO: make sure bbox and fe region are in same coordinate system */
212 bbox.x0 = (ICoord) _region_x.computed;
213 bbox.x1 = bbox.x0 + (ICoord) _region_width.computed;
214 bbox.y0 = (ICoord) _region_y.computed;
215 bbox.y1 = bbox.y0 + (ICoord) _region_height.computed;
216 } else {
217 g_warning("Error in NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
218 }
219 }
221 /* Constructor table holds pointers to static methods returning filter
222 * primitives. This table is indexed with FilterPrimitiveType, so that
223 * for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
224 * returns a filter object of type NR::FilterGaussian.
225 */
226 typedef FilterPrimitive*(*FilterConstructor)();
227 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
229 void Filter::_create_constructor_table()
230 {
231 // Constructor table won't change in run-time, so no need to recreate
232 static bool created = false;
233 if(created) return;
235 /* Filter effects not yet implemented are set to NULL */
236 _constructor[NR_FILTER_BLEND] = NULL;
237 _constructor[NR_FILTER_COLORMATRIX] = NULL;
238 _constructor[NR_FILTER_COMPONENTTRANSFER] = NULL;
239 _constructor[NR_FILTER_COMPOSITE] = NULL;
240 _constructor[NR_FILTER_CONVOLVEMATRIX] = NULL;
241 _constructor[NR_FILTER_DIFFUSELIGHTING] = NULL;
242 _constructor[NR_FILTER_DISPLACEMENTMAP] = NULL;
243 _constructor[NR_FILTER_FLOOD] = NULL;
244 _constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
245 _constructor[NR_FILTER_IMAGE] = NULL;
246 _constructor[NR_FILTER_MERGE] = NULL;
247 _constructor[NR_FILTER_MORPHOLOGY] = NULL;
248 _constructor[NR_FILTER_OFFSET] = NULL;
249 _constructor[NR_FILTER_SPECULARLIGHTING] = NULL;
250 _constructor[NR_FILTER_TILE] = NULL;
251 _constructor[NR_FILTER_TURBULENCE] = NULL;
252 created = true;
253 }
255 /** Helper method for enlarging table of filter primitives. When new
256 * primitives are added, but we have no space for them, this function
257 * makes some more space.
258 */
259 void Filter::_enlarge_primitive_table() {
260 FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
261 for (int i = 0 ; i < _primitive_count ; i++) {
262 new_tbl[i] = _primitive[i];
263 }
264 _primitive_table_size *= 2;
265 for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
266 new_tbl[i] = NULL;
267 }
268 delete[] _primitive;
269 _primitive = new_tbl;
270 }
272 FilterPrimitive *Filter::add_primitive(FilterPrimitiveType type)
273 {
274 _create_constructor_table();
276 // Check that we can create a new filter of specified type
277 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
278 return NULL;
279 if (!_constructor[type]) return NULL;
280 FilterPrimitive *created = _constructor[type]();
282 // If there is no space for new filter primitive, enlarge the table
283 if (_primitive_count >= _primitive_table_size) {
284 _enlarge_primitive_table();
285 }
287 _primitive[_primitive_count] = created;
288 _primitive_count++;
289 return created;
290 }
292 FilterPrimitive *Filter::replace_primitive(FilterPrimitive *target, FilterPrimitiveType type)
293 {
294 _create_constructor_table();
296 // Check that target is valid primitive inside this filter
297 int place = -1;
298 for (int i = 0 ; i < _primitive_count ; i++) {
299 if (target == _primitive[i]) {
300 place = i;
301 break;
302 }
303 }
304 if (place < 0) return NULL;
306 // Check that we can create a new filter of specified type
307 if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
308 return NULL;
309 if (!_constructor[type]) return NULL;
310 FilterPrimitive *created = _constructor[type]();
312 // If there is no space for new filter primitive, enlarge the table
313 if (_primitive_count >= _primitive_table_size) {
314 _enlarge_primitive_table();
315 }
317 delete target;
318 _primitive[place] = created;
319 return created;
320 }
322 void Filter::clear_primitives()
323 {
324 for (int i = 0 ; i < _primitive_count ; i++) {
325 if (_primitive[i]) delete _primitive[i];
326 }
327 _primitive_count = 0;
328 }
330 void Filter::set_x(SVGLength &length)
331 {
332 if (length._set)
333 _region_x = length;
334 }
335 void Filter::set_y(SVGLength &length)
336 {
337 if (length._set)
338 _region_y = length;
339 }
340 void Filter::set_width(SVGLength &length)
341 {
342 if (length._set)
343 _region_width = length;
344 }
345 void Filter::set_height(SVGLength &length)
346 {
347 if (length._set)
348 _region_height = length;
349 }
351 } /* namespace NR */
353 /*
354 Local Variables:
355 mode:c++
356 c-file-style:"stroustrup"
357 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
358 indent-tabs-mode:nil
359 fill-column:99
360 End:
361 */
362 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :