Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_imagemagick / mptr_imagemagick.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: mptr_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 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 <ETL/stringf>
32 #include "mptr_imagemagick.h"
33 #include <stdio.h>
34 #include <algorithm>
35 #include <functional>
36 #include <ETL/stringf>
37 #include <synfig/general.h>
38
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 SYNFIG_IMPORTER_INIT(imagemagick_mptr);
50 SYNFIG_IMPORTER_SET_NAME(imagemagick_mptr,"imagemagick");
51 SYNFIG_IMPORTER_SET_EXT(imagemagick_mptr,"miff");
52 SYNFIG_IMPORTER_SET_VERSION(imagemagick_mptr,"0.1");
53 SYNFIG_IMPORTER_SET_CVS_ID(imagemagick_mptr,"$Id: mptr_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
54
55 /* === M E T H O D S ======================================================= */
56
57
58 imagemagick_mptr::imagemagick_mptr(const char *f)
59 {
60
61         filename=f;
62         file=NULL;
63 }
64
65 imagemagick_mptr::~imagemagick_mptr()
66 {
67         if(file)
68                 pclose(file);
69 }
70
71 bool
72 imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *cb)
73 {
74 //#define HAS_LIBPNG 1
75
76 #if 1
77         if(file)
78                 pclose(file);
79
80         string command;
81
82         if(filename.empty())
83         {
84                 if(cb)cb->error(_("No file to load"));
85                 else synfig::error(_("No file to load"));
86                 return false;
87         }
88         string temp_file="/tmp/deleteme.png";
89         
90         if(filename.find("psd")!=String::npos)
91                 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
92         else
93                 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
94         
95         synfig::info("command=%s",command.c_str());
96         
97         if(system(command.c_str())!=0)
98                 return false;
99
100         Importer::Handle importer(Importer::open(temp_file));
101         
102         DEBUGPOINT();
103
104         if(!importer)
105         {
106                 if(cb)cb->error(_("Unable to open ")+temp_file);
107                 else synfig::error(_("Unable to open ")+temp_file);
108                 return false;
109         }
110         
111         DEBUGPOINT();
112
113         if(!importer->get_frame(surface,0,cb))
114         {
115                 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
116                 else synfig::error(_("Unable to get frame from ")+temp_file);
117                 return false;
118         }
119         
120         if(!surface)
121         {
122                 if(cb)cb->error(_("Bad surface from ")+temp_file);
123                 else synfig::error(_("Bad surface from ")+temp_file);
124                 return false;           
125         }
126
127         if(1)
128         {
129                 // remove odd premultiplication
130                 for(int i=0;i<surface.get_w()*surface.get_h();i++)
131                 {
132                         Color c(surface[0][i]);
133
134                         if(c.get_a())
135                         {
136                                 surface[0][i].set_r(c.get_r()/c.get_a()/c.get_a());
137                                 surface[0][i].set_g(c.get_g()/c.get_a()/c.get_a());
138                                 surface[0][i].set_b(c.get_b()/c.get_a()/c.get_a());
139                         }
140                         else
141                         {
142                                 surface[0][i].set_r(0);
143                                 surface[0][i].set_g(0);
144                                 surface[0][i].set_b(0);
145                         }
146                         surface[0][i].set_a(c.get_a());
147                 }
148         }
149
150         Surface bleh(surface);
151         surface=bleh;
152         
153
154         //remove(temp_file.c_str());
155         DEBUGPOINT();
156         return true;
157         
158 #else
159         if(file)
160                 pclose(file);
161
162         string command;
163
164         if(filename.empty())
165         {
166                 if(cb)cb->error(_("No file to load"));
167                 else synfig::error(_("No file to load"));
168                 return false;
169         }
170
171         command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
172
173         file=popen(command.c_str(),"r");
174
175         if(!file)
176         {
177                 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
178                 else synfig::error(_("Unable to open pipe to imagemagick"));
179                 return false;
180         }
181         int w,h;
182         float divisor;
183         char cookie[2];
184
185         while((cookie[0]=fgetc(file))!='P' && !feof(file));
186
187         if(feof(file))
188         {
189                 if(cb)cb->error(_("Reached end of stream without finding PPM header"));
190                 else synfig::error(_("Reached end of stream without finding PPM header"));
191                 return false;
192         }
193
194         cookie[1]=fgetc(file);
195
196         if(cookie[0]!='P' || cookie[1]!='6')
197         {
198                 if(cb)cb->error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
199                 else synfig::error(string(_("stream not in PPM format"))+" \""+cookie[0]+cookie[1]+'"');
200                 return false;
201         }
202
203         fgetc(file);
204         fscanf(file,"%d %d\n",&w,&h);
205         fscanf(file,"%f",&divisor);
206         fgetc(file);
207
208         if(feof(file))
209         {
210                 if(cb)cb->error(_("Premature end of file (after header)"));
211                 else synfig::error(_("Premature end of file (after header)"));
212                 return false;
213         }
214
215         int x;
216         int y;
217         frame.set_wh(w,h);
218         for(y=0;y<frame.get_h();y++)
219                 for(x=0;x<frame.get_w();x++)
220                 {
221                         if(feof(file))
222                         {
223                                 if(cb)cb->error(_("Premature end of file"));
224                                 else synfig::error(_("Premature end of file"));
225                                 return false;
226                         }
227                         float b=gamma().r_U8_to_F32((unsigned char)fgetc(file));
228                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
229                         float r=gamma().b_U8_to_F32((unsigned char)fgetc(file));
230 /*
231                         float b=(float)(unsigned char)fgetc(file)/divisor;
232                         float g=(float)(unsigned char)fgetc(file)/divisor;
233                         float r=(float)(unsigned char)fgetc(file)/divisor;
234 */
235                         frame[y][x]=Color(
236                                 b,
237                                 g,
238                                 r,
239                                 1.0
240                         );
241                 }
242
243         surface=frame;
244
245         return true;
246 #endif
247                 
248         
249 }