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,217 @@
1
+%  internal function
2
+
3
+%  - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
4
+
5
+function hdr = load_nii_hdr(fileprefix, machine, filetype)
6
+
7
+   if filetype == 2
8
+      fn = sprintf('%s.nii',fileprefix);
9
+
10
+      if ~exist(fn)
11
+         msg = sprintf('Cannot find file "%s.nii".', fileprefix);
12
+         error(msg);
13
+      end
14
+   else
15
+      fn = sprintf('%s.hdr',fileprefix);
16
+
17
+      if ~exist(fn)
18
+         msg = sprintf('Cannot find file "%s.hdr".', fileprefix);
19
+         error(msg);
20
+      end
21
+   end
22
+
23
+   fid = fopen(fn,'r',machine);
24
+    
25
+   if fid < 0,
26
+      msg = sprintf('Cannot open file %s.',fn);
27
+      error(msg);
28
+   else
29
+      fseek(fid,0,'bof');
30
+      hdr = read_header(fid);
31
+      fclose(fid);
32
+   end
33
+
34
+   return					% load_nii_hdr
35
+
36
+
37
+%---------------------------------------------------------------------
38
+function [ dsr ] = read_header(fid)
39
+
40
+        %  Original header structures
41
+	%  struct dsr
42
+	%       { 
43
+	%       struct header_key hk;            /*   0 +  40       */
44
+	%       struct image_dimension dime;     /*  40 + 108       */
45
+	%       struct data_history hist;        /* 148 + 200       */
46
+	%       };                               /* total= 348 bytes*/
47
+
48
+    dsr.hk   = header_key(fid);
49
+    dsr.dime = image_dimension(fid);
50
+    dsr.hist = data_history(fid);
51
+
52
+    %  For Analyze data format
53
+    %
54
+    if ~strcmp(dsr.hist.magic, 'n+1') & ~strcmp(dsr.hist.magic, 'ni1')
55
+        dsr.hist.qform_code = 0;
56
+        dsr.hist.sform_code = 0;
57
+    end
58
+
59
+    return					% read_header
60
+
61
+
62
+%---------------------------------------------------------------------
63
+function [ hk ] = header_key(fid)
64
+
65
+    fseek(fid,0,'bof');
66
+    
67
+	%  Original header structures	
68
+	%  struct header_key                     /* header key      */ 
69
+	%       {                                /* off + size      */
70
+	%       int sizeof_hdr                   /*  0 +  4         */
71
+	%       char data_type[10];              /*  4 + 10         */
72
+	%       char db_name[18];                /* 14 + 18         */
73
+	%       int extents;                     /* 32 +  4         */
74
+	%       short int session_error;         /* 36 +  2         */
75
+	%       char regular;                    /* 38 +  1         */
76
+	%       char dim_info;   % char hkey_un0;        /* 39 +  1 */
77
+	%       };                               /* total=40 bytes  */
78
+	%
79
+	% int sizeof_header   Should be 348.
80
+	% char regular        Must be 'r' to indicate that all images and 
81
+	%                     volumes are the same size. 
82
+
83
+    v6 = version;
84
+    if str2num(v6(1))<6
85
+       directchar = '*char';
86
+    else
87
+       directchar = 'uchar=>char';
88
+    end
89
+	
90
+    hk.sizeof_hdr    = fread(fid, 1,'int32')';	% should be 348!
91
+    hk.data_type     = deblank(fread(fid,10,directchar)');
92
+    hk.db_name       = deblank(fread(fid,18,directchar)');
93
+    hk.extents       = fread(fid, 1,'int32')';
94
+    hk.session_error = fread(fid, 1,'int16')';
95
+    hk.regular       = fread(fid, 1,directchar)';
96
+    hk.dim_info      = fread(fid, 1,'uchar')';
97
+    
98
+    return					% header_key
99
+
100
+
101
+%---------------------------------------------------------------------
102
+function [ dime ] = image_dimension(fid)
103
+
104
+	%  Original header structures    
105
+	%  struct image_dimension
106
+	%       {                                /* off + size      */
107
+	%       short int dim[8];                /* 0 + 16          */
108
+        %       /*
109
+        %           dim[0]      Number of dimensions in database; usually 4. 
110
+        %           dim[1]      Image X dimension;  number of *pixels* in an image row. 
111
+        %           dim[2]      Image Y dimension;  number of *pixel rows* in slice. 
112
+        %           dim[3]      Volume Z dimension; number of *slices* in a volume. 
113
+        %           dim[4]      Time points; number of volumes in database
114
+        %       */
115
+	%       float intent_p1;   % char vox_units[4];   /* 16 + 4       */
116
+	%       float intent_p2;   % char cal_units[8];   /* 20 + 4       */
117
+	%       float intent_p3;   % char cal_units[8];   /* 24 + 4       */
118
+	%       short int intent_code;   % short int unused1;   /* 28 + 2 */
119
+	%       short int datatype;              /* 30 + 2          */
120
+	%       short int bitpix;                /* 32 + 2          */
121
+	%       short int slice_start;   % short int dim_un0;   /* 34 + 2 */
122
+	%       float pixdim[8];                 /* 36 + 32         */
123
+	%	/*
124
+	%		pixdim[] specifies the voxel dimensions:
125
+	%		pixdim[1] - voxel width, mm
126
+	%		pixdim[2] - voxel height, mm
127
+	%		pixdim[3] - slice thickness, mm
128
+	%		pixdim[4] - volume timing, in msec
129
+	%					..etc
130
+	%	*/
131
+	%       float vox_offset;                /* 68 + 4          */
132
+	%       float scl_slope;   % float roi_scale;     /* 72 + 4 */
133
+	%       float scl_inter;   % float funused1;      /* 76 + 4 */
134
+	%       short slice_end;   % float funused2;      /* 80 + 2 */
135
+	%       char slice_code;   % float funused2;      /* 82 + 1 */
136
+	%       char xyzt_units;   % float funused2;      /* 83 + 1 */
137
+	%       float cal_max;                   /* 84 + 4          */
138
+	%       float cal_min;                   /* 88 + 4          */
139
+	%       float slice_duration;   % int compressed; /* 92 + 4 */
140
+	%       float toffset;   % int verified;          /* 96 + 4 */
141
+	%       int glmax;                       /* 100 + 4         */
142
+	%       int glmin;                       /* 104 + 4         */
143
+	%       };                               /* total=108 bytes */
144
+	
145
+    dime.dim        = fread(fid,8,'int16')';
146
+    dime.intent_p1  = fread(fid,1,'float32')';
147
+    dime.intent_p2  = fread(fid,1,'float32')';
148
+    dime.intent_p3  = fread(fid,1,'float32')';
149
+    dime.intent_code = fread(fid,1,'int16')';
150
+    dime.datatype   = fread(fid,1,'int16')';
151
+    dime.bitpix     = fread(fid,1,'int16')';
152
+    dime.slice_start = fread(fid,1,'int16')';
153
+    dime.pixdim     = fread(fid,8,'float32')';
154
+    dime.vox_offset = fread(fid,1,'float32')';
155
+    dime.scl_slope  = fread(fid,1,'float32')';
156
+    dime.scl_inter  = fread(fid,1,'float32')';
157
+    dime.slice_end  = fread(fid,1,'int16')';
158
+    dime.slice_code = fread(fid,1,'uchar')';
159
+    dime.xyzt_units = fread(fid,1,'uchar')';
160
+    dime.cal_max    = fread(fid,1,'float32')';
161
+    dime.cal_min    = fread(fid,1,'float32')';
162
+    dime.slice_duration = fread(fid,1,'float32')';
163
+    dime.toffset    = fread(fid,1,'float32')';
164
+    dime.glmax      = fread(fid,1,'int32')';
165
+    dime.glmin      = fread(fid,1,'int32')';
166
+        
167
+    return					% image_dimension
168
+
169
+
170
+%---------------------------------------------------------------------
171
+function [ hist ] = data_history(fid)
172
+        
173
+	%  Original header structures
174
+	%  struct data_history       
175
+	%       {                                /* off + size      */
176
+	%       char descrip[80];                /* 0 + 80          */
177
+	%       char aux_file[24];               /* 80 + 24         */
178
+	%       short int qform_code;            /* 104 + 2         */
179
+	%       short int sform_code;            /* 106 + 2         */
180
+	%       float quatern_b;                 /* 108 + 4         */
181
+	%       float quatern_c;                 /* 112 + 4         */
182
+	%       float quatern_d;                 /* 116 + 4         */
183
+	%       float qoffset_x;                 /* 120 + 4         */
184
+	%       float qoffset_y;                 /* 124 + 4         */
185
+	%       float qoffset_z;                 /* 128 + 4         */
186
+	%       float srow_x[4];                 /* 132 + 16        */
187
+	%       float srow_y[4];                 /* 148 + 16        */
188
+	%       float srow_z[4];                 /* 164 + 16        */
189
+	%       char intent_name[16];            /* 180 + 16        */
190
+	%       char magic[4];   % int smin;     /* 196 + 4         */
191
+	%       };                               /* total=200 bytes */
192
+
193
+    v6 = version;
194
+    if str2num(v6(1))<6
195
+       directchar = '*char';
196
+    else
197
+       directchar = 'uchar=>char';
198
+    end
199
+    
200
+    hist.descrip     = deblank(fread(fid,80,directchar)');
201
+    hist.aux_file    = deblank(fread(fid,24,directchar)');
202
+    hist.qform_code  = fread(fid,1,'int16')';
203
+    hist.sform_code  = fread(fid,1,'int16')';
204
+    hist.quatern_b   = fread(fid,1,'float32')';
205
+    hist.quatern_c   = fread(fid,1,'float32')';
206
+    hist.quatern_d   = fread(fid,1,'float32')';
207
+    hist.qoffset_x   = fread(fid,1,'float32')';
208
+    hist.qoffset_y   = fread(fid,1,'float32')';
209
+    hist.qoffset_z   = fread(fid,1,'float32')';
210
+    hist.srow_x      = fread(fid,4,'float32')';
211
+    hist.srow_y      = fread(fid,4,'float32')';
212
+    hist.srow_z      = fread(fid,4,'float32')';
213
+    hist.intent_name = deblank(fread(fid,16,directchar)');
214
+    hist.magic       = deblank(fread(fid,4,directchar)');
215
+    
216
+    return					% data_history
217
+