Code

fix imagemagick effects for file:// uris
[inkscape.git] / src / extension / internal / bitmap / imagemagick.cpp
1 /*
2  * Authors:
3  *   Christopher Brown <audiere@gmail.com>
4  *   Ted Gould <ted@gould.cx>
5  *
6  * Copyright (C) 2007 Authors
7  *
8  * Released under GNU GPL, read the file 'COPYING' for more information
9  */
11 #include <libintl.h>
13 #include <gtkmm/box.h>
14 #include <gtkmm/adjustment.h>
15 #include <gtkmm/spinbutton.h>
16 #include <gtkmm.h>
18 #include <glib/gstdio.h>
20 #include "desktop.h"
21 #include "desktop-handles.h"
22 #include "selection.h"
23 #include "sp-object.h"
24 #include "util/glib-list-iterators.h"
26 #include "extension/effect.h"
27 #include "extension/system.h"
29 #include "imagemagick.h"
31 namespace Inkscape {
32 namespace Extension {
33 namespace Internal {
34 namespace Bitmap {
36 class ImageMagickDocCache: public Inkscape::Extension::Implementation::ImplementationDocumentCache {
37         friend class ImageMagick;
38 private:
39         void readImage(char const *xlink, Magick::Image *image);
40 protected:
41         Inkscape::XML::Node** _nodes;   
42         
43         Magick::Image** _images;
44         int _imageCount;
45         char** _caches;
46         unsigned* _cacheLengths;
47         
48         const char** _originals;
49 public:
50         ImageMagickDocCache(Inkscape::UI::View::View * view);
51         ~ImageMagickDocCache ( );
52 };
54 ImageMagickDocCache::ImageMagickDocCache(Inkscape::UI::View::View * view) :
55         Inkscape::Extension::Implementation::ImplementationDocumentCache(view),
56         _nodes(NULL),
57         _images(NULL),
58         _imageCount(0),
59         _caches(NULL),
60         _cacheLengths(NULL),
61         _originals(NULL)
62 {
63         SPDesktop *desktop = (SPDesktop*)view;
64         const GSList *selectedReprList = desktop->selection->reprList();
65         int selectCount = g_slist_length((GSList *)selectedReprList);
67         // Init the data-holders
68         _nodes = new Inkscape::XML::Node*[selectCount];
69         _originals = new const char*[selectCount];
70         _caches = new char*[selectCount];
71         _cacheLengths = new unsigned int[selectCount];
72         _images = new Magick::Image*[selectCount];
73         _imageCount = 0;
75         // Loop through selected nodes
76         for (; selectedReprList != NULL; selectedReprList = g_slist_next(selectedReprList))
77         {
78                 Inkscape::XML::Node *node = reinterpret_cast<Inkscape::XML::Node *>(selectedReprList->data);
79                 if (!strcmp(node->name(), "image") || !strcmp(node->name(), "svg:image"))
80                 {
81                         _nodes[_imageCount] = node;     
82                         char const *xlink = node->attribute("xlink:href");
84                         _originals[_imageCount] = xlink;
85                         _caches[_imageCount] = "";
86                         _cacheLengths[_imageCount] = 0;
87                         _images[_imageCount] = new Magick::Image();
88                         readImage(xlink, _images[_imageCount]);                 
90                         _imageCount++;
91                 }                       
92         }
93 }
95 ImageMagickDocCache::~ImageMagickDocCache ( ) {
96         if (_nodes)
97                 delete _nodes;
98         if (_originals)
99                 delete _originals;
100         if (_caches)
101                 delete _caches;
102         if (_cacheLengths)
103                 delete _cacheLengths;
104         if (_images)
105                 delete _images;
107         return;
110 void
111 ImageMagickDocCache::readImage(const char *xlink, Magick::Image *image)
113         // Find if the xlink:href is base64 data, i.e. if the image is embedded 
114         char *search = (char *) g_strndup(xlink, 30);
115         if (strstr(search, "base64") != (char*)NULL) {
116                 // 7 = strlen("base64") + strlen(",")
117                 const char* pureBase64 = strstr(xlink, "base64") + 7;           
118                 Magick::Blob blob;
119                 blob.base64(pureBase64);
120                 image->read(blob);
121         }
122         else {
123                 if (!strncmp(xlink, "file://", 7) && strlen(xlink) > 7)
124                         image->read(xlink + 7);
125                 else
126                         image->read(xlink);
127         }
130 bool
131 ImageMagick::load(Inkscape::Extension::Extension */*module*/)
133         return true;
136 Inkscape::Extension::Implementation::ImplementationDocumentCache *
137 ImageMagick::newDocCache (Inkscape::Extension::Extension * /*ext*/, Inkscape::UI::View::View * view) {
138         return new ImageMagickDocCache(view);
141 void
142 ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
144         refreshParameters(module);
146         if (docCache == NULL) { // should never happen
147                 docCache = newDocCache(module, document);
148         }
149         ImageMagickDocCache * dc = dynamic_cast<ImageMagickDocCache *>(docCache);
150         if (dc == NULL) { // should really never happen
151                 printf("AHHHHHHHHH!!!!!");
152                 exit(1);
153         }
154         
155         for (int i = 0; i < dc->_imageCount; i++)
156         {
157                 try
158                 {
159                         Magick::Image effectedImage = *dc->_images[i]; // make a copy
160                         applyEffect(&effectedImage);
162                         Magick::Blob *blob = new Magick::Blob();
163                         effectedImage.write(blob);
165                         std::string raw_string = blob->base64();
166                         const int raw_len = raw_string.length();
167                         const char *raw_i = raw_string.c_str();
169                         unsigned new_len = (int)(raw_len * (77.0 / 76.0) + 100);
170                         if (new_len > dc->_cacheLengths[i]) {
171                                 dc->_cacheLengths[i] = (int)(new_len * 1.2);
172                                 dc->_caches[i] = new char[dc->_cacheLengths[i]];
173                         }
174                         char *formatted_i = dc->_caches[i];
175                         const char *src;
177                         for (src = "data:image/"; *src; )
178                                 *formatted_i++ = *src++;
179                         for (src = effectedImage.magick().c_str(); *src ; )
180                                 *formatted_i++ = *src++;
181                         for (src = ";base64, \n" ; *src; )
182                                 *formatted_i++ = *src++;
184                         int col = 0;
185                         while (*raw_i) {
186                            *formatted_i++ = *raw_i++;
187                            if (col++ > 76) {
188                                    *formatted_i++ = '\n';
189                                    col = 0;
190                            }
191                         }                       
192                         if (col) {
193                            *formatted_i++ = '\n';
194                         }
195                         *formatted_i = '\0';
197                         dc->_nodes[i]->setAttribute("xlink:href", dc->_caches[i], true);                        
198                         dc->_nodes[i]->setAttribute("sodipodi:absref", NULL, true);
199                 }
200                 catch (Magick::Exception &error_) {
201                         printf("Caught exception: %s \n", error_.what());
202                 }
204                 //while(Gtk::Main::events_pending()) {
205                 //      Gtk::Main::iteration();
206                 //}
207         }
210 /** \brief  A function to get the prefences for the grid
211     \param  moudule  Module which holds the params
212     \param  view     Unused today - may get style information in the future.
214     Uses AutoGUI for creating the GUI.
215 */
216 Gtk::Widget *
217 ImageMagick::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
219     SPDocument * current_document = view->doc();
221     using Inkscape::Util::GSListConstIterator;
222     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
223     Inkscape::XML::Node * first_select = NULL;
224     if (selected != NULL) 
225         first_select = SP_OBJECT_REPR(*selected);
227     return module->autogui(current_document, first_select, changeSignal);
230 }; /* namespace Bitmap */
231 }; /* namespace Internal */
232 }; /* namespace Extension */
233 }; /* namespace Inkscape */