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