DGtal 1.4.0
Loading...
Searching...
No Matches
exampleImageFactoryFromHDF5.cpp
Go to the documentation of this file.
1
40#include <iostream>
41#include "DGtal/base/Common.h"
42#include "DGtal/helpers/StdDefs.h"
43#include "DGtal/images/ImageSelector.h"
44#include "DGtal/images/ImageFactoryFromImage.h"
45#include "DGtal/images/ImageFactoryFromHDF5.h"
46#include "DGtal/images/ImageCache.h"
47#include "DGtal/images/TiledImage.h"
48
49#include "ConfigExamples.h"
51
52using namespace std;
53using namespace DGtal;
54
56
57#define H5FILE_NAME_3D_TILED "exampleImageFactoryFromHDF5_TILED_3D.h5"
58
59#define DATASETNAME_3D "UInt8Array3D"
60#define NX_3D_TILED 300//1980 // dataset dimensions
61#define NY_3D_TILED 300//1980
62#define NZ_3D_TILED 300//400
63#define CHUNK_X_3D_TILED 50 // chunk dimensions
64#define CHUNK_Y_3D_TILED 50
65#define CHUNK_Z_3D_TILED 50
66#define RANK_3D_TILED 3
67
69{
70 trace.beginBlock("Example : writeHDF5_3D (3D)");
71
72 trace.info() << "begin" << endl;
73
74 hid_t file, dataset; // file and dataset handles
75 hid_t datatype, dataspace; // handles
76 hsize_t dimsf[RANK_3D_TILED]; // dataset dimensions
77 herr_t status;
78 //DGtal::uint8_t data[NZ_3D_TILED][NY_3D_TILED][NX_3D_TILED]; // data to write
79 DGtal::uint8_t *data;
80 int i, j, k;
81
82 // compressed dataset
83 hid_t plist_id;
84 hsize_t cdims[RANK_3D_TILED];
85 // compressed dataset
86
88 if (data == NULL)
89 {
90 trace.error() << " malloc error" << endl;
91 return false;
92 }
93
94 // Data and output buffer initialization.
95 for(k = 0; k < NZ_3D_TILED; k++)
96 for(j = 0; j < NY_3D_TILED; j++)
97 for(i = 0; i < NX_3D_TILED; i++)
98 if (i>=15 && j>=15 && k>=15 && i<35 && j<35 && k<35)
99 data[k*NY_3D_TILED*NX_3D_TILED + j*NX_3D_TILED + i] = 1;
100 else
101 data[k*NY_3D_TILED*NX_3D_TILED + j*NX_3D_TILED + i] = 0;
102
103 /*
104 * Create a new file using H5F_ACC_TRUNC access,
105 * default file creation properties, and default file
106 * access properties.
107 */
108 file = H5Fcreate(H5FILE_NAME_3D_TILED, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
109
110 // Describe the size of the array and create the data space for fixed size dataset.
111 dimsf[0] = NZ_3D_TILED;
112 dimsf[1] = NY_3D_TILED;
113 dimsf[2] = NX_3D_TILED;
114 dataspace = H5Screate_simple(RANK_3D_TILED, dimsf, NULL);
115
116 // compressed dataset
117 plist_id = H5Pcreate (H5P_DATASET_CREATE);
118
119 // Dataset must be chunked for compression.
120 cdims[0] = CHUNK_Z_3D_TILED;
121 cdims[1] = CHUNK_Y_3D_TILED;
122 cdims[2] = CHUNK_X_3D_TILED;
123 status = H5Pset_chunk (plist_id, RANK_3D_TILED, cdims);
124 if (status)
125 {
126 trace.error() << " H5Dchunck error" << std::endl;
127 free(data);
128 return false;
129 }
130
131
132 // --> Compression levels :
133 // 0 No compression
134 // 1 Best compression speed; least compression
135 // 2 through 8 Compression improves; speed degrades
136 // 9 Best compression ratio; slowest speed
137 //
138 // Set ZLIB / DEFLATE Compression using compression level 6.
139 status = H5Pset_deflate (plist_id, 6);
140 if (status)
141 {
142 trace.error() << " H5Ddeflate error" << std::endl;
143 free(data);
144 return false;
145 }
146 // compressed dataset
147
148 /*
149 * Define datatype for the data in the file.
150 */
151 datatype = H5Tcopy(H5T_NATIVE_UINT8);
152 status = H5Tset_order(datatype, H5T_ORDER_LE);
153 if (status)
154 {
155 trace.error() << " H5Dsetorder error" << std::endl;
156 free(data);
157 return false;
158 }
159
160 /*
161 * Create a new dataset within the file using defined dataspace and
162 * datatype and default dataset creation properties.
163 */
164 dataset = H5Dcreate2(file, DATASETNAME_3D, datatype, dataspace,
165 H5P_DEFAULT, /*H5P_DEFAULT*/plist_id, H5P_DEFAULT); // here to activate compressed dataset
166
167 // Write the data to the dataset using default transfer properties.
168 status = H5Dwrite(dataset, H5T_NATIVE_UINT8, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
169 if (status)
170 {
171 trace.error() << " H5Dwrite error" << std::endl;
172 free(data);
173 return false;
174 }
175
176 // Close/release resources.
177 H5Sclose(dataspace);
178 H5Tclose(datatype);
179 H5Dclose(dataset);
180 // compressed dataset
181 H5Pclose(plist_id);
182 // compressed dataset
183 H5Fclose(file);
184
185 free(data);
186
187 trace.info() << "end" << endl;
188
189 trace.endBlock();
190 trace.info() << endl;
191
192 return true;
193}
194
196{
197 int i, j, k;
198
199 trace.beginBlock("Example : ImageV (3D)");
200
202 typedef ImageV::Domain DomainV;
203 typedef ImageV::Point PointV;
204 DomainV domainV(PointV(0,0,0), PointV(NX_3D_TILED-1, NY_3D_TILED-1, NZ_3D_TILED-1));
205 ImageV imageV(domainV);
206
207 for(k = 0; k < NZ_3D_TILED; k++)
208 for(j = 0; j < NY_3D_TILED; j++)
209 for(i = 0; i < NX_3D_TILED; i++)
210 if (i>=15 && j>=15 && k>=15 && i<35 && j<35 && k<35)
211 imageV.setValue(PointV(i,j,k), 1);
212 else
213 imageV.setValue(PointV(i,j,k), 0);
214
215 int cpt=0;
216 trace.beginBlock("Counting ones in the original image (domain)");
217 for(ImageV::Domain::ConstIterator it = imageV.domain().begin(), itend = imageV.domain().end();
218 it != itend; ++it)
219 if (imageV( *it ) == 1)
220 cpt++;
221 trace.info() << "imageV cpt: " << cpt << endl;
222 trace.endBlock();
223
224 cpt=0;
225 trace.beginBlock("Counting ones in the original image (range)");
226 for(ImageV::Range::ConstIterator it = imageV.range().begin(), itend = imageV.range().end();
227 it != itend; ++it)
228 if (( *it ) == 1)
229 cpt++;
230 trace.info() << "imageV cpt: " << cpt << endl;
231 trace.endBlock();
232
233 trace.endBlock();
234 trace.info() << endl;
235
236 return true;
237}
238
240{
241 int i, j, k;
242
243 trace.beginBlock("Example : TiledImage with ImageFactoryFromImage - 1 block (3D)");
244
246 typedef ImageV::Domain DomainV;
247 typedef ImageV::Point PointV;
248 DomainV domainV(PointV(0,0,0), PointV(NX_3D_TILED-1, NY_3D_TILED-1, NZ_3D_TILED-1));
249 ImageV imageV(domainV);
250
251 for(k = 0; k < NZ_3D_TILED; k++)
252 for(j = 0; j < NY_3D_TILED; j++)
253 for(i = 0; i < NX_3D_TILED; i++)
254 if (i>=15 && j>=15 && k>=15 && i<35 && j<35 && k<35)
255 imageV.setValue(PointV(i,j,k), 1);
256 else
257 imageV.setValue(PointV(i,j,k), 0);
258
259 typedef ImageFactoryFromImage<ImageV> MyImageFactoryFromImage;
260 MyImageFactoryFromImage factImage(imageV);
261
262 typedef MyImageFactoryFromImage::OutputImage OutputImage;
263
264 //typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromImage> MyImageCacheReadPolicyFIFO;
267 //MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(factImage, 3);
268 MyImageCacheReadPolicyLAST imageCacheReadPolicyLAST(factImage);
269 MyImageCacheWritePolicyWT imageCacheWritePolicyWT(factImage);
270
272 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
273 MyTiledImage tiledImage1block(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWT, 1);
274
275 typedef MyTiledImage::OutputImage OutputImage;
276 //OutputImage::Value aValue;
277
278 trace.info() << tiledImage1block << std::endl;
279
280 int cpt=0;
281 trace.beginBlock("Counting ones in the tiled image - 1x1x1 blocks - LAST/WT - TiledIterator");
282 for(MyTiledImage::TiledIterator it = tiledImage1block.begin(), itend = tiledImage1block.end();
283 it != itend; ++it)
284 if ((*it) == 1)
285 cpt++;
286 trace.info() << "Cpt: " << cpt << " - cacheMissRead:" << tiledImage1block.getCacheMissRead() << " - cacheMissWrite:" << tiledImage1block.getCacheMissWrite() << endl;
287 trace.endBlock();
288
289 cpt=0; tiledImage1block.clearCacheAndResetCacheMisses();
290 trace.beginBlock("Counting ones in the tiled image - 1x1x1 blocks - LAST/WT");
291 for(ImageV::Domain::ConstIterator it = tiledImage1block.domain().begin(), itend = tiledImage1block.domain().end();
292 it != itend; ++it)
293 if (tiledImage1block(*it) == 1)
294 cpt++;
295 trace.info() << "Cpt: " << cpt << " - cacheMissRead:" << tiledImage1block.getCacheMissRead() << " - cacheMissWrite:" << tiledImage1block.getCacheMissWrite() << endl;
296 trace.endBlock();
297
298 trace.endBlock();
299 trace.info() << endl;
300
301 return true;
302}
303
305{
306 trace.beginBlock("Example : TiledImage with ImageFactoryFromHDF5 - 1 block (3D)");
307
309
310 typedef ImageFactoryFromHDF5<Image> MyImageFactoryFromHDF5;
311 MyImageFactoryFromHDF5 factImage(H5FILE_NAME_3D_TILED, DATASETNAME_3D);
312
313 typedef MyImageFactoryFromHDF5::OutputImage OutputImage;
314
315 //typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromHDF5> MyImageCacheReadPolicyFIFO;
316 typedef ImageCacheReadPolicyLAST<OutputImage, MyImageFactoryFromHDF5> MyImageCacheReadPolicyLAST;
317 typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromHDF5> MyImageCacheWritePolicyWT;
318 //MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO(factImage, 3);
319 MyImageCacheReadPolicyLAST imageCacheReadPolicyLAST(factImage);
320 MyImageCacheWritePolicyWT imageCacheWritePolicyWT(factImage);
321
323 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
324 MyTiledImage tiledImage1block(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWT, 1);
325
326 typedef MyTiledImage::OutputImage OutputImage;
327 //OutputImage::Value aValue;
328
329 trace.info() << tiledImage1block << std::endl;
330
331 int cpt=0;
332 trace.beginBlock("Counting ones in the tiled image - 1x1x1 blocks - LAST/WT - TiledIterator");
333 for(MyTiledImage::TiledIterator it = tiledImage1block.begin(), itend = tiledImage1block.end();
334 it != itend; ++it)
335 if ((*it) == 1)
336 cpt++;
337 trace.info() << "Cpt: " << cpt << " - cacheMissRead:" << tiledImage1block.getCacheMissRead() << " - cacheMissWrite:" << tiledImage1block.getCacheMissWrite() << endl;
338 trace.endBlock();
339
340 cpt=0; tiledImage1block.clearCacheAndResetCacheMisses();
341 trace.beginBlock("Counting ones in the tiled image - 1x1x1 blocks - LAST/WT");
342 for(Image::Domain::ConstIterator it = tiledImage1block.domain().begin(), itend = tiledImage1block.domain().end();
343 it != itend; ++it)
344 if (tiledImage1block(*it) == 1)
345 cpt++;
346 trace.info() << "Cpt: " << cpt << " - cacheMissRead:" << tiledImage1block.getCacheMissRead() << " - cacheMissWrite:" << tiledImage1block.getCacheMissWrite() << endl;
347 trace.endBlock();
348
349 trace.endBlock();
350 trace.info() << endl;
351
352 return true;
353}
354
356{
357 int i, j, k;
358
359 trace.beginBlock("Example : TiledImage with ImageFactoryFromImage - 10 blocks (3D)");
360
362 typedef ImageV::Domain DomainV;
363 typedef ImageV::Point PointV;
364 DomainV domainV(PointV(0,0,0), PointV(NX_3D_TILED-1, NY_3D_TILED-1, NZ_3D_TILED-1));
365 ImageV imageV(domainV);
366
367 for(k = 0; k < NZ_3D_TILED; k++)
368 for(j = 0; j < NY_3D_TILED; j++)
369 for(i = 0; i < NX_3D_TILED; i++)
370 if (i>=15 && j>=15 && k>=15 && i<35 && j<35 && k<35)
371 imageV.setValue(PointV(i,j,k), 1);
372 else
373 imageV.setValue(PointV(i,j,k), 0);
374
375 typedef ImageFactoryFromImage<ImageV> MyImageFactoryFromImage;
376 MyImageFactoryFromImage factImage(imageV);
377
378 typedef MyImageFactoryFromImage::OutputImage OutputImage;
379
383 MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO_A(factImage, 10);
384 MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO_B(factImage, 5);
385 MyImageCacheReadPolicyLAST imageCacheReadPolicyLAST(factImage);
386 MyImageCacheWritePolicyWT imageCacheWritePolicyWT(factImage);
387
389 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
390 MyTiledImage tiledImage(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWT, 10);
391
393 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImageFIFO > ));
394 MyTiledImageFIFO tiledImageFIFO_A(factImage, imageCacheReadPolicyFIFO_A, imageCacheWritePolicyWT, 10);
395 MyTiledImageFIFO tiledImageFIFO_B(factImage, imageCacheReadPolicyFIFO_B, imageCacheWritePolicyWT, 10);
396
397 typedef MyTiledImage::OutputImage OutputImage;
398 //OutputImage::Value aValue;
399
400 int cpt=0;
401 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(10)/WT - TiledIterator");
402 for(MyTiledImageFIFO::TiledIterator it = tiledImageFIFO_A.begin(), itend = tiledImageFIFO_A.end();
403 it != itend; ++it)
404 {
405 if ((*it) == 1)
406 cpt++;
407 }
408 trace.endBlock();
409
410 cpt=0; tiledImageFIFO_A.clearCacheAndResetCacheMisses();
411 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(10)/WT");
412 for(ImageV::Domain::ConstIterator it = tiledImageFIFO_A.domain().begin(), itend = tiledImageFIFO_A.domain().end();
413 it != itend; ++it)
414 {
415 if (tiledImageFIFO_A(*it) == 1)
416 cpt++;
417 }
418 trace.endBlock();
419
420 // ---
421
422 cpt=0;
423 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(5)/WT - TiledIterator");
424 for(MyTiledImageFIFO::TiledIterator it = tiledImageFIFO_B.begin(), itend = tiledImageFIFO_B.end();
425 it != itend; ++it)
426 {
427 if ((*it) == 1)
428 cpt++;
429 }
430 trace.endBlock();
431
432 cpt=0; tiledImageFIFO_B.clearCacheAndResetCacheMisses();
433 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(5)/WT");
434 for(ImageV::Domain::ConstIterator it = tiledImageFIFO_B.domain().begin(), itend = tiledImageFIFO_B.domain().end();
435 it != itend; ++it)
436 {
437 if (tiledImageFIFO_B(*it) == 1)
438 cpt++;
439 }
440 trace.endBlock();
441
442 // ---
443
444 cpt=0;
445 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - LAST/WT - TiledIterator");
446 for(MyTiledImage::TiledIterator it = tiledImage.begin(), itend = tiledImage.end();
447 it != itend; ++it)
448 {
449 if ((*it) == 1)
450 cpt++;
451 }
452 trace.endBlock();
453
454 cpt=0; tiledImage.clearCacheAndResetCacheMisses();
455 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - LAST/WT");
456 for(ImageV::Domain::ConstIterator it = tiledImage.domain().begin(), itend = tiledImage.domain().end();
457 it != itend; ++it)
458 {
459 if (tiledImage(*it) == 1)
460 cpt++;
461 }
462 trace.endBlock();
463
464 trace.endBlock();
465 trace.info() << endl;
466
467 return true;
468}
469
471{
472 trace.beginBlock("Example : TiledImage with ImageFactoryFromHDF5 - 10 blocks (3D)");
473
475
476 typedef ImageFactoryFromHDF5<Image> MyImageFactoryFromHDF5;
477 MyImageFactoryFromHDF5 factImage(H5FILE_NAME_3D_TILED, DATASETNAME_3D);
478
479 typedef MyImageFactoryFromHDF5::OutputImage OutputImage;
480
481 typedef ImageCacheReadPolicyFIFO<OutputImage, MyImageFactoryFromHDF5> MyImageCacheReadPolicyFIFO;
482 typedef ImageCacheReadPolicyLAST<OutputImage, MyImageFactoryFromHDF5> MyImageCacheReadPolicyLAST;
483 typedef ImageCacheWritePolicyWT<OutputImage, MyImageFactoryFromHDF5> MyImageCacheWritePolicyWT;
484 MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO_A(factImage, 10);
485 MyImageCacheReadPolicyFIFO imageCacheReadPolicyFIFO_B(factImage, 5);
486 MyImageCacheReadPolicyLAST imageCacheReadPolicyLAST(factImage);
487 MyImageCacheWritePolicyWT imageCacheWritePolicyWT(factImage);
488
490 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImage > ));
491 MyTiledImage tiledImage(factImage, imageCacheReadPolicyLAST, imageCacheWritePolicyWT, 10);
492
494 BOOST_CONCEPT_ASSERT(( concepts::CImage< MyTiledImageFIFO > ));
495 MyTiledImageFIFO tiledImageFIFO_A(factImage, imageCacheReadPolicyFIFO_A, imageCacheWritePolicyWT, 10);
496 MyTiledImageFIFO tiledImageFIFO_B(factImage, imageCacheReadPolicyFIFO_B, imageCacheWritePolicyWT, 10);
497
498 typedef MyTiledImage::OutputImage OutputImage;
499 //OutputImage::Value aValue;
500
501 int cpt=0;
502 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(10)/WT - TiledIterator");
503 for(MyTiledImageFIFO::TiledIterator it = tiledImageFIFO_A.begin(), itend = tiledImageFIFO_A.end();
504 it != itend; ++it)
505 {
506 if ((*it) == 1)
507 cpt++;
508 }
509 trace.endBlock();
510
511 cpt=0; tiledImageFIFO_A.clearCacheAndResetCacheMisses();
512 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(10)/WT");
513 for(Image::Domain::ConstIterator it = tiledImageFIFO_A.domain().begin(), itend = tiledImageFIFO_A.domain().end();
514 it != itend; ++it)
515 {
516 if (tiledImageFIFO_A(*it) == 1)
517 cpt++;
518 }
519 trace.endBlock();
520
521 // ---
522
523 cpt=0;
524 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(5)/WT - TiledIterator");
525 for(MyTiledImageFIFO::TiledIterator it = tiledImageFIFO_B.begin(), itend = tiledImageFIFO_B.end();
526 it != itend; ++it)
527 {
528 if ((*it) == 1)
529 cpt++;
530 }
531 trace.endBlock();
532
533 cpt=0; tiledImageFIFO_B.clearCacheAndResetCacheMisses();
534 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - FIFO(5)/WT");
535 for(Image::Domain::ConstIterator it = tiledImageFIFO_B.domain().begin(), itend = tiledImageFIFO_B.domain().end();
536 it != itend; ++it)
537 {
538 if (tiledImageFIFO_B(*it) == 1)
539 cpt++;
540 }
541 trace.endBlock();
542
543 // ---
544
545 cpt=0;
546 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - LAST/WT - TiledIterator");
547 for(MyTiledImage::TiledIterator it = tiledImage.begin(), itend = tiledImage.end();
548 it != itend; ++it)
549 {
550 if ((*it) == 1)
551 cpt++;
552 }
553 trace.endBlock();
554
555 cpt=0; tiledImage.clearCacheAndResetCacheMisses();
556 trace.beginBlock("Counting ones in the tiled image - 10x10x10 blocks - LAST/WT");
557 for(Image::Domain::ConstIterator it = tiledImage.domain().begin(), itend = tiledImage.domain().end();
558 it != itend; ++it)
559 {
560 if (tiledImage(*it) == 1)
561 cpt++;
562 }
563 trace.endBlock();
564
565 trace.endBlock();
566 trace.info() << endl;
567
568 return true;
569}
571// Standard services - public :
572
573int main( /*int argc, char** argv*/ )
574{
575 //if (argc==1)
577
579
582
585
586 return 0;
587}
588// //
Aim: implements a 'FIFO' read policy cache.
Aim: implements a 'LAST' read policy cache.
Aim: implements a 'WT (Write-through)' write policy cache.
Aim: implements a factory from an HDF5 file.
Aim: implements a factory to produce images from a "bigger/original" one according to a given domain.
Aim: implements a tiled image from a "bigger/original" one from an ImageFactory.
Definition TiledImage.h:77
void beginBlock(const std::string &keyword="")
std::ostream & error()
std::ostream & info()
double endBlock()
bool exampleTiledImageFromHDF5_1block3D()
bool writeHDF5_3D_TILED()
bool exampleTiledImageFromImage_10blocks3D()
#define NZ_3D_TILED
#define CHUNK_Z_3D_TILED
#define DATASETNAME_3D
#define CHUNK_X_3D_TILED
#define H5FILE_NAME_3D_TILED
#define CHUNK_Y_3D_TILED
#define RANK_3D_TILED
#define NX_3D_TILED
#define NY_3D_TILED
bool exampleTiledImageFromImage_1block3D()
bool exampleTiledImageFromHDF5_10blocks3D()
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint8_t uint8_t
unsigned 8-bit integer.
Definition BasicTypes.h:59
Trace trace
Definition Common.h:153
STL namespace.
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition CImage.h:103
ImageContainerBySTLVector< Domain, Value > Image