DGtal 1.3.0
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Data Fields | Protected Attributes | Private Member Functions
DGtal::ImageFactoryFromHDF5< TImageContainer > Class Template Reference

Aim: implements a factory from an HDF5 file. More...

#include <DGtal/images/ImageFactoryFromHDF5.h>

Public Types

typedef ImageFactoryFromHDF5< TImageContainerSelf
 
typedef TImageContainer ImageContainer
 Types copied from the container. More...
 
typedef ImageContainer::Domain Domain
 
typedef ImageContainer OutputImage
 New types. More...
 
typedef OutputImage::Value Value
 

Public Member Functions

 BOOST_CONCEPT_ASSERT ((concepts::CImage< TImageContainer >))
 Checking concepts. More...
 
 BOOST_CONCEPT_ASSERT ((concepts::CBoundedNumber< Value >))
 
 ImageFactoryFromHDF5 (const std::string &aFilename, const std::string &aDataset)
 
 ~ImageFactoryFromHDF5 ()
 
const Domaindomain () const
 
void selfDisplay (std::ostream &out) const
 
bool isValid () const
 
OutputImagerequestImage (const Domain &aDomain)
 
void flushImage (OutputImage *outputImage)
 
void detachImage (OutputImage *outputImage)
 

Data Fields

hid_t file
 
hid_t dataset
 
hid_t datatype
 
hid_t dataspace
 

Protected Attributes

DomainmyDomain
 Alias on the image domain. More...
 
const std::string myFilename
 HDF5 filename and datasetname. More...
 
const std::string myDataset
 

Private Member Functions

 ImageFactoryFromHDF5 (const ImageFactoryFromHDF5 &other)
 
ImageFactoryFromHDF5operator= (const ImageFactoryFromHDF5 &other)
 

Detailed Description

template<typename TImageContainer>
class DGtal::ImageFactoryFromHDF5< TImageContainer >

Aim: implements a factory from an HDF5 file.

Description of template class 'ImageFactoryFromHDF5'

Template Parameters
TImageContaineran image container type (model of CImage).

