Browse code

searchlight ready. missing nifti-image-write support. added timing to FBS

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@167 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on30/03/2009 17:54:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,207 @@
1
+%  internal function
2
+
3
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
4
+
5
+function save_nii_hdr(hdr, fid)
6
+
7
+   if ~isequal(hdr.hk.sizeof_hdr,348),
8
+      error('hdr.hk.sizeof_hdr must be 348.');
9
+   end
10
+
11
+   write_header(hdr, fid);
12
+
13
+   return;					% save_nii_hdr
14
+
15
+
16
+%---------------------------------------------------------------------
17
+function write_header(hdr, fid)
18
+
19
+        %  Original header structures
20
+	%  struct dsr				/* dsr = hdr */
21
+	%       { 
22
+	%       struct header_key hk;            /*   0 +  40       */
23
+	%       struct image_dimension dime;     /*  40 + 108       */
24
+	%       struct data_history hist;        /* 148 + 200       */
25
+	%       };                               /* total= 348 bytes*/
26
+   
27
+   header_key(fid, hdr.hk);
28
+   image_dimension(fid, hdr.dime);
29
+   data_history(fid, hdr.hist);
30
+   
31
+   %  check the file size is 348 bytes
32
+   %
33
+   fbytes = ftell(fid);
34
+   
35
+   if ~isequal(fbytes,348),
36
+      msg = sprintf('Header size is not 348 bytes.');
37
+      warning(msg);
38
+   end
39
+    
40
+   return;					% write_header
41
+
42
+
43
+%---------------------------------------------------------------------
44
+function header_key(fid, hk)
45
+   
46
+   fseek(fid,0,'bof');
47
+
48
+	%  Original header structures    
49
+	%  struct header_key                      /* header key      */ 
50
+	%       {                                /* off + size      */
51
+	%       int sizeof_hdr                   /*  0 +  4         */
52
+	%       char data_type[10];              /*  4 + 10         */
53
+	%       char db_name[18];                /* 14 + 18         */
54
+	%       int extents;                     /* 32 +  4         */
55
+	%       short int session_error;         /* 36 +  2         */
56
+	%       char regular;                    /* 38 +  1         */
57
+	%       char dim_info;   % char hkey_un0;        /* 39 +  1 */
58
+	%       };                               /* total=40 bytes  */
59
+        
60
+   fwrite(fid, hk.sizeof_hdr(1),    'int32');	% must be 348.
61
+    
62
+   % data_type = sprintf('%-10s',hk.data_type);	% ensure it is 10 chars from left
63
+   % fwrite(fid, data_type(1:10), 'uchar');
64
+   pad = zeros(1, 10-length(hk.data_type));
65
+   hk.data_type = [hk.data_type  char(pad)];
66
+   fwrite(fid, hk.data_type(1:10), 'uchar');
67
+    
68
+   % db_name   = sprintf('%-18s', hk.db_name);	% ensure it is 18 chars from left
69
+   % fwrite(fid, db_name(1:18), 'uchar');
70
+   pad = zeros(1, 18-length(hk.db_name));
71
+   hk.db_name = [hk.db_name  char(pad)];
72
+   fwrite(fid, hk.db_name(1:18), 'uchar');
73
+    
74
+   fwrite(fid, hk.extents(1),       'int32');
75
+   fwrite(fid, hk.session_error(1), 'int16');
76
+   fwrite(fid, hk.regular(1),       'uchar');	% might be uint8
77
+    
78
+   % fwrite(fid, hk.hkey_un0(1),    'uchar');
79
+   % fwrite(fid, hk.hkey_un0(1),    'uint8');
80
+   fwrite(fid, hk.dim_info(1),      'uchar');
81
+    
82
+   return;					% header_key
83
+
84
+
85
+%---------------------------------------------------------------------
86
+function image_dimension(fid, dime)
87
+
88
+	%  Original header structures        
89
+	%  struct image_dimension
90
+	%       {                                /* off + size      */
91
+	%       short int dim[8];                /* 0 + 16          */
92
+	%       float intent_p1;   % char vox_units[4];   /* 16 + 4       */
93
+	%       float intent_p2;   % char cal_units[8];   /* 20 + 4       */
94
+	%       float intent_p3;   % char cal_units[8];   /* 24 + 4       */
95
+	%       short int intent_code;   % short int unused1;   /* 28 + 2 */
96
+	%       short int datatype;              /* 30 + 2          */
97
+	%       short int bitpix;                /* 32 + 2          */
98
+	%       short int slice_start;   % short int dim_un0;   /* 34 + 2 */
99
+	%       float pixdim[8];                 /* 36 + 32         */
100
+	%			/*
101
+	%				pixdim[] specifies the voxel dimensions:
102
+	%				pixdim[1] - voxel width
103
+	%				pixdim[2] - voxel height
104
+	%				pixdim[3] - interslice distance
105
+	%				pixdim[4] - volume timing, in msec
106
+	%					..etc
107
+	%			*/
108
+	%       float vox_offset;                /* 68 + 4          */
109
+	%       float scl_slope;   % float roi_scale;     /* 72 + 4 */
110
+	%       float scl_inter;   % float funused1;      /* 76 + 4 */
111
+	%       short slice_end;   % float funused2;      /* 80 + 2 */
112
+	%       char slice_code;   % float funused2;      /* 82 + 1 */
113
+	%       char xyzt_units;   % float funused2;      /* 83 + 1 */
114
+	%       float cal_max;                   /* 84 + 4          */
115
+	%       float cal_min;                   /* 88 + 4          */
116
+	%       float slice_duration;   % int compressed; /* 92 + 4 */
117
+	%       float toffset;   % int verified;          /* 96 + 4 */
118
+	%       int glmax;                       /* 100 + 4         */
119
+	%       int glmin;                       /* 104 + 4         */
120
+	%       };                               /* total=108 bytes */
121
+	
122
+   fwrite(fid, dime.dim(1:8),        'int16');
123
+   fwrite(fid, dime.intent_p1(1),  'float32');
124
+   fwrite(fid, dime.intent_p2(1),  'float32');
125
+   fwrite(fid, dime.intent_p3(1),  'float32');
126
+   fwrite(fid, dime.intent_code(1),  'int16');
127
+   fwrite(fid, dime.datatype(1),     'int16');
128
+   fwrite(fid, dime.bitpix(1),       'int16');
129
+   fwrite(fid, dime.slice_start(1),  'int16');
130
+   fwrite(fid, dime.pixdim(1:8),   'float32');
131
+   fwrite(fid, dime.vox_offset(1), 'float32');
132
+   fwrite(fid, dime.scl_slope(1),  'float32');
133
+   fwrite(fid, dime.scl_inter(1),  'float32');
134
+   fwrite(fid, dime.slice_end(1),    'int16');
135
+   fwrite(fid, dime.slice_code(1),   'uchar');
136
+   fwrite(fid, dime.xyzt_units(1),   'uchar');
137
+   fwrite(fid, dime.cal_max(1),    'float32');
138
+   fwrite(fid, dime.cal_min(1),    'float32');
139
+   fwrite(fid, dime.slice_duration(1), 'float32');
140
+   fwrite(fid, dime.toffset(1),    'float32');
141
+   fwrite(fid, dime.glmax(1),        'int32');
142
+   fwrite(fid, dime.glmin(1),        'int32');
143
+   
144
+   return;					% image_dimension
145
+
146
+
147
+%---------------------------------------------------------------------
148
+function data_history(fid, hist)
149
+    
150
+	% Original header structures
151
+	%struct data_history       
152
+	%       {                                /* off + size      */
153
+	%       char descrip[80];                /* 0 + 80          */
154
+	%       char aux_file[24];               /* 80 + 24         */
155
+	%       short int qform_code;            /* 104 + 2         */
156
+	%       short int sform_code;            /* 106 + 2         */
157
+	%       float quatern_b;                 /* 108 + 4         */
158
+	%       float quatern_c;                 /* 112 + 4         */
159
+	%       float quatern_d;                 /* 116 + 4         */
160
+	%       float qoffset_x;                 /* 120 + 4         */
161
+	%       float qoffset_y;                 /* 124 + 4         */
162
+	%       float qoffset_z;                 /* 128 + 4         */
163
+	%       float srow_x[4];                 /* 132 + 16        */
164
+	%       float srow_y[4];                 /* 148 + 16        */
165
+	%       float srow_z[4];                 /* 164 + 16        */
166
+	%       char intent_name[16];            /* 180 + 16        */
167
+	%       char magic[4];   % int smin;     /* 196 + 4         */
168
+	%       };                               /* total=200 bytes */
169
+	
170
+   % descrip     = sprintf('%-80s', hist.descrip);     % 80 chars from left
171
+   % fwrite(fid, descrip(1:80),    'uchar');
172
+   pad = zeros(1, 80-length(hist.descrip));
173
+   hist.descrip = [hist.descrip  char(pad)];
174
+   fwrite(fid, hist.descrip(1:80), 'uchar');
175
+    
176
+   % aux_file    = sprintf('%-24s', hist.aux_file);    % 24 chars from left
177
+   % fwrite(fid, aux_file(1:24),   'uchar');
178
+   pad = zeros(1, 24-length(hist.aux_file));
179
+   hist.aux_file = [hist.aux_file  char(pad)];
180
+   fwrite(fid, hist.aux_file(1:24), 'uchar');
181
+    
182
+   fwrite(fid, hist.qform_code,    'int16');
183
+   fwrite(fid, hist.sform_code,    'int16');
184
+   fwrite(fid, hist.quatern_b,   'float32');
185
+   fwrite(fid, hist.quatern_c,   'float32');
186
+   fwrite(fid, hist.quatern_d,   'float32');
187
+   fwrite(fid, hist.qoffset_x,   'float32');
188
+   fwrite(fid, hist.qoffset_y,   'float32');
189
+   fwrite(fid, hist.qoffset_z,   'float32');
190
+   fwrite(fid, hist.srow_x(1:4), 'float32');
191
+   fwrite(fid, hist.srow_y(1:4), 'float32');
192
+   fwrite(fid, hist.srow_z(1:4), 'float32');
193
+
194
+   % intent_name = sprintf('%-16s', hist.intent_name);	% 16 chars from left
195
+   % fwrite(fid, intent_name(1:16),    'uchar');
196
+   pad = zeros(1, 16-length(hist.intent_name));
197
+   hist.intent_name = [hist.intent_name  char(pad)];
198
+   fwrite(fid, hist.intent_name(1:16), 'uchar');
199
+    
200
+   % magic	= sprintf('%-4s', hist.magic);		% 4 chars from left
201
+   % fwrite(fid, magic(1:4),           'uchar');
202
+   pad = zeros(1, 4-length(hist.magic));
203
+   hist.magic = [hist.magic  char(pad)];
204
+   fwrite(fid, hist.magic(1:4),        'uchar');
205
+    
206
+   return;					% data_history
207
+