aimsdata  5.1.2
Neuroimaging data handling
fileFormat_cartovolume_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * Data reader class
36  */
37 #ifndef AIMS_IO_FILEFORMAT_CARTOVOLUME_D_H
38 #define AIMS_IO_FILEFORMAT_CARTOVOLUME_D_H
39 
41 #include <aims/data/header.h>
44 #include <aims/io/reader.h>
45 #include <aims/io/writer.h>
46 
47 namespace aims
48 {
49 
50  template<typename T>
51  VolumeFormat<T>::VolumeFormat( const std::string & prefformat )
52  : FileFormat<carto::Volume<T> >(), _preferredFormat( prefformat )
53  {
54  }
55 
56 
57  template<typename T>
59  {
60  }
61 
62 
63  template<typename T>
64  bool VolumeFormat<T>::read( const std::string & filename,
65  carto::Volume<T> & vol,
66  const carto::AllocatorContext & context,
67  carto::Object options )
68  {
69  /*
70  std::cout << "VolumeFormat<" << carto::DataTypeCode<T>::name()
71  << ">::read(" << filename << ")\n";
72  */
73  Reader<AimsData<T> > r( filename );
74  r.setAllocatorContext( context );
75  r.setOptions( options );
76  carto::rc_ptr<carto::Volume<T> > ptr( &vol );
77  int border = 0;
78  options->getProperty( "border", border );
79  {
80  AimsData<T> d( ptr );
81  std::string *fmt = 0;
82  if( !_preferredFormat.empty() )
83  fmt = &_preferredFormat;
84  r.read( d, border, fmt );
85  // Up to now I don't know how to avoid a full copy
86  vol = *d.volume(); // #####
87  }
88  ptr.release(); // don't destroy vol !
89  return true;
90  }
91 
92 
93  template<typename T>
95  VolumeFormat<T>::read( const std::string & filename,
96  const carto::AllocatorContext & context,
97  carto::Object options )
98  {
99  /*
100  std::cout << "VolumeFormat<" << carto::DataTypeCode<T>::name()
101  << ">::read*(" << filename << ")\n";
102  */
103  Reader<AimsData<T> > r( filename );
104  r.setAllocatorContext( context );
105  r.setOptions( options );
107  int border = 0;
108  options->getProperty( "border", border );
109  {
110  AimsData<T> d;
111  std::string *fmt = 0;
112  if( !_preferredFormat.empty() )
113  fmt = &_preferredFormat;
114  if( !r.read( d, border, fmt ) )
115  return 0;
116  ptr = d.volume();
117  }
118  carto::Volume<T> *vol = ptr.release(); // don't destroy vol !
119  return vol;
120  }
121 
122 
123  template<typename T>
124  bool VolumeFormat<T>::write( const std::string & filename,
125  const carto::Volume<T> & vol,
126  carto::Object options )
127  {
128  Writer<AimsData<T> > w( filename, options );
130  ptr( const_cast<carto::Volume<T> *>( &vol ) );
131  {
132  AimsData<T> d( ptr );
133  std::string *fmt = 0;
134  if( !_preferredFormat.empty() )
135  fmt = &_preferredFormat;
136  bool ascii = false;
137  try
138  {
139  if( !options.isNull() )
140  {
141  carto::Object aso = options->getProperty( "ascii" );
142  if( !aso.isNull() )
143  ascii = (bool) aso->getScalar();
144  }
145  }
146  catch( ... )
147  {
148  }
149  w.write( d, ascii, fmt );
150  }
151  ptr.release(); // don't destroy vol !
152  return true;
153  }
154 
155 
156  /*
157  template<typename T>
158  void FileFormatDictionary<carto::Volume<T> >::registerBaseFormats()
159  {
160  std::set<std::string> f = FileFormatDictionary<AimsData<T> >::formats();
161  std::set<std::string>::iterator i, e = f.end();
162  const std::multimap<std::string, std::string>
163  & ext = FileFormatDictionary<AimsData<T> >::extensions();
164  std::multimap<std::string, std::string>::const_iterator ie, ee;
165 
166  for( i=f.begin(); i!=e; ++i )
167  {
168  std::vector<std::string> ext2;
169  for( ie=ext.lower_bound( *i ), ee=ext.upper_bound( *i ); ie!=ee;
170  ++ie )
171  ext2.push_back( ie->second );
172  VolumeFormat<T> *vf = new VolumeFormat<T>;
173  registerFormat( *i, vf, ext2 );
174  }
175  }
176  */
177 
178  // -----------
179 
180  template<typename T>
181  VolumeRefFormat<T>::VolumeRefFormat( const std::string & prefformat )
182  : FileFormat<carto::VolumeRef<T> >(), _volformat( prefformat )
183  {
184  }
185 
186 
187  template<typename T>
189  {
190  }
191 
192 
193  template<typename T>
194  bool VolumeRefFormat<T>::read( const std::string & filename,
195  carto::VolumeRef<T> & vol,
196  const carto::AllocatorContext & context,
197  carto::Object options )
198  {
199  carto::Volume<T> *vref = _volformat.read( filename, context, options );
200  if( vref )
201  {
202  vol.reset( vref );
203  return true;
204  }
205  return false;
206  }
207 
208 
209  template<typename T>
211  VolumeRefFormat<T>::read( const std::string & filename,
212  const carto::AllocatorContext & context,
213  carto::Object options )
214  {
215  carto::Volume<T> *vol = _volformat.read( filename, context, options );
216  if( vol )
217  return new carto::VolumeRef<T>( vol );
218  return 0;
219  }
220 
221 
222  template<typename T>
223  bool VolumeRefFormat<T>::write( const std::string & filename,
224  const carto::VolumeRef<T> & vol,
225  carto::Object options )
226  {
227  return _volformat.write( filename, *vol, options );
228  }
229 
230 }
231 
232 #endif
233 
carto::VolumeRef< T > & volume()
Low-level object IO format: each specific format has such a reader / writer.
Definition: fileFormat.h:62
Generic reader for every format of Aims object.
Definition: reader.h:70
void setAllocatorContext(const carto::AllocatorContext &ac)
allocator control (not used by every format yet)
Definition: reader_d.h:106
virtual bool read(T &obj, int border=0, const std::string *format=0, int frame=-1)
Finds the correct format and reads the object. if format is specified, this format is tried first,...
Definition: reader_d.h:142
void setOptions(carto::Object options)
Definition: reader_d.h:124
virtual bool read(const std::string &filename, carto::Volume< T > &vol, const carto::AllocatorContext &context, carto::Object options)
virtual bool write(const std::string &filename, const carto::Volume< T > &vol, carto::Object options=carto::none())
VolumeFormat(const std::string &prefformat=std::string())
virtual bool write(const std::string &filename, const carto::VolumeRef< T > &vol, carto::Object options=carto::none())
VolumeRefFormat(const std::string &prefformat=std::string())
virtual bool read(const std::string &filename, carto::VolumeRef< T > &vol, const carto::AllocatorContext &context, carto::Object options)
Generic writer for every format of Aims object.
Definition: writer.h:94
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
Finds the correct format and writes the object.
Definition: writer_d.h:108
bool isNull() const
void reset(T *p=NULL)
T * release()
The class for EcatSino data write operation.
Definition: borderfiller.h:13