primatologist-gpl  5.1.2
io.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef PRIMATOLOGIST_UTILITY_IO_H
12 #define PRIMATOLOGIST_UTILITY_IO_H
13 
14 #include <aims/utility/converter_g.h> // aims::ShallowConverter
15 #include <aims/io/io_g.h> // aims::Process/Finder/Reader/Writer
16 #include <aims/io/channelR.h> // aims::ChannelReader
17 #include <cartodata/volume/volume.h>
18 
19 namespace aims {
20 namespace io {
21 
23  template <typename T>
24  void write( const T & object, const std::string & fname, bool doit = true )
25  {
26  if( doit )
27  {
28  Writer<T> w( fname );
29  w << object;
30  }
31  }
32 
35  template <typename T>
36  void read( const std::string & fname, T & object, bool doit = true )
37  {
38  if( doit )
39  {
40  Reader<T> r( fname );
41  r >> object;
42  }
43  }
44 
48  template <typename T>
49  void readChannel( const std::string & fname, T & object, int8_t channel, bool doit = true )
50  {
51  if( doit )
52  {
53  ChannelReader<T> r( fname );
54  r.read( object, channel );
55  }
56  }
57 
58 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
59  // Specialization for VolumeRef
60  template <typename T>
61  void readChannel( const std::string & fname, carto::VolumeRef<T> & object, int8_t channel, bool doit = true )
62  {
63  if( doit )
64  {
65  carto::VolumeRef<T> data( object );
66  ChannelReader<carto::VolumeRef<T> > r( fname );
67  r.read( data, channel );
68  if( object.get() != data.get() )
69  object = data;
70  }
71  }
72 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
73 
74 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
75  // utility for convert reader
76  namespace internal {
77 
78  template<typename IN, typename OUT>
79  bool convert( Process & p, const std::string & fname, Finder & f );
80 
81  template <typename T>
82  class VolumeConvertReader: public Process
83  {
84  public:
85  VolumeConvertReader( carto::VolumeRef<T> & vol ):
86  Process(),
87  data( vol ),
88  options()
89  {
90  registerProcessType( "Volume", "S8", &convert<int8_t,T> );
91  registerProcessType( "Volume", "U8", &convert<uint8_t,T> );
92  registerProcessType( "Volume", "S16", &convert<int16_t,T> );
93  registerProcessType( "Volume", "U16", &convert<uint16_t,T> );
94  registerProcessType( "Volume", "S32", &convert<int32_t,T> );
95  registerProcessType( "Volume", "U32", &convert<uint32_t,T> );
96  registerProcessType( "Volume", "S64", &convert<int64_t,T> );
97  registerProcessType( "Volume", "U64", &convert<uint64_t,T> );
98  registerProcessType( "Volume", "FLOAT", &convert<float,T> );
99  registerProcessType( "Volume", "DOUBLE", &convert<double,T> );
100  }
101  virtual ~VolumeConvertReader() {}
102 
103  carto::Object options;
104  carto::VolumeRef<T> & data;
105  };
106 
107  template<typename IN, typename OUT>
108  bool convert( Process & p, const std::string & fname, Finder & f )
109  {
111  std::string fmt = f.format();
112  Reader<carto::VolumeRef<IN> > r( fname );
113  carto::VolumeRef<IN> data;
114  r.read( data, 0, &fmt );
115  r.setOptions( cc.options );
116  if( cc.data.get() )
117  cc.data.reallocate( data.getSizeX(), data.getSizeY(),
118  data.getSizeZ(), data.getSizeT() );
119  else
120  cc.data = carto::VolumeRef<OUT>( data.getSizeX(), data.getSizeY(),
121  data.getSizeZ(), data.getSizeT() );
122  carto::ShallowConverter<carto::VolumeRef<IN>, carto::VolumeRef<OUT> > conv;
123  conv.convert( data, cc.data );
124  cc.data.copyHeaderFrom( data.header() );
125  return true;
126  }
127 
128  } // namespace internal
129 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
130 
131  // Convert read
132  // If the provided object data type is not the same as the disk data type,
133  // a conversion (without dynamic resampling) is performed.
134  template <typename T>
135  void readConvert( const std::string & fname, carto::VolumeRef<T> & object, bool doit = true )
136  {
137  if( doit )
138  {
140  cr.execute(fname);
141  }
142  }
143 
144 } // namespace io
145 } // namespace aims
146 
147 #endif // PRIMATOLOGIST_UTILITY_IO_H
VolumeConvertReader(carto::VolumeRef< T > &vol)
Definition: io.h:85
carto::VolumeRef< T > & data
Definition: io.h:104
bool convert(Process &p, const std::string &fname, Finder &f)
Definition: io.h:108
void readChannel(const std::string &fname, T &object, int8_t channel, bool doit=true)
Read a given channel A conversion is done between the channel type and the object type if necessaey.
Definition: io.h:49
void read(const std::string &fname, T &object, bool doit=true)
Basic read Provided object type must be the same as disk data type.
Definition: io.h:36
void write(const T &object, const std::string &fname, bool doit=true)
Basic write.
Definition: io.h:24
void readConvert(const std::string &fname, carto::VolumeRef< T > &object, bool doit=true)
Definition: io.h:135