1
#define __NR_FILTER_CPP__
4
* SVG filters rendering
7
* Niko Kiirala <niko@kiirala.com>
9
* Copyright (C) 2006-2008 Niko Kiirala
11
* Released under GNU GPL, read the file 'COPYING' for more information
19
#include "display/nr-filter.h"
20
#include "display/nr-filter-primitive.h"
21
#include "display/nr-filter-slot.h"
22
#include "display/nr-filter-types.h"
23
#include "display/nr-filter-units.h"
25
#include "display/nr-filter-blend.h"
26
#include "display/nr-filter-composite.h"
27
#include "display/nr-filter-convolve-matrix.h"
28
#include "display/nr-filter-colormatrix.h"
29
#include "display/nr-filter-component-transfer.h"
30
#include "display/nr-filter-diffuselighting.h"
31
#include "display/nr-filter-displacement-map.h"
32
#include "display/nr-filter-flood.h"
33
#include "display/nr-filter-gaussian.h"
34
#include "display/nr-filter-image.h"
35
#include "display/nr-filter-merge.h"
36
#include "display/nr-filter-morphology.h"
37
#include "display/nr-filter-offset.h"
38
#include "display/nr-filter-specularlighting.h"
39
#include "display/nr-filter-tile.h"
40
#include "display/nr-filter-turbulence.h"
42
#include "display/nr-arena.h"
43
#include "display/nr-arena-item.h"
44
#include "libnr/nr-pixblock.h"
45
#include "libnr/nr-blit.h"
46
#include <2geom/matrix.h>
47
#include <2geom/rect.h>
48
#include "svg/svg-length.h"
49
#include "sp-filter-units.h"
50
#include "preferences.h"
52
#if defined (SOLARIS) && (SOLARIS == 8)
54
using Inkscape::round;
63
static Geom::OptRect get_item_bbox(NRArenaItem const *item) {
65
if (item->item_bbox) {
66
item_bbox = *(item->item_bbox);
68
// Bounding box might not exist, so create a dummy one.
69
Geom::Point zero(0, 0);
70
item_bbox = Geom::Rect(zero, zero);
72
if (item_bbox.min()[X] > item_bbox.max()[X]
73
|| item_bbox.min()[Y] > item_bbox.max()[Y])
75
// In case of negative-size bbox, return an empty OptRect
76
return Geom::OptRect();
78
return Geom::OptRect(item_bbox);
84
_primitive_table_size = 1;
85
_primitive = new FilterPrimitive*[1];
87
//_primitive_count = 1;
88
//_primitive[0] = new FilterGaussian;
95
_primitive_table_size = n;
96
_primitive = new FilterPrimitive*[n];
97
for ( int i = 0 ; i < n ; i++ ) {
103
void Filter::_common_init() {
105
// Having "not set" here as value means the output of last filter
106
// primitive will be used as output of this filter
107
_output_slot = NR_FILTER_SLOT_NOT_SET;
109
// These are the default values for filter region,
110
// as specified in SVG standard
111
// NB: SVGLength.set takes prescaled percent values: -.10 means -10%
112
_region_x.set(SVGLength::PERCENT, -.10, 0);
113
_region_y.set(SVGLength::PERCENT, -.10, 0);
114
_region_width.set(SVGLength::PERCENT, 1.20, 0);
115
_region_height.set(SVGLength::PERCENT, 1.20, 0);
117
// Filter resolution, negative value here stands for "automatic"
121
_filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
122
_primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
132
int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
134
if (!_primitive[0]) {
135
// TODO: Should clear the input buffer instead of just returning
139
FilterQuality const filterquality = (FilterQuality)item->arena->filterquality;
140
int const blurquality = item->arena->blurquality;
142
Geom::Matrix trans = item->ctm;
143
FilterSlot slot(_slot_count, item);
144
slot.set_quality(filterquality);
145
slot.set_blurquality(blurquality);
147
Geom::Rect item_bbox;
149
Geom::OptRect maybe_bbox = get_item_bbox(item);
150
if (maybe_bbox.isEmpty()) {
151
// Code below needs a bounding box
154
item_bbox = *maybe_bbox;
157
Geom::Rect filter_area = filter_effect_area(item_bbox);
158
if (item_bbox.hasZeroArea()) {
159
// It's no use to try and filter an empty object.
163
FilterUnits units(_filter_units, _primitive_units);
164
units.set_ctm(trans);
165
units.set_item_bbox(item_bbox);
166
units.set_filter_area(filter_area);
168
// TODO: with filterRes of 0x0 should return an empty image
169
std::pair<double,double> resolution
170
= _filter_resolution(filter_area, trans, filterquality);
171
units.set_resolution(resolution.first, resolution.second);
173
units.set_automatic_resolution(false);
176
units.set_automatic_resolution(true);
179
units.set_paraller(false);
180
for (int i = 0 ; i < _primitive_count ; i++) {
181
if (_primitive[i]->get_input_traits() & TRAIT_PARALLER) {
182
units.set_paraller(true);
187
slot.set_units(units);
189
NRPixBlock *in = new NRPixBlock;
190
nr_pixblock_setup_fast(in, pb->mode, pb->area.x0, pb->area.y0,
191
pb->area.x1, pb->area.y1, true);
192
if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) {
193
g_warning("Inkscape::Filters::Filter::render: failed to reserve temporary buffer");
196
nr_blit_pixblock_pixblock(in, pb);
198
slot.set(NR_FILTER_SOURCEGRAPHIC, in);
200
// Check that we are rendering a non-empty area
201
in = slot.get(NR_FILTER_SOURCEGRAPHIC);
202
if (in->area.x1 - in->area.x0 <= 0 || in->area.y1 - in->area.y0 <= 0) {
203
if (in->area.x1 - in->area.x0 < 0 || in->area.y1 - in->area.y0 < 0) {
204
g_warning("Inkscape::Filters::Filter::render: negative area! (%d, %d) (%d, %d)",
205
in->area.x0, in->area.y0, in->area.x1, in->area.y1);
209
in = NULL; // in is now handled by FilterSlot, we should not touch it
211
for (int i = 0 ; i < _primitive_count ; i++) {
212
_primitive[i]->render(slot, units);
215
slot.get_final(_output_slot, pb);
217
// Take note of the amount of used image slots
218
// -> next time this filter is rendered, we can reserve enough slots
220
_slot_count = slot.get_slot_count();
224
void Filter::area_enlarge(NRRectL &bbox, NRArenaItem const *item) const {
225
for (int i = 0 ; i < _primitive_count ; i++) {
226
if (_primitive[i]) _primitive[i]->area_enlarge(bbox, item->ctm);
229
TODO: something. See images at the bottom of filters.svg with medium-low
232
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
233
FilterQuality const filterquality = (FilterQuality)prefs->getInt("/options/filterquality/value");
235
if (_x_pixels <= 0 && (filterquality == FILTER_QUALITY_BEST ||
236
filterquality == FILTER_QUALITY_BETTER)) {
240
Geom::Rect item_bbox;
241
Geom::OptRect maybe_bbox = get_item_bbox(item);
242
if (maybe_bbox.isEmpty()) {
243
// Code below needs a bounding box
246
item_bbox = *maybe_bbox;
248
std::pair<double,double> res_low
249
= _filter_resolution(item_bbox, item->ctm, filterquality);
250
//std::pair<double,double> res_full
251
// = _filter_resolution(item_bbox, item->ctm, FILTER_QUALITY_BEST);
252
double pixels_per_block = fmax(item_bbox.width() / res_low.first,
253
item_bbox.height() / res_low.second);
254
bbox.x0 -= (int)pixels_per_block;
255
bbox.x1 += (int)pixels_per_block;
256
bbox.y0 -= (int)pixels_per_block;
257
bbox.y1 += (int)pixels_per_block;
261
void Filter::bbox_enlarge(NRRectL &bbox) {
262
// Modifying empty bounding boxes confuses rest of the renderer, so
263
// let's not do that.
264
if (bbox.x0 > bbox.x1 || bbox.y0 > bbox.y1) return;
266
/* TODO: this is wrong. Should use bounding box in user coordinates
267
* and find its extents in display coordinates. */
268
Geom::Point min(bbox.x0, bbox.y0);
269
Geom::Point max(bbox.x1, bbox.y1);
270
Geom::Rect tmp_bbox(min, max);
272
Geom::Rect enlarged = filter_effect_area(tmp_bbox);
274
bbox.x0 = (NR::ICoord)enlarged.min()[X];
275
bbox.y0 = (NR::ICoord)enlarged.min()[Y];
276
bbox.x1 = (NR::ICoord)enlarged.max()[X];
277
bbox.y1 = (NR::ICoord)enlarged.max()[Y];
280
Geom::Rect Filter::filter_effect_area(Geom::Rect const &bbox)
282
Geom::Point minp, maxp;
283
double len_x = bbox.max()[X] - bbox.min()[X];
284
double len_y = bbox.max()[Y] - bbox.min()[Y];
285
/* TODO: fetch somehow the object ex and em lengths */
286
_region_x.update(12, 6, len_x);
287
_region_y.update(12, 6, len_y);
288
_region_width.update(12, 6, len_x);
289
_region_height.update(12, 6, len_y);
290
if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
291
if (_region_x.unit == SVGLength::PERCENT) {
292
minp[X] = bbox.min()[X] + _region_x.computed;
294
minp[X] = bbox.min()[X] + _region_x.computed * len_x;
296
if (_region_width.unit == SVGLength::PERCENT) {
297
maxp[X] = minp[X] + _region_width.computed;
299
maxp[X] = minp[X] + _region_width.computed * len_x;
302
if (_region_y.unit == SVGLength::PERCENT) {
303
minp[Y] = bbox.min()[Y] + _region_y.computed;
305
minp[Y] = bbox.min()[Y] + _region_y.computed * len_y;
307
if (_region_height.unit == SVGLength::PERCENT) {
308
maxp[Y] = minp[Y] + _region_height.computed;
310
maxp[Y] = minp[Y] + _region_height.computed * len_y;
312
} else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
313
/* TODO: make sure bbox and fe region are in same coordinate system */
314
minp[X] = _region_x.computed;
315
maxp[X] = minp[X] + _region_width.computed;
316
minp[Y] = _region_y.computed;
317
maxp[Y] = minp[Y] + _region_height.computed;
319
g_warning("Error in Inkscape::Filters::Filter::bbox_enlarge: unrecognized value of _filter_units");
321
Geom::Rect area(minp, maxp);
325
/* Constructor table holds pointers to static methods returning filter
326
* primitives. This table is indexed with FilterPrimitiveType, so that
327
* for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
328
* returns a filter object of type Inkscape::Filters::FilterGaussian.
330
typedef FilterPrimitive*(*FilterConstructor)();
331
static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
333
void Filter::_create_constructor_table()
335
// Constructor table won't change in run-time, so no need to recreate
336
static bool created = false;
339
/* Some filter classes are not implemented yet.
340
Some of them still have only boilerplate code.*/
341
_constructor[NR_FILTER_BLEND] = &FilterBlend::create;
342
_constructor[NR_FILTER_COLORMATRIX] = &FilterColorMatrix::create;
343
_constructor[NR_FILTER_COMPONENTTRANSFER] = &FilterComponentTransfer::create;
344
_constructor[NR_FILTER_COMPOSITE] = &FilterComposite::create;
345
_constructor[NR_FILTER_CONVOLVEMATRIX] = &FilterConvolveMatrix::create;
346
_constructor[NR_FILTER_DIFFUSELIGHTING] = &FilterDiffuseLighting::create;
347
_constructor[NR_FILTER_DISPLACEMENTMAP] = &FilterDisplacementMap::create;
348
_constructor[NR_FILTER_FLOOD] = &FilterFlood::create;
349
_constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
350
_constructor[NR_FILTER_IMAGE] = &FilterImage::create;
351
_constructor[NR_FILTER_MERGE] = &FilterMerge::create;
352
_constructor[NR_FILTER_MORPHOLOGY] = &FilterMorphology::create;
353
_constructor[NR_FILTER_OFFSET] = &FilterOffset::create;
354
_constructor[NR_FILTER_SPECULARLIGHTING] = &FilterSpecularLighting::create;
355
_constructor[NR_FILTER_TILE] = &FilterTile::create;
356
_constructor[NR_FILTER_TURBULENCE] = &FilterTurbulence::create;
360
/** Helper method for enlarging table of filter primitives. When new
361
* primitives are added, but we have no space for them, this function
362
* makes some more space.
364
void Filter::_enlarge_primitive_table() {
365
FilterPrimitive **new_tbl = new FilterPrimitive*[_primitive_table_size * 2];
366
for (int i = 0 ; i < _primitive_count ; i++) {
367
new_tbl[i] = _primitive[i];
369
_primitive_table_size *= 2;
370
for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
374
_primitive = new_tbl;
377
int Filter::add_primitive(FilterPrimitiveType type)
379
_create_constructor_table();
381
// Check that we can create a new filter of specified type
382
if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
384
if (!_constructor[type]) return -1;
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();
392
_primitive[_primitive_count] = created;
393
int handle = _primitive_count;
398
int Filter::replace_primitive(int target, FilterPrimitiveType type)
400
_create_constructor_table();
402
// Check that target is valid primitive inside this filter
403
if (target < 0) return -1;
404
if (target >= _primitive_count) return -1;
405
if (!_primitive[target]) return -1;
407
// Check that we can create a new filter of specified type
408
if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
410
if (!_constructor[type]) return -1;
411
FilterPrimitive *created = _constructor[type]();
413
// If there is no space for new filter primitive, enlarge the table
414
if (_primitive_count >= _primitive_table_size) {
415
_enlarge_primitive_table();
418
delete _primitive[target];
419
_primitive[target] = created;
423
FilterPrimitive *Filter::get_primitive(int handle) {
424
if (handle < 0 || handle >= _primitive_count) return NULL;
425
return _primitive[handle];
428
void Filter::clear_primitives()
430
for (int i = 0 ; i < _primitive_count ; i++) {
431
if (_primitive[i]) delete _primitive[i];
433
_primitive_count = 0;
436
void Filter::set_x(SVGLength const &length)
441
void Filter::set_y(SVGLength const &length)
446
void Filter::set_width(SVGLength const &length)
449
_region_width = length;
451
void Filter::set_height(SVGLength const &length)
454
_region_height = length;
457
void Filter::set_resolution(double const pixels) {
464
void Filter::set_resolution(double const x_pixels, double const y_pixels) {
465
if (x_pixels >= 0 && y_pixels >= 0) {
466
_x_pixels = x_pixels;
467
_y_pixels = y_pixels;
471
void Filter::reset_resolution() {
476
int Filter::_resolution_limit(FilterQuality const quality) const {
479
case FILTER_QUALITY_WORST:
482
case FILTER_QUALITY_WORSE:
485
case FILTER_QUALITY_NORMAL:
488
case FILTER_QUALITY_BETTER:
489
case FILTER_QUALITY_BEST:
496
std::pair<double,double> Filter::_filter_resolution(
497
Geom::Rect const &area, Geom::Matrix const &trans,
498
FilterQuality const filterquality) const
500
std::pair<double,double> resolution;
506
y_len = (_x_pixels * (area.max()[Y] - area.min()[Y]))
507
/ (area.max()[X] - area.min()[X]);
509
resolution.first = _x_pixels;
510
resolution.second = y_len;
512
Geom::Point origo = area.min();
514
Geom::Point max_i(area.max()[X], area.min()[Y]);
516
Geom::Point max_j(area.min()[X], area.max()[Y]);
518
double i_len = sqrt((origo[X] - max_i[X]) * (origo[X] - max_i[X])
519
+ (origo[Y] - max_i[Y]) * (origo[Y] - max_i[Y]));
520
double j_len = sqrt((origo[X] - max_j[X]) * (origo[X] - max_j[X])
521
+ (origo[Y] - max_j[Y]) * (origo[Y] - max_j[Y]));
522
int limit = _resolution_limit(filterquality);
523
if (limit > 0 && (i_len > limit || j_len > limit)) {
524
double aspect_ratio = i_len / j_len;
527
j_len = i_len / aspect_ratio;
531
i_len = j_len * aspect_ratio;
534
resolution.first = i_len;
535
resolution.second = j_len;
540
} /* namespace Filters */
541
} /* namespace Inkscape */
546
c-file-style:"stroustrup"
547
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
552
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :