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