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