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