Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_imagemagick / trgt_imagemagick.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_imagemagick.cpp
3 **      \brief ppm Target Module
4 **
5 **      \legal
6 ** $Id$
7 **
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #define SYNFIG_TARGET
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include <ETL/stringf>
37 #include "trgt_imagemagick.h"
38 #include <stdio.h>
39 #include <algorithm>
40 #include <functional>
41 #include <ETL/clock>
42 #include <ETL/misc>
43
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 using namespace synfig;
49 using namespace std;
50 using namespace etl;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_TARGET_INIT(imagemagick_trgt);
55 SYNFIG_TARGET_SET_NAME(imagemagick_trgt,"imagemagick");
56 SYNFIG_TARGET_SET_EXT(imagemagick_trgt,"miff");
57 SYNFIG_TARGET_SET_VERSION(imagemagick_trgt,"0.1");
58 SYNFIG_TARGET_SET_CVS_ID(imagemagick_trgt,"$Id$");
59
60 /* === M E T H O D S ======================================================= */
61
62 imagemagick_trgt::imagemagick_trgt(const char *Filename)
63 {
64         file=NULL;
65         filename=Filename;
66         multi_image=false;
67         buffer=NULL;
68         color_buffer=0;
69 }
70
71 imagemagick_trgt::~imagemagick_trgt()
72 {
73         if(file)
74                 pclose(file);
75         file=NULL;
76         delete [] buffer;
77         delete [] color_buffer;
78 }
79
80 bool
81 imagemagick_trgt::set_rend_desc(RendDesc *given_desc)
82 {
83         String  ext(find(filename.begin(),filename.end(),'.')+1,filename.end());
84         if(ext=="xpm")
85                 pf=PF_RGB;
86         else
87                 pf=PF_RGB|PF_A;
88
89         desc=*given_desc;
90         return true;
91 }
92
93 bool
94 imagemagick_trgt::init()
95 {
96         imagecount=desc.get_frame_start();
97         if(desc.get_frame_end()-desc.get_frame_start()>0)
98                 multi_image=true;
99
100         delete [] buffer;
101         buffer=new unsigned char[channels(pf)*desc.get_w()];
102         delete [] color_buffer;
103         color_buffer=new Color[desc.get_w()];
104         return true;
105 }
106
107 void
108 imagemagick_trgt::end_frame()
109 {
110         if(file)
111         {
112                 fputc(0,file);
113                 fflush(file);
114                 pclose(file);
115         }
116         file=NULL;
117 }
118
119 bool
120 imagemagick_trgt::start_frame(synfig::ProgressCallback *cb)
121 {
122         string command;
123
124         if(channels(pf)==4)
125                 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());
126         else
127                 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());
128
129         file=popen(command.c_str(),"w");
130
131         if(!file)
132         {
133                 const char *msg=_("Unable to open pipe to imagemagick's convert utility");
134                 if(cb)cb->error(N_(msg));
135                 else synfig::error(N_(msg));
136                 return false;
137         }
138
139         //etl::yield();
140
141         return true;
142 }
143
144 Color *
145 imagemagick_trgt::start_scanline(int scanline)
146 {
147         return color_buffer;
148 }
149
150 bool
151 imagemagick_trgt::end_scanline(void)
152 {
153         if(!file)
154                 return false;
155
156         convert_color_format(buffer, color_buffer, desc.get_w(), pf, gamma());
157
158         if(!fwrite(buffer,channels(pf),desc.get_w(),file))
159                 return false;
160
161         return true;
162 }