6abc584be408a8488a0ac483293b37cf99d3cabc
[goodguy/history.git] / cinelerra-5.0 / plugins / svg / svg.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "clip.h"
23 #include "filexml.h"
24 #include "svg.h"
25 #include "svgwin.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/mman.h>
33
34
35 #include <libintl.h>
36 #define _(String) gettext(String)
37 #define gettext_noop(String) String
38 #define N_(String) gettext_noop (String)
39
40 //#include "empty_svg.h"
41
42 REGISTER_PLUGIN(SvgMain)
43
44 SvgConfig::SvgConfig()
45 {
46         in_x = 0;
47         in_y = 0;
48         in_w = 720;
49         in_h = 480;
50         out_x = 0;
51         out_y = 0;
52         out_w = 720;
53         out_h = 480;
54         strcpy(svg_file, "");
55 }
56
57 int SvgConfig::equivalent(SvgConfig &that)
58 {
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);
68 }
69
70 void SvgConfig::copy_from(SvgConfig &that)
71 {
72         in_x = that.in_x;
73         in_y = that.in_y;
74         in_w = that.in_w;
75         in_h = that.in_h;
76         out_x = that.out_x;
77         out_y = that.out_y;
78         out_w = that.out_w;
79         out_h = that.out_h;
80         strcpy(svg_file, that.svg_file);
81 }
82
83 void SvgConfig::interpolate(SvgConfig &prev, 
84         SvgConfig &next, 
85         long prev_frame, 
86         long next_frame, 
87         long current_frame)
88 {
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);
91
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);
101 }
102
103
104
105
106
107
108
109
110 SvgMain::SvgMain(PluginServer *server)
111  : PluginVClient(server)
112 {
113         ofrm = 0;
114         overlayer = 0;
115         need_reconfigure = 1;
116 }
117
118 SvgMain::~SvgMain()
119 {
120         delete ofrm;
121         delete overlayer;
122 }
123
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; }
127
128
129 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
130
131 void SvgMain::save_data(KeyFrame *keyframe)
132 {
133         FileXML output;
134
135 // cause data to be stored directly in text
136         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
137
138 // Store data
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);
149         output.append_tag();
150         output.tag.set_title("/SVG");
151         output.append_tag();
152
153         output.terminate_string();
154 // data is now in *text
155 }
156
157 void SvgMain::read_data(KeyFrame *keyframe)
158 {
159         FileXML input;
160
161         const char *data = keyframe->get_data();
162         input.set_shared_input((char*)data, strlen(data));
163
164         int result = 0;
165
166         while(!result)
167         {
168                 result = input.read_tag();
169
170                 if(!result)
171                 {
172                         if(input.tag.title_is("SVG"))
173                         {
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);
183                         }
184                 }
185         }
186 }
187
188
189
190
191
192
193
194
195 int SvgMain::process_realtime(VFrame *input, VFrame *output)
196 {
197
198         need_reconfigure |= load_configuration();
199         output->copy_from(input);
200         if( config.svg_file[0] == 0 ) return 0;
201
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
210                         char command[1024];
211                         sprintf(command,
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);
216                         system(command);
217                         // in order for lockf to work it has to be open for writing
218                         fd = open(filename_png, O_RDWR);
219                         if( fd < 0 )
220                                 printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
221                 }
222                 if( fd >= 0 ) {
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);
226                         struct stat st_png;
227                         fstat(fd, &st_png);
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(),
238                                                         0, 0, 0, 0, 0, 0,
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;
245                                         }
246                                 }
247                                 else
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);
251                         }
252                         else
253                                 printf(_("Access mmap to %s as %s failed.\n"), config.svg_file, filename_png);
254                         lockf(fd, F_ULOCK, 0);
255                         close(fd);
256                 }
257         }
258         if( ofrm ) {
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(),
265                         1, TRANSFER_NORMAL,
266                         get_interpolation_type());
267         }
268         return 0;
269 }
270
271
272 NEW_WINDOW_MACRO(SvgMain, SvgWin)
273
274 void SvgMain::update_gui()
275 {
276         if(thread)
277         {
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();
291         }
292 }