Code

handling of default values and checking for invalid values.
authorjucablues <jucablues@users.sourceforge.net>
Mon, 6 Aug 2007 20:54:33 +0000 (20:54 +0000)
committerjucablues <jucablues@users.sourceforge.net>
Mon, 6 Aug 2007 20:54:33 +0000 (20:54 +0000)
src/display/nr-filter-convolve-matrix.cpp
src/sp-feconvolvematrix.cpp
src/sp-feconvolvematrix.h

index 8ce9241fed2f74eb1a5494ef977bb21af09e231b..562f3ffc470e1c83bc4fca2c01640de26a9854c6 100644 (file)
 namespace NR {
 
 FilterConvolveMatrix::FilterConvolveMatrix()
-: orderX(3),
-  orderY(3),
-  targetX((int)(orderX/2)),
-  targetY((int)(orderY/2)),
-  bias(0),
-  edgeMode((NR::FilterConvolveMatrixEdgeMode) 0),
-  preserveAlpha(false)
 {}
 
 FilterPrimitive * FilterConvolveMatrix::create() {
@@ -55,16 +48,6 @@ int FilterConvolveMatrix::render(FilterSlot &slot, Matrix const &trans) {
     int width = in->area.x1 - in->area.x0;
     int height = in->area.y1 - in->area.y0;
 
-    double div=0;
-
-    if (divisor != 0){
-        div = divisor;
-    } else {
-        for (i=0;i<orderX*orderY;i++){
-            div += kernelMatrix[i];
-        }
-    }
-
     for (x=targetX; x < width - (orderX - targetX); x++){
         for (y=targetY; y < height - (orderY - targetY); y++){
             result_R = 0;
@@ -82,10 +65,10 @@ int FilterConvolveMatrix::render(FilterSlot &slot, Matrix const &trans) {
                 }
             }
 
-            out_data[4*( x + width*y )] = CLAMP_D_TO_U8(result_R / div + bias);
-            out_data[4*( x + width*y )+1] = CLAMP_D_TO_U8(result_G / div + bias);
-            out_data[4*( x + width*y )+2] = CLAMP_D_TO_U8(result_B / div + bias);
-            out_data[4*( x + width*y )+3] = CLAMP_D_TO_U8(result_A / div + bias);
+            out_data[4*( x + width*y )] = CLAMP_D_TO_U8(result_R / divisor + bias);
+            out_data[4*( x + width*y )+1] = CLAMP_D_TO_U8(result_G / divisor + bias);
+            out_data[4*( x + width*y )+2] = CLAMP_D_TO_U8(result_B / divisor + bias);
+            out_data[4*( x + width*y )+3] = CLAMP_D_TO_U8(result_A / divisor + bias);
         }
     }
 
index 434689385b04255d502d055dc216c88c960f0769..5a3edfc50f8512be66f4c99d23dd02f5ba3577ba 100644 (file)
@@ -25,6 +25,7 @@
 #include "helper-fns.h"
 #include "xml/repr.h"
 #include "display/nr-filter-convolve-matrix.h"
+#include <math.h>
 
 /* FeConvolveMatrix base class */
 
@@ -80,7 +81,19 @@ sp_feConvolveMatrix_class_init(SPFeConvolveMatrixClass *klass)
 
 static void
 sp_feConvolveMatrix_init(SPFeConvolveMatrix *feConvolveMatrix)
-{}
+{
+    //Setting default values:
+    feConvolveMatrix->order.set("3 3");
+    feConvolveMatrix->targetX = 1;
+    feConvolveMatrix->targetY = 1;
+    feConvolveMatrix->edgeMode = NR::CONVOLVEMATRIX_EDGEMODE_DUPLICATE;
+    feConvolveMatrix->preserveAlpha = false;
+
+    //some helper variables:
+    feConvolveMatrix->targetXIsSet = false;
+    feConvolveMatrix->targetYIsSet = false;
+    feConvolveMatrix->kernelMatrixIsSet = false;
+}
 
 /**
  * Reads the Inkscape::XML::Node, and initializes SPFeConvolveMatrix variables.  For this to get called,
@@ -104,7 +117,6 @@ sp_feConvolveMatrix_build(SPObject *object, SPDocument *document, Inkscape::XML:
     sp_object_read_attr(object, "edgeMode");
     sp_object_read_attr(object, "kernelUnitLength");
     sp_object_read_attr(object, "preserveAlpha");
-
 }
 
 /**
@@ -153,21 +165,43 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             //From SVG spec: If <orderY> is not provided, it defaults to <orderX>.
             if (feConvolveMatrix->order.optNumIsSet() == false)
                 feConvolveMatrix->order.setOptNumber(feConvolveMatrix->order.getNumber());
+            if (feConvolveMatrix->targetXIsSet == false) feConvolveMatrix->targetX = (int) floor(feConvolveMatrix->order.getNumber()/2);
+            if (feConvolveMatrix->targetYIsSet == false) feConvolveMatrix->targetY = (int) floor(feConvolveMatrix->order.getOptNumber()/2);
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_KERNELMATRIX:
-            feConvolveMatrix->kernelMatrix = helperfns_read_vector(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
-            object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            if (value){
+                feConvolveMatrix->kernelMatrixIsSet = true;
+                feConvolveMatrix->kernelMatrix = helperfns_read_vector(value, (int) (feConvolveMatrix->order.getNumber() * feConvolveMatrix->order.getOptNumber()));
+                object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            } else {
+                g_warning("You MUST pass a kernelMatrix parameter!");
+            }
             break;
         case SP_ATTR_DIVISOR:
-            read_num = helperfns_read_number(value);
+            if (!value){
+                read_num = 1; 
+            } else {
+                read_num = helperfns_read_number(value);
+                if (read_num == 0) {
+                    if (feConvolveMatrix->kernelMatrixIsSet){
+                        g_warning("You shouldn't pass a divisor value equal to 0! Assuming the sum of all values in kernelMatrix as the default value.");
+                        for (unsigned int i = 0; i< feConvolveMatrix->kernelMatrix.size(); i++)
+                            read_num += feConvolveMatrix->kernelMatrix[i];
+                    } else {
+                        g_warning("You shouldn't pass a divisor value equal to 0! Assuming 1 as the default value.");
+                        read_num = 1;
+                    }
+                }
+            }
             if (read_num != feConvolveMatrix->divisor){
                 feConvolveMatrix->divisor = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_BIAS:
-            read_num = helperfns_read_number(value);
+            read_num = 0;
+            if (value) read_num = helperfns_read_number(value);
             if (read_num != feConvolveMatrix->bias){
                 feConvolveMatrix->bias = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -175,6 +209,11 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             break;
         case SP_ATTR_TARGETX:
             read_int = (int) helperfns_read_number(value);
+            if (read_int < 0 || read_int > feConvolveMatrix->order.getNumber()){
+                g_warning("targetX must be a value between 0 and orderX! Assuming orderX as default value.");
+                read_int = (int) feConvolveMatrix->order.getNumber();
+            }
+            feConvolveMatrix->targetXIsSet = true;
             if (read_int != feConvolveMatrix->targetX){
                 feConvolveMatrix->targetX = read_int;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -182,6 +221,11 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             break;
         case SP_ATTR_TARGETY:
             read_int = (int) helperfns_read_number(value);
+            if (read_int < 0 || read_int > feConvolveMatrix->order.getOptNumber()){
+                g_warning("targetY must be a value between 0 and orderY! Assuming orderY as default value.");
+                read_int = (int) feConvolveMatrix->order.getOptNumber();
+            }
+            feConvolveMatrix->targetYIsSet = true;
             if (read_int != feConvolveMatrix->targetY){
                 feConvolveMatrix->targetY = read_int;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -196,6 +240,9 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             break;
         case SP_ATTR_KERNELUNITLENGTH:
             feConvolveMatrix->kernelUnitLength.set(value);
+            //From SVG spec: If the <dy> value is not specified, it defaults to the same value as <dx>.
+            if (feConvolveMatrix->kernelUnitLength.optNumIsSet() == false)
+                feConvolveMatrix->kernelUnitLength.setOptNumber(feConvolveMatrix->kernelUnitLength.getNumber());
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_PRESERVEALPHA:
index d757d7b3d83e2f30e7f0168e34ec650107e864c6..2cdeee2e8af373581853a47462c21365f1c10edd 100644 (file)
@@ -32,6 +32,11 @@ struct SPFeConvolveMatrix : public SPFilterPrimitive {
     NR::FilterConvolveMatrixEdgeMode edgeMode;
     NumberOptNumber kernelUnitLength;
     bool preserveAlpha;
+    //some helper variables:
+    bool targetXIsSet;
+    bool targetYIsSet;
+    bool divisorIsSet;
+    bool kernelMatrixIsSet;
 };
 
 struct SPFeConvolveMatrixClass {