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