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