The factory images production (images are copied, so it's a creation process) is done with the function 'requestImage' so the deletion must be done with the function 'detachImage'.

The update of the original image is done with the function 'flushImage'.

Examples
images/exampleImageFactoryFromHDF5.cpp, and images/raw2HDF5.cpp.

Definition at line 196 of file ImageFactoryFromHDF5.h.

Member Typedef Documentation

◆ Domain

Definition at line 209 of file ImageFactoryFromHDF5.h.

◆ ImageContainer

template<typename TImageContainer >
typedef TImageContainer DGtal::ImageFactoryFromHDF5< TImageContainer >::ImageContainer

Types copied from the container.

Definition at line 208 of file ImageFactoryFromHDF5.h.

◆ OutputImage

template<typename TImageContainer >
typedef ImageContainer DGtal::ImageFactoryFromHDF5< TImageContainer >::OutputImage

New types.

Definition at line 212 of file ImageFactoryFromHDF5.h.

◆ Self

Definition at line 202 of file ImageFactoryFromHDF5.h.

◆ Value

Definition at line 213 of file ImageFactoryFromHDF5.h.

Constructor & Destructor Documentation

◆ ImageFactoryFromHDF5() [1/2]

template<typename TImageContainer >
DGtal::ImageFactoryFromHDF5< TImageContainer >::ImageFactoryFromHDF5 ( const std::string &  aFilename,
const std::string &  aDataset 
)
inline

Constructor.

Parameters
aFilenameHDF5 filename.
aDatasetdatasetname.

Definition at line 226 of file ImageFactoryFromHDF5.h.

226 :
227 myFilename(aFilename), myDataset(aDataset)
228 {
229 const int ddim = Domain::dimension;
230
231 hsize_t dims_out[ddim]; // dataset dimensions
232
233 // Open the file and the dataset.
234 file = H5Fopen(myFilename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
235 dataset = H5Dopen2(file, myDataset.c_str(), H5P_DEFAULT);
236
237 // Get datatype and dataspace handles and then query dataset class, order, size, rank and dimensions.
238 datatype = H5Dget_type(dataset); // datatype handle
239
240 dataspace = H5Dget_space(dataset); // dataspace handle
241
242 H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
243
244 // --
245
246 typedef SpaceND<ddim> TSpace;
247 typename TSpace::Point low, up;
248
249 typename Domain::Integer d;
250 for(d=0; d<ddim; d++)
251 {
252 low[d]=0;
253 up[d]=dims_out[ddim-d-1]-1;
254 }
255
256 myDomain = new Domain(low, up);
257 }
Domain * myDomain
Alias on the image domain.
const std::string myFilename
HDF5 filename and datasetname.
ImageContainer::Domain Domain

References DGtal::ImageFactoryFromHDF5< TImageContainer >::dataset, DGtal::ImageFactoryFromHDF5< TImageContainer >::dataspace, DGtal::ImageFactoryFromHDF5< TImageContainer >::datatype, DGtal::ImageFactoryFromHDF5< TImageContainer >::file, DGtal::ImageFactoryFromHDF5< TImageContainer >::myDataset, DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain, and DGtal::ImageFactoryFromHDF5< TImageContainer >::myFilename.

◆ ~ImageFactoryFromHDF5()

◆ ImageFactoryFromHDF5() [2/2]

template<typename TImageContainer >
DGtal::ImageFactoryFromHDF5< TImageContainer >::ImageFactoryFromHDF5 ( const ImageFactoryFromHDF5< TImageContainer > &  other)
private

Member Function Documentation

◆ BOOST_CONCEPT_ASSERT() [1/2]

template<typename TImageContainer >
DGtal::ImageFactoryFromHDF5< TImageContainer >::BOOST_CONCEPT_ASSERT ( (concepts::CBoundedNumber< Value >)  )

◆ BOOST_CONCEPT_ASSERT() [2/2]

template<typename TImageContainer >
DGtal::ImageFactoryFromHDF5< TImageContainer >::BOOST_CONCEPT_ASSERT ( (concepts::CImage< TImageContainer >)  )

Checking concepts.

◆ detachImage()

template<typename TImageContainer >
void DGtal::ImageFactoryFromHDF5< TImageContainer >::detachImage ( OutputImage outputImage)
inline

Free (i.e. delete) an OutputImage.

Parameters
outputImagethe OutputImage.

Definition at line 543 of file ImageFactoryFromHDF5.h.

544 {
545 delete outputImage;
546 }

◆ domain()

template<typename TImageContainer >
const Domain & DGtal::ImageFactoryFromHDF5< TImageContainer >::domain ( ) const
inline

Returns a reference to the underlying image domain.

Returns
a reference to the domain.

Definition at line 291 of file ImageFactoryFromHDF5.h.

292 {
293 return *myDomain;
294 }

References DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain.

◆ flushImage()

template<typename TImageContainer >
void DGtal::ImageFactoryFromHDF5< TImageContainer >::flushImage ( OutputImage outputImage)
inline

Flush (i.e. write/synchronize) an OutputImage.

Parameters
outputImagethe OutputImage.

Definition at line 435 of file ImageFactoryFromHDF5.h.

436 {
437 DGtal::IOException dgtalio;
438
439 const int ddim = Domain::dimension;
440
441 // --
442
443 hsize_t offset[ddim]; // hyperslab offset in the file
444 hsize_t count[ddim]; // size of the hyperslab in the file
445
446 herr_t status;
447 hsize_t dimsm[ddim]; // memory space dimensions
448 hid_t memspace;
449
450 hsize_t offset_in[ddim]; // hyperslab offset in memory
451 hsize_t count_in[ddim]; // size of the hyperslab in memory
452
453 //int i[ddim];
454 int N_SUB[ddim];
455 typename Domain::Integer d;
456
457 int malloc_size=1;
458 for(d=0; d<ddim; d++)
459 {
460 N_SUB[d] = (outputImage->domain().upperBound()[ddim-d-1]-outputImage->domain().lowerBound()[ddim-d-1])+1;
461 malloc_size = malloc_size*N_SUB[d];
462 }
463
464 Value *data_in = (Value*) malloc (malloc_size * sizeof(Value)); // input buffer
465 if (data_in == NULL)
466 {
467 trace.error() << "data_in malloc error in flushImage: " << (malloc_size * sizeof(Value)) << std::endl;
468 throw dgtalio;
469 }
470
471 // Define hyperslab in the dataset.
472 for(d=0; d<ddim; d++)
473 offset[d] = outputImage->domain().lowerBound()[ddim-d-1]-myDomain->lowerBound()[ddim-d-1];
474 for(d=0; d<ddim; d++)
475 count[d] = N_SUB[d];
476 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);
477 if (status)
478 {
479 trace.error() << " H5Sselect_hyperslab from dataspace error" << std::endl;
480 throw dgtalio;
481 }
482
483 // Define the memory dataspace.
484 for(d=0; d<ddim; d++)
485 dimsm[d] = N_SUB[d];
486 memspace = H5Screate_simple(ddim,dimsm,NULL);
487
488 // Define memory hyperslab.
489 for(d=0; d<ddim; d++)
490 offset_in[d] = 0;
491 for(d=0; d<ddim; d++)
492 count_in[d] = N_SUB[d];
493 status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_in, NULL, count_in, NULL);
494 if (status)
495 {
496 trace.error() << " H5Sselect_hyperslab from memspace error" << std::endl;
497 throw dgtalio;
498 }
499
500 typedef SpaceND<ddim> TSpace;
501 typename TSpace::Point a, b;
502 for(d=0; d<ddim; d++)
503 {
504 a[d]=offset[ddim-d-1]+myDomain->lowerBound()[d];
505 b[d]=a[d]+N_SUB[ddim-d-1]-1;
506 }
507 HyperRectDomain<TSpace> hrdomain(a,b);
508
509 std::vector<typename TSpace::Dimension> v(ddim);
510 for(d=0; d<ddim; d++)
511 v[d]=d;
512
513 int p=0;
515 it = hrdomain.subRange(v, a).begin(), itend = hrdomain.subRange(v, a).end();
516 it != itend;
517 ++it)
518 {
519 data_in[ p++ ] = outputImage->operator()((*it));
520 }
521
522 // Write data from hyperslab in memory into the hyperslab in the file.
523 //status = H5Dwrite(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data_in);
524 status = H5DSpecializations<Self, Value>::H5DwriteS(*this, memspace, data_in);
525 if (status)
526 {
527 trace.error() << " H5DSpecializations/H5DwriteS error" << std::endl;
528 throw dgtalio;
529 }
530
531 H5Sclose(memspace);
532
533 // --
534
535 free(data_in);
536 }
std::ostream & error()
Trace trace
Definition: Common.h:154
static int H5DwriteS(ImageFactory &anImageFactory, hid_t memspace, Value *data_in)
HyperRectDomain_subIterator< Point > ConstIterator

