Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / 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 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "trgt_ppm.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, const synfig::TargetParam& /* params */)
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 newfilename(filename_sans_extension(filename) +
105                                                    etl::strprintf(".%04d",imagecount) +
106                                                    filename_extension(filename));
107                 file=SmartFILE(fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE));
108                 if(callback)callback->task(newfilename);
109         }
110         else
111         {
112                 file=SmartFILE(fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE));
113                 if(callback)callback->task(filename);
114         }
115
116         if(!file)
117                 return false;
118
119         fprintf(file.get(), "P6\n");
120         fprintf(file.get(), "%d %d\n", w, h);
121         fprintf(file.get(), "%d\n", 255);
122
123         delete [] buffer;
124         buffer=new unsigned char[3*w];
125
126         delete [] color_buffer;
127         color_buffer=new Color[desc.get_w()];
128
129         return true;
130 }
131
132 Color *
133 ppm::start_scanline(int /*scanline*/)
134 {
135         return color_buffer;
136 }
137
138 bool
139 ppm::end_scanline()
140 {
141         if(!file)
142                 return false;
143
144         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
145
146         if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
147                 return false;
148
149         return true;
150 }