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