A.I.M.S


bckMapR.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  * Bucket reader class
36  */
37 #ifndef AIMS_IO_BCKMAPR_H
38 #define AIMS_IO_BCKMAPR_H
39 
41 #include <aims/bucket/bucketMap.h>
42 #include <aims/io/bckheader.h>
43 #include <aims/io/defaultItemR.h>
46 
47 
48 namespace aims
49 {
50 
55  template<class T> class BckMapReader
56  {
57  public:
58  BckMapReader( const std::string& name ) : _name(name), _itemr( 0 ) {}
59  ~BckMapReader() { delete _itemr; }
60 
61  void read( BucketMap<T> & thing, int frame = -1 );
63  { delete _itemr; _itemr = ir; }
64 
65  private:
66  std::string _name;
67  ItemReader<T> *_itemr;
68  };
69 
70 
71  template<class T> inline
72  void BckMapReader<T>::read( BucketMap<T> & thing, int frame )
73  {
74  if( frame >= 0 )
75  std::cerr
76  << "Warning : .bck single frame reading not implemented yet -\n"
77  << "reading whole bucket\n";
78 
79  BckHeader hdr( _name );
80  uint32_t buf;
81  hdr.read( &buf );
82  std::ifstream::off_type offset = buf; // ## does not support large files!
83 
84  std::string fname = hdr.filename();
85  bool ascii = ( hdr.openMode() == "ascii" );
86  std::ifstream is( fname.c_str(), std::ios::in | std::ios::binary );
87  if( !is )
88  carto::io_error::launchErrnoExcept( fname );
89  is.unsetf( std::ios::skipws );
90  is.seekg( offset );
91  if( !is )
92  carto::io_error::launchErrnoExcept( fname );
93 
94  if( !_itemr )
95  _itemr = new DefaultItemReader<T>;
96  ItemReader<T> *ir
97  = _itemr->reader( hdr.openMode(), hdr.byteOrder() != AIMS_MAGIC_NUMBER );
100  = sr1.reader( hdr.openMode(), hdr.byteOrder() != AIMS_MAGIC_NUMBER );
102  ItemReader<Point3d> *posr
103  = posr1.reader( hdr.openMode(), hdr.byteOrder() != AIMS_MAGIC_NUMBER );
104 
105  thing.clear();
106  thing.setHeader( hdr );
107 
108  uint32_t time=0, nitem=0;
109  int size = hdr.dimT();
110  std::string tmp;
111 
112  for( int t=0; t<size; ++t )
113  {
114  if( ascii )
115  {
116  carto::StreamUtil::skip( is );
117  is >> tmp;
118  if( tmp != "-time" )
119  {
120  delete sr;
121  delete ir;
122  delete posr;
123  throw carto::invalid_format_error( fname );
124  }
125  }
126  sr->read( is, time );
127  if( ascii )
128  {
129  carto::StreamUtil::skip( is );
130  is >> tmp;
131  if( tmp != "-dim" )
132  {
133  delete sr;
134  delete ir;
135  delete posr;
136  throw carto::invalid_format_error( fname );
137  }
138  }
139  sr->read( is, nitem );
140  if( !is )
141  {
142  delete sr;
143  delete ir;
144  delete posr;
145  carto::io_error::launchErrnoExcept( fname );
146  }
147  typename BucketMap<T>::Bucket & items = thing[ time ];
148  Point3d pos;
149  T item;
150 
151  //std::cout << "time: " << time << ", nitem: " << nitem << "\n";
152 
153  for( uint32_t n=0; n<nitem; ++n )
154  {
155  posr->read( is, pos );
156  ir->read( is, item );
157  items[ pos ] = item;
158  }
159  if( !is )
160  {
161  delete sr;
162  delete ir;
163  delete posr;
164  carto::io_error::launchErrnoExcept( fname );
165  }
166  }
167 
168  delete sr;
169  delete ir;
170  delete posr;
171  }
172 
173 }
174 
175 
176 template<class T> inline
178  aims::BucketMap<T> & thing )
179 {
180  reader.read( thing );
181  return reader;
182 }
183 
184 
185 #endif
aims::BckMapReader< T > & operator>>(aims::BckMapReader< T > &reader, aims::BucketMap< T > &thing)
Definition: bckMapR.h:177
std::string openMode() const
Definition: bckheader.h:73
Descriptor class for the .bck BUCKET file format header.
Definition: bckheader.h:51
virtual int dimT() const
Definition: bckheader.h:66
uint byteOrder() const
Definition: bckheader.h:72
BucketMap objects can be read / written by BckMapReader / BckMapWriter, these IO classes operate on t...
Definition: bckMapR.h:55
BckMapReader(const std::string &name)
Definition: bckMapR.h:58
Default low-levels readers.
Definition: defaultItemR.h:55
An alternate, ordered, representation for buckets (voxels lists).
Definition: bucket.h:57
std::map< Point3d, T, BucketMapLess > Bucket
Definition: bucketMap.h:102
virtual ItemReader< T > * reader(const std::string &openmode="binar", bool bswap=false) const
Definition: defaultItemR.h:154
std::string filename() const
void read(BucketMap< T > &thing, int frame=-1)
Definition: bckMapR.h:72
#define AIMS_MAGIC_NUMBER
Definition: general.h:51
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 setHeader(const aims::PythonHeader &hdr)
Definition: bucketMap.h:150
Low-level "small item" reader, used by higher-level file readers.
Definition: itemR.h:98
void setItemReader(ItemReader< T > *ir)
Definition: bckMapR.h:62
void read(uint32_t *offset=0)