aimsdata  5.1.2
Neuroimaging data handling
giftitextureformat_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_GIFTITEXTUREFORMAT_D_H
35 #define AIMS_IO_GIFTITEXTUREFORMAT_D_H
36 
37 #include <aims/io/giftiformat.h>
38 #include <aims/io/giftiheader.h>
39 #include <aims/io/giftiutil.h>
40 #include <cartobase/thread/mutex.h>
41 #include <aims/io/gifti.h>
42 
43 
44 namespace aims
45 {
46 
47  template<typename T>
48  bool GiftiTextureFormat<T>::read( const std::string & filename,
49  TimeTexture<T> & vol,
50  const carto::AllocatorContext
51  & /*context*/,
52  carto::Object options )
53  {
54 
55  // std::cout << "gifti texture read\n";
56 
57  GiftiHeader hdr( filename );
58 
59  setOptions(options);
60 
61  if( !hdr.read() )
63 
65  gifti_image *gim = gifti_read_image( hdr.name().c_str(), 1 );
67  if( !gim )
68  {
69  throw carto::format_error( "could not re-read GIFTI file", hdr.name() );
70  }
71 
72  int nda = gim->numDA, i;
73  int ttex = 0;
74  for( i=0; i<nda; ++i )
75  {
76  giiDataArray *da = gim->darray[i];
77  switch( da->intent )
78  {
79  case NIFTI_INTENT_POINTSET:
80  break;
81  case NIFTI_INTENT_VECTOR:
82  break;
83  case NIFTI_INTENT_TRIANGLE:
84  break;
85 
86  default:
87  {
88  // std::cout << "type : " << gifti_intent_to_string(da->intent) << "\n";
89 
90  int vnum = da->dims[0];
91  int j;
92  std::vector<T> & tex = vol[ttex].data();
93  tex.clear();
94  tex.reserve( vnum );
95  for( j=0; j<vnum; ++j )
96  {
97  tex.push_back( convertedNiftiArrayValue<T>( da->data, j,
98  da->datatype,
99  vnum ) );
100  }
101  ++ttex;
102  }
103  }
104  }
105 
106  vol.setHeader( hdr );
107  gifti_free_image( gim );
108 
109  // std::cout << "OK\n";
110  return true;
111  }
112 
113  template<typename T>
114  bool GiftiTextureFormat<T>::write( const std::string & filename,
115  const TimeTexture<T> & thing,
116  carto::Object )
117  {
118 
119  // std::cout << "gifti texture write\n";
120 
121  try
122  {
123  const PythonHeader & thdr = thing.header();
124  GiftiHeader hdr( filename );
125  hdr.copy( thdr );
126  hdr.setProperty( "data_type", carto::DataTypeCode<T>::name() );
127 
128  if( hdr.hasProperty( "nb_t_pos" ) )
129  hdr.removeProperty( "nb_t_pos" );
130 
131  gifti_image *gim = hdr.giftiImageBase();
132  std::string fname = hdr.name();
133 
134  int hdrtexda = 0;
135  carto::Object da_info;
136  try
137  {
138  da_info = thdr.getProperty( "GIFTI_dataarrays_info" );
139  }
140  catch( ... )
141  {
142  }
143 
144  hdr.setOptions(options());
145 
146  hdr.giftiAddTexture( gim, thing);
147  // add external textures
148  hdr.giftiAddExternalTextures( gim, hdrtexda, da_info );
149 
150  carto::Object da_label;
151  try
152  {
153  da_label = thdr.getProperty( "GIFTI_labels_table" );
154  // std::cout << "ecriture label OK\n";
155  }
156  catch( ... )
157  {
158  }
159  // labels table
160  hdr.giftiAddLabelTable( gim );
161 
162  // write all
164  gifti_write_image( gim, fname.c_str(), 1 );
166  gifti_free_image( gim );
167  // .minf header
168  if( hdr.hasProperty( "GIFTI_metadata") )
169  hdr.removeProperty( "GIFTI_metadata" );
170  if( hdr.hasProperty( "GIFTI_version" ) )
171  hdr.removeProperty( "GIFTI_version" );
172  if( hdr.hasProperty( "GIFTI_dataarrays_info" ) )
173  hdr.removeProperty( "GIFTI_dataarrays_info" );
174  if( hdr.hasProperty( "file_type" ) )
175  hdr.removeProperty( "file_type" );
176  if( hdr.hasProperty( "GIFTI_labels_table") )
177  hdr.removeProperty( "GIFTI_labels_table" );
178  hdr.setProperty( "object_type", "Texture" );
179  hdr.writeMinf( fname + ".minf" );
180 
181  // std::cout << "OK\n";
182  return true;
183  }
184  catch( std::exception & e )
185  {
186  return false;
187  }
188  return true;
189  }
190 }
191 
192 #endif
const aims::PythonHeader & header() const
Get the header.
Definition: texture.h:143
void reserve(size_t size)
Definition: texture.h:155
void setHeader(const aims::PythonHeader &hdr)
Set the header.
Definition: texture.h:147
GIFTI Header class.
Definition: giftiheader.h:56
static carto::Mutex & giftiMutex()
const std::string & name() const
void setOptions(carto::Object opt)
Definition: giftiheader.h:67
virtual bool read(const std::string &filename, TimeTexture< T > &vol, const carto::AllocatorContext &context, carto::Object options)
virtual bool write(const std::string &filename, const TimeTexture< T > &vol, carto::Object options=carto::none())
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:52
virtual void copy(const PythonHeader &, bool keepUuid=false)
virtual bool writeMinf(const std::string &filename)
write meta-info header, non-const version (may change some attributes)
virtual bool getProperty(const std::string &, Object &) const
virtual bool removeProperty(const std::string &)
virtual void setProperty(const std::string &, Object)
virtual bool hasProperty(const std::string &) const
static void launchErrnoExcept(const std::string &filename="")
giiDataArray * da
The class for EcatSino data write operation.
Definition: borderfiller.h:13