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