123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- % Load NIfTI / Analyze header without applying any appropriate affine
- % geometric transform or voxel intensity scaling. It is equivalent to
- % hdr field when using load_untouch_nii to load dataset. Support both
- % *.nii and *.hdr file extension. If file extension is not provided,
- % *.hdr will be used as default.
- %
- % Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename)
- %
- % filename - NIfTI / Analyze file name.
- %
- % Returned values:
- %
- % header - struct with NIfTI / Analyze header fields.
- %
- % ext - NIfTI extension if it is not empty.
- %
- % filetype - 0 for Analyze format (*.hdr/*.img);
- % 1 for NIFTI format in 2 files (*.hdr/*.img);
- % 2 for NIFTI format in 1 file (*.nii).
- %
- % machine - a string, see below for details. The default here is 'ieee-le'.
- %
- % 'native' or 'n' - local machine format - the default
- % 'ieee-le' or 'l' - IEEE floating point with little-endian
- % byte ordering
- % 'ieee-be' or 'b' - IEEE floating point with big-endian
- % byte ordering
- % 'vaxd' or 'd' - VAX D floating point and VAX ordering
- % 'vaxg' or 'g' - VAX G floating point and VAX ordering
- % 'cray' or 'c' - Cray floating point with big-endian
- % byte ordering
- % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
- % byte ordering and 64 bit long data type
- % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
- % ordering and 64 bit long data type.
- %
- % Part of this file is copied and modified from:
- % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
- %
- % NIFTI data format can be found on: http://nifti.nimh.nih.gov
- %
- % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
- %
- function [hdr, ext, filetype, machine] = load_untouch_header_only(filename)
- if ~exist('filename','var')
- error('Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename)');
- end
- v = version;
- % Check file extension. If .gz, unpack it into temp folder
- %
- if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
- if ~strcmp(filename(end-6:end), '.img.gz') & ...
- ~strcmp(filename(end-6:end), '.hdr.gz') & ...
- ~strcmp(filename(end-6:end), '.nii.gz')
- error('Please check filename.');
- end
- if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
- error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
- elseif strcmp(filename(end-6:end), '.img.gz')
- filename1 = filename;
- filename2 = filename;
- filename2(end-6:end) = '';
- filename2 = [filename2, '.hdr.gz'];
- tmpDir = tempname;
- mkdir(tmpDir);
- gzFileName = filename;
- filename1 = gunzip(filename1, tmpDir);
- filename2 = gunzip(filename2, tmpDir);
- filename = char(filename1); % convert from cell to string
- elseif strcmp(filename(end-6:end), '.hdr.gz')
- filename1 = filename;
- filename2 = filename;
- filename2(end-6:end) = '';
- filename2 = [filename2, '.img.gz'];
- tmpDir = tempname;
- mkdir(tmpDir);
- gzFileName = filename;
- filename1 = gunzip(filename1, tmpDir);
- filename2 = gunzip(filename2, tmpDir);
- filename = char(filename1); % convert from cell to string
- elseif strcmp(filename(end-6:end), '.nii.gz')
- tmpDir = tempname;
- mkdir(tmpDir);
- gzFileName = filename;
- filename = gunzip(filename, tmpDir);
- filename = char(filename); % convert from cell to string
- end
- end
- % Read the dataset header
- %
- [hdr, filetype, fileprefix, machine] = load_nii_hdr(filename);
- if filetype == 0
- hdr = load_untouch0_nii_hdr(fileprefix, machine);
- ext = [];
- else
- hdr = load_untouch_nii_hdr(fileprefix, machine, filetype);
- % Read the header extension
- %
- ext = load_nii_ext(filename);
- end
- % Set bitpix according to datatype
- %
- % /*Acceptable values for datatype are*/
- %
- % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN
- % 1 Binary (ubit1, bitpix=1) % DT_BINARY
- % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8
- % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16
- % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32
- % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32
- % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64
- % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64
- % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24
- % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8
- % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96
- % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16
- % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32
- % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64
- % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64
- % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128
- % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
- % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
- %
- switch hdr.dime.datatype
- case 1,
- hdr.dime.bitpix = 1; precision = 'ubit1';
- case 2,
- hdr.dime.bitpix = 8; precision = 'uint8';
- case 4,
- hdr.dime.bitpix = 16; precision = 'int16';
- case 8,
- hdr.dime.bitpix = 32; precision = 'int32';
- case 16,
- hdr.dime.bitpix = 32; precision = 'float32';
- case 32,
- hdr.dime.bitpix = 64; precision = 'float32';
- case 64,
- hdr.dime.bitpix = 64; precision = 'float64';
- case 128,
- hdr.dime.bitpix = 24; precision = 'uint8';
- case 256
- hdr.dime.bitpix = 8; precision = 'int8';
- case 511
- hdr.dime.bitpix = 96; precision = 'float32';
- case 512
- hdr.dime.bitpix = 16; precision = 'uint16';
- case 768
- hdr.dime.bitpix = 32; precision = 'uint32';
- case 1024
- hdr.dime.bitpix = 64; precision = 'int64';
- case 1280
- hdr.dime.bitpix = 64; precision = 'uint64';
- case 1792,
- hdr.dime.bitpix = 128; precision = 'float64';
- otherwise
- error('This datatype is not supported');
- end
- tmp = hdr.dime.dim(2:end);
- tmp(find(tmp < 1)) = 1;
- hdr.dime.dim(2:end) = tmp;
- % Clean up after gunzip
- %
- if exist('gzFileName', 'var')
- rmdir(tmpDir,'s');
- end
- return % load_untouch_header_only
|