1 /*! ========================================================================
4 ** $Id: mptr_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <ETL/stringf>
32 #include "mptr_imagemagick.h"
36 #include <ETL/stringf>
37 #include <synfig/general.h>
41 /* === M A C R O S ========================================================= */
43 using namespace synfig;
47 /* === G L O B A L S ======================================================= */
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 $");
55 /* === M E T H O D S ======================================================= */
58 imagemagick_mptr::imagemagick_mptr(const char *f)
65 imagemagick_mptr::~imagemagick_mptr()
72 imagemagick_mptr::get_frame(synfig::Surface &surface,Time time, synfig::ProgressCallback *cb)
74 //#define HAS_LIBPNG 1
84 if(cb)cb->error(_("No file to load"));
85 else synfig::error(_("No file to load"));
88 string temp_file="/tmp/deleteme.png";
90 if(filename.find("psd")!=String::npos)
91 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
93 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
95 synfig::info("command=%s",command.c_str());
97 if(system(command.c_str())!=0)
100 Importer::Handle importer(Importer::open(temp_file));
106 if(cb)cb->error(_("Unable to open ")+temp_file);
107 else synfig::error(_("Unable to open ")+temp_file);
113 if(!importer->get_frame(surface,0,cb))
115 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
116 else synfig::error(_("Unable to get frame from ")+temp_file);
122 if(cb)cb->error(_("Bad surface from ")+temp_file);
123 else synfig::error(_("Bad surface from ")+temp_file);
129 // remove odd premultiplication
130 for(int i=0;i<surface.get_w()*surface.get_h();i++)
132 Color c(surface[0][i]);
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());
142 surface[0][i].set_r(0);
143 surface[0][i].set_g(0);
144 surface[0][i].set_b(0);
146 surface[0][i].set_a(c.get_a());
150 Surface bleh(surface);
154 //remove(temp_file.c_str());
166 if(cb)cb->error(_("No file to load"));
167 else synfig::error(_("No file to load"));
171 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
173 file=popen(command.c_str(),"r");
177 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
178 else synfig::error(_("Unable to open pipe to imagemagick"));
185 while((cookie[0]=fgetc(file))!='P' && !feof(file));
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"));
194 cookie[1]=fgetc(file);
196 if(cookie[0]!='P' || cookie[1]!='6')
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]+'"');
204 fscanf(file,"%d %d\n",&w,&h);
205 fscanf(file,"%f",&divisor);
210 if(cb)cb->error(_("Premature end of file (after header)"));
211 else synfig::error(_("Premature end of file (after header)"));
218 for(y=0;y<frame.get_h();y++)
219 for(x=0;x<frame.get_w();x++)
223 if(cb)cb->error(_("Premature end of file"));
224 else synfig::error(_("Premature end of file"));
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));
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;