Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / 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 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "trgt_ppm.h"
38 #include <ETL/stringf>
39 #include <cstdio>
40 #include <algorithm>
41 #include <functional>
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_TARGET_INIT(ppm);
53 SYNFIG_TARGET_SET_NAME(ppm,"ppm");
54 SYNFIG_TARGET_SET_EXT(ppm,"ppm");
55 SYNFIG_TARGET_SET_VERSION(ppm,"0.1");
56 SYNFIG_TARGET_SET_CVS_ID(ppm,"$Id$");
57
58 /* === M E T H O D S ======================================================= */
59
60 ppm::ppm(const char *Filename)
61 {
62         filename=Filename;
63         multi_image=false;
64         buffer=NULL;
65         color_buffer=0;
66         set_remove_alpha();
67 }
68
69 ppm::~ppm()
70 {
71         delete [] buffer;
72         delete [] color_buffer;
73 }
74
75 bool
76 ppm::set_rend_desc(RendDesc *given_desc)
77 {
78         //given_desc->set_pixel_format(PF_RGB);
79         desc=*given_desc;
80         imagecount=desc.get_frame_start();
81         if(desc.get_frame_end()-desc.get_frame_start()>0)
82                 multi_image=true;
83         else
84                 multi_image=false;
85         return true;
86 }
87
88 void
89 ppm::end_frame()
90 {
91         imagecount++;
92 }
93
94 bool
95 ppm::start_frame(synfig::ProgressCallback *callback)
96 {
97         int w=desc.get_w(),h=desc.get_h();
98
99         if(filename=="-")
100         {
101                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
102                 file=SmartFILE(stdout);
103         }
104         else if(multi_image)
105         {
106                 String newfilename(filename_sans_extension(filename) +
107                                                    etl::strprintf(".%04d",imagecount) +
108                                                    filename_extension(filename));
109                 file=SmartFILE(fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE));
110                 if(callback)callback->task(newfilename);
111         }
112         else
113         {
114                 file=SmartFILE(fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE));
115                 if(callback)callback->task(filename);
116         }
117
118         if(!file)
119                 return false;
120
121         fprintf(file.get(), "P6\n");
122         fprintf(file.get(), "%d %d\n", w, h);
123         fprintf(file.get(), "%d\n", 255);
124
125         delete [] buffer;
126         buffer=new unsigned char[3*w];
127
128         delete [] color_buffer;
129         color_buffer=new Color[desc.get_w()];
130
131         return true;
132 }
133
134 Color *
135 ppm::start_scanline(int /*scanline*/)
136 {
137         return color_buffer;
138 }
139
140 bool
141 ppm::end_scanline()
142 {
143         if(!file)
144                 return false;
145
146         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
147
148         if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
149                 return false;
150
151         return true;
152 }