Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_ppm / trgt_ppm.cpp
1 /*! ========================================================================
2 ** Sinfg
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 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #define SINFG_TARGET
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "trgt_ppm.h"
33 #include <sinfg/sinfg.h>
34 #include <ETL/stringf>
35 #include <cstdio>
36 #include <algorithm>
37 #include <functional>
38 #endif
39
40 /* === M A C R O S ========================================================= */
41
42 using namespace sinfg;
43 using namespace std;
44 using namespace etl;
45
46 /* === G L O B A L S ======================================================= */
47
48 SINFG_TARGET_INIT(ppm);
49 SINFG_TARGET_SET_NAME(ppm,"ppm");
50 SINFG_TARGET_SET_EXT(ppm,"ppm");
51 SINFG_TARGET_SET_VERSION(ppm,"0.1");
52 SINFG_TARGET_SET_CVS_ID(ppm,"$Id: trgt_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
53
54 /* === M E T H O D S ======================================================= */
55
56 ppm::ppm(const char *Filename)
57 {
58         filename=Filename;
59         multi_image=false;
60         buffer=NULL;
61         color_buffer=0;
62         set_remove_alpha();
63 }
64
65 ppm::~ppm()
66 {
67         delete [] buffer;
68         delete [] color_buffer;
69 }
70
71 bool
72 ppm::set_rend_desc(RendDesc *given_desc)
73 {
74         //given_desc->set_pixel_format(PF_RGB);
75         desc=*given_desc;
76         imagecount=desc.get_frame_start();
77         if(desc.get_frame_end()-desc.get_frame_start()>0)
78                 multi_image=true;
79         else
80                 multi_image=false;
81         return true;
82 }
83
84 void
85 ppm::end_frame()
86 {
87         imagecount++;
88 }
89
90 bool
91 ppm::start_frame(sinfg::ProgressCallback *callback)
92 {
93         int w=desc.get_w(),h=desc.get_h();
94         
95         if(filename=="-")
96         {
97                 if(callback)callback->task(strprintf("(stdout) %d",imagecount).c_str());
98                 file=SmartFILE(stdout);
99         }
100         else if(multi_image)
101         {
102                 String
103                         newfilename(filename),
104                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
105                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
106                 
107                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
108                 file=SmartFILE(fopen(newfilename.c_str(),"wb"));
109                 if(callback)callback->task(newfilename);
110         }
111         else
112         {
113                 file=SmartFILE(fopen(filename.c_str(),"wb"));
114                 if(callback)callback->task(filename);
115         }
116         
117         if(!file)
118                 return false;
119         
120         fprintf(file.get(), "P6\n");
121         fprintf(file.get(), "%d %d\n", w, h);
122         fprintf(file.get(), "%d\n", 255);       
123         
124         delete [] buffer;
125         buffer=new unsigned char[3*w];
126
127         delete [] color_buffer;
128         color_buffer=new Color[desc.get_w()];
129         
130         return true;
131 }
132
133 Color *
134 ppm::start_scanline(int scanline)
135 {
136         return color_buffer;
137 }
138
139 bool
140 ppm::end_scanline()
141 {
142         if(!file)
143                 return false;
144
145         convert_color_format(buffer, color_buffer, desc.get_w(), PF_RGB, gamma());
146                         
147         if(!fwrite(buffer,1,desc.get_w()*3,file.get()))
148                 return false;
149
150         return true;
151 }