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