From aab21a6024aabcff6b1287fb5271d22e14a86437 Mon Sep 17 00:00:00 2001 From: jucablues Date: Mon, 6 Aug 2007 20:54:33 +0000 Subject: [PATCH] handling of default values and checking for invalid values. --- src/display/nr-filter-convolve-matrix.cpp | 25 ++-------- src/sp-feconvolvematrix.cpp | 59 ++++++++++++++++++++--- src/sp-feconvolvematrix.h | 5 ++ 3 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/display/nr-filter-convolve-matrix.cpp b/src/display/nr-filter-convolve-matrix.cpp index 8ce9241fe..562f3ffc4 100644 --- a/src/display/nr-filter-convolve-matrix.cpp +++ b/src/display/nr-filter-convolve-matrix.cpp @@ -15,13 +15,6 @@ 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 /* 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 is not provided, it defaults to . 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 value is not specified, it defaults to the same value as . + if (feConvolveMatrix->kernelUnitLength.optNumIsSet() == false) + feConvolveMatrix->kernelUnitLength.setOptNumber(feConvolveMatrix->kernelUnitLength.getNumber()); object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG); break; case SP_ATTR_PRESERVEALPHA: diff --git a/src/sp-feconvolvematrix.h b/src/sp-feconvolvematrix.h index d757d7b3d..2cdeee2e8 100644 --- a/src/sp-feconvolvematrix.h +++ b/src/sp-feconvolvematrix.h @@ -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 { -- 2.30.2