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