Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_04 / synfig-core / src / modules / mod_imagemagick / trgt_imagemagick.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: trgt_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
5 **
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 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SYNFIG_TARGET
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <ETL/stringf>
34 #include "trgt_imagemagick.h"
35 #include <stdio.h>
36 #include <algorithm>
37 #include <functional>
38 #include <ETL/clock>
39 #include <ETL/misc>
40
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_TARGET_INIT(imagemagick_trgt);
52 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
53 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
54 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id: trgt_imagemagick.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
56
57 /* === M E T H O D S ======================================================= */
58
59 imagemagick_trgt::imagemagick_trgt(const char *Filename)
60 {
61         file=NULL;
62         filename=Filename;
63         multi_image=false;
64         buffer=NULL;
65         color_buffer=0;
66 }
67
68 imagemagick_trgt::~imagemagick_trgt()
69 {
70         if(file)
71                 pclose(file);
72         file=NULL;
73         delete [] buffer;
74         delete [] color_buffer;
75 }
76
77 bool
78 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
79 {
80         String  ext(find(filename.begin(),filename.end(),'.')+1,filename.end());
81         if(ext=="xpm")
82                 pf=PF_RGB;
83         else
84                 pf=PF_RGB|PF_A;
85
86         desc=*given_desc;
87         return true;
88 }
89
90 bool
91 imagemagick_trgt::init()
92 {
93         imagecount=desc.get_frame_start();
94         if(desc.get_frame_end()-desc.get_frame_start()>0)
95                 multi_image=true;
96
97         delete [] buffer;
98         buffer=new unsigned char[channels(pf)*desc.get_w()];
99         delete [] color_buffer;
100         color_buffer=new Color[desc.get_w()];
101         return true;
102 }
103
104 void
105 imagemagick_trgt::end_frame()
106 {
107         if(file)
108         {
109                 fputc(0,file);
110                 fflush(file);
111                 pclose(file);
112         }
113         file=NULL;
114 }
115
116 bool
117 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
118 {
119         string command;
120
121         if(channels(pf)==4)
122                 command=strprintf("convert -depth 8 -size %dx%d rgba:-[0] -density %dx%d  \"%s\"\n",desc.get_w(),desc.get_h(),round_to_int(desc.get_x_res()/39.3700787402),round_to_int(desc.get_y_res()/39.3700787402),filename.c_str());
123         else
124                 command=strprintf("convert -depth 8 -size %dx%d rgb:-[0] -density %dx%d \"%s\"\n",desc.get_w(),desc.get_h(),round_to_int(desc.get_x_res()/39.3700787402),round_to_int(desc.get_y_res()/39.3700787402),filename.c_str());
125
126         file=popen(command.c_str(),"w");
127
128         if(!file)
129         {
130                 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
131                 if(cb)cb->error(N_(msg));
132                 else synfig::error(N_(msg));
133                 return false;
134         }
135
136         //etl::yield(); 
137
138         return true;
139 }
140
141 Color *
142 imagemagick_trgt::start_scanline(int scanline)
143 {
144         return color_buffer;
145 }
146
147 bool
148 imagemagick_trgt::end_scanline(void)
149 {
150         if(!file)
151                 return false;
152
153         convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
154
155         if(!fwrite(buffer,channels(pf),desc.get_w(),file))
156                 return false;
157         
158         return true;
159 }