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 
ppm(const char * Filename,const synfig::TargetParam & params)58 ppm::ppm(const char *Filename, const synfig::TargetParam &params):
59 	imagecount(),
60 	multi_image(false),
61 	file(),
62 	filename(Filename),
63 	color_buffer(NULL),
64 	buffer(NULL),
65 	sequence_separator(params.sequence_separator)
66 {
67 	set_alpha_mode(TARGET_ALPHA_MODE_FILL);
68 }
69 
~ppm()70 ppm::~ppm()
71 {
72 	delete [] buffer;
73 	delete [] color_buffer;
74 }
75 
76 bool
set_rend_desc(RendDesc * given_desc)77 ppm::set_rend_desc(RendDesc *given_desc)
78 {
79 	//given_desc->set_pixel_format(PF_RGB);
80 	desc=*given_desc;
81 	imagecount=desc.get_frame_start();
82 	if(desc.get_frame_end()-desc.get_frame_start()>0)
83 		multi_image=true;
84 	else
85 		multi_image=false;
86 	return true;
87 }
88 
89 void
end_frame()90 ppm::end_frame()
91 {
92 	imagecount++;
93 }
94 
95 bool
start_frame(synfig::ProgressCallback * callback)96 ppm::start_frame(synfig::ProgressCallback *callback)
97 {
98 	int w=desc.get_w(),h=desc.get_h();
99 
100 	if(filename=="-")
101 	{
102 		if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
103 		file=SmartFILE(stdout);
104 	}
105 	else if(multi_image)
106 	{
107 		String newfilename(filename_sans_extension(filename) +
108 						   sequence_separator +
109 						   etl::strprintf("%04d",imagecount) +
110 						   filename_extension(filename));
111 		file=SmartFILE(fopen(newfilename.c_str(),POPEN_BINARY_WRITE_TYPE));
112 		if(callback)callback->task(newfilename);
113 	}
114 	else
115 	{
116 		file=SmartFILE(fopen(filename.c_str(),POPEN_BINARY_WRITE_TYPE));
117 		if(callback)callback->task(filename);
118 	}
119 
120 	if(!file)
121 		return false;
122 
123 	fprintf(file.get(), "P6\n");
124 	fprintf(file.get(), "%d %d\n", w, h);
125 	fprintf(file.get(), "%d\n", 255);
126 
127 	delete [] buffer;
128 	buffer=new unsigned char[3*w];
129 
130 	delete [] color_buffer;
131 	color_buffer=new Color[desc.get_w()];
132 
133 	return true;
134 }
135 
136 Color *
start_scanline(int)137 ppm::start_scanline(int /*scanline*/)
138 {
139 	return color_buffer;
140 }
141 
142 bool
end_scanline()143 ppm::end_scanline()
144 {
145 	if(!file)
146 		return false;
147 
148 	convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
149 
150 	if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
151 		return false;
152 
153 	return true;
154 }
155