PICCANTE  0.4
The hottest HDR imaging library!
exr.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_IO_EXR_HPP
19 #define PIC_IO_EXR_HPP
20 
21 #include "../base.hpp"
22 
23 #ifdef PIC_ENABLE_OPEN_EXR
24 
25 //include for OpenEXR 1.xx
26 
27 #include <ImfRgbaFile.h>
28 #include <ImfStringAttribute.h>
29 #include <ImfMatrixAttribute.h>
30 #include <ImfArray.h>
31 
32 #ifdef PIC_WIN32
33  #pragma comment( lib, "Iex_dll" )
34  #pragma comment( lib, "Half_dll" )
35  #pragma comment( lib, "IlmImf_dll" )
36  #pragma comment( lib, "IlmThread_dll" )
37  #pragma comment( lib, "Imath_dll" )
38  #pragma comment( lib, "zlib" )
39 #endif
40 
41 //SYSTEM: X POS Y NEG
42 namespace pic {
43 
53 PIC_INLINE Imf::Rgba *ReadEXR(std::string nameFile, int &width, int &height,
54  int &channels, Imf::Rgba *pixelBuffer = NULL)
55 {
56  try {
57  Imf::RgbaInputFile in(nameFile.c_str());
58  Imath::Box2i win = in.dataWindow();
59 
60  Imath::V2i dim(win.max.x - win.min.x + 1, win.max.y - win.min.y + 1);
61 
62  //printf("%d %d %d %d\n",win.max.x,win.min.x,win.max.y,win.min.y);
63 
64  if(pixelBuffer == NULL) {
65  pixelBuffer = new Imf::Rgba[dim.x * dim.y];
66  }
67 
68  int dx = win.min.x;
69  int dy = win.min.y;
70 
71  in.setFrameBuffer(pixelBuffer - dx - dy * dim.x, 1, dim.x);
72  in.readPixels(win.min.y, win.max.y);
73 
74  width = dim.x;
75  height = dim.y;
76  channels = 3;
77 
78  return pixelBuffer;
79 
80  } catch(Iex::BaseExc &e) {
81  std::cerr << e.what() << std::endl;
82  return NULL;
83  }
84 }
85 
96 PIC_INLINE float *ReadEXR(std::string nameFile, float *data, int &width,
97  int &height, int &channels, Imf::Rgba *pixelBuffer = NULL)
98 {
99  try {
100  Imf::RgbaInputFile in(nameFile.c_str());
101  Imath::Box2i win = in.dataWindow();
102 
103  Imath::V2i dim(win.max.x - win.min.x + 1, win.max.y - win.min.y + 1);
104 
105  //printf("%d %d %d %d\n",win.max.x,win.min.x,win.max.y,win.min.y);
106 
107  bool bPixelBufferNULL = false;
108 
109  if(pixelBuffer == NULL) {
110  pixelBuffer = new Imf::Rgba[dim.x * dim.y];
111  bPixelBufferNULL = true;
112  }
113 
114  int dx = win.min.x;
115  int dy = win.min.y;
116 
117  in.setFrameBuffer(pixelBuffer - dx - dy * dim.x, 1, dim.x);
118  in.readPixels(win.min.y, win.max.y);
119 
120  //Allocate into memory
121  if(data == NULL) {
122  data = new float[dim.x * dim.y * 3];
123  }
124 
125  width = dim.x;
126  height = dim.y;
127  channels = 3;
128 
129  int tot = width * height * channels;
130 
131  for(int i = 0; i < tot; i += channels) { //swizzle
132  int j = i / 3;
133  data[i ] = pixelBuffer[j].r;
134  data[i + 1] = pixelBuffer[j].g;
135  data[i + 2] = pixelBuffer[j].b;
136  }
137 
138  if(bPixelBufferNULL) {
139  delete[] pixelBuffer;
140  }
141 
142  return data;
143  } catch(Iex::BaseExc &e) {
144  std::cerr << e.what() << std::endl;
145  return NULL;
146  }
147 }
148 
159 PIC_INLINE bool WriteEXR(std::string nameFile, const float *data, int width,
160  int height, int channels = 3, Imf::Rgba *pixelBuffer = NULL)
161 {
162  Imath::Box2i win;
163  win.max.x = width - 1;
164  win.max.y = height - 1;
165  win.min.x = 0;
166  win.min.y = 0;
167  Imf::RgbaOutputFile outC(nameFile.c_str(), win, win, Imf::WRITE_RGBA);
168 
169  //Copy data
170  int tot = width * height;
171  bool bPixelBufferNULL = false;
172 
173  if(pixelBuffer == NULL) {
174  pixelBuffer = new Imf::Rgba[tot];
175  bPixelBufferNULL = true;
176  }
177 
178  int j = 0;
179 
180  for(int i = 0; i < tot; i++) {
181  pixelBuffer[i].r = data[j];
182  j++;
183  pixelBuffer[i].g = data[j];
184  j++;
185  pixelBuffer[i].b = data[j];
186  j++;
187  pixelBuffer[i].a = 1.0f;
188  }
189 
190  outC.setFrameBuffer(pixelBuffer, 1, width);
191  outC.writePixels(height);
192 
193  if(bPixelBufferNULL) {
194  delete[] pixelBuffer;
195  }
196 
197  return true;
198 }
199 
200 } // end namespace pic
201 #endif //PIC_ENABLE_OPEN_EXR
202 
203 #endif /* PIC_IO_EXR_HPP */
204 
PIC_INLINE bool WriteEXR(std::string nameFile, float *data, int width, int height, int channels=3)
WriteEXR.
Definition: exr_tiny.hpp:95
PIC_INLINE float * ReadEXR(std::string nameFile, float *data, int &width, int &height, int &channels)
Definition: exr_tiny.hpp:33
#define PIC_INLINE
Definition: base.hpp:33
Definition: bilateral_separation.hpp:25