312b0498206d3b0930bb5f526eb08d044090f0ed
[goodguy/history.git] / cinelerra-5.1 / 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 "language.h"
25 #include "svg.h"
26 #include "svgwin.h"
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/mman.h>
34
35
36 //#include "empty_svg.h"
37
38 REGISTER_PLUGIN(SvgMain)
39
40 SvgConfig::SvgConfig()
41 {
42         in_x = 0;
43         in_y = 0;
44         in_w = 720;
45         in_h = 480;
46         out_x = 0;
47         out_y = 0;
48         out_w = 720;
49         out_h = 480;
50         strcpy(svg_file, "");
51 }
52
53 int SvgConfig::equivalent(SvgConfig &that)
54 {
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);
64 }
65
66 void SvgConfig::copy_from(SvgConfig &that)
67 {
68         in_x = that.in_x;
69         in_y = that.in_y;
70         in_w = that.in_w;
71         in_h = that.in_h;
72         out_x = that.out_x;
73         out_y = that.out_y;
74         out_w = that.out_w;
75         out_h = that.out_h;
76         strcpy(svg_file, that.svg_file);
77 }
78
79 void SvgConfig::interpolate(SvgConfig &prev, 
80         SvgConfig &next, 
81         long prev_frame, 
82         long next_frame, 
83         long current_frame)
84 {
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);
87
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);
97 }
98
99
100
101
102
103
104
105
106 SvgMain::SvgMain(PluginServer *server)
107  : PluginVClient(server)
108 {
109         ofrm = 0;
110         overlayer = 0;
111         need_reconfigure = 1;
112 }
113
114 SvgMain::~SvgMain()
115 {
116         delete ofrm;
117         delete overlayer;
118 }
119
120 const char* SvgMain::plugin_title() { return _("SVG via Inkscape"); }
121 int SvgMain::is_realtime() { return 1; }
122 int SvgMain::is_synthesis() { return 1; }
123
124
125 LOAD_CONFIGURATION_MACRO(SvgMain, SvgConfig)
126
127 void SvgMain::save_data(KeyFrame *keyframe)
128 {
129         FileXML output;
130
131 // cause data to be stored directly in text
132         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
133
134 // Store data
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);
145         output.append_tag();
146         output.tag.set_title("/SVG");
147         output.append_tag();
148
149         output.terminate_string();
150 // data is now in *text
151 }
152
153 void SvgMain::read_data(KeyFrame *keyframe)
154 {
155         FileXML input;
156
157         const char *data = keyframe->get_data();
158         input.set_shared_input((char*)data, strlen(data));
159
160         int result = 0;
161
162         while(!result)
163         {
164                 result = input.read_tag();
165
166                 if(!result)
167                 {
168                         if(input.tag.title_is("SVG"))
169                         {
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);
179                         }
180                 }
181         }
182 }
183
184
185
186
187
188
189
190
191 int SvgMain::process_realtime(VFrame *input, VFrame *output)
192 {
193
194         need_reconfigure |= load_configuration();
195         output->copy_from(input);
196         if( config.svg_file[0] == 0 ) return 0;
197
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
206                         char command[1024];
207                         sprintf(command,
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);
212                         system(command);
213                         // in order for lockf to work it has to be open for writing
214                         fd = open(filename_png, O_RDWR);
215                         if( fd < 0 )
216                                 printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
217                 }
218                 if( fd >= 0 ) {
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);
222                         struct stat st_png;
223                         fstat(fd, &st_png);
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(),
234                                                         0, 0, 0, 0, 0, 0,
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;
241                                         }
242                                 }
243                                 else
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);
247                         }
248                         else
249                                 printf(_("Access mmap to %s as %s failed.\n"), config.svg_file, filename_png);
250                         lockf(fd, F_ULOCK, 0);
251                         close(fd);
252                 }
253         }
254         if( ofrm ) {
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(),
261                         1, TRANSFER_NORMAL,
262                         get_interpolation_type());
263         }
264         return 0;
265 }
266
267
268 NEW_WINDOW_MACRO(SvgMain, SvgWin)
269
270 void SvgMain::update_gui()
271 {
272         if(thread)
273         {
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();
287         }
288 }