DGtal  0.9.3
testTiledImage.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 #include "DGtal/images/ImageContainerBySTLVector.h"
36 #include "DGtal/images/ImageFactoryFromImage.h"
37 #include "DGtal/images/TiledImage.h"
38 
39 #include "ConfigTest.h"
41 
42 using namespace std;
43 using namespace DGtal;
44 
46 // Functions for testing class TiledImage.
48 bool testSimple()
49 {
50  unsigned int nbok = 0;
51  unsigned int nb = 0;
52 
53  trace.beginBlock("Testing simple TiledImage");
54 
56  VImage image(Z2i::Domain(Z2i::Point(1,1), Z2i::Point(16,16)));
57 
58  int i = 1;
59  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
60  *it = i++;
61 
62  trace.info() << "ORIGINAL image: " << image << endl;
63 
64  typedef ImageFactoryFromImage<VImage> MyImageFactoryFromImage;
65  typedef MyImageFactoryFromImage::OutputImage OutputImage;
66  MyImageFactoryFromImage imageFactoryFromImage(image);
67 
68  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
69  typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
70  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(imageFactoryFromImage, 2);
71  MyImageCacheWritePolicyWT imageCacheWritePolicyWT(imageFactoryFromImage);
72 
74  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
75  MyTiledImage tiledImage(imageFactoryFromImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWT, 4);
76 
77  typedef MyTiledImage::OutputImage OutputImage;
78  OutputImage::Value aValue;
79 
80  trace.info() << "Read value for Point 4,2: " << tiledImage(Z2i::Point(4,2)) << endl;
81  nbok += (tiledImage(Z2i::Point(4,2)) == 20) ? 1 : 0;
82  nb++;
83 
84  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
85 
86  trace.info() << "Read value for Point 10,6: " << tiledImage(Z2i::Point(10,6)) << endl;
87  nbok += (tiledImage(Z2i::Point(10,6)) == 90) ? 1 : 0;
88  nb++;
89 
90  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
91 
92  aValue = 1; tiledImage.setValue(Z2i::Point(11,7), aValue);
93  trace.info() << "Write value for Point 11,7: " << aValue << endl;
94  nbok += (tiledImage(Z2i::Point(11,7)) == 1) ? 1 : 0;
95  nb++;
96 
97  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
98 
99  trace.info() << "Read value for Point 2,3: " << tiledImage(Z2i::Point(2,3)) << endl;
100  nbok += (tiledImage(Z2i::Point(2,3)) == 34) ? 1 : 0;
101  nb++;
102 
103  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
104 
105  trace.info() << "Read value for Point 16,1: " << tiledImage(Z2i::Point(16,1)) << endl;
106  nbok += (tiledImage(Z2i::Point(16,1)) == 16) ? 1 : 0;
107  nb++;
108 
109  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
110 
111  aValue = 128; tiledImage.setValue(Z2i::Point(16,1), aValue);
112  trace.info() << "Write value for Point 16,1: " << aValue << endl;
113  nbok += (tiledImage(Z2i::Point(16,1)) == 128) ? 1 : 0;
114  nb++;
115 
116  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
117 
118  trace.info() << " Point 16,1 on ORIGINAL image, value: " << image(Z2i::Point(16,1)) << endl;
119  nbok += (image(Z2i::Point(16,1)) == 128) ? 1 : 0;
120  nb++;
121 
122  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
123 
124  trace.endBlock();
125 
126  return nbok == nb;
127 }
128 
129 bool test3d()
130 {
131  unsigned int nbok = 0;
132  unsigned int nb = 0;
133 
134  trace.beginBlock("Testing 3d TiledImage");
135 
137  VImage image(Z3i::Domain(Z3i::Point(1,1,1), Z3i::Point(4,4,4)));
138 
139  int i = 1;
140  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
141  *it = i++;
142 
143  trace.info() << "ORIGINAL image: " << image << endl;
144 
145  typedef ImageFactoryFromImage<VImage> MyImageFactoryFromImage;
146  typedef MyImageFactoryFromImage::OutputImage OutputImage;
147  MyImageFactoryFromImage imageFactoryFromImage(image);
148 
149  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
150  typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
151  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(imageFactoryFromImage, 2);
152  MyImageCacheWritePolicyWT imageCacheWritePolicyWT(imageFactoryFromImage);
153 
155  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
156  MyTiledImage tiledImage(imageFactoryFromImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWT, 4);
157 
158  typedef MyTiledImage::OutputImage OutputImage;
159 
160  trace.info() << "Read value for Point 1,1,1: " << tiledImage(Z3i::Point(1,1,1)) << endl;
161  nbok += (tiledImage(Z3i::Point(1,1,1)) == 1) ? 1 : 0;
162  nb++;
163 
164  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
165 
166  trace.info() << "Read value for Point 4,4,4: " << tiledImage(Z3i::Point(4,4,4)) << endl;
167  nbok += (tiledImage(Z3i::Point(4,4,4)) == 64) ? 1 : 0;
168  nb++;
169 
170  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
171 
172  trace.info() << "Read value for Point 4,3,2: " << tiledImage(Z3i::Point(4,3,2)) << endl;
173  nbok += (tiledImage(Z3i::Point(4,3,2)) == 28) ? 1 : 0;
174  nb++;
175 
176  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
177 
178  trace.endBlock();
179 
180  return nbok == nb;
181 }
182 
184 {
185  unsigned int nbok = 0;
186  unsigned int nb = 0;
187 
188  trace.beginBlock("Testing iterators with TiledImage");
189 
191  VImage image(Z2i::Domain(Z2i::Point(1,1), Z2i::Point(10,10)));
192  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
193  *it = 10;
194  trace.info() << "ORIGINAL image: " << image << endl;
195  typedef ImageFactoryFromImage<VImage> MyImageFactoryFromImage;
196  typedef MyImageFactoryFromImage::OutputImage OutputImage;
197  MyImageFactoryFromImage imageFactoryFromImage(image);
198 
199  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
200  typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
201  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(imageFactoryFromImage, 2);
202  MyImageCacheWritePolicyWT imageCacheWritePolicyWT(imageFactoryFromImage);
203 
205  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
206  MyTiledImage tiledImage(imageFactoryFromImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWT, 4);
207 
208  tiledImage.setValue( Z2i::Point(5,5), 42 );
209  tiledImage.setValue( Z2i::Point(1,1), 1 );
210 
211  // typedefs
213  typedef MyTiledImage::OutputIterator OutputIterator;
214 
215  ConstIterator itbegin = tiledImage.constRange().begin();
216  trace.info() << "Value at range begin (1) = "<< *itbegin << std::endl;
217  nbok += (*itbegin == 1) ? 1 : 0; nb++;
218  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
219 
220  ConstIterator itbegin2 = tiledImage.constRange().begin(Z2i::Point(5,5));
221  trace.info() << "Value at range begin from point (42) = "<< *itbegin2 << std::endl;
222  nbok += (*itbegin2 == 42) ? 1 : 0; nb++;
223  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
224 
225  OutputIterator itbegino = tiledImage.range().begin();
226  trace.info() << "Value at range begin (1) = "<< *itbegino << std::endl;
227  nbok += (*itbegino == 1) ? 1 : 0; nb++;
228  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
229 
230  *itbegino = 5;
231  trace.info() << "Value at range begin after writing (5) = "<< *itbegino << std::endl;
232  nbok += (*itbegino == 5) ? 1 : 0; nb++;
233  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
234 
235  trace.endBlock();
236 
237  return nbok == nb;
238 }
239 
241 {
242  unsigned int nbok = 0;
243  unsigned int nb = 0;
244 
245  trace.beginBlock("Testing range/constRange with TiledImage");
246 
248 
249  VImage image(Z2i::Domain(Z2i::Point(1,1), Z2i::Point(10,10)));
250  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
251  *it = 10;
252 
253  trace.info() << "ORIGINAL image: " << image << endl;
254 
255  typedef ImageFactoryFromImage<VImage> MyImageFactoryFromImage;
256  typedef MyImageFactoryFromImage::OutputImage OutputImage;
257  MyImageFactoryFromImage imageFactoryFromImage(image);
258 
259  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
260  //typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
261  typedef ImageCacheWritePolicyWB<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWB;
262  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(imageFactoryFromImage, 2);
263  //MyImageCacheWritePolicyWT imageCacheWritePolicyWT(imageFactoryFromImage);
264  MyImageCacheWritePolicyWB imageCacheWritePolicyWB(imageFactoryFromImage);
265 
267  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
268  MyTiledImage tiledImage(imageFactoryFromImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWB, 4);
269 
270  // writing values
271  const int maximalValue = tiledImage.domain().size();
272  MyTiledImage::Range::OutputIterator it = tiledImage.range().outputIterator();
273  for (int i = 0; i < maximalValue; ++i)
274  {
275  *it++ = i;
276  }
277 
278  // reading values
279  MyTiledImage::ConstRange r = tiledImage.constRange();
280  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") );
281  cout << endl;
282 
283  std::vector<int> to_vector(100);
284  std::copy(r.begin(), r.end(), to_vector.begin());
285  for (int i = 0; i < 100; i++)
286  {
287  if (to_vector[i]==i)
288  {
289  cout << "ok, ";
290  nbok += true ? 1 : 0; nb++;
291  }
292  else
293  {
294  cout << "!ok -> " << to_vector[i] << ", ";
295  nbok += false ? 1 : 0; nb++;
296  }
297  }
298  cout << endl;
299 
300  cout << endl;
301 
302  // reading values
303  std::copy( r.begin(Z2i::Point(7,7)), r.end(), std::ostream_iterator<int>(cout,", ") );
304  cout << endl;
305 
306  std::vector<int> to_vector2(28);
307  std::copy(r.begin(Z2i::Point(7,7)), r.end(), to_vector2.begin());
308  for (int i = 0; i < 28; i++)
309  {
310  if (to_vector2[i]==i+72)
311  {
312  cout << "ok, ";
313  nbok += true ? 1 : 0; nb++;
314  }
315  else
316  {
317  cout << "!ok -> " << to_vector2[i] << ", ";
318  nbok += false ? 1 : 0; nb++;
319  }
320  }
321  cout << endl;
322 
323  trace.endBlock();
324 
325  return nbok == nb;
326 }
327 
329 // Standard services - public :
330 
331 int main( int argc, char** argv )
332 {
333  trace.beginBlock ( "Testing class TiledImage" );
334  trace.info() << "Args:";
335  for ( int i = 0; i < argc; ++i )
336  trace.info() << " " << argv[ i ];
337  trace.info() << endl;
338 
339  bool res = testSimple() && test3d() && testIterators() && test_range_constRange(); // && ... other tests
340 
341  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
342  trace.endBlock();
343  return res ? 0 : 1;
344 }
345 // //
void beginBlock(const std::string &keyword="")
bool testIterators()
bool test3d()
MyDigitalSurface::ConstIterator ConstIterator
Trace trace
Definition: Common.h:137
Aim: implements a &#39;WB (Write-back or Write-behind)&#39; write policy cache.
Image::ConstRange ConstRange
STL namespace.
double endBlock()
int main(int argc, char **argv)
Aim: implements a tiled image from a "bigger/original" one from an ImageFactory.
Definition: TiledImage.h:76
Aim: implements a factory to produce images from a "bigger/original" one according to a given domain...
std::ostream & emphase()
bool testSimple()
Aim: implements a &#39;WT (Write-through)&#39; write policy cache.
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
Aim: implements a &#39;FIFO&#39; read policy cache.
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
bool test_range_constRange()
Image image(domain)