Debian Codegraph @

conversion/ana2mnc/doc/spm_hwrite.m

raw
1 function [s] = spm_hwrite(P,DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP)
2 % writes a header
3 % FORMAT [s] = spm_hwrite(P,DIM,VOX,SCALE,TYPE,OFFSET,ORIGIN,DESCRIP);
4 %
5 % P - filename (e.g 'spm' or 'spm.img')
6 % DIM - image size [i j k [l]] (voxels)
7 % VOX - voxel size [x y z [t]] (mm [sec])
8 % SCALE - scale factor
9 % TYPE - datatype (integer - see spm_type)
10 % OFFSET - offset (bytes)
11 % ORIGIN - [i j k] of origin (default = [0 0 0])
12 % DESCRIP - description string (default = 'spm compatible')
13 %
14 % s - number of elements successfully written (should be 348)
15 %___________________________________________________________________________
16 %
17 % spm_hwrite writes variables from working memory into a SPM/ANALYZE
18 % compatible header file. The 'originator' field of the ANALYZE format has
19 % been changed to ORIGIN in the SPM version of the header. funused1
20 % of the ANALYZE format is used for SCALE
21 %
22 % see also dbh.h (ANALYZE) spm_hread.m and spm_type.m
23 %
24 %__________________________________________________________________________
25 % @(#)spm_hwrite.m 2.2 99/10/29
26
27
28 % ensure correct suffix {.hdr} and open header file
29 %---------------------------------------------------------------------------
30 P = P(P ~= ' ');
31 q = length(P);
32 if q>=4 & P(q - 3) == '.', P = P(1:(q - 4)); end;
33 P = [P '.hdr'];
34
35 % For byte swapped data-types, also swap the bytes around in the headers.
36 mach = 'native';
37 if spm_type(TYPE,'swapped'),
38 if spm_platform('bigend'),
39 mach = 'ieee-le';
40 else,
41 mach = 'ieee-be';
42 end;
43 TYPE = spm_type(spm_type(TYPE));
44 end;
45 fid = fopen(P,'w',mach);
46
47 if (fid == -1),
48 error(['Error opening ' P '. Check that you have write permission.']);
49 end;
50 %---------------------------------------------------------------------------
51 data_type = ['dsr ' 0];
52
53 P = [P ' '];
54 db_name = [P(1:17) 0];
55
56 % set header variables
57 %---------------------------------------------------------------------------
58 DIM = DIM(:)'; if size(DIM,2) < 4; DIM = [DIM 1]; end
59 VOX = VOX(:)'; if size(VOX,2) < 4; VOX = [VOX 0]; end
60 dim = [4 DIM(1:4) 0 0 0];
61 pixdim = [0 VOX(1:4) 0 0 0];
62 vox_offset = OFFSET;
63 funused1 = SCALE;
64 glmax = 1;
65 glmin = 0;
66 bitpix = 0;
67 descrip = zeros(1,80);
68 aux_file = ['none ' 0];
69 origin = [0 0 0 0 0];
70
71 %---------------------------------------------------------------------------
72 if TYPE == 1; bitpix = 1; glmax = 1; glmin = 0; end
73 if TYPE == 2; bitpix = 8; glmax = 255; glmin = 0; end
74 if TYPE == 4; bitpix = 16; glmax = 32767; glmin = 0; end
75 if TYPE == 8; bitpix = 32; glmax = (2^31-1); glmin = 0; end
76 if TYPE == 16; bitpix = 32; glmax = 1; glmin = 0; end
77 if TYPE == 64; bitpix = 64; glmax = 1; glmin = 0; end
78
79 %---------------------------------------------------------------------------
80 if nargin >= 7; origin = [ORIGIN(:)' 0 0]; end
81 if nargin < 8; DESCRIP = 'spm compatible'; end
82
83 d = 1:min([length(DESCRIP) 79]);
84 descrip(d) = DESCRIP(d);
85
86 fseek(fid,0,'bof');
87
88 % write (struct) header_key
89 %---------------------------------------------------------------------------
90 fwrite(fid,348, 'int32');
91 fwrite(fid,data_type, 'char' );
92 fwrite(fid,db_name, 'char' );
93 fwrite(fid,0, 'int32');
94 fwrite(fid,0, 'int16');
95 fwrite(fid,'r', 'char' );
96 fwrite(fid,'0', 'char' );
97
98 % write (struct) image_dimension
99 %---------------------------------------------------------------------------
100 fseek(fid,40,'bof');
101
102 fwrite(fid,dim, 'int16');
103 fwrite(fid,'mm', 'char' );
104 fwrite(fid,0, 'char' );
105 fwrite(fid,0, 'char' );
106
107 fwrite(fid,zeros(1,8), 'char' );
108 fwrite(fid,0, 'int16');
109 fwrite(fid,TYPE, 'int16');
110 fwrite(fid,bitpix, 'int16');
111 fwrite(fid,0, 'int16');
112 fwrite(fid,pixdim, 'float');
113 fwrite(fid,vox_offset, 'float');
114 fwrite(fid,funused1, 'float');
115 fwrite(fid,0, 'float');
116 fwrite(fid,0, 'float');
117 fwrite(fid,0, 'float');
118 fwrite(fid,0, 'float');
119 fwrite(fid,0, 'int32');
120 fwrite(fid,0, 'int32');
121 fwrite(fid,glmax, 'int32');
122 fwrite(fid,glmin, 'int32');
123
124 % write (struct) image_dimension
125 %---------------------------------------------------------------------------
126 fwrite(fid,descrip, 'char');
127 fwrite(fid,aux_file, 'char');
128 fwrite(fid,0, 'char');
129 fwrite(fid,origin, 'int16');
130 if fwrite(fid,zeros(1,85), 'char')~=85
131 fclose(fid);
132 spm_unlink(P);
133 error(['Error writing ' P '. Check your disk space.']);
134 end
135
136 s = ftell(fid);
137 fclose(fid);
138