DGtal  0.9.2
exampleTiledImage.cpp
1 
30 #include <iostream>
32 
33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "ConfigExamples.h"
36 
37 #include "DGtal/io/boards/Board2D.h"
38 
40 #include "DGtal/io/colormaps/HueShadeColorMap.h"
41 
42 #include "DGtal/images/ImageContainerBySTLVector.h"
43 #include "DGtal/images/ImageFactoryFromImage.h"
44 #include "DGtal/images/TiledImage.h"
46 
48 
49 using namespace std;
50 using namespace DGtal;
51 
52 int main( /*int argc, char** argv*/ )
53 {
54  trace.beginBlock("ORIGINAL image");
55 
56  Board2D aBoard;
57 
59  typedef HueShadeColorMap<int> HueShade; // a simple HueShadeColorMap varying on 'int' values
61 
64  VImage image(Z2i::Domain(Z2i::Point(1,1), Z2i::Point(16,16)));
66 
68  int i = 1;
69  for (VImage::Iterator it = image.begin(); it != image.end(); ++it)
70  *it = i++;
72 
73  aBoard.clear();
74  Display2DFactory::drawImage<HueShade>(aBoard, image, (int)0, (int)255);
75  aBoard.saveSVG("tiledImage-image.svg");
76 #ifdef WITH_CAIRO
77  aBoard.saveCairo("tiledImage-image.png", Board2D::CairoPNG);
78 #endif
79 
80  trace.info() << "ORIGINAL image: " << image << endl;
81 
82  trace.endBlock();
83 
84  // ---
85 
86  trace.beginBlock("tiledImage");
87 
89  // here we create an image factory
90  typedef ImageFactoryFromImage<VImage> MyImageFactoryFromImage;
91  typedef MyImageFactoryFromImage::OutputImage OutputImage;
92  MyImageFactoryFromImage imageFactoryFromImage(image);
93 
94  // here we create read and write policies
95  typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
96  typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromImage> MyImageCacheWritePolicyWT;
97  MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(imageFactoryFromImage, 2);
98  MyImageCacheWritePolicyWT imageCacheWritePolicyWT(imageFactoryFromImage);
99 
100  // here we create the TiledImage
102  BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
103  MyTiledImage tiledImage(imageFactoryFromImage, imageCacheReadPolicyFIFO, imageCacheWritePolicyWT, 4);
105 
106  trace.info() << "tiledImage image: " << tiledImage << endl;
107 
108  typedef MyTiledImage::OutputImage OutputImage;
109  /*VImage*/OutputImage::Value aValue;
110 
111  trace.endBlock();
112 
113  // ---
114 
115  /*int cpt, sumcRp, sumcRrp, sumTp, sumTm, sumTrp, sumC;
116 
117  cpt=sumcRp=0;
118  trace.beginBlock("test ConstRange");
119  for(MyTiledImage::ConstRange::ConstIterator tiled_it = tiledImage.constRange().begin(), tiled_itend = tiledImage.constRange().end();
120  tiled_it != tiled_itend; ++tiled_it)
121  {
122  trace.info() << (*tiled_it) << ",";
123  sumcRp += (*tiled_it);
124  cpt++;
125  }
126  trace.info() << "Cpt: " << cpt << " - sumcRp: " << sumcRp << " - cacheMissRead:" << tiledImage.getCacheMissRead() << endl;
127  trace.endBlock();
128 
129  cpt=sumcRrp=0; tiledImage.clearCacheAndResetCacheMisses();
130  trace.beginBlock("test ConstRange (reverse)");
131  for(MyTiledImage::ConstRange::ConstReverseIterator rtiled_it = tiledImage.constRange().rbegin(), rtiled_itend = tiledImage.constRange().rend();
132  rtiled_it != rtiled_itend; ++rtiled_it)
133  {
134  trace.info() << (*rtiled_it) << ",";
135  sumcRrp += (*rtiled_it);
136  cpt++;
137  }
138  trace.info() << "Cpt: " << cpt << " - sumcRrp: " << sumcRrp << " - cacheMissRead:" << tiledImage.getCacheMissRead() << endl;
139  trace.endBlock();
140 
141  // ---
142 
143  cpt=sumTp=0; tiledImage.clearCacheAndResetCacheMisses();
144  trace.beginBlock("test TiledIterator (++)");
145  for(MyTiledImage::TiledIterator tiled_it = tiledImage.begin(), tiled_itend = tiledImage.end();
146  tiled_it != tiled_itend; ++tiled_it)
147  {
148  trace.info() << (*tiled_it) << ",";
149  sumTp += (*tiled_it);
150  cpt++;
151  }
152  trace.info() << "Cpt: " << cpt << " - sumTp: " << sumTp << " - cacheMissRead:" << tiledImage.getCacheMissRead() << endl;
153  trace.endBlock();
154 
155  cpt=sumTm=0; tiledImage.clearCacheAndResetCacheMisses();
156  trace.beginBlock("test TiledIterator (--)");
157  {
158  MyTiledImage::TiledIterator tiled_it = tiledImage.end(), tiled_itend = tiledImage.begin();
159  do
160  {
161  --tiled_it;
162 
163  trace.info() << (*tiled_it) << ",";
164  sumTm += (*tiled_it);
165  cpt++;
166  }
167  while (tiled_it != tiled_itend);
168  }
169  trace.info() << "Cpt: " << cpt << " - sumTm: " << sumTm << " - cacheMissRead:" << tiledImage.getCacheMissRead() << endl;
170  trace.endBlock();
171 
172  // ---
173 
174  cpt=sumTrp=0; tiledImage.clearCacheAndResetCacheMisses();
175  trace.beginBlock("test ReverseTiledIterator (++)");
176  for(MyTiledImage::ReverseTiledIterator rtiled_it = tiledImage.rbegin(), rtiled_itend = tiledImage.rend();
177  rtiled_it != rtiled_itend; ++rtiled_it)
178  {
179  trace.info() << (*rtiled_it) << ",";
180  sumTrp += (*rtiled_it);
181  cpt++;
182  }
183  trace.info() << "Cpt: " << cpt << " - sumTrp: " << sumTrp << " - cacheMissRead:" << tiledImage.getCacheMissRead() << endl;
184  trace.endBlock();
185 
186  // ---
187 
188  tiledImage.clearCacheAndResetCacheMisses();
189  trace.beginBlock("test Range (writing)");
190  {
191  const int maximalValue = tiledImage.domain().size();
192  MyTiledImage::Range::OutputIterator it = tiledImage.range().outputIterator();
193  for (int ii = 0; ii < maximalValue; ++ii)
194  {
195  *it++ = 10; // TODO : don't work with WT
196  //it.setValue(10); it++;
197  }
198  }
199  trace.endBlock();
200 
201  // ---
202 
203  cpt=sumC=0;
204  trace.beginBlock("test ConstIterator");
205  for(VImage::Domain::ConstIterator it = tiledImage.domain().begin(), itend = tiledImage.domain().end();
206  it != itend; ++it)
207  {
208  trace.info() << tiledImage(*it) << ",";
209  sumC += tiledImage(*it);
210  cpt++;
211  }
212  trace.info() << "Cpt: " << cpt << " - sumC: " << sumC << endl;
213  trace.endBlock();*/
214 
215  // ---
216 
217  trace.beginBlock("read and write on MyTiledImage");
218 
220  trace.info() << "Read value for Point 4,2: " << tiledImage(Z2i::Point(4,2)) << endl;
222 
224  trace.info() << "Read value for Point 10,6: " << tiledImage(Z2i::Point(10,6)) << endl;
226 
228  aValue = 1; tiledImage.setValue(Z2i::Point(11,7), aValue);
229  trace.info() << "Write value for Point 11,7: " << aValue << endl;
231 
233  trace.info() << "Read value for Point 2,3: " << tiledImage(Z2i::Point(2,3)) << endl;
235 
237  trace.info() << "Read value for Point 16,1: " << tiledImage(Z2i::Point(16,1)) << endl;
239 
241  aValue = 128; tiledImage.setValue(Z2i::Point(16,1), aValue);
242  trace.info() << "Write value for Point 16,1: " << aValue << endl;
244 
245  trace.info() << " Point 16,1 on ORIGINAL image, value: " << image(Z2i::Point(16,1)) << endl;
246 
247  aBoard.clear();
248  Display2DFactory::drawImage<HueShade>(aBoard, image, (int)0, (int)255);
249  aBoard.saveSVG("tiledImage-image2.svg");
250 #ifdef WITH_CAIRO
251  aBoard.saveCairo("tiledImage-image2.png", Board2D::CairoPNG);
252 #endif
253 
254  trace.endBlock();
255 
256  return 0;
257 }
258 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
STL namespace.
double endBlock()
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
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...
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
Aim: implements a 'WT (Write-through)' write policy cache.
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
Aim: implements a 'FIFO' read policy cache.
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70