A.I.M.S


gisR.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_GISR_H
35 #define AIMS_IO_GISR_H
36 
38 #include <aims/io/defaultItemR.h>
39 #include <aims/io/gisheader.h>
40 #include <aims/io/datatypecode.h>
41 #include <aims/data/data.h>
48 // protection against MacOS macros
49 #ifdef MAP_COPY
50 #undef MAP_COPY
51 #endif
52 
53 namespace aims
54 {
55 
56  template<typename T>
57  class GisReader
58  {
59  public:
60  GisReader( const std::string& name, ItemReader<T>* ir = 0 )
61  : _name( name ), _itemr( ir )
62  {}
63  ~GisReader() { delete _itemr; }
64 
65  void read( AimsData<T>& thing, const carto::AllocatorContext & context,
66  carto::Object options );
68  { delete _itemr; _itemr = ir; }
69 
70  private:
71  std::string _name;
72  ItemReader<T> *_itemr;
73  };
74 
75 
76  template <class T>
77  inline AIMSDATA_API GisReader<T> &
78  operator >> ( GisReader<T> & reader, AimsData<T> & thing )
79  {
80  reader.read( thing );
81  return reader;
82  }
83 
84 
85  template <class T>
86  inline
88  const carto::AllocatorContext & context,
89  carto::Object options )
90  {
91  GisHeader *hdr = new GisHeader( _name );
92  try
93  {
94  hdr->read( options );
95  }
96  catch( std::exception & e )
97  {
98  delete hdr;
99  throw;
100  }
101 
103  if( hdr->dataType() != dtc.dataType() )
105  ( std::string( "Wrong data type / reader type : file is " )
106  + hdr->dataType() + ", expecting " + dtc.dataType(), _name );
107 
108  int frame = -1, border = 0;
109  options->getProperty( "frame", frame );
110  options->getProperty( "border", border );
111 
112  int dimt, tmin;
113  if( frame >= 0 )
114  {
115  if( frame >= hdr->dimT() )
116  {
117  delete hdr;
118  throw carto::invalid_format_error( "frame higher than file dimT",
119  _name );
120  }
121  dimt = 1;
122  tmin = frame;
123  }
124  else
125  {
126  dimt = hdr->dimT();
127  tmin = 0;
128  }
129 
130  std::string name = hdr->removeExtension( _name ) + ".ima";
131 
132  carto::AllocatorContext
133  cont2( context.accessMode(),
135  ( new carto::FileDataSource
136  ( name, 0,
137  context.accessMode() == carto::AllocatorStrategy::ReadWrite ?
138  carto::DataSource::ReadWrite : carto::DataSource::Read ) ),
139  carto::AllocatorStrategy::isMMapCompatible
140  ( hdr->openMode() != "binar", hdr->byteOrder(), border ),
141  context.useFactor() );
142 
143  AimsData<T> data( hdr->dimX(), hdr->dimY(), hdr->dimZ(), dimt,
144  border, cont2 );
145 
146  data.setSizeX( hdr->sizeX() );
147  data.setSizeY( hdr->sizeY() );
148  data.setSizeZ( hdr->sizeZ() );
149  data.setSizeT( hdr->sizeT() );
150 
151  carto::AllocatorStrategy::MappingMode
152  mode = data.allocator().allocatorType();
153  // if there is a border, the allocated volume is the "parent" volume.
154  if( mode == carto::AllocatorStrategy::Unallocated
155  && !data.volume()->refVolume().isNull() )
156  mode = data.volume()->refVolume()->allocatorContext().allocatorType();
157 
158  if( mode == carto::AllocatorStrategy::Memory
159  || mode == carto::AllocatorStrategy::CopyMap
160  || mode == carto::AllocatorStrategy::MAP )
161  {
162  std::ios::openmode omd = std::ios::in;
163  if( hdr->openMode() == "binar" )
164  omd |= std::ios::binary;
165  std::ifstream is( name.c_str(), omd );
166  if( !is )
167  carto::io_error::launchErrnoExcept( name );
168  if( hdr->openMode() == "binar" )
169  is.unsetf( std::ios::skipws );
170 
171  if( !_itemr )
172  _itemr = new DefaultItemReader<T>;
173  ItemReader<T> *ir
174  = _itemr->reader( hdr->openMode(),
175  hdr->byteOrder() != AIMS_MAGIC_NUMBER );
176 
177  // skip volumes to the first we want to read
178  is.seekg( ((off_t) tmin) * data.dimZ() * data.dimY() * data.dimX()
179  * sizeof( T ), std::ios::cur);
180  if( !is || is.eof() )
181  carto::io_error::launchErrnoExcept( name );
182 
183  for( int t=0; t<dimt; ++t )
184  for( int z=0; z<data.dimZ(); ++z )
185  for( int y=0; y<data.dimY(); ++y )
186  {
187  ir->read( is, &data(0,y,z,t), data.dimX() );
188  if( !is || is.eof() )
189  carto::io_error::launchErrnoExcept( name );
190  }
191 
192  delete ir;
193  }
194 
195  thing = data;
196  thing.setHeader( hdr );
197  }
198 
199 
200 }
201 
202 #endif
float sizeY() const
Get resolution in Y axis.
Definition: gisheader.h:102
GenesisReader< T > & operator>>(GenesisReader< T > &reader, AimsData< T > &thing)
Definition: genesisR.h:70
std::string dataType()
virtual bool getProperty(const std::string &key, Object &value) const =0
void read(AimsData< T > &thing, const carto::AllocatorContext &context, carto::Object options)
Definition: gisR.h:87
float sizeT() const
Get resolution in T axis.
Definition: gisheader.h:106
#define AIMSDATA_API
int dimY() const
Get dimension in Y axis.
Definition: gisheader.h:93
std::string openMode() const
Get the open mode ("binar" or "ascii")
Definition: gisheader.h:117
GisReader(const std::string &name, ItemReader< T > *ir=0)
Definition: gisR.h:60
The descriptor class of the .dim GIS header.
Definition: gisheader.h:52
float sizeZ() const
Get resolution in Z axis.
Definition: gisheader.h:104
Default low-levels readers.
Definition: defaultItemR.h:55
void setSizeX(float sizex)
void setHeader(aims::Header *hdr)
virtual std::string removeExtension(const std::string &) const
int dimX() const
Get dimension in X axis.
Definition: gisheader.h:91
const carto::AllocatorStrategy::MappingMode MAP
float sizeX() const
Get resolution in X axis.
Definition: gisheader.h:100
~GisReader()
Definition: gisR.h:63
uint byteOrder() const
Get the byte order (magic uint number)
Definition: gisheader.h:114
#define AIMS_MAGIC_NUMBER
Definition: general.h:51
int dimZ() const
Get dimension in Z axis.
Definition: gisheader.h:95
std::string dataType() const
Get type of items ("U08", "S08", "U16", "S16", ...)
Definition: gisheader.h:109
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const =0
virtual void read(std::istream &is, T &item) const
Definition: itemR.h:103
void read(carto::Object options=carto::none())
Read the header.
void setItemReader(ItemReader< T > *ir)
Definition: gisR.h:67
Low-level "small item" reader, used by higher-level file readers.
Definition: itemR.h:98
int dimT() const
Get dimension in T axis.
Definition: gisheader.h:97