Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / src / modules / mptr_mplayer / mptr_mplayer.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_mplayer.cpp
3 **      \brief ppm Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <synfig/synfig.h>
35 #include <ETL/stringf>
36 #include "mptr_mplayer.h"
37 #include <stdio.h>
38 #include <iostream>
39 #include <algorithm>
40 #include <functional>
41 #include <ETL/stringf>
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === G L O B A L S ======================================================= */
51
52 const char mplayer_mptr::Name[]="avi";
53 const char mplayer_mptr::Ext[]="avi";
54
55 /* === M E T H O D S ======================================================= */
56
57 Importer *
58 mplayer_mptr::New(const char *file)
59 {
60         return new mplayer_mptr(file);
61 }
62
63 mplayer_mptr::mplayer_mptr(const char *file)
64 {
65         filename=file;
66 }
67
68 mplayer_mptr::~mplayer_mptr()
69 {
70 }
71
72 bool
73 mplayer_mptr::GetFrame(Time time, synfig::Surface &surface, synfig::ProgressCallback *)
74 {
75
76 #error This code has vulnerabilites: arbitrary shell command execution and tmpfile issues
77
78         int ret;
79         ret=system(
80                 strprintf("/usr/local/bin/mencoder \"%s\" -ovc rawrgb -ss %f -endpos 0 -nosound -o /tmp/tmp.synfig.rgbdata | grep \"VIDEO\" > /tmp/tmp.synfig.size",
81                         filename.c_str(),
82                         time
83                 ).c_str()
84         );
85         /*
86         if(ret!=0)
87         {
88                 cerr<<"mencoder execution failed."<<endl;
89                 return false;
90         }
91 */
92         FILE *sizefile=fopen("/tmp/tmp.synfig.size","rt");
93         FILE *rgbfile=fopen("/tmp/tmp.synfig.rgbdata","rb");
94         if(!rgbfile)
95         {
96                 cerr<<"unable to open /tmp/tmp.synfig.rgbdata"<<endl;
97                 return false;
98         }
99         if(!sizefile)
100         {
101                 cerr<<"unable to open /tmp/tmp.synfig.size"<<endl;
102                 return false;
103         }
104
105         int w=4,h=4,x,y;
106         char bleh[500];
107
108         fscanf(sizefile,"%s %s %dx%d",bleh,bleh,&w,&h);
109
110         cerr<<strprintf("w:%d, h:%d, time:%f",w,h,time)<<endl;
111         fseek(rgbfile,2047+3*8,SEEK_CUR);
112         surface.set_wh(w,h);
113         for(y=0;y<h;y++)
114                 for(x=0;x<w;x++)
115                 {
116                         unsigned char
117                                 b=(unsigned char)fgetc(rgbfile),
118                                 g=(unsigned char)fgetc(rgbfile),
119                                 r=(unsigned char)fgetc(rgbfile);
120
121                         surface[h-y-1][x]=Color(
122                                 (float)r/255.0,
123                                 (float)g/255.0,
124                                 (float)b/255.0,
125                                 1.0
126                         );
127                 }
128
129         fclose(rgbfile);
130         fclose(sizefile);
131
132         return true;
133 }