-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
845 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function varargout = loadnifti (varargin) | ||
% | ||
% jnii=loadnifti(filename) | ||
% or | ||
% nii=loadnifti(filename,option) | ||
% | ||
% Read a NIfTI-1/2 (*.nii/.nii.gz) or Analyze 7.5 (*.hdr/*.img/.hdr.gz/.img.gz) | ||
% image file. | ||
% | ||
% author: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% Please run `help nii2jnii` to see the input output outputs. | ||
% This function is an alias to nii2jnii | ||
% | ||
% | ||
% this file is part of JNIfTI specification: https://github.com/fangq/jnifti | ||
% | ||
% License: Apache 2.0, see https://github.com/fangq/jnifti for details | ||
% | ||
|
||
[varargout{1:nargout}]=nii2jnii(varargin{:}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
function bytestream=savenifti(img, filename, varargin) | ||
% | ||
% savenifti(img, filename) | ||
% or | ||
% savenifti(img, filename, rawhdr) | ||
% savenifti(img, filename, 'nifti2') | ||
% bytestream=savenifti(img) | ||
% | ||
% Write an image to a NIfTI (*.nii) or compressed NIfTI file (.nii.gz) | ||
% | ||
% author: Qianqian Fang (q.fang <at> neu.edu) | ||
% | ||
% input: | ||
% img: this is a numerical array to be stored in the NIfTI file | ||
% filename: output file name, can have a suffix of '.nii' or '.nii.gz' | ||
% if a .gz suffix is used, this function needs the JSONLab | ||
% (http://gitlab.com/fangq/jsonlab) and ZMat (http://gitlab.com/fangq/zmat) | ||
% to perform the compression. | ||
% rawhdr (optional): a struct, as a pre-created/loaded NIfTI header data structure | ||
% if rawhdr is 'nifti1' or 'nifti2', this function calls | ||
% nifticreate to create a default header. | ||
% output: | ||
% bytestream (optional): the output file byte stream. it only returns this output if | ||
% no filename is given. | ||
% | ||
% example: | ||
% a=single(rand(10,20,30)); | ||
% savenifti(a,'randnii.nii'); | ||
% savenifti(a,'randnii2.nii.gz','nifti2'); % needs zmat | ||
% | ||
% | ||
% this file is part of JNIfTI specification: https://github.com/fangq/jnifti | ||
% | ||
% License: Apache 2.0, see https://github.com/fangq/jnifti for details | ||
% | ||
|
||
|
||
if(~isempty(varargin)) | ||
if(isstruct(varargin{1})) | ||
header=varargin{1}; | ||
elseif(ischar(varargin{1})) | ||
header=nifticreate(img,varargin{1}); | ||
end | ||
else | ||
header=nifticreate(img); | ||
end | ||
|
||
names=fieldnames(header); | ||
buf=[]; | ||
for i=1:length(names) | ||
buf=[buf,typecast(header.(names{i}),'uint8')]; | ||
end | ||
|
||
if(length(buf)~=352 && length(buf)~=544) | ||
error('incorrect nifti-1/2 header %d',length(buf)); | ||
end | ||
|
||
buf=[buf,typecast(img(:)','uint8')]; | ||
|
||
if(nargout>1 && nargin<2) | ||
bytestream=buf; | ||
return; | ||
end | ||
|
||
if(regexp(filename,'\.[Gg][Zz]$')) | ||
buf=gzipencode(buf); | ||
end | ||
|
||
fid=fopen(filename,'wb'); | ||
if(fid==0) | ||
error('can not write to the specified file'); | ||
end | ||
fwrite(fid,buf); | ||
fclose(fid); |