source: aedes_smisread.m

Last change on this file was 166, checked in by tjniskan, 6 years ago
  • Fixed a confusing error message when plugin menu construction failed.
  • Fixed a bug in reading S.M.I.S. data files when a footer was present in SUR-files.

M aedes.m
M aedes_revision.m
M aedes_smisread.m

File size: 7.7 KB
Line 
1function DATA = aedes_smisread(filename,opt)
2% AEDES_SMISREAD - Read S.M.I.S. Data Files (*.sur)
3%   
4%
5% Synopsis:
6%       DATA = aedes_smisread(filename)
7%
8% Description:
9%       Read image data from S.M.I.S. SUR-Files.
10%
11% Examples:
12%
13% See also:
14%       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% *************************************************************************
33% SMIS Data format documentation:
34%
35% SMIS reconstructed image files are in a format very similar to the first
36% part of that used for raw data (.MRD).  They consist of:
37%
38% 1)      a 256 byte header
39% 2)      a 256 byte text description
40% 3)      the data proper
41% 4)      the pule program parameters (PPR file)
42% 5)      other parameters
43%
44% 256 byte header
45% Bytes (hex)     'C' data type           Usage
46% 0-3             Long (4 byte integer)   Number of pixels in X direction
47% 4-7             Long                    Number of pixels in Y direction
48% 8-B             Long                    Number of pixels in Z direction (currently 1)
49% C-F             Long                    Dimension 4 (currently 1)
50% ...                                     Unspecified
51% 12-13           Int (2 byte integer)    Data type code
52% ...                                     Unspecified
53% 30-33           Float   (4 bytes)       Scaling factor from highest pixel to 4095
54% ...                                     Unspecified
55% 6D              Unsigned char (1 byte)  Number of bits per pixel (currently 12)
56% ...                                     Unspecified   
57%
58% The data type code indicates the format of individual data elements, as
59% follows:
60%
61%         0x00    unsigned char           1 byte
62%         0x01    signed char             1 byte
63%         0x02    short                   2 bytes
64%         0x03    int                     2 bytes
65%         0x04    long                    4 bytes
66%         0x05    float                   4 bytes
67%         0x06    double                  8 bytes
68%
69%         0x10    bit mask indicating complex data as real, imaginary pairs of
70% base data type
71%
72% All SMIS .SUR files are at present integer, indicated by data type 0x03.
73%
74% 256 text description
75% At present this is unused and contains the ASCII text "There is no script
76% !".
77%
78% Data
79% Data follows.  The pixel positions increments in the X dimension until the
80% next increment in the Y dimension.
81% *************************************************************************
82
83DATA = [];
84ReadHeader = true;
85ReadData = true;
86
87if nargin == 0 || isempty(filename)
88  % Prompt for a file
89  [fname,fpath,findex] = uigetfile({'*.sur',...
90        'S.M.I.S. Data Files (*.sur)';...
91        '*.*','All Files (*.*)'},'Open S.M.I.S. Data File');
92  if isequal(fname,0)
93        return
94  end
95  filename = fullfile(fpath,fname);
96end
97[fp,fn,fe]=fileparts(filename);
98
99if nargin == 2
100  % Read only header
101  if strcmpi(opt,'header')
102        ReadData = false;
103  end
104end
105
106% Open the file for reading
107fid = fopen(filename,'r');
108if fid < 0
109  error('Could not open file "%s" for reading!',filename)
110end
111
112% Read data header
113if ReadHeader
114  hdr=l_ReadHdrInfo(fid);
115end
116
117% Read image data
118if ReadData
119  [data,msg]=l_ReadData(hdr,fid);
120  if ~isempty(msg)
121        fclose(fid);
122        error(msg);
123  end
124end
125
126% Close file
127fclose(fid);
128
129% Construct DATA structure
130DATA.DataFormat = 'SUR';
131DATA.FTDATA = data;
132DATA.KSPACE = [];
133DATA.HDR.FileHeader = hdr;
134DATA.HDR.fname = [fn,fe];
135DATA.HDR.fpath = [fp,filesep];
136
137
138%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139% Read Header Information
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141function hdr=l_ReadHdrInfo(file_fid)
142
143% 256 byte header
144% Bytes (hex)     'C' data type           Usage
145% 0-3             Long (4 byte integer)   Number of pixels in X direction
146% 4-7             Long                    Number of pixels in Y direction
147% 8-B             Long                    Number of pixels in Z direction (currently 1)
148% C-F             Long                    Dimension 4 (currently 1)
149% ...                                     Unspecified
150% 12-13           Int (2 byte integer)    Data type code
151% ...                                     Unspecified
152% 30-33           Float   (4 bytes)       Scaling factor from highest pixel to 4095
153% ...                                     Unspecified
154% 6D              Unsigned char (1 byte)  Number of bits per pixel (currently 12)
155% ...                                     Unspecified   
156%
157% The data type code indicates the format of individual data elements, as
158% follows:
159%
160%         0x00    unsigned char           1 byte
161%         0x01    signed char             1 byte
162%         0x02    short                   2 bytes
163%         0x03    int                     2 bytes
164%         0x04    long                    4 bytes
165%         0x05    float                   4 bytes
166%         0x06    double                  8 bytes
167%
168%         0x10    bit mask indicating complex data as real, imaginary pairs of
169% base data type
170
171hdr = [];
172
173% Seek to the beginning of the file
174fseek(file_fid,0,-1);
175
176hdr.nPixelsX=fread(file_fid,1,'int32');  % Number of pixels in X direction
177hdr.nPixelsY=fread(file_fid,1,'int32');      % Number of pixels in Y direction
178hdr.nPixelsZ=fread(file_fid,1,'int32');      % Number of pixels in Z direction
179hdr.nPixels4D=fread(file_fid,1,'int32');             % Number of pixels in 4 dimension
180
181% Seek over the unspecified bytes
182fseek(file_fid,18,-1);
183
184hdr.DataTypeCode = fread(file_fid,1,'int16'); % Data type code
185
186% Seek over the unspecified bytes
187fseek(file_fid,48,-1);
188
189hdr.ScalingFactor = fread(file_fid,1,'float'); % Scaling factor from highest
190                                          % pixel to 4095
191
192% Seek over the unspecified bytes
193fseek(file_fid,109,-1);
194
195hdr.BitsPerPixel = fread(file_fid,1,'uchar');  % Number of bits per pixel
196                                          % (currently 12)
197
198% Read the text description
199fseek(file_fid,256,-1);
200hdr.TextDescription = char(fread(file_fid,256,'char')).';
201
202
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204% Read Image Data
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206function [data,msg]=l_ReadData(hdr,file_fid)
207
208data = [];
209msg = '';
210
211% Determine data type
212%         0x00    unsigned char           1 byte
213%         0x01    signed char             1 byte
214%         0x02    short                   2 bytes
215%         0x03    int                     2 bytes
216%         0x04    long                    4 bytes
217%         0x05    float                   4 bytes
218%         0x06    double                  8 bytes
219dataTypes = {...
220  '*uint8',1;...
221  '*int8',1;...
222  '*int16',2;...
223  '*int16',2;...
224  '*int32',4;...
225  '*single',4;...
226  '*double',6};
227precision = dataTypes{hdr.DataTypeCode+1,1};
228
229% Seek to the beginning of the data
230fseek(file_fid,512,-1);
231
232% Size of the data in bytes
233data_size = hdr.nPixelsX*hdr.nPixelsY*hdr.nPixelsZ*hdr.nPixels4D;
234
235try
236  % Read image data
237  data = fread(file_fid,data_size,precision);
238 
239  % Reshape and permute to correct size
240  data = reshape(data,[hdr.nPixelsX hdr.nPixelsY hdr.nPixelsZ hdr.nPixels4D]);
241  data = permute(data,[2 1 3 4]);
242catch
243  msg = ['Could not read data: ',lasterr];
244  data = [];
245end
246
Note: See TracBrowser for help on using the repository browser.

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