Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / filter-chemistry.cpp
index 9317ed0826dd6e036b33df726f12cfc10d84b06d..d6900b1f675ae8f28cffe0b598d8b0a42598cbcc 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_FILTER_CHEMISTRY_C__
-
 /*
  * Various utility methods for filters
  *
@@ -7,8 +5,10 @@
  *   Hugo Rodrigues
  *   bulia byak
  *   Niko Kiirala
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
- * Copyright (C) 2006,2007 authors
+ * Copyright (C) 2006-2008 authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 #include "filter-chemistry.h"
 #include "filter-enums.h"
 
-#include "sp-feblend.h"
+#include "filters/blend.h"
 #include "sp-filter.h"
 #include "sp-filter-reference.h"
 #include "sp-gaussian-blur.h"
 #include "svg/css-ostringstream.h"
+#include "libnr/nr-matrix-fns.h"
 
 #include "xml/repr.h"
 
@@ -33,8 +34,7 @@
  * Count how many times the filter is used by the styles of o and its
  * descendants
  */
-static guint
-count_filter_hrefs(SPObject *o, SPFilter *filter)
+static guint count_filter_hrefs(SPObject *o, SPFilter *filter)
 {
     if (!o)
         return 1;
@@ -49,8 +49,7 @@ count_filter_hrefs(SPObject *o, SPFilter *filter)
         i ++;
     }
 
-    for (SPObject *child = sp_object_first_child(o);
-         child != NULL; child = SP_OBJECT_NEXT(child)) {
+    for ( SPObject *child = o->firstChild(); child; child = child->getNext() ) {
         i += count_filter_hrefs(child, filter);
     }
 
@@ -93,14 +92,14 @@ SPFilter *new_filter(SPDocument *document)
 
     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
 
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
 
     // create a new filter
     Inkscape::XML::Node *repr;
     repr = xml_doc->createElement("svg:filter");
 
     // Append the new filter node to defs
-    SP_OBJECT_REPR(defs)->appendChild(repr);
+    defs->appendChild(repr);
     Inkscape::GC::release(repr);
 
     // get corresponding object
@@ -114,59 +113,61 @@ SPFilter *new_filter(SPDocument *document)
 }
 
 SPFilterPrimitive *
