PICCANTE  0.4
The hottest HDR imaging library!
filter_npasses.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_FILTERING_FILTER_NPASSES_HPP
19 #define PIC_FILTERING_FILTER_NPASSES_HPP
20 
21 #include "../util/std_util.hpp"
22 
23 #include "../filtering/filter.hpp"
24 
25 namespace pic {
26 
30 class FilterNPasses: public Filter
31 {
32 protected:
36 
42  virtual void PreProcess(ImageVec imgIn, Image *imgOut){}
43 
50  Image *setupAuxNGen(ImageVec imgIn, Image *imgOut);
51 
58  Image *setupAuxNSame(ImageVec imgIn, Image *imgOut);
59 
65  virtual Filter* getFilter(int i);
66 
71  virtual int getIterations();
72 
76  void release();
77 
85  Image *ProcessGen(ImageVec imgIn, Image *imgOut, bool parallel);
86 
94  Image *ProcessSame(ImageVec imgIn, Image *imgOut, bool parallel);
95 
96 public:
97 
101  FilterNPasses();
102 
103  ~FilterNPasses();
104 
113  void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames);
114 
121  Image *Process(ImageVec imgIn, Image *imgOut);
122 };
123 
125 {
126  imgAllocated = NULL;
127 
128  for(int i = 0; i < 2; i++) {
129  imgTmpSame[i] = NULL;
130  }
131 
132  imgTmp.clear();
133 }
134 
136 {
137  release();
138 }
139 
141 {
142  if(imgAllocated != NULL) {
143  delete imgAllocated;
144  imgAllocated = NULL;
145  }
146 
147  imgTmpSame[0] = NULL;
148  imgTmpSame[1] = NULL;
149 
150  stdVectorClear<Image>(imgTmp);
151 }
152 
154 {
155  int j = i % filters.size();
156  return filters[j];
157 }
158 
160 {
161  return int(filters.size());
162 }
163 
164 PIC_INLINE void FilterNPasses::OutputSize(ImageVec imgIn, int &width, int &height, int &frames, int &channels)
165 {
166  Image *imgIn0 = new Image(imgIn[0], false);
167 
168  auto *tmp = imgIn[0];
169  imgIn[0] = imgIn0;
170 
171  int n = getIterations();
172 
173  for(int i = 0; i < n; i++) {
174  auto flt_i = getFilter(i);
175  flt_i->changePass(i, n);
176  flt_i->OutputSize(imgIn, width, height, channels, frames);
177 
178  imgIn0->width = width;
179  imgIn0->height = height;
180  imgIn0->channels = channels;
181  imgIn0->frames = frames;
182  }
183 
184  imgIn[0] = tmp;
185 
186  delete imgIn0;
187 }
188 
190  Image *imgOut)
191 {
192  int width, height, frames, channels;
193  OutputSize(imgIn, width, height, frames, channels);
194 
195  int n = getIterations();
196 
197  if(imgTmp.empty()) {
198  setToANullVector<Image>(imgTmp, n);
199  } else {
200  int tw, th, tf, tc;
201 
202  filters[0]->OutputSize(imgIn, tw, th, tf, tc);
203 
204  if(tw != imgTmp[0]->width ||
205  th != imgTmp[0]->height ||
206  tf != imgTmp[0]->frames ||
207  tc != imgTmp[0]->channels) {
208 
209  stdVectorClear<Image>(imgTmp);
210 
211  setToANullVector<Image>(imgTmp, n);
212  }
213  }
214 
215  //output
216  if(imgOut == NULL) {
217  imgOut = new Image(frames, width, height, channels);
218  } else {
219  if(imgOut->height != height ||
220  imgOut->width != width ||
221  imgOut->channels != channels ||
222  imgOut->frames != frames) {
223  imgOut = new Image(frames, width, height, channels);
224  }
225  }
226 
227  return imgOut;
228 }
229 
231  Image *imgOut)
232 {
233  if(imgOut == NULL) {
234  imgOut = imgIn[0]->allocateSimilarOne();
235  } else {
236  if(!imgOut->isSimilarType(imgIn[0])) {
237  imgOut = imgIn[0]->allocateSimilarOne();
238  }
239  }
240 
241  if(imgAllocated == NULL) {
242  imgAllocated = imgIn[0]->allocateSimilarOne();
243  } else {
244  if(!imgAllocated->isSimilarType(imgIn[0])) {
245  delete imgAllocated;
246  imgAllocated = imgIn[0]->allocateSimilarOne();
247  }
248  }
249 
250  if((getIterations() % 2) == 0) {
252  imgTmpSame[1] = imgOut;
253  } else {
254  imgTmpSame[0] = imgOut;
256  }
257 
258  return imgOut;
259 }
260 
262  bool parallel = false)
263 {
264  imgOut = setupAuxNGen(imgIn, imgOut);
265 
266  int n = getIterations();
267  int n2 = n - 1;
268 
269  for(int i = 0; i < n2; i++) {
270  auto flt_i = getFilter(i);
271  flt_i->changePass(i, n);
272 
273  imgTmp[i] = flt_i->Process(imgIn, imgTmp[i]);
274 
275  imgIn[0] = imgTmp[i];
276  }
277 
278  auto flt_n = getFilter(n2);
279  flt_n->changePass(n2, n);
280 
281  imgOut = filters[n2]->Process(imgIn, imgOut);
282 
283  return imgOut;
284 }
285 
287  bool parallel = false)
288 {
289  //setup
290  imgOut = setupAuxNSame(imgIn, imgOut);
291 
292  int n = getIterations();
293  auto flt_0 = getFilter(0);
294  flt_0->changePass(0, n);
295 
296  flt_0->Process(imgIn, imgTmpSame[0]);
297 
298  for(int i = 1; i < n; i++) {
299  auto flt_i = getFilter(i);
300  flt_i->changePass(i, n);
301 
302  imgIn[0] = imgTmpSame[(i + 1) % 2];
303 
304  flt_i->Process(imgIn, imgTmpSame[i % 2]);
305  }
306 
307  return imgOut;
308 }
309 
311  Image *imgOut)
312 {
313  if(imgIn.empty() || filters.empty()) {
314  return imgOut;
315  }
316 
317  PreProcess(imgIn, imgOut);
318 
319  int width, height, frames, channels;
320  OutputSize(imgIn, width, height, frames, channels);
321 
322  bool bSame = (imgIn[0]->width == width) &&
323  (imgIn[0]->height == height) &&
324  (imgIn[0]->frames == frames) &&
325  (imgIn[0]->channels == channels);
326 
327  if(bSame) {
328  imgOut = ProcessSame(imgIn, imgOut);
329  } else {
330  imgOut = ProcessGen(imgIn, imgOut);
331  }
332 
333  return imgOut;
334 }
335 
336 } // end namespace pic
337 
338 #endif /* PIC_FILTERING_FILTER_NPASSES_HPP */
339 
virtual Filter * getFilter(int i)
getFilter
Definition: filter_npasses.hpp:153
void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames)
OutputSize.
Definition: filter_npasses.hpp:164
int channels
Definition: image.hpp:80
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
Image * ProcessGen(ImageVec imgIn, Image *imgOut, bool parallel)
ProcessGen.
Definition: filter_npasses.hpp:261
bool isSimilarType(const Image *img)
isSimilarType checks if the current image is similar to img; i.e. if they have the same width...
The Filter class.
Definition: filter.hpp:50
virtual void PreProcess(ImageVec imgIn, Image *imgOut)
PreProcess.
Definition: filter_npasses.hpp:42
int frames
Definition: image.hpp:80
Image * imgAllocated
Definition: filter_npasses.hpp:33
virtual int getIterations()
getIterations
Definition: filter_npasses.hpp:159
Image * setupAuxNGen(ImageVec imgIn, Image *imgOut)
setupAuxNGen
Definition: filter_npasses.hpp:189
Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter_npasses.hpp:310
Image * setupAuxNSame(ImageVec imgIn, Image *imgOut)
setupAuxNSame
Definition: filter_npasses.hpp:230
~FilterNPasses()
Definition: filter_npasses.hpp:135
FilterNPasses()
FilterNPasses.
Definition: filter_npasses.hpp:124
#define PIC_INLINE
Definition: base.hpp:33
Image * ProcessSame(ImageVec imgIn, Image *imgOut, bool parallel)
ProcessSame.
Definition: filter_npasses.hpp:286
The Image class stores an image as buffer of float.
Definition: image.hpp:60
Image * allocateSimilarOne()
allocateSimilarOne creates an Image with similar size of the calling instance.
void release()
release
Definition: filter_npasses.hpp:140
Definition: bilateral_separation.hpp:25
ImageVec imgTmp
Definition: filter_npasses.hpp:35
int width
Definition: image.hpp:80
int height
Definition: image.hpp:80
std::vector< Filter * > filters
Definition: filter_radial_basis_function.hpp:121
Image * imgTmpSame[2]
Definition: filter_npasses.hpp:34
The FilterNPasses class.
Definition: filter_npasses.hpp:30