PICCANTE  0.4
The hottest HDR imaging library!
filter_crop.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_CROP_HPP
19 #define PIC_FILTERING_FILTER_CROP_HPP
20 
21 #include "../filtering/filter.hpp"
22 #include "../util/vec.hpp"
23 
24 namespace pic {
25 
29 class FilterCrop: public Filter
30 {
31 protected:
32  bool flag;
35 
42  void ProcessBBox(Image *dst, ImageVec src, BBox *box);
43 
44 public:
45 
51  FilterCrop(Vec2i min, Vec2i max);
52 
58  FilterCrop(Vec3i min, Vec3i max);
59 
65  FilterCrop(Vec4i min, Vec4i max);
66 
72  FilterCrop(Vec3f min, Vec3f max);
73 
82  void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames);
83 
92  static Image *execute(Image *imgIn, Image *imgOut, Vec4i min,
93  Vec4i max)
94  {
95  FilterCrop fltCrop(min, max);
96  return fltCrop.Process(Single(imgIn), imgOut);
97  }
98 
107  static Image *execute(Image *imgIn, Image *imgOut, Vec2i min,
108  Vec2i max)
109  {
110  FilterCrop fltCrop(min, max);
111  return fltCrop.Process(Single(imgIn), imgOut);
112  }
113 
117  static void test()
118  {
119  Image img(1, 512, 512, 3);
120  img = 1.0f;
121 
122  FilterCrop flt(Vec2i(100, 100), Vec2i(200, 200));
123 
124  Image *out = flt.Process(Single(&img), NULL);
125 
126  out->Write("test_crop_2d_output.png");
127  }
128 };
129 
131 {
132  mini[0] = min[0];
133  mini[1] = min[1];
134  mini[2] = 0;
135  mini[3] = 0;
136 
137  maxi[0] = max[0];
138  maxi[1] = max[1];
139  maxi[2] = 1;
140  maxi[3] = INT_MAX;
141 
142  flag = false;
143 }
144 
146 {
147  for(int i = 0; i < 3; i++) {
148  this->mini[i] = min[i];
149  this->maxi[i] = max[i];
150  }
151 
152  mini[3] = 0;
153  maxi[3] = INT_MAX;
154 
155  flag = false;
156 }
157 
159 {
160  this->mini = min;
161  this->maxi = max;
162 
163  flag = false;
164 }
165 
167 {
168  this->minf = min;
169  this->maxf = max;
170 
171  flag = true;
172 }
173 
174 PIC_INLINE void FilterCrop::OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames)
175 {
176  if(flag) {
177  mini[0] = int(minf[0] * imgIn[0]->widthf);
178  mini[1] = int(minf[1] * imgIn[0]->heightf);
179  mini[2] = int(minf[2] * imgIn[0]->framesf);
180 
181  maxi[0] = int(maxf[0] * imgIn[0]->widthf);
182  maxi[1] = int(maxf[1] * imgIn[0]->heightf);
183  maxi[2] = int(maxf[2] * imgIn[0]->framesf);
184  }
185 
186  channels = MIN(imgIn[0]->channels, maxi[3]) - mini[3];
187 
188  if(mini[3] > 0) {
189  channels++;
190  }
191 
192  int delta[3];
193  for(int i = 0; i < 3; i++) {
194  delta[i] = maxi[i] - mini[i];
195  }
196 
197  if(delta[0] <= 0) {
198  delta[0] = imgIn[0]->width;
199  mini[0] = 0;
200  maxi[0] = imgIn[0]->width;
201  }
202 
203  if(delta[1] <= 0) {
204  delta[1] = imgIn[0]->height;
205  mini[1] = 0;
206  maxi[1] = imgIn[0]->height;
207  }
208 
209  if(delta[2] <= 0) {
210  delta[2] = imgIn[0]->frames;
211  mini[2] = 0;
212  maxi[2] = imgIn[0]->frames;
213  }
214 
215  width = delta[0];
216  height = delta[1];
217  frames = delta[2];
218 }
219 
221 {
222  maxi[3] = MIN(maxi[3], src[0]->channels);
223 
224  for(int p = box->z0; p < box->z1; p++) {
225  for(int j = box->y0; j < box->y1; j++) {
226  for(int i = box->x0; i < box->x1; i++) {
227  float *dst_data = (*dst)(i - mini[0], j - mini[1], p - mini[2]);
228  float *src_data = (*src[0])(i, j, p);
229 
230  for(int k = mini[3]; k <= maxi[3]; k++) {
231  dst_data[k - mini[3]] = src_data[k];
232  }
233  }
234  }
235  }
236 }
237 
238 } // end namespace pic
239 
240 #endif /* PIC_FILTERING_FILTER_CROP_HPP */
241 
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
Vec4i maxi
Definition: filter_crop.hpp:33
std::vector< Image * > ImageVec
ImageVec an std::vector of pic::Image.
Definition: image_vec.hpp:29
int x0
Definition: bbox.hpp:32
static Image * execute(Image *imgIn, Image *imgOut, Vec2i min, Vec2i max)
execute
Definition: filter_crop.hpp:107
static Image * execute(Image *imgIn, Image *imgOut, Vec4i min, Vec4i max)
execute
Definition: filter_crop.hpp:92
The Filter class.
Definition: filter.hpp:50
void OutputSize(ImageVec imgIn, int &width, int &height, int &channels, int &frames)
OutputSize.
Definition: filter_crop.hpp:174
Vec3f minf
Definition: filter_crop.hpp:34
virtual Image * Process(ImageVec imgIn, Image *imgOut)
Process.
Definition: filter.hpp:390
static void test()
test
Definition: filter_crop.hpp:117
Vec< 2, int > Vec2i
Vec2i.
Definition: vec.hpp:829
void ProcessBBox(Image *dst, ImageVec src, BBox *box)
ProcessBBox.
Definition: filter_crop.hpp:220
FilterCrop(Vec2i min, Vec2i max)
FilterCrop.
Definition: filter_crop.hpp:130
int y0
Definition: bbox.hpp:32
bool flag
Definition: filter_crop.hpp:32
Vec4i mini
Definition: filter_crop.hpp:33
The FilterCrop class.
Definition: filter_crop.hpp:29
#define PIC_INLINE
Definition: base.hpp:33
Vec3f maxf
Definition: filter_crop.hpp:34
#define MIN(a, b)
Definition: math.hpp:69
The Image class stores an image as buffer of float.
Definition: image.hpp:60
PIC_INLINE ImageVec Single(Image *img)
Single creates an std::vector which contains img; this is for filters input.
Definition: image_vec.hpp:36
Definition: bilateral_separation.hpp:25
int z0
Definition: bbox.hpp:32
bool Write(std::string nameFile, LDR_type typeWrite, int writerCounter)
Write saves an Image into a file on the disk.
The Vec class.
Definition: vec.hpp:35