Code

r16894@shi: ted | 2007-10-30 09:26:42 -0700
authorgouldtj <gouldtj@users.sourceforge.net>
Tue, 30 Oct 2007 19:30:04 +0000 (19:30 +0000)
committergouldtj <gouldtj@users.sourceforge.net>
Tue, 30 Oct 2007 19:30:04 +0000 (19:30 +0000)
 OH MY GOD IT WORKS!!!

src/extension/execution-env.cpp
src/extension/implementation/implementation.h
src/extension/internal/bitmap/imagemagick.cpp
src/extension/internal/bitmap/imagemagick.h

index 0dbb32b11c4a23fbd92164c1c40726a8cedce9ea..8e33fe64aeabcc7450666d9dab29eb36b3000852 100644 (file)
@@ -102,8 +102,8 @@ ExecutionEnv::~ExecutionEnv (void) {
 void
 ExecutionEnv::genDocCache (void) {
        if (_docCache == NULL) {
-               printf("Gen Doc Cache\n");
-               Implementation::ImplementationDocumentCache * _docCache = _effect->get_imp()->newDocCache(_effect, _doc);
+               // printf("Gen Doc Cache\n");
+               _docCache = _effect->get_imp()->newDocCache(_effect, _doc);
        }
        return;
 }
@@ -111,7 +111,7 @@ ExecutionEnv::genDocCache (void) {
 void
 ExecutionEnv::killDocCache (void) {
        if (_docCache != NULL) {
-               printf("Killed Doc Cache\n");
+               // printf("Killed Doc Cache\n");
                delete _docCache;
                _docCache = NULL;
        }
@@ -287,6 +287,13 @@ ExecutionEnv::run (void) {
     return;
 }
 
+/** \brief  Set the state of live preview
+    \param state  The current state
+       
+       This will cancel the document preview and and configure
+       whether we should be waiting on the human.  It will also
+       clear the document cache.
+*/
 void
 ExecutionEnv::livePreview (bool state) { 
     _mainloop->quit();
index ad1bbf1d2fff70f3364ec21d65b94fa24fb20083..f2855a168076946201774d067eaf101a9b6a0095 100644 (file)
@@ -56,7 +56,7 @@ public:
     virtual bool load(Inkscape::Extension::Extension *module);
 
     virtual void unload(Inkscape::Extension::Extension *module);
-       ImplementationDocumentCache * newDocCache (Inkscape::Extension::Extension * ext, Inkscape::UI::View::View * doc);
+       virtual ImplementationDocumentCache * newDocCache (Inkscape::Extension::Extension * ext, Inkscape::UI::View::View * doc);
 
     /** Verify any dependencies. */
     virtual bool check(Inkscape::Extension::Extension *module);
index ddb7a0145840bb66c0512238f2bc138ab628d595..9b6ed5390f158788e58d538c1c9efb755ed36d82 100644 (file)
@@ -33,34 +33,82 @@ namespace Extension {
 namespace Internal {
 namespace Bitmap {
 
-bool
-ImageMagick::load(Inkscape::Extension::Extension *module)
-{
-       _loaded = FALSE;
+class ImageMagickDocCache: public Inkscape::Extension::Implementation::ImplementationDocumentCache {
+       friend class ImageMagick;
+private:
+       void readImage(char const *xlink, Magick::Image *image);
+protected:
+       Inkscape::XML::Node** _nodes;   
        
-       return TRUE;
-}
+       Magick::Image** _images;
+       int _imageCount;
+       char** _caches;
+       unsigned* _cacheLengths;
+       
+       const char** _originals;
+public:
+       ImageMagickDocCache(Inkscape::UI::View::View * view);
+       ~ImageMagickDocCache ( );
+};
+
+ImageMagickDocCache::ImageMagickDocCache(Inkscape::UI::View::View * view) :
+       Inkscape::Extension::Implementation::ImplementationDocumentCache(view),
+       _nodes(NULL),
+       _images(NULL),
+       _imageCount(0),
+       _caches(NULL),
+       _cacheLengths(NULL),
+       _originals(NULL)
+{
+       SPDesktop *desktop = (SPDesktop*)view;
+       const GSList *selectedReprList = desktop->selection->reprList();
+       int selectCount = g_slist_length((GSList *)selectedReprList);
+
+       // Init the data-holders
+       _nodes = new Inkscape::XML::Node*[selectCount];
+       _originals = new const char*[selectCount];
+       _caches = new char*[selectCount];
+       _cacheLengths = new unsigned int[selectCount];
+       _images = new Magick::Image*[selectCount];
+       _imageCount = 0;
+
+       // Loop through selected nodes
+       for (; selectedReprList != NULL; selectedReprList = g_slist_next(selectedReprList))
+       {
+               Inkscape::XML::Node *node = reinterpret_cast<Inkscape::XML::Node *>(selectedReprList->data);
+               if (!strcmp(node->name(), "image") || !strcmp(node->name(), "svg:image"))
+               {
+                       _nodes[_imageCount] = node;     
+                       char const *xlink = node->attribute("xlink:href");
 
-/*
-void
-ImageMagick::commitDocument(void) {
-       _loaded = FALSE;
-}
+                       _originals[_imageCount] = xlink;
+                       _caches[_imageCount] = "";
+                       _cacheLengths[_imageCount] = 0;
+                       _images[_imageCount] = new Magick::Image();
+                       readImage(xlink, _images[_imageCount]);                 
 
-void
-ImageMagick::cancelDocument(void) {    
-       for (int i = 0; i < _imageCount; i++) {
-               _nodes[i]->setAttribute("xlink:href", _originals[i], true);
-               
-               if (strlen(_originals[i]) < 256)
-                       _nodes[i]->setAttribute("sodipodi:absref", _originals[i], true);
+                       _imageCount++;
+               }                       
        }
-       
-       _loaded = FALSE;
-}*/
+}
+
+ImageMagickDocCache::~ImageMagickDocCache ( ) {
+       if (_nodes)
+               delete _nodes;
+       if (_originals)
+               delete _originals;
+       if (_caches)
+               delete _caches;
+       if (_cacheLengths)
+               delete _cacheLengths;
+       if (_images)
+               delete _images;
+
+       return;
+}
 
 void
-ImageMagick::readImage(const char *xlink, Magick::Image *image)
+ImageMagickDocCache::readImage(const char *xlink, Magick::Image *image)
 {
        // Find if the xlink:href is base64 data, i.e. if the image is embedded 
        char *search = (char *) g_strndup(xlink, 30);
@@ -76,53 +124,36 @@ ImageMagick::readImage(const char *xlink, Magick::Image *image)
        }
 }
 
+bool
+ImageMagick::load(Inkscape::Extension::Extension *module)
+{
+       return true;
+}
+
+Inkscape::Extension::Implementation::ImplementationDocumentCache *
+ImageMagick::newDocCache (Inkscape::Extension::Extension * ext, Inkscape::UI::View::View * view) {
+       return new ImageMagickDocCache(view);
+}
+
 void
 ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
 {
        refreshParameters(module);
-       _loaded = FALSE;
-       
-       if (!_loaded)
-       {
-               SPDesktop *desktop = (SPDesktop*)document;
-               const GSList *selectedReprList = desktop->selection->reprList();
-               int selectCount = g_slist_length((GSList *)selectedReprList);
-
-               // Init the data-holders
-               _nodes = new Inkscape::XML::Node*[selectCount];
-               _originals = new const char*[selectCount];
-               _caches = new char*[selectCount];
-               _cacheLengths = new unsigned[selectCount];
-               _images = new Magick::Image*[selectCount];
-               _imageCount = 0;
-
-               // Loop through selected nodes
-               for (; selectedReprList != NULL; selectedReprList = g_slist_next(selectedReprList))
-               {
-                       Inkscape::XML::Node *node = reinterpret_cast<Inkscape::XML::Node *>(selectedReprList->data);
-                       if (!strcmp(node->name(), "image") || !strcmp(node->name(), "svg:image"))
-                       {
-                               _nodes[_imageCount] = node;     
-                               char const *xlink = node->attribute("xlink:href");
-
-                               _originals[_imageCount] = xlink;
-                               _caches[_imageCount] = "";
-                               _cacheLengths[_imageCount] = 0;
-                               _images[_imageCount] = new Magick::Image();
-                               readImage(xlink, _images[_imageCount]);                 
-
-                               _imageCount++;
-                       }                       
-               }
 
-               _loaded = 1;
+       if (docCache == NULL) { // should never happen
+               docCache = newDocCache(module, document);
        }
-
-       for (int i = 0; i < _imageCount; i++)
+       ImageMagickDocCache * dc = dynamic_cast<ImageMagickDocCache *>(docCache);
+       if (dc == NULL) { // should really never happen
+               printf("AHHHHHHHHH!!!!!");
+               exit(1);
+       }
+       
+       for (int i = 0; i < dc->_imageCount; i++)
        {
                try
                {
-                       Magick::Image effectedImage = *_images[i]; // make a copy
+                       Magick::Image effectedImage = *dc->_images[i]; // make a copy
                        applyEffect(&effectedImage);
 
                        Magick::Blob *blob = new Magick::Blob();
@@ -133,11 +164,11 @@ ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::Vi
                        const char *raw_i = raw_string.c_str();
 
                        unsigned new_len = (int)(raw_len * (77.0 / 76.0) + 100);
-                       if (new_len > _cacheLengths[i]) {
-                               _cacheLengths[i] = (int)(new_len * 1.2);
-                               _caches[i] = new char[_cacheLengths[i]];
+                       if (new_len > dc->_cacheLengths[i]) {
+                               dc->_cacheLengths[i] = (int)(new_len * 1.2);
+                               dc->_caches[i] = new char[dc->_cacheLengths[i]];
                        }
-                       char *formatted_i = _caches[i];
+                       char *formatted_i = dc->_caches[i];
                        const char *src;
 
                        for (src = "data:image/"; *src; )
@@ -160,15 +191,16 @@ ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::Vi
                        }
                        *formatted_i = '\0';
 
-                       _nodes[i]->setAttribute("xlink:href", _caches[i], true);                        
-                       _nodes[i]->setAttribute("sodipodi:absref", NULL, true);
+                       dc->_nodes[i]->setAttribute("xlink:href", dc->_caches[i], true);                        
+                       dc->_nodes[i]->setAttribute("sodipodi:absref", NULL, true);
                }
                catch (Magick::Exception &error_) {
                        printf("Caught exception: %s \n", error_.what());
                }
 
-               while(Gtk::Main::events_pending())
+               while(Gtk::Main::events_pending()) {
                        Gtk::Main::iteration();
+               }
        }
 }
 
index 1fb79f826e4d0be1b09319c264bacb4c698937a8..4a8a318fc8b620e412fdacd56f9ad7bfaf113e6c 100644 (file)
@@ -18,36 +18,16 @@ namespace Extension {
 namespace Internal {
 namespace Bitmap {
 
-class ImageMagickDocCache: public Inkscape::Extension::Implementation::ImplementationDocumentCache {
-public:
-       ImageMagickDocCache(Inkscape::UI::View::View * view) : Inkscape::Extension::Implementation::ImplementationDocumentCache(view) { };
-       ~ImageMagickDocCache ( ) { };
-};
-
 class ImageMagick : public Inkscape::Extension::Implementation::Implementation {
-
-private:
-       bool _loaded;
-
-       Inkscape::XML::Node** _nodes;   
-       
-       Magick::Image** _images;
-       int _imageCount;
-       char** _caches;
-       unsigned* _cacheLengths;
-       
-       const char** _originals;
 public:
+       /* Functions to be implemented by subclasses */
        virtual void applyEffect(Magick::Image *image) { };
        virtual void refreshParameters(Inkscape::Extension::Effect *module) { };
-       bool load(Inkscape::Extension::Extension *module);
-    
-       /*void commitDocument(void);*/
-       /*void cancelDocument(void);*/
 
-       void readImage(char const *xlink, Magick::Image *image);
+       /* Functions implemented from ::Implementation */
+       bool load(Inkscape::Extension::Extension *module);
+       Inkscape::Extension::Implementation::ImplementationDocumentCache * newDocCache (Inkscape::Extension::Extension * ext, Inkscape::UI::View::View * doc);
        void effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache);
-       
        Gtk::Widget* prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache);
 };