📄 uff_readfile.m
字号:
function imdata = uff_ReadFile(hfile, filename, numimages)
% UFF::ReadFile - read a UFF specified file
%
% FORMAT: imdata = uff.ReadFile(filename [, numimages])
%
% Input fields:
%
% filename filename of image data file
% numimages number of images to read from file
%
% Output fields:
%
% imdata image data (N-D array)
% Version: v0.7b
% Build: 7090213
% Date: Sep-02 2007, 1:50 PM CEST
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check
if nargin < 2 || ...
numel(hfile) ~= 1 || ...
~isBVQXfile(hfile, 'uff') || ...
~ischar(filename) || ...
isempty(filename) || ...
exist(filename(:)', 'file') ~= 2
error( ...
'BVQXfile:BadArgument', ...
'Invalid call to %s or file not found.', ...
mfilename ...
);
end
ui = bvqxfile_getcont(hfile.L);
if nargin < 3 || ...
~isa(numimages, 'double') || ...
numel(numimages) ~= 1 || ...
isinf(numimages) || ...
isnan(numimages) || ...
numimages < 1
numimages = Inf;
else
numimages = fix(real(numimages));
end
% get shortcuts
sh = ui.SubHeaderSize;
% reject DICOM files
if ui.IsDICOM
error( ...
'BVQXfile:FileTypeNotSupported', ...
'Please use a real DICOM reader.' ...
);
end
% open file according to syntax
filename = filename(:)';
try
if ui.IsBigEndian
fid = fopen(filename, 'rb', 'ieee-be');
else
fid = fopen(filename, 'rb', 'ieee-le');
end
if fid < 1
error('INVALID_FID');
end
catch
error( ...
'BVQXfile:FileOpenError', ...
'Error opening specified file.' ...
);
end
% get file size
fseek(fid, 0, 1);
flen = ftell(fid);
% position IO pointer after header
fseek(fid, ui.HeaderSize, -1);
% element size
switch (ui.PixelFormat(1))
case {1}
rclass = '*uint8';
rcsize = 1;
imdata = uint8(0);
case {2}
rclass = '*uint16';
rcsize = 2;
imdata = uint16(0);
case {3}
rclass = '*uint32';
rcsize = 4;
imdata = uint32(0);
case {4}
rclass = '*single';
rcsize = 4;
imdata = single(0);
otherwise
error( ...
'BVQXfile:InvalidValue', ...
'Invalid PixelFormat value.' ...
);
end
% slice size
noc = ui.NrOfCols;
nor = ui.NrOfRows;
sls = noc * nor * rcsize;
fdt = ui.FirstDimIsTime;
% if time dim is first, calculate size
if any([1, 2] == ui.SingleFuncType(1))
tdim = floor((flen - ui.HeaderSize) / sls);
else
tdim = 1;
end
% skip images ?
if ~isempty(ui.ImageIndex) && ...
fix(ui.ImageIndex(1)) > 1 && ...
fdt == 0
for sc = 2:fix(ui.ImageIndex)
try
fseek(fid, sh + sls, 0);
catch
warning( ...
'BVQXfile:FileReadError', ...
'Error skipping slice images UFF file.' ...
);
return;
end
end
end
% read slices (time is not first)
if fdt == 0
% estimate number of slices if needed
if isinf(numimages)
sm = 1 + ceil((flen - ui.HeaderSize) / (sh + sls));
else
sm = numimages;
end
% initialize imdata
imdata(1:nor, 1:noc, 1:sm) = imdata(1);
sc = 1;
while numimages > 0
try
fseek(fid, sh, 0);
imdata(:, :, sc) = fread(fid, [nor, noc], rclass);
sc = sc + 1;
catch
warning( ...
'BVQXfile:FileReadError', ...
'Error reading slice %d in file.', ...
sc ...
);
return;
end
if ~isinf(numimages)
numimages = numimages - 1;
end
if (ftell(fid) + sh + sls) > flen
break;
end
end
% remove unneeded slices
imdata(:, :, sc:end) = [];
% time is first
else
% ignore sub header size!
imdata = reshape(fread(fid, [tdim, nor * noc], rclass), ...
[tdim, nor, noc]);
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -