nr-filter.cpp revision 3a3365c39fe3932d005ae1aa4324ef48606f5e18
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free/*
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free * SVG filters rendering
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free *
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free * Author:
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free * Niko Kiirala <niko@kiirala.com>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free *
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen * Copyright (C) 2006-2008 Niko Kiirala
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free *
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen * Released under GNU GPL, read the file 'COPYING' for more information
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free */
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <glib.h>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <cmath>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <cstring>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <string>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <cairo.h>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-primitive.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-slot.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-types.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-units.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-blend.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-composite.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-convolve-matrix.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-colormatrix.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-component-transfer.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-diffuselighting.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-displacement-map.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-flood.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-gaussian.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-image.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-merge.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-morphology.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-offset.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/nr-filter-specularlighting.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-tile.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/nr-filter-turbulence.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/drawing.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "display/drawing-item.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen#include "display/drawing-context.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <2geom/affine.h>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include <2geom/rect.h>
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "svg/svg-length.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "sp-filter-units.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "preferences.h"
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#if defined (SOLARIS) && (SOLARIS == 8)
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#include "round.h"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeusing Inkscape::round;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free#endif
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelennamespace Inkscape {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freenamespace Filters {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeusing Geom::X;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeusing Geom::Y;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeFilter::Filter()
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free{
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _common_init();
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. EngelenFilter::Filter(int n)
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free{
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (n > 0) _primitive.reserve(n);
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen _common_init();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelenvoid Filter::_common_init() {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _slot_count = 1;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // Having "not set" here as value means the output of last filter
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // primitive will be used as output of this filter
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _output_slot = NR_FILTER_SLOT_NOT_SET;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // These are the default values for filter region,
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // as specified in SVG standard
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // NB: SVGLength.set takes prescaled percent values: -.10 means -10%
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_x.set(SVGLength::PERCENT, -.10, 0);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_y.set(SVGLength::PERCENT, -.10, 0);
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen _region_width.set(SVGLength::PERCENT, 1.20, 0);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_height.set(SVGLength::PERCENT, 1.20, 0);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // Filter resolution, negative value here stands for "automatic"
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _x_pixels = -1.0;
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen _y_pixels = -1.0;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen _filter_units = SP_FILTER_UNITS_OBJECTBOUNDINGBOX;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _primitive_units = SP_FILTER_UNITS_USERSPACEONUSE;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeFilter::~Filter()
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free{
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free clear_primitives();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelenint Filter::render(Inkscape::DrawingItem const *item, DrawingContext &bgct, DrawingContext &graphic)
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free{
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_primitive.empty()) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // when no primitives are defined, clear source graphic
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen graphic.setSource(0,0,0,0);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free graphic.setOperator(CAIRO_OPERATOR_SOURCE);
dca8db2be67c87ad222c2c58cca33d3e57f1b650tavmjong-free graphic.paint();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free graphic.setOperator(CAIRO_OPERATOR_OVER);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return 1;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free FilterQuality const filterquality = (FilterQuality)item->drawing().filterQuality();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free int const blurquality = item->drawing().blurQuality();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Affine trans = item->ctm();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen Geom::Rect item_bbox;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::OptRect maybe_bbox = item->itemBounds();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (maybe_bbox.isEmpty()) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // Code below needs a bounding box
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return 1;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free item_bbox = *maybe_bbox;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (item_bbox.hasZeroArea()) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // It's no use to try and filter an empty object.
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return 1;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Rect filter_area = filter_effect_area(item_bbox);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free FilterUnits units(_filter_units, _primitive_units);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_ctm(trans);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_item_bbox(item_bbox);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_filter_area(filter_area);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free std::pair<double,double> resolution
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free = _filter_resolution(filter_area, trans, filterquality);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (!(resolution.first > 0 && resolution.second > 0)) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // zero resolution - clear source graphic and return
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free graphic.setSource(0,0,0,0);
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen graphic.setOperator(CAIRO_OPERATOR_SOURCE);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free graphic.paint();
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen graphic.setOperator(CAIRO_OPERATOR_OVER);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return 1;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_resolution(resolution.first, resolution.second);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_x_pixels > 0) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_automatic_resolution(false);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free else {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_automatic_resolution(true);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_paraller(false);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Affine pbtrans = units.get_matrix_display2pb();
94bd7adc978850d91de1a92a8b5c05d01162a74cJohan B. C. Engelen for (unsigned i = 0 ; i < _primitive.size() ; i++) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (!_primitive[i]->can_handle_affine(pbtrans)) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free units.set_paraller(true);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free break;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free FilterSlot slot(const_cast<Inkscape::DrawingItem*>(item), bgct, graphic, units);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free slot.set_quality(filterquality);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free slot.set_blurquality(blurquality);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner
81efba162b1fc851b6bf0a5ca33928c37effa779apenner for (unsigned i = 0 ; i < _primitive.size() ; i++) {
81efba162b1fc851b6bf0a5ca33928c37effa779apenner _primitive[i]->render_cairo(slot);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner }
81efba162b1fc851b6bf0a5ca33928c37effa779apenner
81efba162b1fc851b6bf0a5ca33928c37effa779apenner Geom::Point origin = graphic.targetLogicalBounds().min();
81efba162b1fc851b6bf0a5ca33928c37effa779apenner cairo_surface_t *result = slot.get_result(_output_slot);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner graphic.setSource(result, origin[Geom::X], origin[Geom::Y]);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner graphic.setOperator(CAIRO_OPERATOR_SOURCE);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner graphic.paint();
81efba162b1fc851b6bf0a5ca33928c37effa779apenner graphic.setOperator(CAIRO_OPERATOR_OVER);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner cairo_surface_destroy(result);
81efba162b1fc851b6bf0a5ca33928c37effa779apenner
81efba162b1fc851b6bf0a5ca33928c37effa779apenner return 0;
81efba162b1fc851b6bf0a5ca33928c37effa779apenner}
81efba162b1fc851b6bf0a5ca33928c37effa779apenner
81efba162b1fc851b6bf0a5ca33928c37effa779apennervoid Filter::set_filter_units(SPFilterUnits unit) {
81efba162b1fc851b6bf0a5ca33928c37effa779apenner _filter_units = unit;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freevoid Filter::set_primitive_units(SPFilterUnits unit) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _primitive_units = unit;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freevoid Filter::area_enlarge(Geom::IntRect &bbox, Inkscape::DrawingItem const *item) const {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free NRRectL b(bbox);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free for (unsigned i = 0 ; i < _primitive.size() ; i++) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_primitive[i]) _primitive[i]->area_enlarge(b, item->ctm());
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free bbox = *b.upgrade_2geom();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free/*
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free TODO: something. See images at the bottom of filters.svg with medium-low
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free filtering quality.
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Inkscape::Preferences *prefs = Inkscape::Preferences::get();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free FilterQuality const filterquality = (FilterQuality)prefs->getInt("/options/filterquality/value");
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_x_pixels <= 0 && (filterquality == FILTER_QUALITY_BEST ||
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free filterquality == FILTER_QUALITY_BETTER)) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Rect item_bbox;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::OptRect maybe_bbox = item->itemBounds();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (maybe_bbox.isEmpty()) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // Code below needs a bounding box
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free item_bbox = *maybe_bbox;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free std::pair<double,double> res_low
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free = _filter_resolution(item_bbox, item->ctm(), filterquality);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free //std::pair<double,double> res_full
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free // = _filter_resolution(item_bbox, item->ctm(), FILTER_QUALITY_BEST);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free double pixels_per_block = fmax(item_bbox.width() / res_low.first,
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free item_bbox.height() / res_low.second);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free bbox.x0 -= (int)pixels_per_block;
81efba162b1fc851b6bf0a5ca33928c37effa779apenner bbox.x1 += (int)pixels_per_block;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free bbox.y0 -= (int)pixels_per_block;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free bbox.y1 += (int)pixels_per_block;
81efba162b1fc851b6bf0a5ca33928c37effa779apenner*/
81efba162b1fc851b6bf0a5ca33928c37effa779apenner}
81efba162b1fc851b6bf0a5ca33928c37effa779apenner
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeGeom::IntRect Filter::compute_drawbox(Inkscape::DrawingItem const *item, Geom::Rect const &item_bbox) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Rect enlarged = filter_effect_area(item_bbox);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free enlarged *= item->ctm();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::IntRect ret(enlarged.roundOutwards());
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free return ret;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free}
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-freeGeom::Rect Filter::filter_effect_area(Geom::Rect const &bbox)
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free{
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free Geom::Point minp, maxp;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free double len_x = bbox.width();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free double len_y = bbox.height();
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free /* TODO: fetch somehow the object ex and em lengths */
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_x.update(12, 6, len_x);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_y.update(12, 6, len_y);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_width.update(12, 6, len_x);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free _region_height.update(12, 6, len_y);
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_region_x.unit == SVGLength::PERCENT) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free minp[X] = bbox.min()[X] + _region_x.computed;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free } else {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free minp[X] = bbox.min()[X] + _region_x.computed * len_x;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_region_width.unit == SVGLength::PERCENT) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free maxp[X] = minp[X] + _region_width.computed;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free } else {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free maxp[X] = minp[X] + _region_width.computed * len_x;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_region_y.unit == SVGLength::PERCENT) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free minp[Y] = bbox.min()[Y] + _region_y.computed;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free } else {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free minp[Y] = bbox.min()[Y] + _region_y.computed * len_y;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free if (_region_height.unit == SVGLength::PERCENT) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free maxp[Y] = minp[Y] + _region_height.computed;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free } else {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free maxp[Y] = minp[Y] + _region_height.computed * len_y;
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free }
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free /* TODO: make sure bbox and fe region are in same coordinate system */
895229950881e4c5be06fc7999ed0ef9aea62e92tavmjong-free minp[X] = _region_x.computed;
maxp[X] = minp[X] + _region_width.computed;
minp[Y] = _region_y.computed;
maxp[Y] = minp[Y] + _region_height.computed;
} else {
g_warning("Error in Inkscape::Filters::Filter::filter_effect_area: unrecognized value of _filter_units");
}
Geom::Rect area(minp, maxp);
return area;
}
/* Constructor table holds pointers to static methods returning filter
* primitives. This table is indexed with FilterPrimitiveType, so that
* for example method in _constructor[NR_FILTER_GAUSSIANBLUR]
* returns a filter object of type Inkscape::Filters::FilterGaussian.
*/
typedef FilterPrimitive*(*FilterConstructor)();
static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
void Filter::_create_constructor_table()
{
// Constructor table won't change in run-time, so no need to recreate
static bool created = false;
if(created) return;
/* Some filter classes are not implemented yet.
Some of them still have only boilerplate code.*/
_constructor[NR_FILTER_BLEND] = &FilterBlend::create;
_constructor[NR_FILTER_COLORMATRIX] = &FilterColorMatrix::create;
_constructor[NR_FILTER_COMPONENTTRANSFER] = &FilterComponentTransfer::create;
_constructor[NR_FILTER_COMPOSITE] = &FilterComposite::create;
_constructor[NR_FILTER_CONVOLVEMATRIX] = &FilterConvolveMatrix::create;
_constructor[NR_FILTER_DIFFUSELIGHTING] = &FilterDiffuseLighting::create;
_constructor[NR_FILTER_DISPLACEMENTMAP] = &FilterDisplacementMap::create;
_constructor[NR_FILTER_FLOOD] = &FilterFlood::create;
_constructor[NR_FILTER_GAUSSIANBLUR] = &FilterGaussian::create;
_constructor[NR_FILTER_IMAGE] = &FilterImage::create;
_constructor[NR_FILTER_MERGE] = &FilterMerge::create;
_constructor[NR_FILTER_MORPHOLOGY] = &FilterMorphology::create;
_constructor[NR_FILTER_OFFSET] = &FilterOffset::create;
_constructor[NR_FILTER_SPECULARLIGHTING] = &FilterSpecularLighting::create;
_constructor[NR_FILTER_TILE] = &FilterTile::create;
_constructor[NR_FILTER_TURBULENCE] = &FilterTurbulence::create;
created = true;
}
int Filter::add_primitive(FilterPrimitiveType type)
{
_create_constructor_table();
// Check that we can create a new filter of specified type
if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
return -1;
if (!_constructor[type]) return -1;
FilterPrimitive *created = _constructor[type]();
int handle = _primitive.size();
_primitive.push_back(created);
return handle;
}
int Filter::replace_primitive(int target, FilterPrimitiveType type)
{
_create_constructor_table();
// Check that target is valid primitive inside this filter
if (target < 0) return -1;
if (static_cast<unsigned>(target) >= _primitive.size()) return -1;
// Check that we can create a new filter of specified type
if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
return -1;
if (!_constructor[type]) return -1;
FilterPrimitive *created = _constructor[type]();
delete _primitive[target];
_primitive[target] = created;
return target;
}
FilterPrimitive *Filter::get_primitive(int handle) {
if (handle < 0 || handle >= static_cast<int>(_primitive.size())) return NULL;
return _primitive[handle];
}
void Filter::clear_primitives()
{
for (unsigned i = 0 ; i < _primitive.size() ; i++) {
delete _primitive[i];
}
_primitive.clear();
}
void Filter::set_x(SVGLength const &length)
{
if (length._set)
_region_x = length;
}
void Filter::set_y(SVGLength const &length)
{
if (length._set)
_region_y = length;
}
void Filter::set_width(SVGLength const &length)
{
if (length._set)
_region_width = length;
}
void Filter::set_height(SVGLength const &length)
{
if (length._set)
_region_height = length;
}
void Filter::set_resolution(double const pixels) {
if (pixels > 0) {
_x_pixels = pixels;
_y_pixels = pixels;
}
}
void Filter::set_resolution(double const x_pixels, double const y_pixels) {
if (x_pixels >= 0 && y_pixels >= 0) {
_x_pixels = x_pixels;
_y_pixels = y_pixels;
}
}
void Filter::reset_resolution() {
_x_pixels = -1;
_y_pixels = -1;
}
int Filter::_resolution_limit(FilterQuality const quality) const {
int limit = -1;
switch (quality) {
case FILTER_QUALITY_WORST:
limit = 32;
break;
case FILTER_QUALITY_WORSE:
limit = 64;
break;
case FILTER_QUALITY_NORMAL:
limit = 256;
break;
case FILTER_QUALITY_BETTER:
case FILTER_QUALITY_BEST:
default:
break;
}
return limit;
}
std::pair<double,double> Filter::_filter_resolution(
Geom::Rect const &area, Geom::Affine const &trans,
FilterQuality const filterquality) const
{
std::pair<double,double> resolution;
if (_x_pixels > 0) {
double y_len;
if (_y_pixels > 0) {
y_len = _y_pixels;
} else {
y_len = (_x_pixels * (area.max()[Y] - area.min()[Y]))
/ (area.max()[X] - area.min()[X]);
}
resolution.first = _x_pixels;
resolution.second = y_len;
} else {
Geom::Point origo = area.min();
origo *= trans;
Geom::Point max_i(area.max()[X], area.min()[Y]);
max_i *= trans;
Geom::Point max_j(area.min()[X], area.max()[Y]);
max_j *= trans;
double i_len = sqrt((origo[X] - max_i[X]) * (origo[X] - max_i[X])
+ (origo[Y] - max_i[Y]) * (origo[Y] - max_i[Y]));
double j_len = sqrt((origo[X] - max_j[X]) * (origo[X] - max_j[X])
+ (origo[Y] - max_j[Y]) * (origo[Y] - max_j[Y]));
int limit = _resolution_limit(filterquality);
if (limit > 0 && (i_len > limit || j_len > limit)) {
double aspect_ratio = i_len / j_len;
if (i_len > j_len) {
i_len = limit;
j_len = i_len / aspect_ratio;
}
else {
j_len = limit;
i_len = j_len * aspect_ratio;
}
}
resolution.first = i_len;
resolution.second = j_len;
}
return resolution;
}
} /* namespace Filters */
} /* namespace Inkscape */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :