Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
openexrio.h
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <map>
6 #include <ImfIO.h>
7 #include <ImfOutputFile.h>
8 #include <ImfInputFile.h>
9 #include <ImfChannelList.h>
10 #include <ImfMatrixAttribute.h>
11 
12 #include "../iucore.h"
13 
14 #ifdef IUIO_EIGEN3
15  #include <eigen3/Eigen/Dense>
16 #endif
17 
18 namespace iu {
19 
20 
35 {
36 public:
41  OpenEXRInputFile(const std::string& filename);
43 
47  struct Channel
48  {
49  Channel(const std::string& name, const std::string& type) {
50  name_ = name;
51  type_ = type;
52  }
53  std::string name_;
54  std::string type_;
55  };
56 
61  iu::Size<2> get_size() { return sz_; }
62 
67  std::vector<Channel> get_channels() { return channels_; }
68 
73  std::vector<std::string> get_attributes();
74 
81  void read_channel(const std::string& name, ImageCpu_32u_C1 &img);
82 
89  void read_channel(const std::string& name, ImageCpu_32f_C1 &img);
90 
97  void read_channel_32f(const std::string& name, ImageCpu_32f_C1 &img);
98 
104  void read_channel(const std::string& name, ImageGpu_32u_C1 &img);
105 
111  void read_channel(const std::string& name, ImageGpu_32f_C1 &img);
112 
119  void read_channel_32f(const std::string& name, ImageGpu_32f_C1 &img);
120 
121 
122  #ifdef IUIO_EIGEN3
123 
128  void read_attribute(const std::string& name, Eigen::Ref<Eigen::Matrix3f> mat);
129 
135  void read_attribute(const std::string& name, Eigen::Ref<Eigen::Matrix4f> mat);
136  #endif
137 
138 private:
139  iu::Size<2> sz_;
140  std::string filename_;
141  std::vector<Channel> channels_;
142  std::map<Imf::PixelType, std::string> pixeltype_to_string_;
143 };
144 
145 
154 {
155 public:
161  OpenEXROutputFile(const std::string& filename, iu::Size<2> size);
163 
164 
171  void add_channel(const std::string& name, iu::ImageCpu_8u_C1& img);
172 
178  void add_channel(const std::string& name, iu::ImageCpu_32u_C1& img);
179 
185  void add_channel(const std::string& name, iu::ImageCpu_32f_C1& img);
186 
193  void add_channel(const std::string& name1, const std::string& name2, iu::ImageCpu_32f_C2& img);
194 
203  void add_channel(const std::string& name1, const std::string& name2,
204  const std::string& name3, const std::string& name4, iu::ImageCpu_32f_C4& img);
205 
211  void add_channel(const std::string& name, iu::ImageGpu_32f_C1& img);
212 
219  void add_channel(const std::string& name1, const std::string& name2, iu::ImageGpu_32f_C2& img);
220 
229  void add_channel(const std::string& name1, const std::string& name2,
230  const std::string& name3, const std::string& name4, iu::ImageGpu_32f_C4& img);
231 
232  #ifdef IUIO_EIGEN3
233 
238  void add_attribute(const std::string& name, Eigen::Ref<Eigen::Matrix3f> mat);
239 
245  void add_attribute(const std::string& name, Eigen::Ref<Eigen::Matrix4f> mat);
246  #endif
247 
251  void write();
252 
253 private:
254  bool check_channel_name(const std::string& name);
255  bool check_attachement_name(const std::string& name);
256 
257  iu::Size<2> sz_;
258  std::string filename_;
259 
260  Imf::Header header_;
261  Imf::FrameBuffer fb_;
262 
263  // temporary memory for calling add_channel with gpu images
264  // cannot use local ImageCpu* copy in add_channel, because data is accessed
265  // only when write() is called, at that point a local copy in add_channel is not
266  // available any more...
267  std::vector<iu::ImageCpu_32f_C1*> pool_32f_C1_;
268  std::vector<iu::ImageCpu_32f_C2*> pool_32f_C2_;
269  std::vector<iu::ImageCpu_32f_C4*> pool_32f_C4_;
270 };
271  // end of OpenEXRIO
273 
274 
275 } // namespace iu
276 
void read_attribute(const std::string &name, Eigen::Ref< Eigen::Matrix3f > mat)
Read a 3x3 matrix attribute into an Eigen::Matrix3f.
OpenEXRInputFile(const std::string &filename)
OpenEXRInputFile constructor. Opens a file for reading.
void write()
Write to disk. Call this function once all channels/attributes have been added.
Interfaces the OpenEXR library with the Imageutilities.
Definition: openexrio.h:153
OpenEXROutputFile(const std::string &filename, iu::Size< 2 > size)
OpenEXROutputFile constructor. Opens a file for writing.
std::vector< Channel > get_channels()
Get a list of all channels in the file.
Definition: openexrio.h:67
void add_attribute(const std::string &name, Eigen::Ref< Eigen::Matrix3f > mat)
add a 3x3 matrix attribute to the OpenEXR file
The OpenEXRInputFile class interfaces the OpenEXR library with the Imageutilities.
Definition: openexrio.h:34
Template specialization for 2-d unsigned int vectors (size vectors).
Definition: vector.h:525
The Channel struct contains the name (string) and datatype (string) of a channel. ...
Definition: openexrio.h:47
void add_channel(const std::string &name, iu::ImageCpu_8u_C1 &img)
add the channel name with image data img to the OpenEXR file. unsigned char data will be converted to...
void read_channel_32f(const std::string &name, ImageCpu_32f_C1 &img)
Convenience function to read into a float image regardless of channel data. If the channel contains u...
Device 2D image class (pitched memory).
Definition: image_gpu.h:34
Host 2D image class (pitched memory).
Definition: image_cpu.h:27
iu::Size< 2 > get_size()
return size of the OpenEXR image
Definition: openexrio.h:61
void read_channel(const std::string &name, ImageCpu_32u_C1 &img)
Search for a channel name and read its contents into img. The size of img must match the size returne...
std::vector< std::string > get_attributes()
Get a list of all attributes (attachements) in the file.