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_ppm / trgt_ppm.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_ppm.cpp
3 **      \brief ppm Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
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 "trgt_ppm.h"
37 #include <ETL/stringf>
38 #include <cstdio>
39 #include <algorithm>
40 #include <functional>
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(ppm);
52 SYNFIG_TARGET_SET_NAME(ppm,"ppm");
53 SYNFIG_TARGET_SET_EXT(ppm,"ppm");
54 SYNFIG_TARGET_SET_VERSION(ppm,"0.1");
55 SYNFIG_TARGET_SET_CVS_ID(ppm,"$Id$");
56
57 /* === M E T H O D S ======================================================= */
58
59 ppm::ppm(const char *Filename)
60 {
61         filename=Filename;
62         multi_image=false;
63         buffer=NULL;
64         color_buffer=0;
65         set_remove_alpha();
66 }
67
68 ppm::~ppm()
69 {
70         delete [] buffer;
71         delete [] color_buffer;
72 }
73
74 bool
75 ppm::set_rend_desc(RendDesc *given_desc)
76 {
77         //given_desc->set_pixel_format(PF_RGB);
78         desc=*given_desc;
79         imagecount=desc.get_frame_start();
80         if(desc.get_frame_end()-desc.get_frame_start()>0)
81                 multi_image=true;
82         else
83                 multi_image=false;
84         return true;
85 }
86
87 void
88 ppm::end_frame()
89 {
90         imagecount++;
91 }
92
93 bool
94 ppm::start_frame(synfig::ProgressCallback *callback)
95 {
96         int w=desc.get_w(),h=desc.get_h();
97
98         if(filename=="-")
99         {
100                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
101                 file=SmartFILE(stdout);
102         }
103         else if(multi_image)
104         {
105                 String
106                         newfilename(filename),
107                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
108                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
109
110                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
111                 file=SmartFILE(fopen(newfilename.c_str(),"wb"));
112                 if(callback)callback->task(newfilename);
113         }
114         else
115         {
116                 file=SmartFILE(fopen(filename.c_str(),"wb"));
117                 if(callback)callback->task(filename);
118         }
119
120         if(!file)
121                 return false;
122
123         fprintf(file.get(), "P6\n");
124         fprintf(file.get(), "%d %d\n", w, h);
125         fprintf(file.get(), "%d\n", 255);
126
127         delete [] buffer;
128         buffer=new unsigned char[3*w];
129
130         delete [] color_buffer;
131         color_buffer=new Color[desc.get_w()];
132
133         return true;
134 }
135
136 Color *
137 ppm::start_scanline(int scanline)
138 {
139         return color_buffer;
140 }
141
142 bool
143 ppm::end_scanline()
144 {
145         if(!file)
146                 return false;
147
148         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
149
150         if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
151                 return false;
152
153         return true;
154 }