aimsdata 6.0.0
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
47namespace 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>
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 );
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 // use a view in LPI orientation
133 carto::VolumeRef<T> vol2( ptr, std::vector<int>( 4, 0 ), vol.getSize() );
134 // vol2->flipToOrientation( "LPI" );
135
136 AimsData<T> d( vol2 );
137 std::string *fmt = 0;
138 if( !_preferredFormat.empty() )
139 fmt = &_preferredFormat;
140 bool ascii = false;
141 try
142 {
143 if( !options.isNull() )
144 {
145 carto::Object aso = options->getProperty( "ascii" );
146 if( !aso.isNull() )
147 ascii = (bool) aso->getScalar();
148 }
149 }
150 catch( ... )
151 {
152 }
153 w.write( d, ascii, fmt );
154 }
155 ptr.release(); // don't destroy vol !
156 return true;
157 }
158
159
160 /*
161 template<typename T>
162 void FileFormatDictionary<carto::Volume<T> >::registerBaseFormats()
163 {
164 std::set<std::string> f = FileFormatDictionary<AimsData<T> >::formats();
165 std::set<std::string>::iterator i, e = f.end();
166 const std::multimap<std::string, std::string>
167 & ext = FileFormatDictionary<AimsData<T> >::extensions();
168 std::multimap<std::string, std::string>::const_iterator ie, ee;
169
170 for( i=f.begin(); i!=e; ++i )
171 {
172 std::vector<std::string> ext2;
173 for( ie=ext.lower_bound( *i ), ee=ext.upper_bound( *i ); ie!=ee;
174 ++ie )
175 ext2.push_back( ie->second );
176 VolumeFormat<T> *vf = new VolumeFormat<T>;
177 registerFormat( *i, vf, ext2 );
178 }
179 }
180 */
181
182 // -----------
183
184 template<typename T>
185 VolumeRefFormat<T>::VolumeRefFormat( const std::string & prefformat )
186 : FileFormat<carto::VolumeRef<T> >(), _volformat( prefformat )
187 {
188 }
189
190
191 template<typename T>
195
196
197 template<typename T>
198 bool VolumeRefFormat<T>::read( const std::string & filename,
199 carto::VolumeRef<T> & vol,
200 const carto::AllocatorContext & context,
201 carto::Object options )
202 {
203 carto::Volume<T> *vref = _volformat.read( filename, context, options );
204 if( vref )
205 {
206 vol.reset( vref );
207 return true;
208 }
209 return false;
210 }
211
212
213 template<typename T>
215 VolumeRefFormat<T>::read( const std::string & filename,
216 const carto::AllocatorContext & context,
217 carto::Object options )
218 {
219 carto::Volume<T> *vol = _volformat.read( filename, context, options );
220 if( vol )
221 return new carto::VolumeRef<T>( vol );
222 return 0;
223 }
224
225
226 template<typename T>
227 bool VolumeRefFormat<T>::write( const std::string & filename,
228 const carto::VolumeRef<T> & vol,
229 carto::Object options )
230 {
231 return _volformat.write( filename, *vol, options );
232 }
233
234 // ----
235
236 template<typename T>
237 VolumeRefAimsFormat<T>::VolumeRefAimsFormat( const std::string & prefformat )
238 : FileFormat<carto::VolumeRef<T> >(), _preferredFormat( prefformat )
239 {
240 }
241
242
243 template<typename T>
247
248
249 template<typename T>
250 bool VolumeRefAimsFormat<T>::read( const std::string & filename,
252 const carto::AllocatorContext & context,
253 carto::Object options )
254 {
255 Reader<carto::Volume<T> > r( filename );
256 r.setAllocatorContext( context );
257 r.setOptions( options );
258 vol.reset( r.read() );
259 return true;
260 }
261
262
263 template<typename T>
265 VolumeRefAimsFormat<T>::read( const std::string & filename,
266 const carto::AllocatorContext & context,
267 carto::Object options )
268 {
269 Reader<carto::Volume<T> > r( filename );
270 r.setAllocatorContext( context );
271 r.setOptions( options );
272 carto::VolumeRef<T> *vol = new carto::VolumeRef( r.read() );
273 return vol;
274 }
275
276
277 template<typename T>
278 bool VolumeRefAimsFormat<T>::write( const std::string & filename,
279 const carto::VolumeRef<T> & vol,
280 carto::Object options )
281 {
282 Writer<carto::Volume<T> > w( filename, options );
283 return w.write( *vol );
284 }
285
286}
287
288#endif
289
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())
virtual bool read(const std::string &filename, carto::VolumeRef< T > &vol, const carto::AllocatorContext &context, carto::Object options)
VolumeRefAimsFormat(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
std::vector< int > getSize() const
bool isNull() const
void reset(T *p=NULL)
T * release()
The class for EcatSino data write operation.