A.I.M.S


baseFormats_volume_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 #ifndef AIMS_IO_BASEFORMATS_VOLUME_D_H
35 #define AIMS_IO_BASEFORMATS_VOLUME_D_H
36 
39 #include <aims/io/gisR.h>
40 #include <aims/io/gisW.h>
41 #include <aims/io/spmR.h>
42 #include <aims/io/spmW.h>
43 #include <aims/io/genesisR.h>
45 #include <soma-io/io/reader.h>
46 #include <soma-io/io/writer.h>
47 
48 namespace aims
49 {
50 
51  // GIS
52 
53  template<typename T>
55  {
56  }
57 
58 
59  template<class T>
60  bool GisFormat<T>::read( const std::string & filename, AimsData<T> & vol,
61  const carto::AllocatorContext & context,
62  carto::Object options )
63  {
64  GisReader<T> r( filename );
65  r.read( vol, context, options );
66  return( true );
67  }
68 
69 
70  template<class T>
71  bool GisFormat<T>::write( const std::string & filename,
72  const AimsData<T> & vol, carto::Object options )
73  {
74  bool ascii = false;
75  try
76  {
77  if( !options.isNull() )
78  {
79  carto::Object aso = options->getProperty( "ascii" );
80  if( !aso.isNull() )
81  ascii = (bool) aso->getScalar();
82  }
83  }
84  catch( ... )
85  {
86  }
87  GisWriter<T> r( filename, ascii );
88  r.write( vol );
89  return( true );
90  }
91 
92  // SPM
93 
94  template<typename T>
96  {
97  }
98 
99 
100  template<class T>
101  bool SpmFormat<T>::read( const std::string & filename, AimsData<T> & vol,
102  const carto::AllocatorContext & context,
103  carto::Object options )
104  {
105  SpmReader<T> r( filename );
106  r.read( vol, context, options );
107  return( true );
108  }
109 
110 
111  template<class T>
112  bool SpmFormat<T>::write( const std::string & filename,
113  const AimsData<T> & vol, carto::Object )
114  {
115  SpmWriter<T> r( filename );
116  r.write( vol );
117  return( true );
118  }
119 
120  // GENESIS
121 
122  template<typename T>
124  {
125  }
126 
127 
128  template<class T>
129  bool GenesisFormat<T>::read( const std::string & filename,
130  AimsData<T> & vol,
131  const carto::AllocatorContext & context,
132  carto::Object options )
133  {
134  GenesisReader<T> r( filename );
135  r.read( vol,context, options );
136  return( true );
137  }
138 
139 
140  // IMAS (sparse)
141 
142  template <typename T>
143  bool ImasVolFormat<T>::read( const std::string & filename,
144  AimsData<T> & obj,
145  const carto::AllocatorContext & context,
146  carto::Object options )
147  {
148  ImasHeader *hdr = new ImasHeader( filename );
149  if( !hdr->read() )
150  {
151  delete hdr;
152  return false;
153  }
154 
155  int ascii = false, bswap = false;
156  hdr->getProperty( "ascii", ascii );
157  hdr->getProperty( "byte_swapping", bswap );
158 
159  std::ifstream is( filename.c_str() );
160 
162  ascii ? "ascii" : "binar", bswap );
164  ascii ? "ascii" : "binar", bswap );
165 
166  uint32_t size1 = 0U;
167  uint32_t size2 = 0U;
168  uint32_t nonZeroElementCount = 0U;
169  uint32_t k, s1, s2;
170  double value;
171 
172  itemR1->read( is, size1 );
173  itemR1->read( is, size2 );
174  itemR1->read( is, nonZeroElementCount );
175 
176  std::streampos p = is.tellg(), e;
177  is.seekg( 0, std::ios_base::end );
178  e = is.tellg();
179  is.seekg( p, std::ios_base::beg );
180  if( nonZeroElementCount > size1 * size2
181  || nonZeroElementCount * 16 != e - p )
183  "Wrong format or corrupted .imas format", filename );
184 
185  carto::Volume<T> *vol = new carto::Volume<T>( size2, size1 );
186  vol->fill( 0 );
187 
188  for ( k = 0; k < nonZeroElementCount; k++ )
189  {
190  itemR1->read( is, s1 );
191  itemR1->read( is, s2 );
192  itemR2->read( is, value );
193  vol->at( s2, s1 ) = value;
194  }
195 
196  obj = carto::rc_ptr<carto::Volume<T> >( vol );
197 
198  is.close();
199 
200  delete itemR1;
201  delete itemR2;
202 
203  obj.setHeader( hdr );
204  return true;
205  }
206 
207 
208  template<class T>
209  bool ImasVolFormat<T>::write( const std::string & filename,
210  const AimsData<T> & obj,
211  carto::Object options )
212  {
213  bool ascii = false;
214  try
215  {
216  if( !options.isNull() )
217  {
218  carto::Object aso = options->getProperty( "ascii" );
219  if( !aso.isNull() )
220  ascii = (bool) aso->getScalar();
221  }
222  }
223  catch( ... )
224  {
225  }
226 
227  std::ofstream os( filename.c_str() );
228 
230  ascii ? "ascii" : "binar", false );
232  ascii ? "ascii" : "binar", false );
233 
234  unsigned x, y, nx = obj.dimX(), ny = obj.dimY();
235  unsigned /*long*/ count = 0;
236 
237  for( y=0; y<ny; ++y )
238  {
239  const T* buf = &obj( 0, y );
240  long inc = &obj( 1, y ) - buf;
241 
242  for( x=0; x<nx; ++x, buf+=inc )
243  if( *buf != 0 )
244  ++count;
245  }
246 
247  itemW1->write( os, obj.dimY() );
248  itemW1->write( os, obj.dimX() );
249  itemW1->write( os, count ); // WARNING should be 64 bits !
250 
251  for( y=0; y<ny; ++y )
252  {
253  const T* buf = &obj( 0, y );
254  long inc = &obj( 1, y ) - buf;
255 
256  for( x=0; x<nx; ++x, buf+=inc )
257  if( *buf != 0 )
258  {
259  itemW1->write( os, y );
260  itemW1->write( os, x );
261  itemW2->write( os, *buf );
262  }
263 
264  }
265 
266  os.close();
267  delete itemW1;
268  delete itemW2;
269 
270  obj.header()->writeMinf( filename + ".minf" );
271  return true;
272  }
273 
274 
275  // SomaIO / carto Volume
276 
277  template<typename T>
279  {
280  }
281 
282 
283  template<typename T>
284  bool SomaIOAimsDataFormat<T>::read( const std::string & filename,
285  AimsData<T> & vol,
286  const carto::AllocatorContext & context,
287  carto::Object options )
288  {
289  // avoid recursive call through aims / carto IO redirections
290  std::vector<std::string> excluded;
291  if( options.isNull() )
293  else
294  {
295  if( options->getProperty( "aims_excluded_formats", excluded ) )
296  {
297  std::vector<std::string>::const_iterator i, e = excluded.end();
298  for( i=excluded.begin(); i!=e; ++i )
299  if( *i == "SOMAIO_VOLUMES" )
300  // we are in the excluded formats: give up
301  return false;
302  }
303  }
304 
305  // Add SOMAIO_VOLUMES in excluded formats
306  // to avoid recursive calls
307  std::vector<std::string>::iterator ei;
308  bool success;
309 
310  excluded.push_back( "SOMAIO_VOLUMES" );
311  options->setProperty( "aims_excluded_formats", excluded );
312 
313  carto::Reader<carto::Volume<T> > r( filename );
314  r.setAllocatorContext( context );
315  r.setOptions( options );
316  carto::Volume<T> *cvol = r.read();
317  if( cvol )
318  {
319  vol = carto::rc_ptr<carto::Volume<T> >( cvol );
320  success = true;
321  }
322  else
323  success = false;
324 
325  // Remove previously added excluded format
326  options = r.options();
327  if( options->getProperty( "aims_excluded_formats", excluded ) )
328  {
329  ei = std::find( excluded.begin(),
330  excluded.end(),
331  "SOMAIO_VOLUMES" );
332 
333  if (ei != excluded.end()) {
334  excluded.erase( ei );
335  options->setProperty( "aims_excluded_formats", excluded );
336  }
337 
338  r.setOptions( options );
339  }
340 
341  return success;
342  }
343 
344 
345  template<typename T>
346  AimsData<T>* SomaIOAimsDataFormat<T>::read( const std::string & filename,
347  const carto::AllocatorContext & context,
348  carto::Object options )
349  {
350  // avoid recursive call through aims / carto IO redirections
351  std::vector<std::string> excluded;
352  if( options.isNull() )
354  else
355  {
356  if( options->getProperty( "aims_excluded_formats", excluded ) )
357  {
358  std::vector<std::string>::const_iterator i, e = excluded.end();
359  for( i=excluded.begin(); i!=e; ++i )
360  if( *i == "SOMAIO_VOLUMES" )
361  // we are in the excluded formats: give up
362  return 0;
363  }
364  }
365  excluded.push_back( "SOMAIO_VOLUMES" );
366  options->setProperty( "aims_excluded_formats", excluded );
367 
368  carto::Reader<carto::Volume<T> > r( filename );
369  r.setAllocatorContext( context );
370  r.setOptions( options );
371  carto::Volume<T> *cvol = r.read();
372  if( cvol )
373  return new AimsData<T>( carto::rc_ptr<carto::Volume<T> >( cvol ) );
374  return 0;
375  }
376 
377 
378  template<class T>
379  bool SomaIOAimsDataFormat<T>::write( const std::string & filename,
380  const AimsData<T> & vol, carto::Object options )
381  {
382  // std::cout << "SomaIOAimsDataFormat::write\n";
383  // avoid recursive call through aims / carto IO redirections
384  std::vector<std::string> excluded;
385  if( options.isNull() )
387  else
388  {
389  if( options->getProperty( "aims_excluded_formats", excluded ) )
390  {
391  std::vector<std::string>::const_iterator i, e = excluded.end();
392  for( i=excluded.begin(); i!=e; ++i )
393  if( *i == "SOMAIO_VOLUMES" )
394  // we are in the excluded formats: give up
395  return false;
396  }
397  }
398  excluded.push_back( "SOMAIO_VOLUMES" );
399  options->setProperty( "aims_excluded_formats", excluded );
400 
401  carto::Writer<carto::Volume<T> > w( filename );
402  return w.write( *vol.volume(), options );
403  }
404 
405 }
406 
407 
408 #endif
409 
410 
Default low-levels writers.
Definition: defaultItemW.h:58
virtual bool getProperty(const std::string &key, Object &value) const =0
virtual bool read(const std::string &filename, AimsData< T > &vol, const carto::AllocatorContext &context, carto::Object options)
virtual bool read(const std::string &filename, AimsData< T > &vol, const carto::AllocatorContext &context, carto::Object options)
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: gisR.h:87
int dimX() const
virtual bool read(uint32_t *offset=0)
Reads the header, and if offset is not null, sets the file offset to the data field.
virtual bool write(const std::string &filename, const AimsData< T > &vol, carto::Object options=carto::none())
the base class has a default implementation which only returns false so if your new format only suppo...
bool isNull() const
virtual bool read(const std::string &filename, AimsData< T > &obj, const carto::AllocatorContext &context, carto::Object options)
Default low-levels readers.
Definition: defaultItemR.h:55
virtual void write(std::ostream &os, const T &item) const
Definition: itemW.h:55
void setHeader(aims::Header *hdr)
void fill(const T &value)
virtual double getScalar() const =0
Low-level "small item" writer, used by higher-level file readers.
Definition: itemW.h:50
virtual bool write(const std::string &filename, const AimsData< T > &obj, carto::Object options=carto::none())
the base class has a default implementation which only returns false so if your new format only suppo...
virtual bool read(const std::string &filename, AimsData< T > &obj, const carto::AllocatorContext &context, carto::Object options)
virtual bool write(const std::string &filename, const AimsData< T > &vol, carto::Object options=carto::none())
the base class has a default implementation which only returns false so if your new format only suppo...
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Read the data with "name" file name from disk.
Definition: spmR_d.h:66
virtual void setProperty(const std::string &key, Object value)=0
const T & at(long x, long y=0, long z=0, long t=0) const
static Object value()
virtual void read(std::istream &is, T &item) const
Definition: itemR.h:103
carto::rc_ptr< carto::Volume< T > > volume()
virtual bool write(const std::string &filename, const AimsData< T > &obj, carto::Object options=carto::none())
the base class has a default implementation which only returns false so if your new format only suppo...
The template class for SPM read operation.
Definition: spmR.h:57
virtual bool getProperty(const std::string &, Object &) const
void write(const AimsData< T > &thing)
Write the data with "name" file name to disk.
Definition: spmW_d.h:52
int dimY() const
const aims::Header * header() const
Low-level "small item" reader, used by higher-level file readers.
Definition: itemR.h:98
virtual bool read(const std::string &filename, AimsData< T > &vol, const carto::AllocatorContext &context, carto::Object options)
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: genesisR.h:78
The template class for SPM write operation.
Definition: spmW.h:48
virtual bool writeMinf(const std::string &filename)
write meta-info header, non-const version (may change some attributes)
bool write(const AimsData< T > &thing)
Definition: gisW.h:111