Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / 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
107                         newfilename(filename),
108                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
109                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
110
111                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
112                 file=SmartFILE(fopen(newfilename.c_str(),"wb"));
113                 if(callback)callback->task(newfilename);
114         }
115         else
116         {
117                 file=SmartFILE(fopen(filename.c_str(),"wb"));
118                 if(callback)callback->task(filename);
119         }
120
121         if(!file)
122                 return false;
123
124         fprintf(file.get(), "P6\n");
125         fprintf(file.get(), "%d %d\n", w, h);
126         fprintf(file.get(), "%d\n", 255);
127
128         delete [] buffer;
129         buffer=new unsigned char[3*w];
130
131         delete [] color_buffer;
132         color_buffer=new Color[desc.get_w()];
133
134         return true;
135 }
136
137 Color *
138 ppm::start_scanline(int /*scanline*/)
139 {
140         return color_buffer;
141 }
142
143 bool
144 ppm::end_scanline()
145 {
146         if(!file)
147                 return false;
148
149         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
150
151         if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
152                 return false;
153
154         return true;
155 }