📄 avw_hdr_write.m
字号:
function avw_hdr_write(avw, fileprefix, machine)
% AVW_HDR_WRITE - Write Analyze header file (*.hdr)
%
% avw_hdr_write(avw, fileprefix, machine)
%
% eg, avw_hdr_write(avw,'test');
%
% avw - a struct with .hdr field, which itself is a struct,
% containing all fields of an Analyze header.
% For details, see avw_hdr_read.m
%
% fileprefix - a string, the filename without the .hdr extension.
% If empty, may use avw.fileprefix
%
% machine - a string, see machineformat in fread for details.
% The default here is 'ieee-le'.
%
% See also, AVW_HDR_READ AVW_HDR_MAKE
% AVW_IMG_READ AVW_IMG_WRITE
%
% $Revision: 1.9 $ $Date: 2004/03/17 06:08:18 $
% Licence: GNU GPL, no express or implied warranties
% History: 05/2002, Darren.Weber@flinders.edu.au
% 02/2003, Bennett.Landman@ieee.org
% - more specific data history var sizes
% - 02/2003 confirmed, Darren
%
% The Analyze format and c code below is copyright
% (c) Copyright, 1986-1995
% Biomedical Imaging Resource, Mayo Foundation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
version = '[$Revision: 1.9 $]';
fprintf('AVW_HDR_WRITE [v%s]\n',version(12:16)); tic;
%----------------------------------------------------------------------------
% Check inputs
if ~exist('avw','var'),
fprintf('...no input avw - calling avw_hdr_make\n');
avw = avw_hdr_make;
elseif isempty(avw),
fprintf('...empty input avw - calling avw_hdr_make\n');
avw = avw_hdr_make;
elseif ~isfield(avw,'hdr'),
fprintf('...empty input avw.hdr - calling avw_hdr_make\n');
avw = avw_hdr_make;
end
if ~isequal(avw.hdr.hk.sizeof_hdr,348),
msg = sprintf('...avw.hdr.hk.sizeof_hdr must be 348!\n');
error(msg);
end
quit = 0;
if ~exist('fileprefix','var'),
if isfield(avw,'fileprefix'),
if ~isempty(avw.fileprefix),
fileprefix = avw.fileprefix;
else,
quit = 1;
end
else
quit = 1;
end
if quit,
fprintf('...no input fileprefix - see help avw_hdr_write\n\n');
helpwin avw_hdr_write;
return;
end
end
if ~exist('machine','var'), machine = 'ieee-le'; end
if findstr('.hdr',fileprefix),
% fprintf('AVW_HDR_WRITE: Removing .hdr extension from ''%s''\n',fileprefix);
fileprefix = strrep(fileprefix,'.hdr','');
end
%----------------------------------------------------------------------------
% MAIN
tic;
fid = fopen(sprintf('%s.hdr',fileprefix),'w',machine);
if fid < 0,
msg = sprintf('Cannot write to file %s.hdr\n',fileprefix);
error(msg);
else
fprintf('...writing %s Analyze header.\n',machine);
write_header(fid,avw);
end
t=toc; fprintf('...done (%5.2f sec).\n\n',t);
return
%----------------------------------------------------------------------------
function write_header(fid,avw)
header_key(fid,avw.hdr.hk);
image_dimension(fid,avw.hdr.dime);
data_history(fid,avw.hdr.hist);
% check the file size is 348 bytes
fbytes = ftell(fid);
fclose(fid);
if ~isequal(fbytes,348),
msg = sprintf('...file size is not 348 bytes!\n');
warning(msg);
end
return
%----------------------------------------------------------------------------
function header_key(fid,hk)
% Original header structures - ANALYZE 7.5
% struct header_key /* header key */
% { /* off + size */
% int sizeof_hdr /* 0 + 4 */
% char data_type[10]; /* 4 + 10 */
% char db_name[18]; /* 14 + 18 */
% int extents; /* 32 + 4 */
% short int session_error; /* 36 + 2 */
% char regular; /* 38 + 1 */
% char hkey_un0; /* 39 + 1 */
% }; /* total=40 bytes */
fseek(fid,0,'bof');
fwrite(fid, hk.sizeof_hdr(1), 'int32'); % must be 348!
data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars
fwrite(fid, hk.data_type(1:10), 'uchar');
db_name = sprintf('%-18s',hk.db_name); % ensure it is 18 chars
fwrite(fid, db_name(1:18), 'uchar');
fwrite(fid, hk.extents(1), 'int32');
fwrite(fid, hk.session_error(1),'int16');
regular = sprintf('%1s',hk.regular); % ensure it is 1 char
fwrite(fid, regular(1), 'uchar'); % might be uint8
%hkey_un0 = sprintf('%1s',hk.hkey_un0); % ensure it is 1 char
%fwrite(fid, hkey_un0(1), 'uchar');
fwrite(fid, hk.hkey_un0(1), 'uint8');
% >Would you set hkey_un0 as char or uint8?
% Really doesn't make any difference. As far as anyone here can remember,
% this was just to pad to an even byte boundary for that structure. I guess
% I'd suggest setting it to a uint8 value of 0 (i.e, truly zero-valued) so
% that it doesn't look like anything important!
% Denny <hanson.dennis2@mayo.edu>
return
%----------------------------------------------------------------------------
function image_dimension(fid,dime)
%struct image_dimension
% { /* off + size */
% short int dim[8]; /* 0 + 16 */
% char vox_units[4]; /* 16 + 4 */
% char cal_units[8]; /* 20 + 8 */
% short int unused1; /* 28 + 2 */
% short int datatype; /* 30 + 2 */
% short int bitpix; /* 32 + 2 */
% short int dim_un0; /* 34 + 2 */
% float pixdim[8]; /* 36 + 32 */
% /*
% pixdim[] specifies the voxel dimensions:
% pixdim[1] - voxel width
% pixdim[2] - voxel height
% pixdim[3] - interslice distance
% ..etc
% */
% float vox_offset; /* 68 + 4 */
% float roi_scale; /* 72 + 4 */
% float funused1; /* 76 + 4 */
% float funused2; /* 80 + 4 */
% float cal_max; /* 84 + 4 */
% float cal_min; /* 88 + 4 */
% int compressed; /* 92 + 4 */
% int verified; /* 96 + 4 */
% int glmax; /* 100 + 4 */
% int glmin; /* 104 + 4 */
% }; /* total=108 bytes */
fwrite(fid, dime.dim(1:8), 'int16');
fwrite(fid, dime.vox_units(1:4),'uchar');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -