DGtal  1.2.0
testImageCache.cpp
Go to the documentation of this file.
1 
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/ImageFactoryFromImage.h"
39 #include "DGtal/images/ImageCache.h"
40 
41 #include "ConfigTest.h"
43 
44 using namespace std;
45 using namespace DGtal;
46 
48 // Functions for testing class ImageCache.
50 bool testSimple()
51 {
52  unsigned int nbok = 0;
53  unsigned int nb = 0;
54 
55  trace.beginBlock("Testing simple ImageCache");
56 
58 
59  VImage image(Z2i::Domain(Z2i::Point(0,0), Z2i::Point(3,3)));
60  int i = 1;
61  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
62  *it = i++;
63 
64  trace.info() << "ORIGINAL image: " << image << endl;
65 
66  // 1) ImageFactoryFromImage
67  typedef ImageFactoryFromImage<VImage > MyImageFactoryFromImage;
68  MyImageFactoryFromImage factImage(image);
69 
70  typedef MyImageFactoryFromImage::OutputImage OutputImage;
71 
72  Z2i::Domain domain1(Z2i::Point(0,0), Z2i::Point(1,1));
73  OutputImage *image1 = factImage.requestImage(domain1);
74  OutputImage::ConstRange r1 = image1->constRange();
75  cout << "image1: "; std::copy( r1.begin(), r1.end(), std::ostream_iterator<int>(cout,", ") ); cout << endl;
76 
77  Z2i::Domain domain1b(Z2i::Point(0,0), Z2i::Point(1,2));
78  OutputImage *image1b = factImage.requestImage(domain1b);
79  OutputImage::ConstRange r1b = image1b->constRange();
80  cout << "image1b: "; std::copy( r1b.begin(), r1b.end(), std::ostream_iterator<int>(cout,", ") ); cout << endl;
81 
82  Z2i::Domain domain2(Z2i::Point(2,0), Z2i::Point(3,1));
83  OutputImage *image2 = factImage.requestImage(domain2);
84  OutputImage::ConstRange r2 = image2->constRange();
85  cout << "image2: "; std::copy( r2.begin(), r2.end(), std::ostream_iterator<int>(cout,", ") ); cout << endl;
86 
87  Z2i::Domain domain3(Z2i::Point(0,2), Z2i::Point(1,3));
88  OutputImage *image3 = factImage.requestImage(domain3);
89  OutputImage::ConstRange r3 = image3->constRange();
90  cout << "image3: "; std::copy( r3.begin(), r3.end(), std::ostream_iterator<int>(cout,", ") ); cout << endl;
91 
92  Z2i::Domain domain4(Z2i::Point(2,2), Z2i::Point(3,3));
93  OutputImage *image4 = factImage.requestImage(domain4);
94  OutputImage::ConstRange r4 = image4->constRange();
95  cout << "image4: "; std::copy( r4.begin(), r4.end(), std::ostream_iterator<int>(cout,", ") ); cout << endl;
96 
97  // 2) ImageCache with DGtal::CACHE_READ_POLICY_LAST, DGtal::CACHE_WRITE_POLICY_WT
98  trace.info() << endl << "ImageCache with DGtal::CACHE_READ_POLICY_LAST, DGtal::CACHE_WRITE_POLICY_WT" << endl;
99 
100  typedef ImageCacheReadPolicyLAST<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyLAST;
101  typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
102  MyImageCacheReadPolicyLAST imageCacheReadPolicyLAST(factImage);
103  MyImageCacheWritePolicyWT imageCacheWritePolicyWT(factImage);
104 
106  MyImageCache imageCache(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWT);
107  OutputImage::Value aValue;
108 
109  trace.info() << "READING from cache (empty cache): " << imageCache << endl;
110  if (imageCache.read(Z2i::Point(2,2), aValue))
111  trace.info() << "READ: Point 2,2 is in an image from cache, value: " << aValue << endl;
112  else
113  trace.info() << "READ: Point 2,2 is not in an image from cache." << endl;
114  nbok += (imageCache.read(Z2i::Point(2,2), aValue) == false) ? 1 : 0;
115  nb++;
116 
117  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
118 
119  imageCache.update(domain1); // image1
120 
121  trace.info() << "READING from cache (not empty but wrong domain): " << imageCache << endl;
122  if (imageCache.read(Z2i::Point(2,2), aValue))
123  trace.info() << "READ: Point 2,2 is in an image from cache, value: " << aValue << endl;
124  else
125  trace.info() << "READ: Point 2,2 is not in an image from cache." << endl;
126  nbok += (imageCache.read(Z2i::Point(2,2), aValue) == false) ? 1 : 0;
127  nb++;
128 
129  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
130 
131  imageCache.update(domain4); // image4
132 
133  trace.info() << "READING from cache (not empty but good domain): " << imageCache << endl;
134  if (imageCache.read(Z2i::Point(2,2), aValue))
135  trace.info() << "READ: Point 2,2 is in an image from cache, value: " << aValue << endl;
136  else
137  trace.info() << "READ: Point 2,2 is not in an image from cache." << endl;
138  nbok += ( (imageCache.read(Z2i::Point(2,2), aValue) && (aValue == 11)) == true ) ? 1 : 0;
139  nb++;
140 
141  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
142 
143  trace.info() << "WRITING from cache (not empty but good domain): " << imageCache << endl;
144  aValue = 22;
145  if (imageCache.write(Z2i::Point(2,2), aValue))
146  trace.info() << "WRITE: Point 2,2 is in an image from cache, value: " << aValue << endl;
147  else
148  trace.info() << "WRITE: Point 2,2 is not in an image from cache." << endl;
149  nbok += ( (imageCache.read(Z2i::Point(2,2), aValue) && (aValue == 22)) == true ) ? 1 : 0;
150  nb++;
151 
152  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
153 
154  trace.info() << " AFTER WRITING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
155  nbok += (image(Z2i::Point(2,2)) == 22) ? 1 : 0;
156  nb++;
157 
158  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
159 
160  imageCache.update(domain3); // image3
161 
162  trace.info() << "WRITING from cache (not empty but wrong domain): " << imageCache << endl;
163  aValue = 22;
164  if (imageCache.write(Z2i::Point(2,2), aValue))
165  trace.info() << "WRITE: Point 2,2 is in an image from cache, value: " << aValue << endl;
166  else
167  trace.info() << "WRITE: Point 2,2 is not in an image from cache." << endl;
168  nbok += (imageCache.read(Z2i::Point(2,2), aValue) == false) ? 1 : 0;
169  nb++;
170 
171  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
172 
173  imageCache.update(domain1); // image1
174 
175  trace.info() << "WRITING from cache (not empty but good domain): " << imageCache << endl;
176  aValue = 7;
177  if (imageCache.write(Z2i::Point(0,0), aValue))
178  trace.info() << "WRITE: Point 0,0 is in an image from cache, value: " << aValue << endl;
179  else
180  trace.info() << "WRITE: Point 0,0 is not in an image from cache." << endl;
181  nbok += ( (imageCache.read(Z2i::Point(0,0), aValue) && (aValue == 7)) == true ) ? 1 : 0;
182  nb++;
183 
184  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
185 
186  trace.info() << " AFTER WRITING: Point 0,0 on ORIGINAL image, value: " << image(Z2i::Point(0,0)) << endl;
187  nbok += (image(Z2i::Point(0,0)) == 7) ? 1 : 0;
188  nb++;
189 
190  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
191 
192  // 3) ImageCache with DGtal::CACHE_READ_POLICY_LAST, DGtal::CACHE_WRITE_POLICY_WB
193  i = 1; // reinit image
194  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
195  *it = i++;
196 
197  trace.info() << endl << "ImageCache with DGtal::CACHE_READ_POLICY_LAST, DGtal::CACHE_WRITE_POLICY_WB" << endl;
198 
199  typedef ImageCacheWritePolicyWB<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWB;
200  MyImageCacheWritePolicyWB imageCacheWritePolicyWB(factImage);
201 
203  MyImageCache2 imageCache2(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWB);
204 
205  imageCache2.update(domain4); // image4
206 
207  trace.info() << "WRITING from cache (not empty but good domain): " << imageCache2 << endl;
208  aValue = 22;
209  if (imageCache2.write(Z2i::Point(2,2), aValue))
210  trace.info() << "WRITE: Point 2,2 is in an image from cache, value: " << aValue << endl;
211  else
212  trace.info() << "WRITE: Point 2,2 is not in an image from cache." << endl;
213  nbok += ( (imageCache2.read(Z2i::Point(2,2), aValue) && (aValue == 22)) == true ) ? 1 : 0;
214  nb++;
215 
216  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
217 
218  trace.info() << " AFTER WRITING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
219  nbok += (image(Z2i::Point(2,2)) == 11) ? 1 : 0;
220  nb++;
221 
222  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
223 
224  imageCache2.update(domain3); // image3 - so flush domain4 (image4)
225 
226  trace.info() << " AFTER FLUSHING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
227  nbok += (image(Z2i::Point(2,2)) == 22) ? 1 : 0;
228  nb++;
229 
230  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
231 
232  // 4) ImageCache with DGtal::CACHE_READ_POLICY_FIFO, DGtal::CACHE_WRITE_POLICY_WB
233  i = 1; // reinit image
234  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
235  *it = i++;
236 
237  trace.info() << endl << "ImageCache with DGtal::CACHE_READ_POLICY_FIFO, DGtal::CACHE_WRITE_POLICY_WB" << endl;
238 
239  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
240  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(factImage, 3);
241 
243  MyImageCache3 imageCache3(factImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWB);
244 
245  imageCache3.update(domain4); // image4
246 
247  trace.info() << "WRITING from cache (not empty but good domain): " << imageCache3 << endl;
248  aValue = 22;
249  if (imageCache3.write(Z2i::Point(2,2), aValue))
250  trace.info() << "WRITE: Point 2,2 is in an image from cache, value: " << aValue << endl;
251  else
252  trace.info() << "WRITE: Point 2,2 is not in an image from cache." << endl;
253  nbok += ( (imageCache3.read(Z2i::Point(2,2), aValue) && (aValue == 22)) == true ) ? 1 : 0;
254  nb++;
255 
256  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
257 
258  trace.info() << " AFTER WRITING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
259  nbok += (image(Z2i::Point(2,2)) == 11) ? 1 : 0;
260  nb++;
261 
262  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
263 
264  imageCache3.update(domain3); // image3
265 
266  trace.info() << " AFTER FLUSHING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
267  nbok += (image(Z2i::Point(2,2)) == 11) ? 1 : 0;
268  nb++;
269 
270  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
271 
272  imageCache3.update(domain2); // image2
273 
274  trace.info() << " AFTER FLUSHING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
275  nbok += (image(Z2i::Point(2,2)) == 11) ? 1 : 0;
276  nb++;
277 
278  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
279 
280  imageCache3.update(domain1); // image1 - so flush domain4 (image4)
281 
282  trace.info() << " AFTER FLUSHING: Point 2,2 on ORIGINAL image, value: " << image(Z2i::Point(2,2)) << endl;
283  nbok += (image(Z2i::Point(2,2)) == 22) ? 1 : 0;
284  nb++;
285 
286  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
287 
288  trace.endBlock();
289 
290  return nbok == nb;
291 }
292 
294 // Standard services - public :
295 
296 int main( int argc, char** argv )
297 {
298  trace.beginBlock ( "Testing class ImageCache" );
299  trace.info() << "Args:";
300  for ( int i = 0; i < argc; ++i )
301  trace.info() << " " << argv[ i ];
302  trace.info() << endl;
303 
304  bool res = testSimple(); // && ... other tests
305 
306  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
307  trace.endBlock();
308  return res ? 0 : 1;
309 }
310 // //
Aim: implements a 'FIFO' read policy cache.
Aim: implements a 'LAST' read policy cache.
Aim: implements a 'WB (Write-back or Write-behind)' write policy cache.
Aim: implements a 'WT (Write-through)' write policy cache.
Aim: implements an images cache with 'read and write' policies.
Definition: ImageCache.h:78
Aim: implements a factory to produce images from a "bigger/original" one according to a given domain.
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
bool testSimple()
int main(int argc, char **argv)
Image image(domain)
Image::ConstRange ConstRange