Enable $Id$ expansion.
[synfig.git] / synfig-core / trunk / src / modules / mod_openexr / trgt_openexr.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file trgt_openexr.cpp
3 **      \brief exr_trgt 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_openexr.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(exr_trgt);
51 SYNFIG_TARGET_SET_NAME(exr_trgt,"openexr");
52 SYNFIG_TARGET_SET_EXT(exr_trgt,"exr");
53 SYNFIG_TARGET_SET_VERSION(exr_trgt,"1.0.4");
54 SYNFIG_TARGET_SET_CVS_ID(exr_trgt,"$Id$");
55
56 /* === M E T H O D S ======================================================= */
57
58 bool
59 exr_trgt::ready()
60 {
61         return (bool)exr_file;
62 }
63
64 exr_trgt::exr_trgt(const char *Filename):
65         multi_image(false),
66         imagecount(0),
67         filename(Filename),
68         exr_file(0)
69 {
70         buffer=0;
71 #ifndef USE_HALF_TYPE
72         buffer_color=0;
73 #endif
74
75         // OpenEXR uses linear gamma
76         gamma().set_gamma(1.0);
77 }
78
79 exr_trgt::~exr_trgt()
80 {
81         if(exr_file)
82                 delete exr_file;
83
84         if(buffer) delete [] buffer;
85 #ifndef USE_HALF_TYPE
86         if(buffer_color) delete [] buffer_color;
87 #endif
88 }
89
90 bool
91 exr_trgt::set_rend_desc(RendDesc *given_desc)
92 {
93         //given_desc->set_pixel_format(PixelFormat((int)PF_RAW_COLOR));
94         desc=*given_desc;
95         imagecount=desc.get_frame_start();
96         if(desc.get_frame_end()-desc.get_frame_start()>0)
97                 multi_image=true;
98         else
99                 multi_image=false;
100         return true;
101 }
102
103 bool
104 exr_trgt::start_frame(synfig::ProgressCallback *cb)
105 {
106         int w=desc.get_w(),h=desc.get_h();
107
108         String frame_name;
109
110         if(exr_file)
111                 delete exr_file;
112         if(multi_image)
113         {
114                 String
115                         newfilename(filename),
116                         ext(find(filename.begin(),filename.end(),'.'),filename.end());
117                 newfilename.erase(find(newfilename.begin(),newfilename.end(),'.'),newfilename.end());
118
119                 newfilename+=etl::strprintf("%04d",imagecount)+ext;
120                 frame_name=newfilename;
121                 if(cb)cb->task(newfilename);
122         }
123         else
124         {
125                 frame_name=filename;
126                 if(cb)cb->task(filename);
127         }
128         exr_file=new Imf::RgbaOutputFile(frame_name.c_str(),w,h,Imf::WRITE_RGBA,desc.get_pixel_aspect());
129 #ifndef USE_HALF_TYPE
130         if(buffer_color) delete [] buffer_color;
131         buffer_color=new Color[w];
132 #endif
133         //if(buffer) delete [] buffer;
134         //buffer=new Imf::Rgba[w];
135         out_surface.set_wh(w,h);
136
137         return true;
138 }
139
140 void
141 exr_trgt::end_frame()
142 {
143         if(exr_file)
144         {
145                 exr_file->setFrameBuffer(out_surface[0],1,desc.get_w());
146                 exr_file->writePixels(desc.get_h());
147
148                 delete exr_file;
149         }
150
151         exr_file=0;
152
153         imagecount++;
154 }
155
156 Color *
157 exr_trgt::start_scanline(int i)
158 {
159         scanline=i;
160 #ifndef USE_HALF_TYPE
161         return reinterpret_cast<Color *>(buffer_color);
162 #else
163         return reinterpret_cast<Color *>(out_surface[scanline]);
164 //      return reinterpret_cast<unsigned char *>(buffer);
165 #endif
166 }
167
168 bool
169 exr_trgt::end_scanline()
170 {
171         if(!ready())
172                 return false;
173
174 #ifndef USE_HALF_TYPE
175         int i;
176         for(i=0;i<desc.get_w();i++)
177         {
178 //              Imf::Rgba &rgba=buffer[i];
179                 Imf::Rgba &rgba=out_surface[scanline][i];
180                 Color &color=buffer_color[i];
181                 rgba.r=color.get_r();
182                 rgba.g=color.get_g();
183                 rgba.b=color.get_b();
184                 rgba.a=color.get_a();
185         }
186 #endif
187
188     //exr_file->setFrameBuffer(buffer,1,desc.get_w());
189         //exr_file->writePixels(1);
190
191         return true;
192 }