Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / 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 **      $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 #define SYNFIG_TARGET
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "trgt_openexr.h"
38 #include <ETL/stringf>
39 #include <cstdio>
40 #include <algorithm>
41 #include <functional>
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_TARGET_INIT(exr_trgt);
53 SYNFIG_TARGET_SET_NAME(exr_trgt,"openexr");
54 SYNFIG_TARGET_SET_EXT(exr_trgt,"exr");
55 SYNFIG_TARGET_SET_VERSION(exr_trgt,"1.0.4");
56 SYNFIG_TARGET_SET_CVS_ID(exr_trgt,"$Id$");
57
58 /* === M E T H O D S ======================================================= */
59
60 bool
61 exr_trgt::ready()
62 {
63         return (bool)exr_file;
64 }
65
66 exr_trgt::exr_trgt(const char *Filename):
67         multi_image(false),
68         imagecount(0),
69         filename(Filename),
70         exr_file(0)
71 {
72         buffer=0;
73 #ifndef USE_HALF_TYPE
74         buffer_color=0;
75 #endif
76
77         // OpenEXR uses linear gamma
78         gamma().set_gamma(1.0);
79 }
80
81 exr_trgt::~exr_trgt()
82 {
83         if(exr_file)
84                 delete exr_file;
85
86         if(buffer) delete [] buffer;
87 #ifndef USE_HALF_TYPE
88         if(buffer_color) delete [] buffer_color;
89 #endif
90 }
91
92 bool
93 exr_trgt::set_rend_desc(RendDesc *given_desc)
94 {
95         //given_desc->set_pixel_format(PixelFormat((int)PF_RAW_COLOR));
96         desc=*given_desc;
97         imagecount=desc.get_frame_start();
98         if(desc.get_frame_end()-desc.get_frame_start()>0)
99                 multi_image=true;
100         else
101                 multi_image=false;
102         return true;
103 }
104
105 bool
106 exr_trgt::start_frame(synfig::ProgressCallback *cb)
107 {
108         int w=desc.get_w(),h=desc.get_h();
109
110         String frame_name;
111
112         if(exr_file)
113                 delete exr_file;
114         if(multi_image)
115         {
116                 frame_name = (filename_sans_extension(filename) +
117                                           etl::strprintf(".%04d",imagecount) +
118                                           filename_extension(filename));
119                 if(cb)cb->task(frame_name);
120         }
121         else
122         {
123                 frame_name=filename;
124                 if(cb)cb->task(filename);
125         }
126         exr_file=new Imf::RgbaOutputFile(frame_name.c_str(),w,h,Imf::WRITE_RGBA,desc.get_pixel_aspect());
127 #ifndef USE_HALF_TYPE
128         if(buffer_color) delete [] buffer_color;
129         buffer_color=new Color[w];
130 #endif
131         //if(buffer) delete [] buffer;
132         //buffer=new Imf::Rgba[w];
133         out_surface.set_wh(w,h);
134
135         return true;
136 }
137
138 void
139 exr_trgt::end_frame()
140 {
141         if(exr_file)
142         {
143                 exr_file->setFrameBuffer(out_surface[0],1,desc.get_w());
144                 exr_file->writePixels(desc.get_h());
145
146                 delete exr_file;
147         }
148
149         exr_file=0;
150
151         imagecount++;
152 }
153
154 Color *
155 exr_trgt::start_scanline(int i)
156 {
157         scanline=i;
158 #ifndef USE_HALF_TYPE
159         return reinterpret_cast<Color *>(buffer_color);
160 #else
161         return reinterpret_cast<Color *>(out_surface[scanline]);
162 //      return reinterpret_cast<unsigned char *>(buffer);
163 #endif
164 }
165
166 bool
167 exr_trgt::end_scanline()
168 {
169         if(!ready())
170                 return false;
171
172 #ifndef USE_HALF_TYPE
173         int i;
174         for(i=0;i<desc.get_w();i++)
175         {
176 //              Imf::Rgba &rgba=buffer[i];
177                 Imf::Rgba &rgba=out_surface[scanline][i];
178                 Color &color=buffer_color[i];
179                 rgba.r=color.get_r();
180                 rgba.g=color.get_g();
181                 rgba.b=color.get_b();
182                 rgba.a=color.get_a();
183         }
184 #endif
185
186     //exr_file->setFrameBuffer(buffer,1,desc.get_w());
187         //exr_file->writePixels(1);
188
189         return true;
190 }