aimsdata 6.0.0
Neuroimaging data handling
genesisR.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
38#ifndef AIMS_IO_GENESISR_H
39#define AIMS_IO_GENESISR_H
40
45#include <aims/data/data.h>
47#include <cartobase/stream/fileutil.h>
48#include <cartobase/exception/file.h>
49#include <cartobase/exception/format.h>
51
52namespace aims
53{
54
55 template<class T> class GenesisReader
56 {
57 public:
58 GenesisReader( const std::string & name ) : _name( name ) {}
60
61 void read( AimsData<T> & thing, const carto::AllocatorContext & context,
62 carto::Object options );
63
64 private:
65 std::string _name;
66 };
67
68 template <class T>
69 inline GenesisReader<T> &
71 {
72 reader.read( thing );
73 return reader;
74 }
75
76 template <class T>
77 inline
79 const carto::AllocatorContext & context,
80 carto::Object options )
81 {
82 GenesisHeader *hdr = new GenesisHeader( _name );
83 try
84 {
85 hdr->read();
86 }
87 catch( std::exception & e )
88 {
89 delete hdr;
90 throw;
91 }
92
94 if( hdr->dataType() != dtc.dataType() )
96 ( std::string( "data type / reader type mismatch : file is " )
97 + hdr->dataType() + ", expecting " + dtc.dataType(), _name );
98
99 int frame = -1, border = 0;
100 options->getProperty( "frame", frame );
101 options->getProperty( "border", border );
102
103 int dimt;
104 if( frame >= 0 )
105 {
106 if( frame >= hdr->dimT() )
107 {
108 delete hdr;
109 throw std::domain_error( "frame higher than file dimT" );
110 }
111 dimt = 1;
112 }
113 else
114 {
115 dimt = hdr->dimT();
116 }
117
118 /*cout << "dimensions: " << hdr->dimX() << ", " << hdr->dimY() << ", "
119 << hdr->dimZ() << ", " << dimt << endl;*/
120
121 carto::AllocatorContext
122 ac( context.accessMode(),
124 ( new carto::FileDataSource( _name, 0, carto::DataSource::Read ) ),
125 false, context.useFactor() );
126
127 AimsData<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(), dimt,
128 border, ac );
129
130 data.setSizeX( hdr->sizeX() );
131 data.setSizeY( hdr->sizeY() );
132 data.setSizeZ( hdr->sizeZ() );
133 data.setSizeT( hdr->sizeT() );
134 data.setHeader( hdr );
135
136 // we must scandir to obtain a files list
137
139 std::string::size_type pos = _name.rfind( s );
140 std::string dirname;
141 if( pos == std::string::npos )
142 dirname = ".";
143 else
144 dirname = _name.substr( 0, pos );
145
146 carto::Directory dir( dirname );
147 std::set<std::string> names = dir.files();
148 std::set<std::string>::iterator in, en = names.end();
149 std::string name;
150 int z, t, ss = 0, es = 0;
151 std::vector<bool> reads( data.dimT() * data.dimZ() );
152
153 z = data.dimZ(); // temp
154 for( in=names.begin(); in!=en; ++in )
155 {
156 name = dirname + s + *in;
157 //cout << "read " << name << endl;
158 GenesisHeader h( name ); // (re)read each header
159
160 //std::cout << "read " << name << "(byte swapping: " << h.byteSwapping() << ")" << std::endl;
161 try
162 {
163 h.read();
164 std::ifstream is( name.c_str(),
165 std::ios::in | std::ios::binary );
166 if( !is )
168 is.unsetf( std::ios::skipws );
169 is.seekg( h.dataOffset() );
170 if( !is )
172 if( h.compressCode() != 1 )
173 throw
174 carto::format_mismatch_error( "Genesis reader can't deal with "
175 "compressed data", name );
176 t = 0; // temporarily
177 h.getProperty( "slice", z );
178 h.getProperty( "start_slice", ss );
179 h.getProperty( "end_slice", es );
180 z = abs(es - z);
181 if( (z >= 0) && (z < data.dimZ()) /*&& (t >= 0) && (t < data.dimT())*/ ) //FR: unuseful checks
182 {
183 if( reads[ t*data.dimZ() + z ] )
184 {
185 std::cerr << "warning: slice z=" << z << ", t=" << t
186 << " already read" << std::endl;
187 }
188 else
189 {
190 std::cerr << "........reading slice z=" << z << ", t=" << t << std::endl;
191 reads[ t*data.dimZ() + z ] = true;
192
194 ItemReader<T> *ir = ib1.reader( "binar", h.byteSwapping() );
195 for( int y=0; y<data.dimY(); ++y )
196 ir->read( is, &data(0,y,z,t), data.dimX() );
197
198 delete ir;
199 }
200 }
201 }
202 catch( std::exception & )
203 {
204 std::cout << name << " not part of genesis volume\n";
205 }
206 }
207
208 //cout << "read finished, checking if we've got all slices...\n";
209 for( t=0; t<data.dimT(); ++t )
210 for( z=0; z<data.dimZ(); ++z )
211 if( !reads[ t*data.dimZ() + z ] )
212 std::cerr << "warning: missing slice " << z << ", t=" << t
213 << std::endl;
214 thing = data;
215 }
216}
217
218#endif
void setSizeY(float sizey)
void setSizeT(float sizet)
void setHeader(aims::Header *hdr)
int dimY() const
int dimX() const
int dimT() const
int dimZ() const
void setSizeZ(float sizez)
void setSizeX(float sizex)
Default low-levels readers.
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const
GE Genesis format header.
int compressCode() const
float sizeZ() const
float sizeT() const
int dataOffset() const
float sizeX() const
bool byteSwapping() const
std::string dataType() const
float sizeY() const
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition genesisR.h:78
GenesisReader(const std::string &name)
Definition genesisR.h:58
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
std::string dataType()
std::set< std::string > files() const
static char separator()
virtual bool getProperty(const std::string &, Object &) const
static void launchErrnoExcept(const std::string &filename="")
The class for EcatSino data write operation.
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
Definition genesisR.h:70