aimsdata 6.0.0
Neuroimaging data handling
fsannotformatreader_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_SOMA_FSANNOTFORMATREADER_D_H
35#define AIMS_IO_SOMA_FSANNOTFORMATREADER_D_H
36
37//-------------------------------------------------------------------
39#include <aims/mesh/texture.h>
41#include <soma-io/datasourceinfo/datasourceinfoloader.h>
43//--- debug ------------------------------------------------------------------
44#include <cartobase/config/verbose.h>
45#define localMsg( message ) cartoCondMsg( 4, message, "FSANNOTFORMATREADER" )
46// localMsg must be undef at end of file
47//----------------------------------------------------------------------------
48
49
50// from http://www.grahamwideman.com/gw/brain/fs/surfacefileformats.htm
51
52namespace soma
53{
54
55 template <typename T>
57 FsAnnotFormatReader<T>::createAndRead( rc_ptr<DataSourceInfo> dsi,
58 const AllocatorContext & context,
59 Object options )
60 {
61 carto::rc_ptr<soma::DataSource> ds = dsi->list().dataSource();
62 localMsg( "createAndRead " + ds->url() );
64 try
65 {
66 read( *mesh, dsi, context, options );
67 }
68 catch( ... )
69 {
70 delete mesh;
71 throw;
72 }
73 mesh->header().copyProperties( dsi->header() );
74 return mesh;
75 }
76
77
78 template <typename T>
80 rc_ptr<DataSourceInfo> dsi,
81 const AllocatorContext & /* context */,
82 Object options )
83 {
84 // std::cout << "FsAnnotFormatReader::read " << dsi->list().dataSource()->url() << std::endl;
85 carto::rc_ptr<DataSource> ds = dsi->list().dataSource();
86
87 if( !ds->isOpen() )
88 {
89 std::cout << "WARNING: reopening freesurfer curv file at beginning\n";
90 ds->open( DataSource::Read ); // FIXME recover offset
91 }
92 if( !ds->isOpen() )
93 throw file_not_found_error( ds->url() );
94
95 carto::Object hdr = dsi->header();
96
97 int asciii = 0;
98 int bswapi = 0;
99 hdr->getProperty( "ascii", asciii );
100 bool ascii = bool( asciii );
101 hdr->getProperty( "byte_swapping", bswapi );
102 bool bswap = bool( bswapi );
103 int nv = 0, np = 0, ps;
104 hdr->getProperty( "vertex_number", nv );
105
106 if( !readTexture( ds, obj, nv, ascii, bswap ) )
107 throw carto::invalid_format_error( ds->url() );
108
109
111 rc_ptr<DataSource> mds( 0 );
112 try
113 {
114 mds = dsi->list().dataSource( "minf" );
115 if( mds.get() )
116 DataSourceInfoLoader::readMinf( *mds, hdr, options );
117 }
118 catch( ... )
119 {
120 }
121
122 obj.header().copyProperties( hdr );
123 if( dsi->header().get() )
124 obj.header().copyProperties( dsi->header() );
125
126 // std::cout << "read FSANNOT OK\n";
127 }
128
129
130 template <typename T>
133 {
134 return new FsAnnotFormatReader;
135 }
136
137
138 template <typename T>
140 TimeTexture<T> & obj,
141 int nv, bool ascii,
142 bool bswap ) const
143 {
144 // std::cout << "readTexture Annot: " << nv << ", " << ascii << ", " << bswap << std::endl;
145
147 std::unique_ptr<ItemReader<uint32_t> > itemr;
148 itemr.reset( dir.reader( !ascii, bswap ) );
149
150 std::vector<T> & tex = obj[0].data();
151 tex.resize( nv );
152
153 std::vector<uint32_t> data( nv * 2 );
154 long n = itemr->read( *ds, &data[0], nv * 2 );
155 if( n != nv * 2 )
156 {
157 std::cout << "not read the right size: " << n << " instead of expected " << nv * 2 << std::endl;
158 return false;
159 }
160
161 if( !ds->isOpen() || ds->eof() )
162 return false; // no color table ?
163
164 std::vector<uint32_t> v( 4, 0 );
165 n = itemr->read( *ds, &v[0], 4 );
166 if( n < 4 )
167 {
168 std::cout << "EOF\n";
169 return false;
170 }
171 if( v[0] != 1 || v[1] != 0xfffffffe )
172 {
173 std::cout << "unexpected values after data table: " << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << std::endl;
174 return false;
175 }
176 if( !ds->isOpen() || ds->eof() )
177 return false; // no color table ?
178
179 std::string line;
180 bool r = StreamUtil::getline( *ds, line );
181 if( !r )
182 {
183 std::cout << "EOF\n";
184 return false;
185 }
186 obj.header().setProperty( "colormap_filename", line );
187 if( line.length() + 1 != v[3] )
188 std::cout << "colortable filename string length mismatch\n";
189 n = itemr->read( *ds, &v[0], 1 );
190 if( n < 1 )
191 {
192 std::cout << "EOF\n";
193 return false;
194 }
195 // std::cout << "cmap values read: " << v[0] << std::endl;
196 unsigned ncmap = v[0];
197
198 carto::Object cmap = Object::value( std::map<int, carto::Object>() );
199 obj.header().setProperty( "GIFTI_labels_table", cmap );
200 std::map<uint32_t, T> rev_cols;
201
202 int i = 0;
203 T lindex = 0;
204 while( ds->isOpen() && !ds->eof() )
205 {
206 n = itemr->read( *ds, &v[0], 2 );
207 if( n != 2 )
208 {
209 std::cout << "EOF in cmap reading\n";
210 return false;
211 }
212 r = StreamUtil::getline( *ds, line );
213 if( !r )
214 {
215 std::cout << "EOF in cmap label reading\n";
216 return false;
217 }
218 if( line.length() + 1 != v[1] )
219 std::cout << "string length mismatch in cmap label: " << line
220 << std::endl;
221 carto::Object cmitem = Object::value( carto::Dictionary() );
222 cmitem->setProperty( "Label", line );
223 lindex = T( v[0] );
224 cmap->setArrayItem( v[0], cmitem );
225 n = itemr->read( *ds, &v[0], 4 );
226 if( n < 4 )
227 {
228 std::cout << "EOF in cmap colors reading\n";
229 return false;
230 }
231 std::vector<float> col( 4, 1. );
232 col[0] = float( v[0] ) / 255.;
233 col[1] = float( v[1] ) / 255.;
234 col[2] = float( v[2] ) / 255.;
235 col[3] = float( 255 - v[3] ) / 255.;
236 cmitem->setProperty( "RGB", col );
237
238 uint32_t rgb = v[2] * 0x10000 + v[1] * 0x100 + v[0];
239 rev_cols[rgb] = lindex;
240 ++i;
241 }
242 if( i != ncmap )
243 std::cout << "wrong cmap entries found: " << i << " items, expected: "
244 << ncmap << std::endl;
245
246 uint32_t index, value;
247 typename std::map<uint32_t, T>::const_iterator ic, ec = rev_cols.end();
248 for( i=0; i<nv; ++i )
249 {
250 index = data[i * 2 ];
251 if( index >= nv )
252 {
253 std::cout << "wrong index at: " << i << ": " << index << std::endl;
254 return false;
255 }
256 value = data[ i * 2 + 1 ];
257
258 ic = rev_cols.find( value );
259 if( ic != ec )
260 tex[i] = ic->second;
261 }
262
263 return true;
264 }
265
266
267}
268
269#undef localMsg
270
271#endif
const aims::PythonHeader & header() const
Get the header.
Definition texture.h:143
virtual void copyProperties(Object source)
static Object value()
virtual void setProperty(const std::string &, Object)
static carto::Object readMinf(DataSource &ds, carto::Object base=carto::none(), carto::Object options=carto::none())
virtual ItemReader< T > * reader(bool binary=true, bool bswap=false) const
Freesurfer texture (curvature) format.
bool readTexture(carto::rc_ptr< DataSource > ds, TimeTexture< T > &obj, int nv, bool ascii, bool bswap) const
virtual FormatReader< TimeTexture< T > > * clone() const
virtual TimeTexture< T > * createAndRead(carto::rc_ptr< DataSourceInfo > dsi, const AllocatorContext &context, carto::Object options)
virtual void read(TimeTexture< T > &obj, carto::rc_ptr< DataSourceInfo > dsi, const AllocatorContext &context, carto::Object options)
static bool getline(DataSource &ds, std::string &)
std::map< std::string, Object > Dictionary
#define localMsg(message)
Definition reader_d.h:48