Code

Added missing (and very important) file.
[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                 const gchar *path = xlink;
124     if (strncmp (xlink,"file:", 5) == 0) {
125       path = g_filename_from_uri(xlink, NULL, NULL);
126                 }
128                 try {
129                         image->read(path);
130                 } catch (...) {}
131         }
134 bool
135 ImageMagick::load(Inkscape::Extension::Extension */*module*/)
137         return true;
140 Inkscape::Extension::Implementation::ImplementationDocumentCache *
141 ImageMagick::newDocCache (Inkscape::Extension::Extension * /*ext*/, Inkscape::UI::View::View * view) {
142         return new ImageMagickDocCache(view);
145 void
146 ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
148         refreshParameters(module);
150         if (docCache == NULL) { // should never happen
151                 docCache = newDocCache(module, document);
152         }
153         ImageMagickDocCache * dc = dynamic_cast<ImageMagickDocCache *>(docCache);
154         if (dc == NULL) { // should really never happen
155                 printf("AHHHHHHHHH!!!!!");
156                 exit(1);
157         }
158         
159         for (int i = 0; i < dc->_imageCount; i++)
160         {
161                 try
162                 {
163                         Magick::Image effectedImage = *dc->_images[i]; // make a copy
164                         applyEffect(&effectedImage);
166                         Magick::Blob *blob = new Magick::Blob();
167                         effectedImage.write(blob);
169                         std::string raw_string = blob->base64();
170                         const int raw_len = raw_string.length();
171                         const char *raw_i = raw_string.c_str();
173                         unsigned new_len = (int)(raw_len * (77.0 / 76.0) + 100);
174                         if (new_len > dc->_cacheLengths[i]) {
175                                 dc->_cacheLengths[i] = (int)(new_len * 1.2);
176                                 dc->_caches[i] = new char[dc->_cacheLengths[i]];
177                         }
178                         char *formatted_i = dc->_caches[i];
179                         const char *src;
181                         for (src = "data:image/"; *src; )
182                                 *formatted_i++ = *src++;
183                         for (src = effectedImage.magick().c_str(); *src ; )
184                                 *formatted_i++ = *src++;
185                         for (src = ";base64, \n" ; *src; )
186                                 *formatted_i++ = *src++;
188                         int col = 0;
189                         while (*raw_i) {
190                            *formatted_i++ = *raw_i++;
191                            if (col++ > 76) {
192                                    *formatted_i++ = '\n';
193                                    col = 0;
194                            }
195                         }                       
196                         if (col) {
197                            *formatted_i++ = '\n';
198                         }
199                         *formatted_i = '\0';
201                         dc->_nodes[i]->setAttribute("xlink:href", dc->_caches[i], true);                        
202                         dc->_nodes[i]->setAttribute("sodipodi:absref", NULL, true);
203                 }
204                 catch (Magick::Exception &error_) {
205                         printf("Caught exception: %s \n", error_.what());
206                 }
208                 //while(Gtk::Main::events_pending()) {
209                 //      Gtk::Main::iteration();
210                 //}
211         }
214 /** \brief  A function to get the prefences for the grid
215     \param  moudule  Module which holds the params
216     \param  view     Unused today - may get style information in the future.
218     Uses AutoGUI for creating the GUI.
219 */
220 Gtk::Widget *
221 ImageMagick::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
223     SPDocument * current_document = view->doc();
225     using Inkscape::Util::GSListConstIterator;
226     GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
227     Inkscape::XML::Node * first_select = NULL;
228     if (selected != NULL) 
229         first_select = SP_OBJECT_REPR(*selected);
231     return module->autogui(current_document, first_select, changeSignal);
234 }; /* namespace Bitmap */
235 }; /* namespace Internal */
236 }; /* namespace Extension */
237 }; /* namespace Inkscape */