PICCANTE  0.4
The hottest HDR imaging library!
raw.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_UTIL_RAW_HPP
19 #define PIC_UTIL_RAW_HPP
20 
21 #include <string>
22 #include <iostream>
23 #include <vector>
24 
25 #include "../base.hpp"
26 #include "../util/array.hpp"
27 #include "../util/file_lister.hpp"
28 #include "../util/string.hpp"
29 
30 namespace pic {
31 
33 
34 template <class T>
35 class RAW: public Array<T>
36 {
37 protected:
38  bool valid;
39 
40 public:
41 
45  RAW() : Array<T>()
46  {
47  valid = false;
48  }
49 
54  RAW(int n): Array<T>(n)
55  {
56  valid = true;
57  }
58 
64  RAW(std::string nameFile, int nData = -1)
65  {
66  valid = false;
67  this->data = NULL;
68  nData = 0;
69 
70  Read(nameFile, nData);
71  }
72 
73  ~RAW()
74  {
75  this->release();
76  }
77 
84  bool Read(std::string nameFile, int nData)
85  {
86  std::ifstream file;
87  file.open(nameFile.c_str(), std::ios::in | std::ios::binary);
88 
89  if(!file.is_open()) {
90  return false;
91  }
92 
93  //Calculate length of the file
94  file.seekg(0, std::ios::end);
95  std::ios::streampos length = file.tellg();
96  file.seekg(0, std::ios::beg);
97 
98  if(nData < 1) {
99  nData = length / sizeof(T);
100  }
101 
102  this->allocate(nData);
103 
104  file.read((char *)this->data, this->nData * sizeof(T) / sizeof(char));
105  file.close();
106 
107  valid = true;
108  return true;
109  }
110 
116  bool Write(std::string nameFile)
117  {
118  std::ofstream file;
119  file.open(nameFile.c_str(), std::ios::binary);
120 
121  if(file.is_open()) {
122  file.write((char *)this->data, this->nData * sizeof(T) / sizeof(char));
123  file.close();
124  valid = true;
125  return true;
126  } else {
127  return false;
128  }
129  }
130 
136  static RAW<T> *getMeanRAWStack(std::vector<RAW<T> > &stack)
137  {
138  if(stack.size() <= 0) {
139  return NULL;
140  }
141 
142  int nData = stack[0].nData;
143  RAW<T> *dataOut = new RAW<T>();
144  dataOut->data = new T[nData];
145  dataOut->nData = nData;
146 
147  int stackSize = stack.size();
148 
149  for(int i = 0; i < nData; i++) {
150  unsigned long tmpData = 0;
151 
152  for(int j = 0; j < stackSize; j++) {
153  tmpData += stack[j].data[i];
154  }
155 
156  dataOut->data[i] = tmpData / stackSize;
157  }
158 
159  dataOut->valid = true;
160  return dataOut;
161  }
162 
163  static unsigned long *getMeanRAWIterative(RAW<T> *img,
164  unsigned long *dataAcc, bool bStart)
165  {
166 
167  if(dataAcc == NULL) {
168  dataAcc = new unsigned long[img->nData];
169  }
170 
171  if(bStart) {
172  for(int i = 0; i < img->nData; i++) {
173  dataAcc[i] = img->data[i];
174  }
175  } else {
176  for(int i = 0; i < img->nData; i++) {
177  dataAcc[i] += img->data[i];
178  }
179  }
180 
181  return dataAcc;
182  }
183 
193  std::string nameDir,
194  std::string nameFilter,
195  int width,
196  int height)
197  {
198  StringVec vec;
199 
200  FileLister::getList(nameDir, nameFilter, &vec);
201 
202  RAW<T> imgRAW;
203  unsigned long *dataAcc = NULL;
204 
205  for(unsigned int i = 0; i < vec.size(); i++) {
206  imgRAW.Read(vec[i], width * height);
207  dataAcc = getMeanRAWIterative(&imgRAW, dataAcc, i == 0);
208  }
209 
210  RAW<T> *imgOut = imgRAW.copy();
211 
212  for(int i = 0; i < imgOut->nData; i++) {
213  imgOut->data[i] = dataAcc[i] / vec.size();
214  }
215 
216  return imgOut;
217  }
218 
227  static void getMeanRAWFromFile(
228  std::string nameDir,
229  std::string nameFilter,
230  std::string nameOut,
231  int width,
232  int height)
233  {
234  (getMeanRAWFromFile(nameDir, nameFilter, width,
235  height))->Write(nameOut);
236  }
237 };
238 
239 
240 } // end namespace pic
241 
242 #endif /* PIC_UTIL_RAW_HPP */
243 
Definition: raw.hpp:32
std::vector< std::string > StringVec
StringVec is an std::vector of std::string.
Definition: string.hpp:49
RAW()
RAW.
Definition: raw.hpp:45
RAW(int n)
RAW.
Definition: raw.hpp:54
static RAW< T > * getMeanRAWStack(std::vector< RAW< T > > &stack)
getMeanRAWStack
Definition: raw.hpp:136
static void getMeanRAWFromFile(std::string nameDir, std::string nameFilter, std::string nameOut, int width, int height)
getMeanRAWFromFile
Definition: raw.hpp:227
Definition: raw.hpp:32
Definition: raw.hpp:35
RAW(std::string nameFile, int nData=-1)
RAW.
Definition: raw.hpp:64
Definition: raw.hpp:32
void release()
release
Definition: saturation.hpp:107
~RAW()
Definition: raw.hpp:73
bool valid
Definition: raw.hpp:38
static unsigned long * getMeanRAWIterative(RAW< T > *img, unsigned long *dataAcc, bool bStart)
Definition: raw.hpp:163
bool Read(std::string nameFile, int nData)
Read.
Definition: raw.hpp:84
Definition: raw.hpp:32
static StringVec * getList(std::string nameDir, std::string nameFilter, StringVec *sVecOut)
Definition: file_lister.hpp:61
The Array class.
Definition: array.hpp:30
Definition: bilateral_separation.hpp:25
RAW_type
Definition: raw.hpp:32
static RAW< T > * getMeanRAWFromFile(std::string nameDir, std::string nameFilter, int width, int height)
getMeanRAWFromFile
Definition: raw.hpp:192
T * data
Definition: saturation.hpp:37
void allocate(int n)
allocate
Definition: saturation.hpp:89
bool Write(std::string nameFile)
Write.
Definition: raw.hpp:116
int nData
Definition: saturation.hpp:38