moreupdates
[synfig.git] / synfig-core / trunk / 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 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include "mptr_openexr.h"
31 #include <synfig/synfig.h>
32 #include <ETL/stringf>
33 #include <cstdio>
34 #include <algorithm>
35 #include <functional>
36
37 #include <ImfArray.h>
38 #include <ImfRgbaFile.h>
39 #include <exception>
40
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SYNFIG_IMPORTER_INIT(exr_mptr);
52 SYNFIG_IMPORTER_SET_NAME(exr_mptr,"exr_mptr");
53 SYNFIG_IMPORTER_SET_EXT(exr_mptr,"exr");
54 SYNFIG_IMPORTER_SET_VERSION(exr_mptr,"0.1");
55 SYNFIG_IMPORTER_SET_CVS_ID(exr_mptr,"$Id: mptr_openexr.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
56
57 /* === M E T H O D S ======================================================= */
58
59 exr_mptr::exr_mptr(const char *file)
60 {
61         filename=file;
62 }
63
64 exr_mptr::~exr_mptr()
65 {
66 }
67
68 bool
69 exr_mptr::get_frame(synfig::Surface &out_surface,Time, synfig::ProgressCallback *cb)
70 {
71     try
72     {
73
74         Imf::RgbaInputFile in(filename.c_str());
75
76     int w = in.dataWindow().max.x - in.dataWindow().min.x + 1;
77     int h = in.dataWindow().max.y - in.dataWindow().min.y + 1;
78     //int dx = in.dataWindow().min.x;
79     //int dy = in.dataWindow().min.y;
80
81 #ifdef USE_HALF_TYPE
82     out_surface.set_wh(w,h);
83         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(out_surface[0]), 1, w);
84
85         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
86
87 #else
88         etl::surface<Imf::Rgba> in_surface;
89         in_surface.set_wh(w,h);
90         in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(in_surface[0]), 1, w);
91
92         in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);
93         
94         int x;
95         int y;
96         out_surface.set_wh(w,h);
97         for(y=0;y<out_surface.get_h();y++)
98                 for(x=0;x<out_surface.get_w();x++)
99                 {
100                         Color &color(out_surface[y][x]);
101                         Imf::Rgba &rgba(in_surface[y][x]);
102                         color.set_r(rgba.r);
103                         color.set_g(rgba.g);
104                         color.set_b(rgba.b);
105                         color.set_a(rgba.a);
106                 }
107 #endif
108         }
109         catch (const std::exception &e)
110     {
111                 if(cb)cb->error(e.what());
112                 else synfig::error(e.what());
113                 return false;
114     }
115
116         return true;            
117 }