Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Pruned forward header.
[inkscape.git] / src / display / nr-filter.cpp
index 346e61686a3ffe460b511693602cfffa5b5d14b5..5ff9d2da5ea5366090d46344c44f4ae72de7912d 100644 (file)
@@ -6,38 +6,84 @@
  * Author:
  *   Niko Kiirala <niko@kiirala.com>
  *
- * Copyright (C) 2006 Niko Kiirala
+ * Copyright (C) 2006-2008 Niko Kiirala
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
 #include <glib.h>
+#include <cmath>
+#include <cstring>
+#include <string>
 
 #include "display/nr-filter.h"
 #include "display/nr-filter-primitive.h"
-#include "display/nr-filter-gaussian.h"
 #include "display/nr-filter-slot.h"
 #include "display/nr-filter-types.h"
-#include "display/pixblock-scaler.h"
-
+#include "display/nr-filter-units.h"
+
+#include "display/nr-filter-blend.h"
+#include "display/nr-filter-composite.h"
+#include "display/nr-filter-convolve-matrix.h"
+#include "display/nr-filter-colormatrix.h"
+#include "display/nr-filter-component-transfer.h"
+#include "display/nr-filter-diffuselighting.h"
+#include "display/nr-filter-displacement-map.h"
+#include "display/nr-filter-flood.h"
+#include "display/nr-filter-gaussian.h"
+#include "display/nr-filter-image.h"
+#include "display/nr-filter-merge.h"
+#include "display/nr-filter-morphology.h"
+#include "display/nr-filter-offset.h"
+#include "display/nr-filter-specularlighting.h"
+#include "display/nr-filter-tile.h"
+#include "display/nr-filter-turbulence.h"
+
+#include "display/nr-arena.h"
 #include "display/nr-arena-item.h"
 #include "libnr/nr-pixblock.h"
 #include "libnr/nr-blit.h"
-#include "libnr/nr-matrix.h"
-#include "libnr/nr-scale.h"
+#include <2geom/matrix.h>
+#include <2geom/rect.h>
 #include "svg/svg-length.h"
 #include "sp-filter-units.h"
+#include "preferences.h"
 
-//#include "display/nr-arena-shape.h"
+#if defined (SOLARIS) && (SOLARIS == 8)
+#include "round.h"
+using Inkscape::round;
+#endif
 
