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 / mptr_openexr.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: mptr_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 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "mptr_openexr.h"
32 #include <synfig/synfig.h>
33 #include <ETL/stringf>
34 #include <cstdio>
35 #include <algorithm>
36 #include <functional>
37
38 #include <ImfArray.h>
39 #include <ImfRgbaFile.h>
40 #include <exception>
41
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_IMPORTER_INIT(exr_mptr);
53 SYNFIG_IMPORTER_SET_NAME(exr_mptr,"exr_mptr");
54 SYNFIG_IMPORTER_SET_EXT(exr_mptr,"exr");
55 SYNFIG_IMPORTER_SET_VERSION(exr_mptr,"0.1");
56 SYNFIG_IMPORTER_SET_CVS_ID(exr_mptr,"$Id: mptr_openexr.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
57
58 /* === M E T H O D S ======================================================= */
59
60 exr_mptr::exr_mptr(const char *file)
61 {
62         filename=file;
63 }
64
65 exr_mptr::~exr_mptr()
66 {
67 }
68
69 bool
70 exr_mptr::get_frame(synfig::Surface &out_surface,Time, synfig::ProgressCallback *cb)
71 {
72     try
73     {
74
75         Imf::RgbaInputFile in(filename.c_str());
76
77     int w = in.dataWindow().max.x - in.dataWindow().min.x + 1;
78     int h = in.dataWindow().max.y - in.dataWindow().min.y + 1;
79     //int dx = in.dataWindow().min.x;
80     //int dy = in.dataWindow().min.y;
81
82 #ifdef USE_HALF_TYPE
83     out_surface.set_wh(w,h);
84         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(out_surface[0]), 1, w);
85
86         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
87
88 #else
89         etl::surface<Imf::Rgba> in_surface;
90         in_surface.set_wh(w,h);
91         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(in_surface[0]), 1, w);
92
93         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
94         
95         int x;
96         int y;
97         out_surface.set_wh(w,h);
98         for(y=0;y<out_surface.get_h();y++)
99                 for(x=0;x<out_surface.get_w();x++)
100                 {
101                         Color &color(out_surface[y][x]);
102                         Imf::Rgba &rgba(in_surface[y][x]);
103                         color.set_r(rgba.r);
104                         color.set_g(rgba.g);
105                         color.set_b(rgba.b);
106                         color.set_a(rgba.a);
107                 }
108 #endif
109         }
110         catch (const std::exception &e)
111     {
112                 if(cb)cb->error(e.what());
113                 else synfig::error(e.what());
114                 return false;
115     }
116
117         return true;            
118 }