PICCANTE  0.4
The hottest HDR imaging library!
tile_list.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_TILE_LIST_HPP
19 #define PIC_UTIL_TILE_LIST_HPP
20 
21 #include <thread>
22 #include <mutex>
23 #include <vector>
24 
25 #include "../base.hpp"
26 #include "../util/tile.hpp"
27 
28 namespace pic {
29 
33 class TileList
34 {
35 protected:
37 
38 #ifndef PIC_DISABLE_THREAD
39  std::mutex mutex;
40 #endif
41 
42 public:
43  int width, height;
44  int h_tile, w_tile;
45  int mod_h, mod_w;
46 
50  std::vector<Tile> tiles;
51 
55  TileList();
56 
63  TileList(int tileSize, int width, int height);
64 
65  ~TileList();
66 
72  BBox getBBox(int index);
73 
78  uint getNext();
79 
84  uint size();
85 
89  void resetCounter();
90 
97  void create(int tileSize, int width, int height);
98 
107  bool read(std::string name, bool flag);
108 
114  bool write(std::string name);
115 
120  void writeIntoMemory(Image *output);
121 };
122 
124 {
125  counter = 0;
126 
127  w_tile = 0;
128  h_tile = 0;
129 
130  mod_h = 0;
131  mod_w = 0;
132 }
133 
134 PIC_INLINE TileList::TileList(int tileSize, int width, int height)
135 {
136  counter = 0;
137  create(tileSize, width, height);
138 }
139 
141 {
142  tiles.clear();
143 }
144 
145 PIC_INLINE BBox TileList::getBBox(int index)
146 {
147  int i = index % tiles.size();
148 
149  return tiles[i].getBBox(width, height);
150 }
151 
153 {
154  uint ret = 0;
155  {
156 #ifndef PIC_DISABLE_THREAD
157  std::lock_guard<std::mutex> lock(mutex);
158 #endif
159  ret = counter;
160  counter++;
161  }
162  return ret;
163 }
164 
166 {
167  return (uint)(tiles.size());
168 }
169 
171 {
172  {
173 #ifndef PIC_DISABLE_THREAD
174  std::lock_guard<std::mutex> lock(mutex);
175 #endif
176  counter = 0;
177  }
178 }
179 
180 PIC_INLINE void TileList::create(int tileSize, int width, int height)
181 {
182  resetCounter();
183 
184  if(!tiles.empty()) {
185  if((tiles[0].width == tileSize) && (this->width == width) &&
186  (this->height == height)) {
187  return;
188  }
189 
190  tiles.clear();
191  }
192 
193  this->width = width;
194  this->height = height;
195 
196  h_tile = height / tileSize;
197  w_tile = width / tileSize;
198  mod_h = height % tileSize;
199  mod_w = width % tileSize;
200 
201  //main blocks
202  bool bWidth = mod_w != 0;
203  for(int i = 0; i < h_tile; i++) {
204  Tile tile;
205  tile.width = tileSize;
206  tile.height = tileSize;
207  tile.startY = i * tileSize;
208 
209  for(int j = 0; j < w_tile; j++) {
210  tile.startX = j * tileSize;
211  tiles.push_back(tile);
212  }
213 
214  //extra blocks
215  if(bWidth) {
216  tile.startX = w_tile * tileSize;
217  tile.width = mod_w;
218  tiles.push_back(tile);
219  }
220  }
221 
222  //fixed height strip blocks
223  if(mod_h != 0) {
224  int i = h_tile;
225 
226  Tile tile;
227  tile.startY = i * tileSize;
228  tile.width = tileSize;
229 
230  for(int j = 0; j < w_tile; j++) {
231  tile.startX = j * tileSize;
232  tile.height = mod_h;
233  tiles.push_back(tile);
234  }
235 
236  if(bWidth) {
237  tile.startX = w_tile * tileSize;
238  tile.width = mod_w;
239  tile.height = mod_h;
240  tiles.push_back(tile);
241  }
242  }
243 }
244 
245 PIC_INLINE void TileList::writeIntoMemory(Image *output)
246 {
247  if(output == NULL) {
248  return;
249  }
250 
251  if(!output->isValid()) {
252  return;
253  }
254 
255  for(uint i = 0; i < tiles.size(); i++) { //for each tile
256  if(tiles[i].tile != NULL) {
257  output->copySubImage(tiles[i].tile, tiles[i].startX, tiles[i].startY);
258  }
259 
260  #ifdef PIC_DEBUG
261  printf("Tile x: %d y: %d\n", tiles[i].startX, tiles[i].startY);
262  #endif
263  }
264 }
265 
266 PIC_INLINE bool TileList::read(std::string name, bool flag)
267 {
268  FILE *file = fopen(name.c_str(), "r");
269 
270  if(file == NULL) {
271  return false;
272  }
273 
274  //tmp vars
275  char tmp[128];
276  char txt[128];
277 
278  //number of tiles
279  int n;
280  fscanf(file, "%s", tmp);
281  fscanf(file, "%d", &n);
282 
283  //flag
284  fscanf(file, "%s", tmp);
285  fscanf(file, "%s", txt);
286 
287  Tile tmpTile;
288  char tmp_name[128];
289 
290  for(int i = 0; i < n; i++) { //for each tile
291  fscanf(file, "%s", tmp);
292  fscanf(file, "%s", tmp_name);
293 
294  fscanf(file, "%s", tmp);
295  fscanf(file, "%d", &tmpTile.startX);
296 
297  fscanf(file, "%s", tmp);
298  fscanf(file, "%d", &tmpTile.startY);
299 
300  fscanf(file, "%s", tmp);
301  fscanf(file, "%d", &tmpTile.width);
302 
303  fscanf(file, "%s", tmp);
304  fscanf(file, "%d", &tmpTile.height);
305 
306  tmpTile.name = tmp_name;
307 
308  if(flag) {
309  tmpTile.tile = new Image(tmpTile.name);
310  } else {
311  tmpTile.tile = new Image(1, tmpTile.width, tmpTile.height, 3);
312  }
313 
314  tiles.push_back(tmpTile);
315  }
316 
317  fclose(file);
318  return true;
319 }
320 
321 PIC_INLINE bool TileList::write(std::string name)
322 {
323  FILE *file = fopen(name.c_str(), "w");
324 
325  if(file == NULL) {
326  return false;
327  }
328 
329  //number of tiles
330  int n = int(tiles.size());
331  fprintf(file, "NUMBER_OF_TILES: %d\n", n);
332 
333  //flag
334  fprintf(file, "FLAG: NONE\n");
335 
336  for(int i = 0; i < n; i++) { //for each tile
337 
338  bool bName = !tiles[i].name.empty();
339  if(bName) {
340  fprintf(file, "Tile_name: %s\n", tiles[i].name.c_str());
341  } else {
342  fprintf(file, "Tile_name: none\n");
343  }
344 
345  fprintf(file, "StartX: %d\n", tiles[i].startX);
346  fprintf(file, "StartY: %d\n", tiles[i].startY);
347 
348  fprintf(file, "Width: %d\n", tiles[i].width);
349  fprintf(file, "Height: %d\n", tiles[i].height);
350 
351  if(bName && tiles[i].tile != NULL) {
352  tiles[i].tile->Write(tiles[i].name);
353  }
354  }
355 
356  fclose(file);
357 
358  return true;
359 }
360 
361 } // end namespace pic
362 
363 #endif /* PIC_UTIL_TILE_LIST_HPP */
364 
void resetCounter()
resetCounter sets the counter to zero.
The BBox class manages the creation of bounding boxes for images.
Definition: bbox.hpp:29
unsigned int uint
Definition: base.hpp:23
uint size()
size
void writeIntoMemory(Image *output)
writeIntoMemory copies tiles inside an output image.
TileList()
TileList basic constructor.
int mod_h
Definition: tile_list.hpp:45
BBox getBBox(int index)
genBBox
int h_tile
Definition: tile_list.hpp:44
int width
Definition: tile_list.hpp:43
std::mutex mutex
Definition: tile_list.hpp:39
The TileList class.
Definition: tile_list.hpp:33
uint counter
Definition: tile_list.hpp:36
uint getNext()
getNext returns the index of the next tile to process.
bool write(std::string name)
write saves a TileList into a file.
#define PIC_INLINE
Definition: base.hpp:33
The Image class stores an image as buffer of float.
Definition: image.hpp:60
int height
Definition: tile_list.hpp:43
int w_tile
Definition: tile_list.hpp:44
void create(int tileSize, int width, int height)
Create creates a list of tiles.
int mod_w
Definition: tile_list.hpp:45
Definition: bilateral_separation.hpp:25
bool read(std::string name, bool flag)
read loads a TileList from a file.
std::vector< Tile > tiles
tiles a list of tiles
Definition: tile_list.hpp:50