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