Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_ppm / trgt_ppm.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: trgt_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
5 **
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #define SYNFIG_TARGET
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "trgt_ppm.h"
34 #include <synfig/synfig.h>
35 #include <ETL/stringf>
36 #include <cstdio>
37 #include <algorithm>
38 #include <functional>
39 #endif
40
41 /* === M A C R O S ========================================================= */
42
43 using namespace synfig;
44 using namespace std;
45 using namespace etl;
46
47 /* === G L O B A L S ======================================================= */
48
49 SYNFIG_TARGET_INIT(ppm);
50 SYNFIG_TARGET_SET_NAME(ppm,"ppm");
51 SYNFIG_TARGET_SET_EXT(ppm,"ppm");
52 SYNFIG_TARGET_SET_VERSION(ppm,"0.1");
53 SYNFIG_TARGET_SET_CVS_ID(ppm,"$Id: trgt_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
54
55 /* === M E T H O D S ======================================================= */
56
57 ppm::ppm(const char *Filename)
58 {
59         filename=Filename;
60         multi_image=false;
61         buffer=NULL;
62         color_buffer=0;
63         set_remove_alpha();
64 }
65
66 ppm::~ppm()
67 {
68         delete [] buffer;
69         delete [] color_buffer;
70 }
71
72 bool
73 ppm::set_rend_desc(RendDesc *given_desc)
74 {
75         //given_desc->set_pixel_format(PF_RGB);
76         desc=*given_desc;
77         imagecount=desc.get_frame_start();
78         if(desc.get_frame_end()-desc.get_frame_start()>0)
79                 multi_image=true;
80         else
81                 multi_image=false;
82         return true;
83 }
84
85 void
86 ppm::end_frame()
87 {
88         imagecount++;
89 }
90
91 bool
92 ppm::start_frame(synfig::ProgressCallback *callback)
93 {
94         int w=desc.get_w(),h=desc.get_h();
95         
96         if(filename=="-")
97         {
98                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
99                 file=SmartFILE(stdout);
100         }
101         else if(multi_image)
102         {
103                 String
104                         newfilename(filename),
105                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
106                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
107                 
108                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
109                 file=SmartFILE(fopen(newfilename.c_str(),"wb"));
110                 if(callback)callback->task(newfilename);
111         }
112         else
113         {
114                 file=SmartFILE(fopen(filename.c_str(),"wb"));
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 }