-namespace NR {
+namespace Inkscape {
+namespace Filters {
+
+using Geom::X;
+using Geom::Y;
+
+static Geom::OptRect get_item_bbox(NRArenaItem const *item) {
+    Geom::Rect item_bbox;
+    if (item->item_bbox) {
+        item_bbox = *(item->item_bbox);
+    } else {
+        // Bounding box might not exist, so create a dummy one.
+        Geom::Point zero(0, 0);
+        item_bbox = Geom::Rect(zero, zero);
+    }
+    if (item_bbox.min()[X] > item_bbox.max()[X]
+        || item_bbox.min()[Y] > item_bbox.max()[Y])
+    {
+        // In case of negative-size bbox, return an empty OptRect
+        return Geom::OptRect();
+    }
+    return Geom::OptRect(item_bbox);
+}
 
 Filter::Filter()
 {
     _primitive_count = 0;
     _primitive_table_size = 1;
     _primitive = new FilterPrimitive*[1];
-       _primitive[0] = NULL;
+    _primitive[0] = NULL;
     //_primitive_count = 1;
     //_primitive[0] = new FilterGaussian;
     _common_init();
@@ -46,9 +92,9 @@ Filter::Filter()
 Filter::Filter(int n)
 {
     _primitive_count = 0;
-    _primitive_table_size = n;
-    _primitive = new FilterPrimitive*[n];
-    for ( int i = 0 ; i < n ; i++ ) {
+    _primitive_table_size = (n > 0) ? n : 1;    // we guarantee there is at least 1(one) filter slot
+    _primitive = new FilterPrimitive*[_primitive_table_size];
+    for ( int i = 0 ; i < _primitive_table_size ; i++ ) {
         _primitive[i] = NULL;
     }
     _common_init();
@@ -85,76 +131,91 @@ Filter::~Filter()
 
 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
 {
-    Matrix trans = *item->ctm;
-    FilterSlot slot(_slot_count, item);
-    NRPixBlock *in = new NRPixBlock;
+    if (!_primitive[0]) {
+        // TODO: Should clear the input buffer instead of just returning
+       return 1;
+    }
 
-    // First, if filter resolution is not set to automatic, we should
-    // scale the input image to correct resolution
-    if (_x_pixels >= 0) {
-        /* If filter resolution is zero, the object should not be rendered */
-        if (_x_pixels == 0 || _y_pixels == 0) {
-            int size = (pb->area.x1 - pb->area.x0)
-                * (pb->area.y1 - pb->area.y0)
-                * NR_PIXBLOCK_BPP(pb);
-            memset(NR_PIXBLOCK_PX(pb), 0, size);
-            return 0;
+    FilterQuality const filterquality = (FilterQuality)item->arena->filterquality;
+    int const blurquality = item->arena->blurquality;
+
+    Geom::Matrix trans = item->ctm;
+    FilterSlot slot(_slot_count, item);
+    slot.set_quality(filterquality);
+    slot.set_blurquality(blurquality);
+
+    Geom::Rect item_bbox;
+    {
+        Geom::OptRect maybe_bbox = get_item_bbox(item);
+        if (maybe_bbox.isEmpty()) {
+            // Code below needs a bounding box
+            return 1;
         }
-        // Resolution is specified as pixel length of our internal buffer.
-        // Though, we might not be rendering the whole object at time,
-        // so we need to calculate the correct pixel size
-        int x_len = (int)round(((pb->area.x1 - pb->area.x0) * _x_pixels) / (item->bbox.x1 - item->bbox.x0));
-        if (x_len < 1) x_len = 1;
-        // If y-resolution is also set, count y-area in the same way as x-area
-        // Otherwise, make y-area so, that aspect ratio of input pixblock and
-        // internal pixblock are the same.
-        int y_len;
-        if (_y_pixels > 0) {
-            y_len = (int)round(((pb->area.y1 - pb->area.y0) * _y_pixels) / (item->bbox.y1 - item->bbox.y0));
-        } else {
-            y_len = (int)round((x_len * (pb->area.y1 - pb->area.y0)) / (double)(pb->area.x1 - pb->area.x0));
+        item_bbox = *maybe_bbox;
+    }
+
+    Geom::Rect filter_area = filter_effect_area(item_bbox);
+    if (item_bbox.hasZeroArea()) {
+        // It's no use to try and filter an empty object.
+        return 1;
+    }
+
+    FilterUnits units(_filter_units, _primitive_units);
+    units.set_ctm(trans);
+    units.set_item_bbox(item_bbox);
+    units.set_filter_area(filter_area);
+
+    // TODO: with filterRes of 0x0 should return an empty image
+    std::pair<double,double> resolution
+        = _filter_resolution(filter_area, trans, filterquality);
+    if(!(resolution.first > 0 && resolution.second > 0))
+       return 1;
+    units.set_resolution(resolution.first, resolution.second);
+    if (_x_pixels > 0) {
+        units.set_automatic_resolution(false);
+    }
+    else {
+        units.set_automatic_resolution(true);
+    }
+
+    units.set_paraller(false);
+    for (int i = 0 ; i < _primitive_count ; i++) {
+        if (_primitive[i]->get_input_traits() & TRAIT_PARALLER) {
+            units.set_paraller(true);
+            break;
         }
-        if (y_len < 1) y_len = 1;
-        nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true);
-        if (in->data.px == NULL) // memory allocation failed
-            return 0;
-        scale_bicubic(in, pb);
-        scale res_scaling(x_len / (double)(pb->area.x1 - pb->area.x0),
-                          y_len / (double)(pb->area.y1 - pb->area.y0));
-        trans *= res_scaling;
-    } else {
-        // If filter resolution is automatic, just make copy of input image
-        nr_pixblock_setup_fast(in, pb->mode,
-                               pb->area.x0, pb->area.y0,
-                               pb->area.x1, pb->area.y1, true);
-        if (in->data.px == NULL) // memory allocation failed
-            return 0;
-        nr_blit_pixblock_pixblock(in, pb);
     }
-    slot.set(NR_FILTER_SOURCEGRAPHIC, in);
-    in = NULL; // in is now handled by FilterSlot, we should not touch it
 
-    // TODO: loop through the primitives and render them one at a time
-    if(_primitive[0])
-               _primitive[0]->render(slot, trans);
+    slot.set_units(units);
 
-    NRPixBlock *out = slot.get(_output_slot);
+    NRPixBlock *in = new NRPixBlock;
+    nr_pixblock_setup_fast(in, pb->mode, pb->area.x0, pb->area.y0,
+                           pb->area.x1, pb->area.y1, true);
+    if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) {
+        g_warning("Inkscape::Filters::Filter::render: failed to reserve temporary buffer");
+        return 0;
+    }
+    nr_blit_pixblock_pixblock(in, pb);
+    in->empty = FALSE;
+    slot.set(NR_FILTER_SOURCEGRAPHIC, in);
 
-    // Clear the pixblock, where the output will be put
-    // -> the original image does not show through
-    int size = (pb->area.x1 - pb->area.x0)
-        * (pb->area.y1 - pb->area.y0)
-        * NR_PIXBLOCK_BPP(pb);
-    memset(NR_PIXBLOCK_PX(pb), 0, size);
+    // Check that we are rendering a non-empty area
+    in = slot.get(NR_FILTER_SOURCEGRAPHIC);
+    if (in->area.x1 - in->area.x0 <= 0 || in->area.y1 - in->area.y0 <= 0) {
+        if (in->area.x1 - in->area.x0 < 0 || in->area.y1 - in->area.y0 < 0) {
+            g_warning("Inkscape::Filters::Filter::render: negative area! (%d, %d) (%d, %d)",
+                      in->area.x0, in->area.y0, in->area.x1, in->area.y1);
+        }
+        return 0;
+    }
+    in = NULL; // in is now handled by FilterSlot, we should not touch it
 
-    // If the filter resolution is automatic, just copy our final image
-    // to output pixblock, otherwise use bicubic scaling
-    if (_x_pixels < 0) {
-        nr_blit_pixblock_pixblock(pb, out);
-    } else {
-        scale_bicubic(pb, out);
+    for (int i = 0 ; i < _primitive_count ; i++) {
+        _primitive[i]->render(slot, units);
     }
 
+    slot.get_final(_output_slot, pb);
+
     // Take note of the amount of used image slots
     // -> next time this filter is rendered, we can reserve enough slots
     // immediately
@@ -162,22 +223,75 @@ int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
     return 0;
 }
 
-int Filter::get_enlarge(Matrix const &m)
-{
-    // Just sum the enlargement factor of all filter elements.
-    // TODO: this both sucks and blows for filters like feOffset
-    // -> ditch this method and design a better one...
-    int enlarge = 0;
-    for ( int i = 0 ; i < _primitive_count ; i++ ) {
-        if(_primitive[i]) enlarge += _primitive[i]->get_enlarge(m);
+void Filter::set_filter_units(SPFilterUnits unit) {
+    _filter_units = unit;
+}
+
+void Filter::set_primitive_units(SPFilterUnits unit) {
+    _primitive_units = unit;
+}
+
+void Filter::area_enlarge(NRRectL &bbox, NRArenaItem const *item) const {
+    for (int i = 0 ; i < _primitive_count ; i++) {
+        if (_primitive[i]) _primitive[i]->area_enlarge(bbox, item->ctm);
+    }
+/*
+  TODO: something. See images at the bottom of filters.svg with medium-low
+  filtering quality.
+
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    FilterQuality const filterquality = (FilterQuality)prefs->getInt("/options/filterquality/value");
+
+    if (_x_pixels <= 0 && (filterquality == FILTER_QUALITY_BEST ||
+                           filterquality == FILTER_QUALITY_BETTER)) {
+        return;
+    }
+
+    Geom::Rect item_bbox;
+    Geom::OptRect maybe_bbox = get_item_bbox(item);
+    if (maybe_bbox.isEmpty()) {
+        // Code below needs a bounding box
+        return;
     }
-    return enlarge;
+    item_bbox = *maybe_bbox;
+
+    std::pair<double,double> res_low
+        = _filter_resolution(item_bbox, item->ctm, filterquality);
+    //std::pair<double,double> res_full
+    //    = _filter_resolution(item_bbox, item->ctm, FILTER_QUALITY_BEST);
+    double pixels_per_block = fmax(item_bbox.width() / res_low.first,
+                                   item_bbox.height() / res_low.second);
+    bbox.x0 -= (int)pixels_per_block;
+    bbox.x1 += (int)pixels_per_block;
+    bbox.y0 -= (int)pixels_per_block;
+    bbox.y1 += (int)pixels_per_block;
+*/
+}
+
+void Filter::bbox_enlarge(NRRectL &bbox) {
+    // Modifying empty bounding boxes confuses rest of the renderer, so
+    // let's not do that.
+    if (bbox.x0 > bbox.x1 || bbox.y0 > bbox.y1) return;
+
+    /* TODO: this is wrong. Should use bounding box in user coordinates
+     * and find its extents in display coordinates. */
+    Geom::Point min(bbox.x0, bbox.y0);
+    Geom::Point max(bbox.x1, bbox.y1);
+    Geom::Rect tmp_bbox(min, max);
+
+    Geom::Rect enlarged = filter_effect_area(tmp_bbox);
+
+    bbox.x0 = (NR::ICoord)enlarged.min()[X];
+    bbox.y0 = (NR::ICoord)enlarged.min()[Y];
+    bbox.x1 = (NR::ICoord)enlarged.max()[X];
+    bbox.y1 = (NR::ICoord)enlarged.max()[Y];
 }
 
-void Filter::bbox_enlarge(NRRectL &bbox)
+Geom::Rect Filter::filter_effect_area(Geom::Rect const &bbox)
 {
-    int len_x = bbox.x1 - bbox.x0;
-    int len_y = bbox.y1 - bbox.y0;
+    Geom::Point minp, maxp;
+    double len_x = bbox.max()[X] - bbox.min()[X];
+    double len_y = bbox.max()[Y] - bbox.min()[Y];
     /* TODO: fetch somehow the object ex and em lengths */
     _region_x.update(12, 6, len_x);
     _region_y.update(12, 6, len_y);
@@ -185,41 +299,43 @@ void Filter::bbox_enlarge(NRRectL &bbox)
     _region_height.update(12, 6, len_y);
     if (_filter_units == SP_FILTER_UNITS_OBJECTBOUNDINGBOX) {
         if (_region_x.unit == SVGLength::PERCENT) {
-            bbox.x0 += (ICoord)_region_x.computed;
+            minp[X] = bbox.min()[X] + _region_x.computed;
         } else {
-            bbox.x0 += (ICoord)(_region_x.computed * len_x);
+            minp[X] = bbox.min()[X] + _region_x.computed * len_x;
         }
         if (_region_width.unit == SVGLength::PERCENT) {
-            bbox.x1 = bbox.x0 + (ICoord)_region_width.computed;
+            maxp[X] = minp[X] + _region_width.computed;
         } else {
-            bbox.x1 = bbox.x0 + (ICoord)(_region_width.computed * len_x);
+            maxp[X] = minp[X] + _region_width.computed * len_x;
         }
 
         if (_region_y.unit == SVGLength::PERCENT) {
-            bbox.y0 += (ICoord)_region_y.computed;
+            minp[Y] = bbox.min()[Y] + _region_y.computed;
         } else {
-            bbox.y0 += (ICoord)(_region_y.computed * len_y);
+            minp[Y] = bbox.min()[Y] + _region_y.computed * len_y;
         }
         if (_region_height.unit == SVGLength::PERCENT) {
-            bbox.y1 = bbox.y0 + (ICoord)_region_height.computed;
+            maxp[Y] = minp[Y] + _region_height.computed;
         } else {
-            bbox.y1 = bbox.y0 + (ICoord)(_region_height.computed * len_y);
+            maxp[Y] = minp[Y] + _region_height.computed * len_y;
         }
     } else if (_filter_units == SP_FILTER_UNITS_USERSPACEONUSE) {
         /* TODO: make sure bbox and fe region are in same coordinate system */
-        bbox.x0 = (ICoord) _region_x.computed;
-        bbox.x1 = bbox.x0 + (ICoord) _region_width.computed;
-        bbox.y0 = (ICoord) _region_y.computed;
-        bbox.y1 = bbox.y0 + (ICoord) _region_height.computed;
+        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 NR::Filter::bbox_enlarge: unrecognized value of _filter_units");
+        g_warning("Error in Inkscape::Filters::Filter::bbox_enlarge: 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 NR::FilterGaussian.
+ * returns a filter object of type Inkscape::Filters::FilterGaussian.
  */
 typedef FilterPrimitive*(*FilterConstructor)();
 static FilterConstructor _constructor[NR_FILTER_ENDPRIMITIVETYPE];
@@ -230,23 +346,24 @@ void Filter::_create_constructor_table()
     static bool created = false;
     if(created) return;
 
-    /* Filter effects not yet implemented are set to NULL */
-    _constructor[NR_FILTER_BLEND] = NULL;
-    _constructor[NR_FILTER_COLORMATRIX] = NULL;
-    _constructor[NR_FILTER_COMPONENTTRANSFER] = NULL;
-    _constructor[NR_FILTER_COMPOSITE] = NULL;
-    _constructor[NR_FILTER_CONVOLVEMATRIX] = NULL;
-    _constructor[NR_FILTER_DIFFUSELIGHTING] = NULL;
-    _constructor[NR_FILTER_DISPLACEMENTMAP] = NULL;
-    _constructor[NR_FILTER_FLOOD] = NULL;
+/* 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] = NULL;
-    _constructor[NR_FILTER_MERGE] = NULL;
-    _constructor[NR_FILTER_MORPHOLOGY] = NULL;
-    _constructor[NR_FILTER_OFFSET] = NULL;
-    _constructor[NR_FILTER_SPECULARLIGHTING] = NULL;
-    _constructor[NR_FILTER_TILE] = NULL;
-    _constructor[NR_FILTER_TURBULENCE] = NULL;
+    _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;
 }
 
@@ -263,18 +380,22 @@ void Filter::_enlarge_primitive_table() {
     for (int i = _primitive_count ; i < _primitive_table_size ; i++) {
         new_tbl[i] = NULL;
     }
-    delete[] _primitive;
+    if(_primitive != NULL) {
+        delete[] _primitive;
+    } else {
+        g_warning("oh oh");
+    }
     _primitive = new_tbl;
 }
 
-FilterPrimitive *Filter::add_primitive(FilterPrimitiveType type)
+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 NULL;
-    if (!_constructor[type]) return NULL;
+        return -1;
+    if (!_constructor[type]) return -1;
     FilterPrimitive *created = _constructor[type]();
 
     // If there is no space for new filter primitive, enlarge the table
@@ -283,28 +404,24 @@ FilterPrimitive *Filter::add_primitive(FilterPrimitiveType type)
     }
 
     _primitive[_primitive_count] = created;
+    int handle = _primitive_count;
     _primitive_count++;
-    return created;
+    return handle;
 }
 
-FilterPrimitive *Filter::replace_primitive(FilterPrimitive *target, FilterPrimitiveType type)
+int Filter::replace_primitive(int target, FilterPrimitiveType type)
 {
     _create_constructor_table();
 
     // Check that target is valid primitive inside this filter
-    int place = -1;
-    for (int i = 0 ; i < _primitive_count ; i++) {
-        if (target == _primitive[i]) {
-            place = i;
-            break;
-        }
-    }
-    if (place < 0) return NULL;
+    if (target < 0) return -1;
+    if (target >= _primitive_count) return -1;
+    if (!_primitive[target]) return -1;
 
     // Check that we can create a new filter of specified type
     if (type < 0 || type >= NR_FILTER_ENDPRIMITIVETYPE)
-        return NULL;
-    if (!_constructor[type]) return NULL;
+        return -1;
+    if (!_constructor[type]) return -1;
     FilterPrimitive *created = _constructor[type]();
 
     // If there is no space for new filter primitive, enlarge the table
@@ -312,9 +429,14 @@ FilterPrimitive *Filter::replace_primitive(FilterPrimitive *target, FilterPrimit
         _enlarge_primitive_table();
     }
 
-    delete target;
-    _primitive[place] = created;
-    return created;
+    delete _primitive[target];
+    _primitive[target] = created;
+    return target;
+}
+
+FilterPrimitive *Filter::get_primitive(int handle) {
+    if (handle < 0 || handle >= _primitive_count) return NULL;
+    return _primitive[handle];
 }
 
 void Filter::clear_primitives()
@@ -325,16 +447,112 @@ void Filter::clear_primitives()
     _primitive_count = 0;
 }
 
-void Filter::set_x(SVGLength &lenght)
-{ /*write me*/ }
-void Filter::set_y(SVGLength &length)
-{ /*write me*/ }
-void Filter::set_width(SVGLength &length)
-{ /*write me*/ }
-void Filter::set_height(SVGLength &length)
-{ /*write me*/ }
+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::Matrix 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 NR */
+} /* namespace Filters */
+} /* namespace Inkscape */
 
 /*
   Local Variables:
@@ -345,4 +563,4 @@ void Filter::set_height(SVGLength &length)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :