File: 2dimgexr.cc

package info (click to toggle)
mia 2.4.7-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,332 kB
  • sloc: cpp: 162,690; python: 1,317; sh: 321; xml: 127; makefile: 41; csh: 24; ansic: 9
file content (231 lines) | stat: -rw-r--r-- 7,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* -*- mia-c++  -*-
 *
 * This file is part of MIA - a toolbox for medical image analysis
 * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
 *
 * MIA is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MIA; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sstream>

#include <ImfOutputFile.h>
#include <ImfInputFile.h>
#include <ImfChannelList.h>
#include <ImfStringAttribute.h>
#include <ImfMatrixAttribute.h>
#include <ImathBox.h>
#include <ImfFrameBuffer.h>
#include <ImfHeader.h>

#include <mia/core/file.hh>
#include <mia/core/filter.hh>
#include <mia/core/msgstream.hh>
#include <mia/2d/imageio.hh>


NS_BEGIN(IMAGEIO_2D_EXR)

NS_MIA_USE;

using namespace std;
using namespace boost;
using namespace Imf;
using namespace Imath;

class CEXR2DImageIOPlugin : public C2DImageIOPlugin
{
public:
       CEXR2DImageIOPlugin();
private:
       PData do_load(const string& fname) const;
       bool do_save(const string& fname, const Data& data) const;
       const string do_get_descr() const;
};

CEXR2DImageIOPlugin::CEXR2DImageIOPlugin():
       C2DImageIOPlugin("exr")
{
       add_supported_type(it_float);
       add_supported_type(it_uint);
       add_suffix(".exr");
       add_suffix(".EXR");
}

CEXR2DImageIOPlugin::PData  CEXR2DImageIOPlugin::do_load(const string& filename) const
{
       try {
              InputFile file (filename.c_str());
              Box2i dw = file.header().dataWindow();
              C2DBounds size(dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1);
              int dx = dw.min.x;
              int dy = dw.min.y;
              cvdebug() << "EXR:Get image of size " << size.x << ", " << size.y << "\n";
              const ChannelList& channels = file.header().channels();
              FrameBuffer frameBuffer;
              std::shared_ptr<C2DImageVector > result(new C2DImageVector);

              for (ChannelList::ConstIterator i = channels.begin(); i != channels.end(); ++i) {
                     const Channel& channel = i.channel();
                     cvdebug() << "channel '" << i.name() << "' of type " << channel.type << "\n";

                     switch (channel.type) {
                     case UINT: {
                            std::shared_ptr<C2DUIImage > img(new C2DUIImage(size));
                            frameBuffer.insert ("Y", Slice(UINT, (char *)(&(*img)(0, 0) - (dx + size.x * dy)),
                                                           4, 4 * size.x, 1, 1, 0.0));
                            result->push_back(img);
                     }
                     break;

                     case FLOAT: {
                            std::shared_ptr<C2DFImage > img(new C2DFImage(size));
                            frameBuffer.insert ("Y", Slice(FLOAT, (char *)(&(*img)(0, 0) - (dx + size.x * dy)),
                                                           4, 4 * size.x, 1, 1, 0.0));
                            result->push_back(img);
                     }
                     break;

                     default:
                            throw invalid_argument("EXRImageIO::load: only FLOAT and UINT supported");
                     };
              }

              file.setFrameBuffer (frameBuffer);
              file.readPixels (dw.min.y, dw.max.y);
              return result;
       } catch (const std::exception& x) {
              // should add an debug message
              cvwarn() << "OpenXER: failed reading image from '" << filename << "':" << x.what() << "\n";
       }

       return CEXR2DImageIOPlugin::PData();
}


struct CImageSaver {
       typedef bool result_type;

       CImageSaver(const string& filename):
              m_fname(filename)
       {
       }

       template <typename T>
       result_type operator ()(const T2DImage<T>& image) const;
private:
       bool write_header(int bpp, int compression, const C2DBounds& size);

       string m_fname;
};

template <typename T>
struct pixel_trait_exr {
       enum {pixel_size = 0 };
       enum { supported = 0 };
};

template <>
struct pixel_trait_exr<float> {
       enum {pixel_size = 4 };
       enum { supported = 1 };
       static PixelType pt()
       {
              return Imf::FLOAT;
       }
};

template <>
struct pixel_trait_exr<unsigned int> {
       static PixelType pt()
       {
              return Imf::UINT;
       }
       enum {pixel_size = 4 };
       enum { supported = 1 };
};

template <typename T, int supported>
struct image_writer_exr {
       static bool apply (const T2DImage<T>& /*image*/, const string& /*fname*/)
       {
              stringstream msg;
              msg << "Pixel type '" << CPixelTypeDict.get_name((EPixelType)pixel_type<T>::value) << "' not supported by 'exr' file format";
              throw runtime_error(msg.str());
       }
};

template <typename T>
struct image_writer_exr<T, 1>  {
       static bool apply (const T2DImage<T>& image, const string& fname)
       {
              cvdebug() << "image_writer<T, 1>::apply image size (" <<
                        image.get_size().x << ", " << image.get_size().y << ")\n";

              try {
                     Header header (image.get_size().x, image.get_size().y);
                     header.channels().insert ("Y", Channel (pixel_trait_exr<T>::pt()));
                     OutputFile file (fname.c_str(), header);
                     FrameBuffer frameBuffer;
                     frameBuffer.insert ("Y",                    // name
                                         Slice (pixel_trait_exr<T>::pt(),               // type
                                                (char *) &image(0, 0), // base
                                                pixel_trait_exr<T>::pixel_size,           // xStride
                                                pixel_trait_exr<T>::pixel_size * image.get_size().x));     // yStride
                     file.setFrameBuffer (frameBuffer);
                     file.writePixels (image.get_size().y);
                     return true;
              } catch (...) {
                     return false;
              }
       }
};

template <typename T>
CImageSaver::result_type
CImageSaver::operator()(const T2DImage<T>& image) const
{
       return image_writer_exr<T, pixel_trait_exr<T>::supported>::apply(image, m_fname);
}



bool CEXR2DImageIOPlugin::do_save(const string& fname, const Data& data) const
{
       cvdebug() << "CEXR2DImageIO::save begin\n";

       if (data.size() != 1)
              throw runtime_error("CEXR2DImageIO::save: only single images can be saved");

       CImageSaver saver(fname);

       for (C2DImageVector::const_iterator iimg = data.begin(); iimg != data.end(); ++iimg)
              filter(saver, **iimg);

       cvdebug() << "CEXR2DImageIO::save end\n";
       return true;
}

const string CEXR2DImageIOPlugin::do_get_descr() const
{
       return "a 2dimage io plugin for OpenEXR images";
}

extern "C" EXPORT  CPluginBase *get_plugin_interface()
{
       return new CEXR2DImageIOPlugin();
}

NS_END