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