source: aedes_smiswrite.m

Last change on this file was 102, checked in by tjniskan, 8 years ago
  • Fixed a small bug in aedes_smiswrite

M aedes_smiswrite.m
M aedes_revision.m

File size: 3.8 KB
Line 
1function aedes_smiswrite(data,filename,varargin)
2% AEDES_SMISWRITE - Write S.M.I.S. Data Files (*.sur)
3%   
4%
5% Synopsis:
6%       aedes_smiswrite(data,filename,param1,value1,param2,value2,...)
7%
8% Description:
9%       Write data to S.M.I.S. SUR-Format files.
10%
11% Examples:
12%
13% See also:
14%       AEDES_SMISREAD, AEDES_READFID, AEDES_READ_NIFTI
15
16% This function is a part of Aedes - A graphical tool for analyzing
17% medical images
18%
19% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
20%
21% Department of Physics, Department of Neurobiology
22% University of Kuopio, FINLAND
23%
24% This program may be used under the terms of the GNU General Public
25% License version 2.0 as published by the Free Software Foundation
26% and appearing in the file LICENSE.TXT included in the packaging of
27% this program.
28%
29% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
30% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31
32% Defaults
33DataTypeCode = 3;
34BitsPerPixel = 12;
35TextDescription = 'Exported from Aedes';
36
37if nargin<1
38  error('Too few input arguments!')
39elseif nargin==1
40  % Prompt for a file
41  [fname,fpath]=uiputfile({'*.sur','S.M.I.S. SUR-Files (*.sur)';...
42        '*.*','All Files (*.*)'},'Save Data in SUR Format',...
43        'surdata.sur');
44  if isequal(fname,0)
45        % Canceled
46        return
47  end
48  filename = fullfile(fpath,fname);
49end
50[fp,fn,fe]=fileparts(filename);
51if isempty(fe) || ~any(strcmpi(fe,{'.sur','.mri'}))
52  fe = '.sur';
53end
54filename = fullfile(fp,[fn,fe]);
55
56if isstruct(data) && isfield(data,'FTDATA')
57  data = data.FTDATA;
58end
59
60% Handle varargin
61for ii=1:2:length(varargin)
62  prop = lower(varargin{ii});
63  value = varargin{ii+1};
64  switch prop
65        case 'datatype'
66          if strcmpi(value,'uint8')
67                DataTypeCode = 0;
68                BitsPerPixel = 8;
69          elseif strcmpi(value,'int8')
70                DataTypeCode = 1;
71                BitsPerPixel = 8;
72          elseif any(strcmpi(value, {'uint16','int16'}))
73                DataTypeCode = 3;
74                BitsPerPixel = 16;
75          elseif strcmpi(value,'int32')
76                DataTypeCode = 4;
77                BitsPerPixel = 32;
78          elseif any(strcmpi(value,{'float','single'}))
79                DataTypeCode = 5;
80                BitsPerPixel = 32;
81          elseif any(strcmpi(value,{'float64','double'}))
82                DataTypeCode = 6;
83                BitsPerPixel = 64;
84          else
85                DataTypeCode = 6;
86                BitsPerPixel = 64;
87          end
88        case 'description'
89          TextDescription = value;
90        otherwise
91          error('Unknown property %s',upper(prop))
92  end
93end
94
95% Get data dimensions
96x_size = size(data,2);
97y_size = size(data,1);
98z_size = size(data,3);
99v_size = size(data,4);
100
101% Open file for writing
102fid = fopen(filename,'w');
103if fid < 0
104  error('Could not open file "%s" for writing!',filename)
105end
106
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108% Write header information
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111% Allocate 512 bytes from the header
112fwrite(fid,zeros(1,512),'char');
113
114% Write size information
115fseek(fid,0,-1);
116fwrite(fid,x_size,'int32');
117fwrite(fid,y_size,'int32');
118fwrite(fid,z_size,'int32');
119fwrite(fid,v_size,'int32');
120
121% Write DataType
122fseek(fid,18,-1);
123fwrite(fid,DataTypeCode,'int16');
124
125% Write Bits Per Pixel
126fseek(fid,109,-1);
127fwrite(fid,BitsPerPixel,'uchar');
128
129% Write Text Description
130fseek(fid,256,-1);
131fwrite(fid,TextDescription(1:min(256,end)),'char');
132
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% Write Image Data
135%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
137% Crop image data to BitsPerPixel if 12-bit
138if BitsPerPixel==12
139  data(data<0) = 0;
140  data(data>4096) = 4096;
141end
142
143% Seek to the beginning of the data
144fseek(fid,512,-1);
145
146% Determine precision
147dataTypes = {...
148  'uint8',...
149  'int8',...
150  'int16',...
151  'int16',...
152  'int32',...
153  'single',...
154  'double'};
155precision = dataTypes{DataTypeCode+1};
156
157% Write image data
158count=fwrite(fid,permute(data,[2 1 3 4]),precision);
159
160% Close file
161fclose(fid);
162
163% Check if all elements were written
164if count~=prod(size(data))
165  error('The file "%s" was not written properly!',filename)
166end
167
168
169% - EOF -
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008