-filter_add_primitive(SPFilter *filter, const NR::FilterPrimitiveType type)
+filter_add_primitive(SPFilter *filter, const Inkscape::Filters::FilterPrimitiveType type)
 {
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(filter->document);
+    Inkscape::XML::Document *xml_doc = filter->document->getReprDoc();
 
     //create filter primitive node
     Inkscape::XML::Node *repr;
     repr = xml_doc->createElement(FPConverter.get_key(type).c_str());
-    repr->setAttribute("inkscape:collect", "always");
 
     // set default values
     switch(type) {
-        case NR::NR_FILTER_BLEND:
+        case Inkscape::Filters::NR_FILTER_BLEND:
             repr->setAttribute("blend", "normal");
             break;
-        case NR::NR_FILTER_COLORMATRIX:
+        case Inkscape::Filters::NR_FILTER_COLORMATRIX:
             break;
-        case NR::NR_FILTER_COMPONENTTRANSFER:
+        case Inkscape::Filters::NR_FILTER_COMPONENTTRANSFER:
             break;
-        case NR::NR_FILTER_COMPOSITE:
+        case Inkscape::Filters::NR_FILTER_COMPOSITE:
             break;
-        case NR::NR_FILTER_CONVOLVEMATRIX:
+        case Inkscape::Filters::NR_FILTER_CONVOLVEMATRIX:
+            repr->setAttribute("order", "3 3");
+            repr->setAttribute("kernelMatrix", "0 0 0 0 0 0 0 0 0");
             break;
-        case NR::NR_FILTER_DIFFUSELIGHTING:
+        case Inkscape::Filters::NR_FILTER_DIFFUSELIGHTING:
             break;
-        case NR::NR_FILTER_DISPLACEMENTMAP:
+        case Inkscape::Filters::NR_FILTER_DISPLACEMENTMAP:
             break;
-        case NR::NR_FILTER_FLOOD:
+        case Inkscape::Filters::NR_FILTER_FLOOD:
             break;
-        case NR::NR_FILTER_GAUSSIANBLUR:
+        case Inkscape::Filters::NR_FILTER_GAUSSIANBLUR:
             repr->setAttribute("stdDeviation", "1");
             break;
-        case NR::NR_FILTER_IMAGE:
+        case Inkscape::Filters::NR_FILTER_IMAGE:
             break;
-        case NR::NR_FILTER_MERGE:
+        case Inkscape::Filters::NR_FILTER_MERGE:
             break;
-        case NR::NR_FILTER_MORPHOLOGY:
+        case Inkscape::Filters::NR_FILTER_MORPHOLOGY:
             break;
-        case NR::NR_FILTER_OFFSET:
+        case Inkscape::Filters::NR_FILTER_OFFSET:
             repr->setAttribute("dx", "0");
             repr->setAttribute("dy", "0");
             break;
-        case NR::NR_FILTER_SPECULARLIGHTING:
+        case Inkscape::Filters::NR_FILTER_SPECULARLIGHTING:
             break;
-        case NR::NR_FILTER_TILE:
+        case Inkscape::Filters::NR_FILTER_TILE:
             break;
-        case NR::NR_FILTER_TURBULENCE:
+        case Inkscape::Filters::NR_FILTER_TURBULENCE:
             break;
         default:
             break;
     }
 
     //set primitive as child of filter node
-    filter->repr->appendChild(repr);
+    // XML tree being used directly while/where it shouldn't be...
+    filter->appendChild(repr);
     Inkscape::GC::release(repr);
     
     // get corresponding object
@@ -188,7 +189,7 @@ new_filter_gaussian_blur (SPDocument *document, gdouble radius, double expansion
 
     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
 
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
 
     // create a new filter
     Inkscape::XML::Node *repr;
@@ -215,7 +216,7 @@ new_filter_gaussian_blur (SPDocument *document, gdouble radius, double expansion
     Inkscape::GC::release(b_repr);
     
     // Append the new filter node to defs
-    SP_OBJECT_REPR(defs)->appendChild(repr);
+    defs->appendChild(repr);
     Inkscape::GC::release(repr);
 
     // get corresponding object
@@ -243,7 +244,7 @@ new_filter_blend_gaussian_blur (SPDocument *document, const char *blendmode, gdo
 
     SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
 
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
 
     // create a new filter
     Inkscape::XML::Node *repr;
@@ -251,7 +252,7 @@ new_filter_blend_gaussian_blur (SPDocument *document, const char *blendmode, gdo
     repr->setAttribute("inkscape:collect", "always");
 
     // Append the new filter node to defs
-    SP_OBJECT_REPR(defs)->appendChild(repr);
+    defs->appendChild(repr);
     Inkscape::GC::release(repr);
  
     // get corresponding object
@@ -317,20 +318,20 @@ new_filter_blend_gaussian_blur (SPDocument *document, const char *blendmode, gdo
 SPFilter *
 new_filter_simple_from_item (SPDocument *document, SPItem *item, const char *mode, gdouble radius)
 {
-    NR::Maybe<NR::Rect> const r = sp_item_bbox_desktop(item);
+    Geom::OptRect const r = item->getBboxDesktop(SPItem::GEOMETRIC_BBOX);
 
     double width;
     double height;
     if (r) {
-        width = r->extent(NR::X);
-        height= r->extent(NR::Y);
+        width = r->dimensions()[Geom::X];
+        height= r->dimensions()[Geom::Y];
     } else {
         width = height = 0;
     }
 
-    NR::Matrix i2d = sp_item_i2d_affine (item);
+    Geom::Matrix i2d (item->i2d_affine () );
 
-    return (new_filter_blend_gaussian_blur (document, mode, radius, i2d.expansion(), i2d.expansionX(), i2d.expansionY(), width, height));
+    return (new_filter_blend_gaussian_blur (document, mode, radius, i2d.descrim(), i2d.expansionX(), i2d.expansionY(), width, height));
 }
 
 /**
@@ -342,42 +343,40 @@ new_filter_simple_from_item (SPDocument *document, SPItem *item, const char *mod
  * duplicated, so that other elements referring that filter are not modified.
  */
 /* TODO: this should be made more generic, not just for blurs */
-SPFilter *
-modify_filter_gaussian_blur_from_item(SPDocument *document, SPItem *item,
-                                      gdouble radius)
+SPFilter *modify_filter_gaussian_blur_from_item(SPDocument *document, SPItem *item,
+                                                gdouble radius)
 {
     if (!item->style || !item->style->filter.set) {
-        //return new_filter_gaussian_blur_from_item(document, item, radius);
+        return new_filter_simple_from_item(document, item, "normal", radius);
     }
 
     SPFilter *filter = SP_FILTER(item->style->getFilter());
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
 
     // If there are more users for this filter, duplicate it
-    if (SP_OBJECT_HREFCOUNT(filter) > count_filter_hrefs(item, filter)) {
-        Inkscape::XML::Node *repr;
-        repr = SP_OBJECT_REPR(item->style->getFilter())->duplicate(xml_doc);
+    if (filter->hrefcount > count_filter_hrefs(item, filter)) {
+        Inkscape::XML::Node *repr = SP_OBJECT_REPR(item->style->getFilter())->duplicate(xml_doc);
         SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
-        SP_OBJECT_REPR(defs)->appendChild(repr);
+        defs->appendChild(repr);
 
         filter = SP_FILTER( document->getObjectByRepr(repr) );
         Inkscape::GC::release(repr);
     }
 
     // Determine the required standard deviation value
-    NR::Matrix i2d = sp_item_i2d_affine (item);
-    double expansion = i2d.expansion();
+    Geom::Matrix i2d (item->i2d_affine ());
+    double expansion = i2d.descrim();
     double stdDeviation = radius;
     if (expansion != 0)
         stdDeviation /= expansion;
 
     // Get the object size
-    NR::Maybe<NR::Rect> const r = sp_item_bbox_desktop(item);
+    Geom::OptRect const r = item->getBboxDesktop(SPItem::GEOMETRIC_BBOX);
     double width;
     double height;
     if (r) {
-        width = r->extent(NR::X);
-        height= r->extent(NR::Y);
+        width = r->dimensions()[Geom::X];
+        height= r->dimensions()[Geom::Y];
     } else {
         width = height = 0;
     }
@@ -455,6 +454,32 @@ void remove_filter_gaussian_blur (SPObject *item)
     }
 }
 
+bool filter_is_single_gaussian_blur(SPFilter *filter)
+{
+    return (SP_OBJECT(filter)->firstChild() && 
+            SP_OBJECT(filter)->firstChild() == SP_OBJECT(filter)->lastChild() &&
+            SP_IS_GAUSSIANBLUR(SP_OBJECT(filter)->firstChild()));
+}
+
+double get_single_gaussian_blur_radius(SPFilter *filter)
+{
+    if (SP_OBJECT(filter)->firstChild() && 
+        SP_OBJECT(filter)->firstChild() == SP_OBJECT(filter)->lastChild() &&
+        SP_IS_GAUSSIANBLUR(SP_OBJECT(filter)->firstChild())) {
+
+        SPGaussianBlur *gb = SP_GAUSSIANBLUR(SP_OBJECT(filter)->firstChild());
+        double x = gb->stdDeviation.getNumber();
+        double y = gb->stdDeviation.getOptNumber();
+        if (x > 0 && y > 0) {
+            return MAX(x, y);
+        }
+        return x;
+    }
+    return 0.0;
+}
+
+
+
 /*
   Local Variables:
   mode:c++
@@ -464,4 +489,4 @@ void remove_filter_gaussian_blur (SPObject *item)
   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 :