DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testImageAdapter.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageContainerBySTLVector.h"
35 
36 //#define DEBUG_VERBOSE
37 
38 #include "DGtal/images/ImageAdapter.h"
39 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
40 #include "DGtal/io/readers/PNMReader.h"
41 #include "DGtal/io/boards/Board2D.h"
42 
43 #include "ConfigTest.h"
45 
46 using namespace std;
47 using namespace DGtal;
48 
50 // Functions for testing class ImageAdapter.
52 bool testSimple()
53 {
54  unsigned int nbok = 0;
55  unsigned int nb = 0;
56 
57  trace.beginBlock("Testing simple ImageAdapter");
58 
60 
61  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
62  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
63  *it = 10;
64 
65  trace.info() << "Original image: " << image << endl;
66 
67  Z2i::Domain domain(Z2i::Point(2,2), Z2i::Point(4,4));
69 
70  DefaultFunctor idD;
71  DefaultFunctor idV;
72  DefaultFunctor idVm1;
73 
74  MyImageAdapter restimage(image, domain, idD, idV, idVm1);
75  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
76 
77  nbok += (restimage(Z2i::Point(3,3)) == 10) ? 1 : 0;
78  nb++;
79  trace.info() << "(" << nbok << "/" << nb << ") "
80  << " read access on restricted Image" << endl;
81 
82  restimage.setValue(Z2i::Point(3,3), 5);
83  nbok += (restimage(Z2i::Point(3,3)) == 5) ? 1 : 0;
84  nb++;
85  trace.info() << "(" << nbok << "/" << nb << ") "
86  << " write on restricted Image" << endl;
87 
88  nbok += (image(Z2i::Point(3,3)) == 5) ? 1 : 0;
89  nb++;
90  trace.info() << "(" << nbok << "/" << nb << ") "
91  << " written on original image" << endl;
92 
93  trace.warning()<< "Original image at (3,3) = "<< (image)(Z2i::Point(3,3)) << std::endl;
94 
95  trace.endBlock();
96 
97  return nbok == nb;
98 }
99 
107  template <typename TValue>
109  {
110  public:
111  typedef TValue Value;
112 
117  MyTransValueFunctor(const Value& aValue = 0)
118  :myValue(aValue) {};
119 
128  template <typename TInput>
129  inline
130  Value operator()(const TInput& aInput) const
131  {
132  return aInput+myValue;
133  }
134 
135  private:
140 
141  };
142 
143 bool test_g_f_fm1()
144 {
145  unsigned int nbok = 0;
146  unsigned int nb = 0;
147 
148  trace.beginBlock("Testing g, f and fm1 with ImageAdapter");
149 
151 
152  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
153  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
154  *it = 10;
155 
156  trace.info() << "Original image: " << image << endl;
157 
158  Z2i::Domain domain(Z2i::Point(2,2), Z2i::Point(4,4));
160 
161  DefaultFunctor idD;
163  DefaultFunctor idVm1;
164 
165  MyImageAdapter restimage(image, domain, idD, idV, idVm1);
166  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
167 
168  nbok += (restimage(Z2i::Point(3,3)) == 3) ? 1 : 0;
169  nb++;
170  trace.info() << "(" << nbok << "/" << nb << ") "
171  << " read access on restricted Image" << endl;
172 
175 
176  DefaultFunctor idD_2, idV_2;
177  Thresholder<VImage::Value> idVm1_2( 4 );
178 
179  MyImageAdapter2 restimage2(image, domain, idD_2, idV_2, idVm1_2);
181 
182  restimage2.setValue(Z2i::Point(2,2), 3);
183  nbok += (restimage2(Z2i::Point(2,2)) == 1) ? 1 : 0;
184  nb++;
185  trace.info() << "(" << nbok << "/" << nb << ") "
186  << " write on restricted Image 2" << endl;
187 
188  trace.warning()<< "Restricted image 2 at (2,2) = "<< (restimage2)(Z2i::Point(2,2)) << std::endl;
189  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
190 
191  restimage2.setValue(Z2i::Point(2,2), 5);
192  nbok += (restimage2(Z2i::Point(2,2)) == 0) ? 1 : 0;
193  nb++;
194  trace.info() << "(" << nbok << "/" << nb << ") "
195  << " write on restricted Image 2" << endl;
196 
197  trace.warning()<< "Restricted image 2 at (2,2) = "<< (restimage2)(Z2i::Point(2,2)) << std::endl;
198  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
199 
200  typedef ImageAdapter<VImage, Z2i::Domain, MyTransValueFunctor<Z2i::Point>, VImage::Value, DefaultFunctor, DefaultFunctor > MyImageAdapter3;
201 
203  DefaultFunctor idV_3;
204  DefaultFunctor idVm1_3;
205 
206  MyImageAdapter3 restimage3(image, domain, idD_3, idV_3, idVm1_3);
207 
208  restimage3.setValue(Z2i::Point(2,2), 5);
209  nbok += (image(Z2i::Point(4,4)) == 5) ? 1 : 0;
210  nb++;
211  trace.info() << "(" << nbok << "/" << nb << ") "
212  << " write on restricted Image 3" << endl;
213 
214  trace.warning()<< "Original image at (2,2) = "<< (image)(Z2i::Point(2,2)) << std::endl;
215  trace.warning()<< "Original image at (4,4) = "<< (image)(Z2i::Point(4,4)) << std::endl;
216  trace.warning()<< "Original image at (5,5) = "<< (image)(Z2i::Point(5,5)) << std::endl;
217 
218  trace.endBlock();
219 
220  return nbok == nb;
221 }
222 
223 bool test_range_constRange()
224 {
225  unsigned int nbok = 0;
226  unsigned int nb = 0;
227 
228  trace.beginBlock("Testing range/constRange with ImageAdapter");
229 
231 
232  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(10,10)));
233  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
234  *it = 10;
235 
236  trace.info() << "Original image: " << image << endl;
237 
238  Z2i::Domain domain(Z2i::Point(2,2), Z2i::Point(4,4));
240 
241  DefaultFunctor idD;
242  DefaultFunctor idV;
243  DefaultFunctor idVm1;
244 
245  MyImageAdapter restimage(image, domain, idD, idV, idVm1);
246  trace.info() << "Restricted Image: " << restimage << " " << restimage.domain() << std::endl;
247 
248  // writing values
249  const int maximalValue = domain.size();
250  MyImageAdapter::Range::OutputIterator it = restimage.range().outputIterator();
251  for (int i = 0; i < maximalValue; ++i)
252  *it++ = i;
253 
254  // reading values
255  MyImageAdapter::ConstRange r = restimage.constRange();
256  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") );
257  cout << endl;
258 
259  std::vector<int> to_vector(9);
260  std::copy(r.begin(), r.end(), to_vector.begin());
261  for (int i = 0; i < 9; i++)
262  {
263  if (to_vector[i]==i)
264  {
265  cout << "ok, ";
266  nbok += true ? 1 : 0; nb++;
267  }
268  else
269  {
270  cout << "!ok, ";
271  nbok += false ? 1 : 0; nb++;
272  }
273  }
274 
275  cout << endl;
276 
277  trace.endBlock();
278 
279  return nbok == nb;
280 }
281 
282 bool testImageAdapter()
283 {
284  unsigned int nbok = 0;
285  unsigned int nb = 0;
286 
287  trace.beginBlock ("Testing ImageAdapter");
288 
291 
292  string filename = testPath + "samples/church-small.pgm";
293  VImage image = PNMReader<VImage>::importPGM(filename);
294  trace.info() << "Imported image: " << image << endl;
295 
296  Board2D aBoard;
297 
298  Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
299  aBoard.saveSVG("church.svg");
300 #ifdef WITH_CAIRO
301  aBoard.saveCairo("church.png", Board2D::CairoPNG);
302 #endif
303 
305  BOOST_CONCEPT_ASSERT(( CImage< MyImageAdapter > ));
306 
307  nbok += true ? 1 : 0;
308  nb++;
309 
310  // 1) bell_tower
312  Z2i::Point p1( 43, 107 );
313  Z2i::Point p2( 73, 177 );
314  Z2i::Domain domain_bell_tower( p1, p2 );
315 
316  DefaultFunctor idbtD, idbtV, idbtVm1;
317  MyImageAdapter bell_tower(image, domain_bell_tower, idbtD, idbtV, idbtVm1);
319 
320  trace.info() << "ImageAdapter: " << bell_tower << " " << bell_tower.domain() << std::endl;
321 
322  nbok += bell_tower.isValid() ? 1 : 0;
323  nb++;
324 
325  aBoard.clear();
326  Display2DFactory::drawImage<Gray>(aBoard, bell_tower, (unsigned char)0, (unsigned char)255);
327  aBoard.saveSVG("bell_tower.svg");
328 #ifdef WITH_CAIRO
329  aBoard.saveCairo("bell_tower.png", Board2D::CairoPNG);
330 #endif
331 
332  // 2) cars
333  Z2i::Point p3( 0, 49 );
334  Z2i::Point p4( 58, 72 );
335  Z2i::Domain domain_cars( p3, p4 );
336 
337  DefaultFunctor idcD;
338  DefaultFunctor idcV;
339  DefaultFunctor idcVm1;
340  MyImageAdapter cars(image, domain_cars, idcD, idcV, idcVm1);
341 
342  trace.info() << "ImageAdapter: " << cars << " " << cars.domain() << std::endl;
343 
344  nbok += cars.isValid() ? 1 : 0;
345  nb++;
346 
347  aBoard.clear();
348  Display2DFactory::drawImage<Gray>(aBoard, cars, (unsigned char)0, (unsigned char)255);
349  aBoard.saveSVG("cars.svg");
350 #ifdef WITH_CAIRO
351  aBoard.saveCairo("cars.png", Board2D::CairoPNG);
352 #endif
353 
354  // 3) fill 255 for 'bell_tower' image
355  typename MyImageAdapter::Domain::ConstIterator bt_it = bell_tower.domain().begin();
356  typename MyImageAdapter::Domain::ConstIterator bt_itEnd = bell_tower.domain().end();
357  for (; bt_it != bt_itEnd; ++bt_it)
358  {
359  bell_tower.setValue(*bt_it, 255);
360  }
361 
362  aBoard.clear();
363  Display2DFactory::drawImage<Gray>(aBoard, bell_tower, (unsigned char)0, (unsigned char)255);
364  aBoard.saveSVG("bell_tower_after_filling.svg");
365 #ifdef WITH_CAIRO
366  aBoard.saveCairo("bell_tower_after_filling.png", Board2D::CairoPNG);
367 #endif
368 
369  // 4) fill 55 for 'cars' image
370  typename MyImageAdapter::Domain::ConstIterator c_it = cars.domain().begin();
371  typename MyImageAdapter::Domain::ConstIterator c_itEnd = cars.domain().end();
372  for (; c_it != c_itEnd; ++c_it)
373  {
374  cars.setValue(*c_it, 55);
375  }
376 
377  aBoard.clear();
378  Display2DFactory::drawImage<Gray>(aBoard, cars, (unsigned char)0, (unsigned char)255);
379  aBoard.saveSVG("cars_after_filling.svg");
380 #ifdef WITH_CAIRO
381  aBoard.saveCairo("cars_after_filling.png", Board2D::CairoPNG);
382 #endif
383 
384  // 5) fill 0 (only for one pixel on two) for 'floor_lamp' image
386  Z2i::Point p5( 56, 33 );
387  Z2i::Point p6( 68, 79 );
388  Z2i::Domain domain_floor_lamp( p5, p6 );
389 
390  // --- DigitalSetDomain
391  Z2i::DigitalSet mySet( domain_floor_lamp );
392 
393  unsigned int i = 0;
394  for ( Z2i::Domain::ConstIterator it = domain_floor_lamp.begin() ;
395  it != domain_floor_lamp.end();
396  ++it, ++i )
397  {
398  if (i%2)
399  mySet.insertNew( *it );
400  }
401 
402  DigitalSetDomain<Z2i::DigitalSet> my_specific_domain_floor_lamp(mySet);
403  // --- DigitalSetDomain
404 
405 
406  typedef ImageAdapter<VImage, DigitalSetDomain<Z2i::DigitalSet>, DefaultFunctor, VImage::Value, DefaultFunctor, DefaultFunctor> MyImageAdapter2;
407 
408  DefaultFunctor idflD, idflV, idflVm1;
409  MyImageAdapter2 floor_lamp(image, my_specific_domain_floor_lamp, idflD, idflV, idflVm1);
411 
412  trace.info() << "ImageAdapter: " << floor_lamp << " " << floor_lamp.domain() << std::endl;
413 
414  nbok += floor_lamp.isValid() ? 1 : 0;
415  nb++;
416 
417  aBoard.clear();
418  Display2DFactory::drawImage<Gray>(aBoard, floor_lamp, (unsigned char)0, (unsigned char)255);
419  aBoard.saveSVG("floor_lamp.svg");
420 #ifdef WITH_CAIRO
421  aBoard.saveCairo("floor_lamp.png", Board2D::CairoPNG);
422 #endif
423 
424  typename MyImageAdapter2::Domain::ConstIterator f_it = floor_lamp.domain().begin();
425  typename MyImageAdapter2::Domain::ConstIterator f_itEnd = floor_lamp.domain().end();
426  for (; f_it != f_itEnd; ++f_it)
427  {
428  floor_lamp.setValue(*f_it, 0);
429  }
430 
431  aBoard.clear();
432  Display2DFactory::drawImage<Gray>(aBoard, floor_lamp, (unsigned char)0, (unsigned char)255);
433  aBoard.saveSVG("floor_lamp_after_filling.svg");
434 #ifdef WITH_CAIRO
435  aBoard.saveCairo("floor_lamp_after_filling.png", Board2D::CairoPNG);
436 #endif
437 
438  aBoard.clear();
439  Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
440  aBoard.saveSVG("church_after_filling.svg");
441 #ifdef WITH_CAIRO
442  aBoard.saveCairo("church_after_filling.png", Board2D::CairoPNG);
443 #endif
444 
445  trace.info() << "(" << nbok << "/" << nb << ") "
446  << "true == true" << endl;
447  trace.endBlock();
448 
449  return nbok == nb;
450 }
451 
452 
454 // Standard services - public :
455 
456 int main( int argc, char** argv )
457 {
458  trace.beginBlock ( "Testing class ImageAdapter" );
459  trace.info() << "Args:";
460  for ( int i = 0; i < argc; ++i )
461  trace.info() << " " << argv[ i ];
462  trace.info() << endl;
463 
464  bool res = testSimple() && test_g_f_fm1() && test_range_constRange() && testImageAdapter(); // && ... other tests
465 
466  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
467  trace.endBlock();
468  return res ? 0 : 1;
469 }
470 // //