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
24 #include "mainerror.h"
27 #include "overlayframe.inc"
29 #include <sys/types.h>
36 #include <sys/types.h>
40 REGISTER_PLUGIN(SvgMain)
42 SvgConfig::SvgConfig()
45 out_w = 640; out_h = 480;
51 int SvgConfig::equivalent(SvgConfig &that)
53 return EQUIV(dpi, that.dpi) &&
54 EQUIV(out_x, that.out_x) &&
55 EQUIV(out_y, that.out_y) &&
56 EQUIV(out_w, that.out_w) &&
57 EQUIV(out_h, that.out_h) &&
58 !strcmp(svg_file, that.svg_file) &&
59 ms_time != 0 && that.ms_time != 0 &&
60 ms_time == that.ms_time;
63 void SvgConfig::copy_from(SvgConfig &that)
70 strcpy(svg_file, that.svg_file);
71 ms_time = that.ms_time;
74 void SvgConfig::interpolate(SvgConfig &prev, SvgConfig &next,
75 long prev_frame, long next_frame, long current_frame)
77 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
78 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
80 this->out_x = prev.out_x * prev_scale + next.out_x * next_scale;
81 this->out_y = prev.out_y * prev_scale + next.out_y * next_scale;
82 this->out_w = prev.out_w * prev_scale + next.out_w * next_scale;
83 this->out_h = prev.out_h * prev_scale + next.out_h * next_scale;
85 strcpy(this->svg_file, prev.svg_file);
86 this->ms_time = prev.ms_time;
90 SvgMain::SvgMain(PluginServer *server)
91 : PluginVClient(server)
104 const char* SvgMain::plugin_title() { return N_("SVG via Inkscape"); }
105 int SvgMain::is_realtime() { return 1; }
106 int SvgMain::is_synthesis() { return 1; }
109 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
111 void SvgMain::save_data(KeyFrame *keyframe)
115 // cause data to be stored directly in text
116 output.set_shared_output(keyframe->xbuf);
118 output.tag.set_title("SVG");
119 output.tag.set_property("OUT_X", config.out_x);
120 output.tag.set_property("OUT_Y", config.out_y);
121 output.tag.set_property("OUT_W", config.out_w);
122 output.tag.set_property("OUT_H", config.out_h);
123 output.tag.set_property("DPI", config.dpi);
124 output.tag.set_property("SVG_FILE", config.svg_file);
125 output.tag.set_property("MS_TIME", config.ms_time);
127 output.tag.set_title("/SVG");
130 output.terminate_string();
133 void SvgMain::read_data(KeyFrame *keyframe)
137 input.set_shared_input(keyframe->xbuf);
140 while( !(result = input.read_tag()) ) {
141 if(input.tag.title_is("SVG")) {
142 config.out_x = input.tag.get_property("OUT_X", config.out_x);
143 config.out_y = input.tag.get_property("OUT_Y", config.out_y);
144 config.out_w = input.tag.get_property("OUT_W", config.out_w);
145 config.out_h = input.tag.get_property("OUT_H", config.out_h);
146 config.dpi = input.tag.get_property("DPI", config.dpi);
147 input.tag.get_property("SVG_FILE", config.svg_file);
148 config.ms_time = input.tag.get_property("MS_TIME", config.ms_time);
153 static int exec_command(char* const*argv)
156 if( pid < 0 ) return -1;
159 waitpid(pid, &stat, 0);
162 sprintf(msg, "%s: error exit status %d", argv[0], stat);
163 MainError::show_error(msg);
167 execvp(argv[0], &argv[0]);
172 int SvgMain::process_realtime(VFrame *input, VFrame *output)
174 if( input != output )
175 output->copy_from(input);
178 float last_dpi = config.dpi;
179 char last_svg_file[BCTEXTLEN];
180 strcpy(last_svg_file, config.svg_file);
181 int64_t last_ms_time = config.ms_time;
182 need_reconfigure = load_configuration();
183 if( !ofrm || last_dpi != config.dpi )
185 if( strcmp(last_svg_file, config.svg_file) ||
186 last_ms_time != config.ms_time )
187 need_reconfigure = 1;
189 if( need_reconfigure || need_export ) {
190 need_reconfigure = 0;
191 if( config.svg_file[0] == 0 ) return 0;
192 delete ofrm; ofrm = 0;
193 char filename_png[BCTEXTLEN];
194 strcpy(filename_png, config.svg_file);
195 strncat(filename_png, ".png", sizeof(filename_png));
197 int64_t ms_time = need_export || stat(filename_png, &st_png) ? 0 :
198 st_png.st_mtim.tv_sec*1000 + st_png.st_mtim.tv_nsec/1000000;
199 int fd = ms_time < config.ms_time ? -1 : open(filename_png, O_RDWR);
200 if( fd < 0 ) { // file does not exist, export it
201 char command[BCTEXTLEN], dpi[BCSTRLEN];
202 snprintf(command, sizeof(command),
203 "inkscape --without-gui --export-background=0x000000 "
204 "--export-background-opacity=0 -d %f %s --export-png=%s",
205 config.dpi, config.svg_file, filename_png);
206 printf(_("Running command %s\n"), command);
207 snprintf(dpi, sizeof(dpi), "%f", config.dpi);
208 snprintf(command, sizeof(command), "--export-png=%s",filename_png);
209 char *const argv[] = {
211 (char*)"--without-gui",
212 (char*)"--export-background=0x000000",
213 (char*)"--export-background-opacity=0",
214 (char*)"-d", dpi, config.svg_file, 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 ofrm = VFramePng::vframe_png(fd);
225 if( ofrm && ofrm->get_color_model() != output->get_color_model() ) {
226 VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
227 output->get_color_model(), 0);
228 vfrm->transfer_from(ofrm);
229 delete ofrm; ofrm = vfrm;
232 printf (_("The file %s that was generated from %s is not in PNG format."
233 " Try to delete all *.png files.\n"), filename_png, config.svg_file);
237 if(!overlayer) overlayer = new OverlayFrame(smp + 1);
238 overlayer->overlay(output, ofrm,
239 0, 0, ofrm->get_w(), ofrm->get_h(),
240 config.out_x, config.out_y,
241 config.out_x + config.out_w,
242 config.out_y + config.out_h,
243 1, TRANSFER_NORMAL, LINEAR_LINEAR);
249 NEW_WINDOW_MACRO(SvgMain, SvgWin)
251 void SvgMain::update_gui()
254 load_configuration();
255 SvgWin *window = (SvgWin*)thread->window;
256 window->update_gui(config);