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 "overlayframe.inc"
28 #include <sys/types.h>
37 REGISTER_PLUGIN(SvgMain)
39 SvgConfig::SvgConfig()
42 out_w = 640; out_h = 480;
48 int SvgConfig::equivalent(SvgConfig &that)
50 return EQUIV(dpi, that.dpi) &&
51 EQUIV(out_x, that.out_x) &&
52 EQUIV(out_y, that.out_y) &&
53 EQUIV(out_w, that.out_w) &&
54 EQUIV(out_h, that.out_h) &&
55 !strcmp(svg_file, that.svg_file) &&
56 ms_time == that.ms_time;
59 void SvgConfig::copy_from(SvgConfig &that)
66 strcpy(svg_file, that.svg_file);
67 ms_time = that.ms_time;
70 void SvgConfig::interpolate(SvgConfig &prev, SvgConfig &next,
71 long prev_frame, long next_frame, long current_frame)
73 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
74 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
76 this->out_x = prev.out_x * prev_scale + next.out_x * next_scale;
77 this->out_y = prev.out_y * prev_scale + next.out_y * next_scale;
78 this->out_w = prev.out_w * prev_scale + next.out_w * next_scale;
79 this->out_h = prev.out_h * prev_scale + next.out_h * next_scale;
81 strcpy(this->svg_file, prev.svg_file);
82 this->ms_time = prev.ms_time;
86 SvgMain::SvgMain(PluginServer *server)
87 : PluginVClient(server)
100 const char* SvgMain::plugin_title() { return _("SVG via Inkscape"); }
101 int SvgMain::is_realtime() { return 1; }
102 int SvgMain::is_synthesis() { return 1; }
105 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
107 void SvgMain::save_data(KeyFrame *keyframe)
111 // cause data to be stored directly in text
112 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
114 output.tag.set_title("SVG");
115 output.tag.set_property("OUT_X", config.out_x);
116 output.tag.set_property("OUT_Y", config.out_y);
117 output.tag.set_property("OUT_W", config.out_w);
118 output.tag.set_property("OUT_H", config.out_h);
119 output.tag.set_property("DPI", config.dpi);
120 output.tag.set_property("SVG_FILE", config.svg_file);
121 output.tag.set_property("MS_TIME", config.ms_time);
123 output.tag.set_title("/SVG");
126 output.terminate_string();
129 void SvgMain::read_data(KeyFrame *keyframe)
133 const char *data = keyframe->get_data();
134 input.set_shared_input((char*)data, strlen(data));
137 while( !(result = input.read_tag()) ) {
138 if(input.tag.title_is("SVG")) {
139 config.out_x = input.tag.get_property("OUT_X", config.out_x);
140 config.out_y = input.tag.get_property("OUT_Y", config.out_y);
141 config.out_w = input.tag.get_property("OUT_W", config.out_w);
142 config.out_h = input.tag.get_property("OUT_H", config.out_h);
143 config.dpi = input.tag.get_property("DPI", config.dpi);
144 input.tag.get_property("SVG_FILE", config.svg_file);
145 config.ms_time = input.tag.get_property("MS_TIME", config.ms_time);
151 int SvgMain::process_realtime(VFrame *input, VFrame *output)
153 if( input != output )
154 output->copy_from(input);
157 float last_dpi = config.dpi;
158 char last_svg_file[BCTEXTLEN];
159 strcpy(last_svg_file, config.svg_file);
160 int64_t last_ms_time = config.ms_time;
161 load_configuration();
162 if( last_dpi != config.dpi )
164 if( strcmp(last_svg_file, config.svg_file) ||
165 last_ms_time != config.ms_time )
166 need_reconfigure = 1;
168 if( need_reconfigure || need_export ) {
169 need_reconfigure = 0;
170 if( config.svg_file[0] == 0 ) return 0;
171 delete ofrm; ofrm = 0;
172 char filename_png[BCTEXTLEN];
173 strcpy(filename_png, config.svg_file);
174 strncat(filename_png, ".png", sizeof(filename_png));
176 int64_t ms_time = need_export || stat(filename_png, &st_png) ? 0 :
177 st_png.st_mtim.tv_sec*1000 + st_png.st_mtim.tv_nsec/1000000;
178 int fd = ms_time < config.ms_time ? -1 : open(filename_png, O_RDWR);
179 if( fd < 0 ) { // file does not exist, export it
180 char command[BCTEXTLEN];
182 "inkscape --without-gui --export-background=0x000000 "
183 "--export-background-opacity=0 -d %f %s --export-png=%s",
184 config.dpi, config.svg_file, filename_png);
185 printf(_("Running command %s\n"), command);
187 // in order for lockf to work it has to be open for writing
188 fd = open(filename_png, O_RDWR);
190 printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
193 ofrm = VFramePng::vframe_png(fd);
195 if( ofrm && ofrm->get_color_model() != output->get_color_model() ) {
196 VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
197 output->get_color_model());
198 vfrm->transfer_from(ofrm);
199 delete ofrm; ofrm = vfrm;
202 printf (_("The file %s that was generated from %s is not in PNG format."
203 " Try to delete all *.png files.\n"), filename_png, config.svg_file);
207 if(!overlayer) overlayer = new OverlayFrame(smp + 1);
208 overlayer->overlay(output, ofrm,
209 0, 0, ofrm->get_w(), ofrm->get_h(),
210 config.out_x, config.out_y,
211 config.out_x + config.out_w,
212 config.out_y + config.out_h,
213 1, TRANSFER_NORMAL, LINEAR_LINEAR);
219 NEW_WINDOW_MACRO(SvgMain, SvgWin)
221 void SvgMain::update_gui()
224 load_configuration();
225 SvgWin *window = (SvgWin*)thread->window;
226 window->update_gui(config);