4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
36 #define _(String) gettext(String)
37 #define gettext_noop(String) String
38 #define N_(String) gettext_noop (String)
40 //#include "empty_svg.h"
42 REGISTER_PLUGIN(SvgMain)
44 SvgConfig::SvgConfig()
57 int SvgConfig::equivalent(SvgConfig &that)
59 return EQUIV(in_x, that.in_x) &&
60 EQUIV(in_y, that.in_y) &&
61 EQUIV(in_w, that.in_w) &&
62 EQUIV(in_h, that.in_h) &&
63 EQUIV(out_x, that.out_x) &&
64 EQUIV(out_y, that.out_y) &&
65 EQUIV(out_w, that.out_w) &&
66 EQUIV(out_h, that.out_h) &&
67 !strcmp(svg_file, that.svg_file);
70 void SvgConfig::copy_from(SvgConfig &that)
80 strcpy(svg_file, that.svg_file);
83 void SvgConfig::interpolate(SvgConfig &prev,
89 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
90 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
92 this->in_x = prev.in_x * prev_scale + next.in_x * next_scale;
93 this->in_y = prev.in_y * prev_scale + next.in_y * next_scale;
94 this->in_w = prev.in_w * prev_scale + next.in_w * next_scale;
95 this->in_h = prev.in_h * prev_scale + next.in_h * next_scale;
96 this->out_x = prev.out_x * prev_scale + next.out_x * next_scale;
97 this->out_y = prev.out_y * prev_scale + next.out_y * next_scale;
98 this->out_w = prev.out_w * prev_scale + next.out_w * next_scale;
99 this->out_h = prev.out_h * prev_scale + next.out_h * next_scale;
100 strcpy(this->svg_file, prev.svg_file);
110 SvgMain::SvgMain(PluginServer *server)
111 : PluginVClient(server)
115 need_reconfigure = 1;
124 const char* SvgMain::plugin_title() { return N_("SVG via Inkscape"); }
125 int SvgMain::is_realtime() { return 1; }
126 int SvgMain::is_synthesis() { return 1; }
129 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
131 void SvgMain::save_data(KeyFrame *keyframe)
135 // cause data to be stored directly in text
136 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
139 output.tag.set_title("SVG");
140 output.tag.set_property("IN_X", config.in_x);
141 output.tag.set_property("IN_Y", config.in_y);
142 output.tag.set_property("IN_W", config.in_w);
143 output.tag.set_property("IN_H", config.in_h);
144 output.tag.set_property("OUT_X", config.out_x);
145 output.tag.set_property("OUT_Y", config.out_y);
146 output.tag.set_property("OUT_W", config.out_w);
147 output.tag.set_property("OUT_H", config.out_h);
148 output.tag.set_property("SVG_FILE", config.svg_file);
150 output.tag.set_title("/SVG");
153 output.terminate_string();
154 // data is now in *text
157 void SvgMain::read_data(KeyFrame *keyframe)
161 const char *data = keyframe->get_data();
162 input.set_shared_input((char*)data, strlen(data));
168 result = input.read_tag();
172 if(input.tag.title_is("SVG"))
174 config.in_x = input.tag.get_property("IN_X", config.in_x);
175 config.in_y = input.tag.get_property("IN_Y", config.in_y);
176 config.in_w = input.tag.get_property("IN_W", config.in_w);
177 config.in_h = input.tag.get_property("IN_H", config.in_h);
178 config.out_x = input.tag.get_property("OUT_X", config.out_x);
179 config.out_y = input.tag.get_property("OUT_Y", config.out_y);
180 config.out_w = input.tag.get_property("OUT_W", config.out_w);
181 config.out_h = input.tag.get_property("OUT_H", config.out_h);
182 input.tag.get_property("SVG_FILE", config.svg_file);
195 int SvgMain::process_realtime(VFrame *input, VFrame *output)
198 need_reconfigure |= load_configuration();
199 output->copy_from(input);
200 if( config.svg_file[0] == 0 ) return 0;
202 if( need_reconfigure ) {
203 need_reconfigure = 0;
204 delete ofrm; ofrm = 0;
205 char filename_png[1024];
206 strcpy(filename_png, config.svg_file);
207 strncat(filename_png, ".png", sizeof(filename_png));
208 int fd = open(filename_png, O_RDWR);
209 if( fd < 0 ) { // file does not exist, export it
212 "inkscape --without-gui --export-background=0x000000 "
213 "--export-background-opacity=0 %s --export-png=%s",
214 config.svg_file, filename_png);
215 printf(_("Running command %s\n"), command);
217 // in order for lockf to work it has to be open for writing
218 fd = open(filename_png, O_RDWR);
220 printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
223 // file exists, ... lock it, mmap it and check time_of_creation
224 // Blocking call - will wait for inkscape to finish!
225 lockf(fd, F_LOCK, 0);
228 unsigned char *png_buffer = (unsigned char *)
229 mmap (NULL, st_png.st_size, PROT_READ, MAP_SHARED, fd, 0);
230 if( png_buffer != MAP_FAILED ) {
231 if( png_buffer[0] == 0x89 && png_buffer[1] == 0x50 &&
232 png_buffer[2] == 0x4e && png_buffer[3] == 0x47 ) {
233 ofrm = new VFrame(png_buffer, st_png.st_size);
234 if( ofrm->get_color_model() != output->get_color_model() ) {
235 VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
236 output->get_color_model());
237 BC_CModels::transfer(vfrm->get_rows(), ofrm->get_rows(),
239 0, 0, ofrm->get_w(), ofrm->get_h(),
240 0, 0, vfrm->get_w(), vfrm->get_h(),
241 ofrm->get_color_model(), vfrm->get_color_model(),
242 0, ofrm->get_bytes_per_line(),
243 vfrm->get_bytes_per_line());
244 delete ofrm; ofrm = vfrm;
248 printf (_("The file %s that was generated from %s is not in PNG format."
249 " Try to delete all *.png files.\n"), filename_png, config.svg_file);
250 munmap(png_buffer, st_png.st_size);
253 printf(_("Access mmap to %s as %s failed.\n"), config.svg_file, filename_png);
254 lockf(fd, F_ULOCK, 0);
259 if(!overlayer) overlayer = new OverlayFrame(smp + 1);
260 overlayer->overlay(output, ofrm,
261 0, 0, ofrm->get_w(), ofrm->get_h(),
262 config.out_x, config.out_y,
263 config.out_x + ofrm->get_w(),
264 config.out_y + ofrm->get_h(),
266 get_interpolation_type());
272 NEW_WINDOW_MACRO(SvgMain, SvgWin)
274 void SvgMain::update_gui()
278 load_configuration();
279 SvgWin *window = (SvgWin*)thread->window;
280 window->lock_window();
281 // window->in_x->update(config.in_x);
282 // window->in_y->update(config.in_y);
283 // window->in_w->update(config.in_w);
284 // window->in_h->update(config.in_h);
285 window->out_x->update(config.out_x);
286 window->out_y->update(config.out_y);
287 // window->out_w->update(config.out_w);
288 // window->out_h->update(config.out_h);
289 window->svg_file_title->update(config.svg_file);
290 window->unlock_window();