1 /*! ========================================================================
4 ** $Id: mptr_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
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.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
30 #include <ETL/stringf>
31 #include "mptr_imagemagick.h"
35 #include <ETL/stringf>
36 #include <sinfg/general.h>
40 /* === M A C R O S ========================================================= */
42 using namespace sinfg;
46 /* === G L O B A L S ======================================================= */
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 $");
54 /* === M E T H O D S ======================================================= */
57 imagemagick_mptr::imagemagick_mptr(const char *f)
64 imagemagick_mptr::~imagemagick_mptr()
71 imagemagick_mptr::get_frame(sinfg::Surface &surface,Time time, sinfg::ProgressCallback *cb)
73 //#define HAS_LIBPNG 1
83 if(cb)cb->error(_("No file to load"));
84 else sinfg::error(_("No file to load"));
87 string temp_file="/tmp/deleteme.png";
89 if(filename.find("psd")!=String::npos)
90 command=strprintf("convert \"%s\" -flatten \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
92 command=strprintf("convert \"%s\" \"png32:%s\"\n",filename.c_str(),temp_file.c_str());
94 sinfg::info("command=%s",command.c_str());
96 if(system(command.c_str())!=0)
99 Importer::Handle importer(Importer::open(temp_file));
105 if(cb)cb->error(_("Unable to open ")+temp_file);
106 else sinfg::error(_("Unable to open ")+temp_file);
112 if(!importer->get_frame(surface,0,cb))
114 if(cb)cb->error(_("Unable to get frame from ")+temp_file);
115 else sinfg::error(_("Unable to get frame from ")+temp_file);
121 if(cb)cb->error(_("Bad surface from ")+temp_file);
122 else sinfg::error(_("Bad surface from ")+temp_file);
128 // remove odd premultiplication
129 for(int i=0;i<surface.get_w()*surface.get_h();i++)
131 Color c(surface[0][i]);
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());
141 surface[0][i].set_r(0);
142 surface[0][i].set_g(0);
143 surface[0][i].set_b(0);
145 surface[0][i].set_a(c.get_a());
149 Surface bleh(surface);
153 //remove(temp_file.c_str());
165 if(cb)cb->error(_("No file to load"));
166 else sinfg::error(_("No file to load"));
170 command=strprintf("convert \"%s\" -flatten ppm:-\n",filename.c_str());
172 file=popen(command.c_str(),"r");
176 if(cb)cb->error(_("Unable to open pipe to imagemagick"));
177 else sinfg::error(_("Unable to open pipe to imagemagick"));
184 while((cookie[0]=fgetc(file))!='P' && !feof(file));
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"));
193 cookie[1]=fgetc(file);
195 if(cookie[0]!='P' || cookie[1]!='6')
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]+'"');
203 fscanf(file,"%d %d\n",&w,&h);
204 fscanf(file,"%f",&divisor);
209 if(cb)cb->error(_("Premature end of file (after header)"));
210 else sinfg::error(_("Premature end of file (after header)"));
217 for(y=0;y<frame.get_h();y++)
218 for(x=0;x<frame.get_w();x++)
222 if(cb)cb->error(_("Premature end of file"));
223 else sinfg::error(_("Premature end of file"));
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));
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;