References DGtal::HyperRectDomain< TSpace >::ConstSubRange::begin(), DGtal::ImageFactoryFromHDF5< TImageContainer >::dataspace, DGtal::ImageContainerBySTLVector< TDomain, TValue >::domain(), DGtal::HyperRectDomain< TSpace >::ConstSubRange::end(), DGtal::Trace::error(), DGtal::H5DSpecializations< TImageFactory, T >::H5DwriteS(), DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain, DGtal::HyperRectDomain< TSpace >::subRange(), and DGtal::trace.

◆ isValid()

template<typename TImageContainer >
bool DGtal::ImageFactoryFromHDF5< TImageContainer >::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns
'true' if the object is valid, 'false' otherwise.

Definition at line 311 of file ImageFactoryFromHDF5.h.

312 {
313 return (myDomain->isValid());
314 }

References DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain.

◆ operator=()

◆ requestImage()

template<typename TImageContainer >
OutputImage * DGtal::ImageFactoryFromHDF5< TImageContainer >::requestImage ( const Domain aDomain)
inline

Returns a pointer of an OutputImage created with the Domain aDomain.

Parameters
aDomainthe domain.
Returns
an ImagePtr.

Definition at line 323 of file ImageFactoryFromHDF5.h.

324 {
325 DGtal::IOException dgtalio;
326
327 const int ddim = Domain::dimension;
328
329 // --
330
331 hsize_t offset[ddim]; // hyperslab offset in the file
332 hsize_t count[ddim]; // size of the hyperslab in the file
333
334 herr_t status;
335 hsize_t dimsm[ddim]; // memory space dimensions
336 hid_t memspace;
337
338 hsize_t offset_out[ddim]; // hyperslab offset in memory
339 hsize_t count_out[ddim]; // size of the hyperslab in memory
340
341 int N_SUB[ddim];
342 typename Domain::Integer d;
343
344 int malloc_size=1;
345 for(d=0; d<ddim; d++)
346 {
347 N_SUB[d] = (aDomain.upperBound()[ddim-d-1]-aDomain.lowerBound()[ddim-d-1])+1;
348 malloc_size = malloc_size*N_SUB[d];
349 }
350
351 Value *data_out = (Value*) malloc (malloc_size * sizeof(Value)); // output buffer
352 if (data_out == NULL)
353 {
354 trace.error() << "data_out malloc error in requestImage: " << (malloc_size * sizeof(Value)) << std::endl;
355 throw dgtalio;
356 }
357
358 // Define hyperslab in the dataset.
359 for(d=0; d<ddim; d++)
360 offset[d] = aDomain.lowerBound()[ddim-d-1]-myDomain->lowerBound()[ddim-d-1];
361 for(d=0; d<ddim; d++)
362 count[d] = N_SUB[d];
363 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL);
364 if (status)
365 {
366 trace.error() << " H5Sselect_hyperslab from dataspace error" << std::endl;
367 throw dgtalio;
368 }
369
370 // Define the memory dataspace.
371 for(d=0; d<ddim; d++)
372 dimsm[d] = N_SUB[d];
373 memspace = H5Screate_simple(ddim,dimsm,NULL);
374
375 // Define memory hyperslab.
376 for(d=0; d<ddim; d++)
377 offset_out[d] = 0;
378 for(d=0; d<ddim; d++)
379 count_out[d] = N_SUB[d];
380 status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL, count_out, NULL);
381 if (status)
382 {
383 trace.error() << " H5Sselect_hyperslab from memspace error" << std::endl;
384 throw dgtalio;
385 }
386
387 // Read data from hyperslab in the file into the hyperslab in memory.
388 //status = H5Dread(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data_out);
389 status = H5DSpecializations<Self, Value>::H5DreadS(*this, memspace, data_out);
390 if (status)
391 {
392 trace.error() << " H5DSpecializations/H5DreadS error" << std::endl;
393 throw dgtalio;
394 }
395
396 OutputImage* outputImage = new OutputImage(aDomain);
397 if (outputImage == NULL)
398 {
399 trace.error() << "outputImage new error in requestImage: " << std::endl;
400 throw dgtalio;
401 }
402
403 typedef SpaceND<ddim> TSpace;
404 typename TSpace::Point a, b;
405 for(d=0; d<ddim; d++)
406 {
407 a[d]=offset[ddim-d-1]+myDomain->lowerBound()[d];
408 b[d]=a[d]+N_SUB[ddim-d-1]-1;
409 }
410 HyperRectDomain<TSpace> hrdomain(a,b);
411
412 int p=0;
414 it = hrdomain.begin(), itend = hrdomain.end();
415 it != itend;
416 ++it)
417 {
418 outputImage->setValue((*it), data_out[ p++ ]);
419 }
420
421 H5Sclose(memspace);
422
423 // --
424
425 free(data_out);
426
427 return outputImage;
428 }
ImageContainer OutputImage
New types.
static int H5DreadS(ImageFactory &anImageFactory, hid_t memspace, Value *data_out)

