75d53927d21b8277ae9c5624940150234f1c1672
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;
43 Magick::Image** _images;
44 int _imageCount;
45 char** _caches;
46 unsigned* _cacheLengths;
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;
108 }
110 void
111 ImageMagickDocCache::readImage(const char *xlink, Magick::Image *image)
112 {
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 }
132 g_free(search);
133 }
135 bool
136 ImageMagick::load(Inkscape::Extension::Extension */*module*/)
137 {
138 return true;
139 }
141 Inkscape::Extension::Implementation::ImplementationDocumentCache *
142 ImageMagick::newDocCache (Inkscape::Extension::Extension * /*ext*/, Inkscape::UI::View::View * view) {
143 return new ImageMagickDocCache(view);
144 }
146 void
147 ImageMagick::effect (Inkscape::Extension::Effect *module, Inkscape::UI::View::View *document, Inkscape::Extension::Implementation::ImplementationDocumentCache * docCache)
148 {
149 refreshParameters(module);
151 if (docCache == NULL) { // should never happen
152 docCache = newDocCache(module, document);
153 }
154 ImageMagickDocCache * dc = dynamic_cast<ImageMagickDocCache *>(docCache);
155 if (dc == NULL) { // should really never happen
156 printf("AHHHHHHHHH!!!!!");
157 exit(1);
158 }
160 for (int i = 0; i < dc->_imageCount; i++)
161 {
162 try
163 {
164 Magick::Image effectedImage = *dc->_images[i]; // make a copy
165 applyEffect(&effectedImage);
167 Magick::Blob *blob = new Magick::Blob();
168 effectedImage.write(blob);
170 std::string raw_string = blob->base64();
171 const int raw_len = raw_string.length();
172 const char *raw_i = raw_string.c_str();
174 unsigned new_len = (int)(raw_len * (77.0 / 76.0) + 100);
175 if (new_len > dc->_cacheLengths[i]) {
176 dc->_cacheLengths[i] = (int)(new_len * 1.2);
177 dc->_caches[i] = new char[dc->_cacheLengths[i]];
178 }
179 char *formatted_i = dc->_caches[i];
180 const char *src;
182 for (src = "data:image/"; *src; )
183 *formatted_i++ = *src++;
184 for (src = effectedImage.magick().c_str(); *src ; )
185 *formatted_i++ = *src++;
186 for (src = ";base64, \n" ; *src; )
187 *formatted_i++ = *src++;
189 int col = 0;
190 while (*raw_i) {
191 *formatted_i++ = *raw_i++;
192 if (col++ > 76) {
193 *formatted_i++ = '\n';
194 col = 0;
195 }
196 }
197 if (col) {
198 *formatted_i++ = '\n';
199 }
200 *formatted_i = '\0';
202 dc->_nodes[i]->setAttribute("xlink:href", dc->_caches[i], true);
203 dc->_nodes[i]->setAttribute("sodipodi:absref", NULL, true);
204 }
205 catch (Magick::Exception &error_) {
206 printf("Caught exception: %s \n", error_.what());
207 }
209 //while(Gtk::Main::events_pending()) {
210 // Gtk::Main::iteration();
211 //}
212 }
213 }
215 /** \brief A function to get the prefences for the grid
216 \param moudule Module which holds the params
217 \param view Unused today - may get style information in the future.
219 Uses AutoGUI for creating the GUI.
220 */
221 Gtk::Widget *
222 ImageMagick::prefs_effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View * view, sigc::signal<void> * changeSignal, Inkscape::Extension::Implementation::ImplementationDocumentCache * /*docCache*/)
223 {
224 Document * current_document = view->doc();
226 using Inkscape::Util::GSListConstIterator;
227 GSListConstIterator<SPItem *> selected = sp_desktop_selection((SPDesktop *)view)->itemList();
228 Inkscape::XML::Node * first_select = NULL;
229 if (selected != NULL)
230 first_select = SP_OBJECT_REPR(*selected);
232 return module->autogui(current_document, first_select, changeSignal);
233 }
235 }; /* namespace Bitmap */
236 }; /* namespace Internal */
237 }; /* namespace Extension */
238 }; /* namespace Inkscape */