Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_openexr / mptr_openexr.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_openexr.cpp
3 **      \brief ppm Target Module
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
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 "mptr_openexr.h"
35 #include <synfig/general.h>
36 #include <synfig/surface.h>
37 #include <ETL/stringf>
38 #include <cstdio>
39 #include <algorithm>
40 #include <functional>
41
42 #include <ImfArray.h>
43 #include <ImfRgbaFile.h>
44 #include <exception>
45
46 #endif
47
48 /* === M A C R O S ========================================================= */
49
50 using namespace synfig;
51 using namespace std;
52 using namespace etl;
53
54 /* === G L O B A L S ======================================================= */
55
56 SYNFIG_IMPORTER_INIT(exr_mptr);
57 SYNFIG_IMPORTER_SET_NAME(exr_mptr,"openexr");
58 SYNFIG_IMPORTER_SET_EXT(exr_mptr,"exr");
59 SYNFIG_IMPORTER_SET_VERSION(exr_mptr,"0.1");
60 SYNFIG_IMPORTER_SET_CVS_ID(exr_mptr,"$Id$");
61
62 /* === M E T H O D S ======================================================= */
63
64 exr_mptr::exr_mptr(const char *file)
65 {
66         filename=file;
67 }
68
69 exr_mptr::~exr_mptr()
70 {
71 }
72
73 bool
74 exr_mptr::get_frame(synfig::Surface &out_surface,Time, synfig::ProgressCallback *cb)
75 {
76     try
77     {
78
79         Imf::RgbaInputFile in(filename.c_str());
80
81     int w = in.dataWindow().max.x - in.dataWindow().min.x + 1;
82     int h = in.dataWindow().max.y - in.dataWindow().min.y + 1;
83     //int dx = in.dataWindow().min.x;
84     //int dy = in.dataWindow().min.y;
85
86 #ifdef USE_HALF_TYPE
87     out_surface.set_wh(w,h);
88         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(out_surface[0]), 1, w);
89
90         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
91
92 #else
93         etl::surface<Imf::Rgba> in_surface;
94         in_surface.set_wh(w,h);
95         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(in_surface[0]), 1, w);
96
97         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
98
99         int x;
100         int y;
101         out_surface.set_wh(w,h);
102         for(y=0;y<out_surface.get_h();y++)
103                 for(x=0;x<out_surface.get_w();x++)
104                 {
105                         Color &color(out_surface[y][x]);
106                         Imf::Rgba &rgba(in_surface[y][x]);
107                         color.set_r(rgba.r);
108                         color.set_g(rgba.g);
109                         color.set_b(rgba.b);
110                         color.set_a(rgba.a);
111                 }
112 #endif
113         }
114         catch (const std::exception &e)
115     {
116                 if(cb)cb->error(e.what());
117                 else synfig::error(e.what());
118                 return false;
119     }
120
121         return true;
122 }