References DGtal::HyperRectDomain< TSpace >::begin(), DGtal::ImageFactoryFromHDF5< TImageContainer >::dataspace, DGtal::HyperRectDomain< TSpace >::end(), DGtal::Trace::error(), DGtal::H5DSpecializations< TImageFactory, T >::H5DreadS(), DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain, DGtal::ImageContainerBySTLVector< TDomain, TValue >::setValue(), and DGtal::trace.

◆ selfDisplay()

template<typename TImageContainer >
void DGtal::ImageFactoryFromHDF5< TImageContainer >::selfDisplay ( std::ostream &  out) const

Writes/Displays the object on an output stream.

Parameters
outthe output stream where the object is written.

Field Documentation

◆ dataset

template<typename TImageContainer >
hid_t DGtal::ImageFactoryFromHDF5< TImageContainer >::dataset

◆ dataspace

template<typename TImageContainer >
hid_t DGtal::ImageFactoryFromHDF5< TImageContainer >::dataspace

◆ datatype

template<typename TImageContainer >
hid_t DGtal::ImageFactoryFromHDF5< TImageContainer >::datatype

◆ file

template<typename TImageContainer >
hid_t DGtal::ImageFactoryFromHDF5< TImageContainer >::file

◆ myDataset

template<typename TImageContainer >
const std::string DGtal::ImageFactoryFromHDF5< TImageContainer >::myDataset
protected

◆ myDomain

template<typename TImageContainer >
Domain* DGtal::ImageFactoryFromHDF5< TImageContainer >::myDomain
protected

◆ myFilename

template<typename TImageContainer >
const std::string DGtal::ImageFactoryFromHDF5< TImageContainer >::myFilename
protected

HDF5 filename and datasetname.

Definition at line 562 of file ImageFactoryFromHDF5.h.

Referenced by DGtal::ImageFactoryFromHDF5< TImageContainer >::ImageFactoryFromHDF5().


The documentation for this class was generated from the following file: