Code

added some more boilerplate code on feTurbulence.
[inkscape.git] / src / display / nr-filter.cpp
index 35d8804e789a3c22d072b313c26f02e8e8645fe4..34a33e2b88bbcd4acc53f4916c7a7858c768aee0 100644 (file)
  */
 
 #include <glib.h>
+#include <cmath>
 
 #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/pixblock-transform.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-item.h"
 #include "libnr/nr-pixblock.h"
 #include "libnr/nr-scale.h"
 #include "svg/svg-length.h"
 #include "sp-filter-units.h"
+#if defined (SOLARIS_2_8)
+#include "round.h"
+using Inkscape::round;
+#endif 
+
+__attribute__ ((const))
+inline static int _max4(const double a, const double b,
+                        const double c, const double d) {
+    double ret = a;
+    if (b > ret) ret = b;
+    if (c > ret) ret = c;
+    if (d > ret) ret = d;
+    return (int)round(ret);
+}
 
-//#include "display/nr-arena-shape.h"
+__attribute__ ((const))
+inline static int _min4(const double a, const double b,
+                        const double c, const double d) {
+    double ret = a;
+    if (b < ret) ret = b;
+    if (c < ret) ret = c;
+    if (d < ret) ret = d;
+    return (int)round(ret);
+}
 
 namespace NR {
 
@@ -37,7 +77,7 @@ 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();
@@ -85,13 +125,66 @@ Filter::~Filter()
 
 int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
 {
+    if(!_primitive[0]) { // if there are no primitives, do nothing
+       return 0; 
+    }
+
     Matrix trans = *item->ctm;
+    Matrix paraller_trans = trans;
+    bool notparaller = false;
     FilterSlot slot(_slot_count, item);
     NRPixBlock *in = new NRPixBlock;
 
-    // First, if filter resolution is not set to automatic, we should
-    // scale the input image to correct resolution
-    if (_x_pixels >= 0) {
+    // If filter effects region is not paraller to viewport,
+    // we must first undo the rotation / shear.
+    // It will be redone after filtering.
+    // If there is only rotation and uniform scaling (zoom), let's skip this,
+    // as it will not make a difference with gaussian blur.
+    if ((fabs(trans[1]) > 1e-6 || fabs(trans[2]) > 1e-6) &&
+        !(fabs(trans[0] - trans[3]) < 1e-6 && fabs(trans[1] + trans[2]) < 1e-6)) {
+        notparaller = true;
+
+        // TODO: if filter resolution is specified, scaling should be set
+        // according to that
+        double scaling_factor = sqrt(trans.expansionX() * trans.expansionX() +
+                                     trans.expansionY() * trans.expansionY());
+        scale scaling(scaling_factor, scaling_factor);
+        scale scaling_inv(1.0 / scaling_factor, 1.0 / scaling_factor);
+        trans *= scaling_inv;
+        paraller_trans.set_identity();
+        paraller_trans *= scaling;
+
+        Matrix itrans = trans.inverse();
+        int x0 = pb->area.x0;
+        int y0 = pb->area.y0;
+        int x1 = pb->area.x1;
+        int y1 = pb->area.y1;
+        int min_x = _min4(itrans[0] * x0 + itrans[2] * y0 + itrans[4],
+                          itrans[0] * x0 + itrans[2] * y1 + itrans[4],
+                          itrans[0] * x1 + itrans[2] * y0 + itrans[4],
+                          itrans[0] * x1 + itrans[2] * y1 + itrans[4]);
+        int max_x = _max4(itrans[0] * x0 + itrans[2] * y0 + itrans[4],
+                          itrans[0] * x0 + itrans[2] * y1 + itrans[4],
+                          itrans[0] * x1 + itrans[2] * y0 + itrans[4],
+                          itrans[0] * x1 + itrans[2] * y1 + itrans[4]);
+        int min_y = _min4(itrans[1] * x0 + itrans[3] * y0 + itrans[5],
+                          itrans[1] * x0 + itrans[3] * y1 + itrans[5],
+                          itrans[1] * x1 + itrans[3] * y0 + itrans[5],
+                          itrans[1] * x1 + itrans[3] * y1 + itrans[5]);
+        int max_y = _max4(itrans[1] * x0 + itrans[3] * y0 + itrans[5],
+                          itrans[1] * x0 + itrans[3] * y1 + itrans[5],
+                          itrans[1] * x1 + itrans[3] * y0 + itrans[5],
+                          itrans[1] * x1 + itrans[3] * y1 + itrans[5]);
+        
+        nr_pixblock_setup_fast(in, pb->mode,
+                               min_x, min_y,
+                               max_x, max_y, true);
+        if (in->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) // memory allocation failed
+            return 0;
+        transform_nearest(in, pb, itrans);
+    } else if (_x_pixels >= 0) {
+        // If filter resolution is not set to automatic, we should
+        // scale the input image to correct resolution
         /* 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)
@@ -116,24 +209,28 @@ int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
         }
         if (y_len < 1) y_len = 1;
         nr_pixblock_setup_fast(in, pb->mode, 0, 0, x_len, y_len, true);
+        if (in->size != NR_PIXBLOCK_SIZE_TINY && 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;
+        paraller_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->size != NR_PIXBLOCK_SIZE_TINY && in->data.px == NULL) // memory allocation failed
+            return 0;
         nr_blit_pixblock_pixblock(in, pb);
     }
+    in->empty = FALSE;
     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);
-
+    for (int i = 0 ; i < _primitive_count ; i++) {
+        _primitive[i]->render(slot, paraller_trans);
+    }
     NRPixBlock *out = slot.get(_output_slot);
 
     // Clear the pixblock, where the output will be put
@@ -143,9 +240,11 @@ int Filter::render(NRArenaItem const *item, NRPixBlock *pb)
         * NR_PIXBLOCK_BPP(pb);
     memset(NR_PIXBLOCK_PX(pb), 0, size);
 
-    // If the filter resolution is automatic, just copy our final image
-    // to output pixblock, otherwise use bicubic scaling
-    if (_x_pixels < 0) {
+    if (notparaller) {
+        transform_nearest(pb, out, trans);
+    } else if (_x_pixels < 0) {
+        // If the filter resolution is automatic, just copy our final image
+        // to output pixblock, otherwise use bicubic scaling
         nr_blit_pixblock_pixblock(pb, out);
     } else {
         scale_bicubic(pb, out);
@@ -158,16 +257,10 @@ 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::area_enlarge(NRRectL &bbox, Matrix const &m) {
+    for (int i = 0 ; i < _primitive_count ; i++) {
+        if (_primitive[i]) _primitive[i]->area_enlarge(bbox, m);
     }
-    return enlarge;
 }
 
 void Filter::bbox_enlarge(NRRectL &bbox)
@@ -226,23 +319,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,14 +357,14 @@ void Filter::_enlarge_primitive_table() {
     _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
@@ -279,27 +373,24 @@ FilterPrimitive *Filter::add_primitive(FilterPrimitiveType type)
     }
 
     _primitive[_primitive_count] = created;
-    return created;
+    int handle = _primitive_count;
+    _primitive_count++;
+    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
@@ -307,9 +398,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()
@@ -320,14 +416,45 @@ 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;
+}
 
 } /* namespace NR */