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
27 #include <sys/types.h>
36 //#include "empty_svg.h"
38 REGISTER_PLUGIN(SvgMain)
40 SvgConfig::SvgConfig()
53 int SvgConfig::equivalent(SvgConfig &that)
55 return EQUIV(in_x, that.in_x) &&
56 EQUIV(in_y, that.in_y) &&
57 EQUIV(in_w, that.in_w) &&
58 EQUIV(in_h, that.in_h) &&
59 EQUIV(out_x, that.out_x) &&
60 EQUIV(out_y, that.out_y) &&
61 EQUIV(out_w, that.out_w) &&
62 EQUIV(out_h, that.out_h) &&
63 !strcmp(svg_file, that.svg_file);
66 void SvgConfig::copy_from(SvgConfig &that)
76 strcpy(svg_file, that.svg_file);
79 void SvgConfig::interpolate(SvgConfig &prev,
85 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
86 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
88 this->in_x = prev.in_x * prev_scale + next.in_x * next_scale;
89 this->in_y = prev.in_y * prev_scale + next.in_y * next_scale;
90 this->in_w = prev.in_w * prev_scale + next.in_w * next_scale;
91 this->in_h = prev.in_h * prev_scale + next.in_h * next_scale;
92 this->out_x = prev.out_x * prev_scale + next.out_x * next_scale;
93 this->out_y = prev.out_y * prev_scale + next.out_y * next_scale;
94 this->out_w = prev.out_w * prev_scale + next.out_w * next_scale;
95 this->out_h = prev.out_h * prev_scale + next.out_h * next_scale;
96 strcpy(this->svg_file, prev.svg_file);
106 SvgMain::SvgMain(PluginServer *server)
107 : PluginVClient(server)
111 need_reconfigure = 1;
120 const char* SvgMain::plugin_title() { return _("SVG via Inkscape"); }
121 int SvgMain::is_realtime() { return 1; }
122 int SvgMain::is_synthesis() { return 1; }
125 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
127 void SvgMain::save_data(KeyFrame *keyframe)
131 // cause data to be stored directly in text
132 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
135 output.tag.set_title("SVG");
136 output.tag.set_property("IN_X", config.in_x);
137 output.tag.set_property("IN_Y", config.in_y);
138 output.tag.set_property("IN_W", config.in_w);
139 output.tag.set_property("IN_H", config.in_h);
140 output.tag.set_property("OUT_X", config.out_x);
141 output.tag.set_property("OUT_Y", config.out_y);
142 output.tag.set_property("OUT_W", config.out_w);
143 output.tag.set_property("OUT_H", config.out_h);
144 output.tag.set_property("SVG_FILE", config.svg_file);
146 output.tag.set_title("/SVG");
149 output.terminate_string();
150 // data is now in *text
153 void SvgMain::read_data(KeyFrame *keyframe)
157 const char *data = keyframe->get_data();
158 input.set_shared_input((char*)data, strlen(data));
164 result = input.read_tag();
168 if(input.tag.title_is("SVG"))
170 config.in_x = input.tag.get_property("IN_X", config.in_x);
171 config.in_y = input.tag.get_property("IN_Y", config.in_y);
172 config.in_w = input.tag.get_property("IN_W", config.in_w);
173 config.in_h = input.tag.get_property("IN_H", config.in_h);
174 config.out_x = input.tag.get_property("OUT_X", config.out_x);
175 config.out_y = input.tag.get_property("OUT_Y", config.out_y);
176 config.out_w = input.tag.get_property("OUT_W", config.out_w);
177 config.out_h = input.tag.get_property("OUT_H", config.out_h);
178 input.tag.get_property("SVG_FILE", config.svg_file);
191 int SvgMain::process_realtime(VFrame *input, VFrame *output)
194 need_reconfigure |= load_configuration();
195 output->copy_from(input);
196 if( config.svg_file[0] == 0 ) return 0;
198 if( need_reconfigure ) {
199 need_reconfigure = 0;
200 delete ofrm; ofrm = 0;
201 char filename_png[1024];
202 strcpy(filename_png, config.svg_file);
203 strncat(filename_png, ".png", sizeof(filename_png));
204 int fd = open(filename_png, O_RDWR);
205 if( fd < 0 ) { // file does not exist, export it
208 "inkscape --without-gui --export-background=0x000000 "
209 "--export-background-opacity=0 %s --export-png=%s",
210 config.svg_file, filename_png);
211 printf(_("Running command %s\n"), command);
213 // in order for lockf to work it has to be open for writing
214 fd = open(filename_png, O_RDWR);
216 printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
219 // file exists, ... lock it, mmap it and check time_of_creation
220 // Blocking call - will wait for inkscape to finish!
221 lockf(fd, F_LOCK, 0);
224 unsigned char *png_buffer = (unsigned char *)
225 mmap (NULL, st_png.st_size, PROT_READ, MAP_SHARED, fd, 0);
226 if( png_buffer != MAP_FAILED ) {
227 if( png_buffer[0] == 0x89 && png_buffer[1] == 0x50 &&
228 png_buffer[2] == 0x4e && png_buffer[3] == 0x47 ) {
229 ofrm = new VFramePng(png_buffer, st_png.st_size, 1., 1.);
230 if( ofrm->get_color_model() != output->get_color_model() ) {
231 VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
232 output->get_color_model());
233 BC_CModels::transfer(vfrm->get_rows(), ofrm->get_rows(),
235 0, 0, ofrm->get_w(), ofrm->get_h(),
236 0, 0, vfrm->get_w(), vfrm->get_h(),
237 ofrm->get_color_model(), vfrm->get_color_model(),
238 0, ofrm->get_bytes_per_line(),
239 vfrm->get_bytes_per_line());
240 delete ofrm; ofrm = vfrm;
244 printf (_("The file %s that was generated from %s is not in PNG format."
245 " Try to delete all *.png files.\n"), filename_png, config.svg_file);
246 munmap(png_buffer, st_png.st_size);
249 printf(_("Access mmap to %s as %s failed.\n"), config.svg_file, filename_png);
250 lockf(fd, F_ULOCK, 0);
255 if(!overlayer) overlayer = new OverlayFrame(smp + 1);
256 overlayer->overlay(output, ofrm,
257 0, 0, ofrm->get_w(), ofrm->get_h(),
258 config.out_x, config.out_y,
259 config.out_x + ofrm->get_w(),
260 config.out_y + ofrm->get_h(),
262 get_interpolation_type());
268 NEW_WINDOW_MACRO(SvgMain, SvgWin)
270 void SvgMain::update_gui()
274 load_configuration();
275 SvgWin *window = (SvgWin*)thread->window;
276 window->lock_window();
277 // window->in_x->update(config.in_x);
278 // window->in_y->update(config.in_y);
279 // window->in_w->update(config.in_w);
280 // window->in_h->update(config.in_h);
281 window->out_x->update(config.out_x);
282 window->out_y->update(config.out_y);
283 // window->out_w->update(config.out_w);
284 // window->out_h->update(config.out_h);
285 window->svg_file_title->update(config.svg_file);
286 window->unlock_window();