Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / 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         int ret;
76         ret=system(
77                 strprintf("/usr/local/bin/mencoder \"%s\" -ovc rawrgb -ss %f -endpos 0 -nosound -o /tmp/tmp.synfig.rgbdata | grep \"VIDEO\" > /tmp/tmp.synfig.size",
78                         filename.c_str(),
79                         time
80                 ).c_str()
81         );
82         /*
83         if(ret!=0)
84         {
85                 cerr<<"mencoder execution failed."<<endl;
86                 return false;
87         }
88 */
89         FILE *sizefile=fopen("/tmp/tmp.synfig.size","rt");
90         FILE *rgbfile=fopen("/tmp/tmp.synfig.rgbdata","rb");
91         if(!rgbfile)
92         {
93                 cerr<<"unable to open /tmp/tmp.synfig.rgbdata"<<endl;
94                 return false;
95         }
96         if(!sizefile)
97         {
98                 cerr<<"unable to open /tmp/tmp.synfig.size"<<endl;
99                 return false;
100         }
101
102         int w=4,h=4,x,y;
103         char bleh[500];
104
105         fscanf(sizefile,"%s %s %dx%d",bleh,bleh,&w,&h);
106
107         cerr<<strprintf("w:%d, h:%d, time:%f",w,h,time)<<endl;
108         fseek(rgbfile,2047+3*8,SEEK_CUR);
109         surface.set_wh(w,h);
110         for(y=0;y<h;y++)
111                 for(x=0;x<w;x++)
112                 {
113                         unsigned char
114                                 b=(unsigned char)fgetc(rgbfile),
115                                 g=(unsigned char)fgetc(rgbfile),
116                                 r=(unsigned char)fgetc(rgbfile);
117
118                         surface[h-y-1][x]=Color(
119                                 (float)r/255.0,
120                                 (float)g/255.0,
121                                 (float)b/255.0,
122                                 1.0
123                         );
124                 }
125
126         fclose(rgbfile);
127         fclose(sizefile);
128
129         return true;
130 }