Skip to content

Commit c3aa8bf

Browse files
committed
jnifti toolbox v0.5
1 parent fdfaa23 commit c3aa8bf

6 files changed

Lines changed: 845 additions & 6 deletions

File tree

LICENSE_GPLv3.txt

Lines changed: 697 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# JNIfTI Toolbox - Fast and portable NIfTI-1/2 reader and NIfTI-to-JNIfTI converter
22

3+
* Copyright (C) 2019 Qianqian Fang <q.fang at neu.edu>
4+
* License: GNU General Public License version 3 (GPL v3) or Apache License 2.0, see License*.txt
5+
* Version: 0.5 (Ascendence)
6+
* URL: http://github.com/fangq/jnifti
7+
38
## Overview
49

510
This is a fully functional NIfTI-1/2 reader/writer that supports both
@@ -27,7 +32,7 @@ The JNIfTI toolbox is also capable of reading/writing gzip-compressed NIfTI and
2732
Analyze7.5 files (.nii.gz, .hdr.gz, .img.gz). This feature is supported in MATLAB
2833
directly without needing another toolbox (MATLAB must be in the JVM-enabled mode).
2934

30-
To process gzip-compressed NIfTI/Analyze files in Octave and MATLAB with -nojvm,
35+
To process gzip-compressed NIfTI/Analyze files in Octave and MATLAB with `-nojvm`,
3136
one need to install the open-source JSONLab and ZMat toolboxes, both supporting
3237
MATLAB and Octave. They can be downloaded at
3338

@@ -39,3 +44,40 @@ data format supports internal compression (as oppose to external compression suc
3944
as \*.gz files). To create or read compressed JNIfTI files in Octave, one must
4045
install the ZMat toolbox, as listed above.
4146

47+
## Usage
48+
49+
### `nii2jnii` - To convert a NIfTI-1/2 file to a JNIfTI file or data structure
50+
Example:
51+
```
52+
nii=nii2jnii('test.nii', 'nii'); % read a .nii file as a nii structure
53+
nii=nii2jnii('test.nii.gz'); % read a .nii.gz file as a jnii structure
54+
nii2jnii('test.nii.gz', 'newdata.jnii') ;% read a .nii.gz file and convert to a text-JNIfTI file
55+
nii2jnii('test.nii.gz', 'newdata.bnii','compression','zlib'); % read a .nii.gz file and convert to a binary-JNIfTI file with compression
56+
```
57+
### `loadnifti` - To read a NIfTI-1/2 (.nii or .nii.gz) file (alias to `nii2jnii`)
58+
Example:
59+
```
60+
nii=loadnifti('test.nii.gz'); % read a .nii.gz file as a jnii structure
61+
nii=loadnifti('test.nii', 'nii'); % read a .nii file as a nii structure
62+
```
63+
### `savenifti` - To write an image as NIfTI-1/2 (.nii or .nii.gz) file
64+
Example:
65+
```
66+
savenifti(img,'test.nii.gz'); % save an array img to a compressed nifti file
67+
savenifti(img, 'test.nii', 'nifti2'); % save an array img to a nifti-2 file file
68+
savenifti(img, 'test.nii', header); % save an array img with an existing header
69+
```
70+
### `loadjnifti` - To read a JNIfTI (.jnii or .bnii) file
71+
Example:
72+
```
73+
jnii=nii2jnii('test.nii.gz');
74+
savejnifti(jnii, 'magic10.bnii','Compression','gzip');
75+
newjnii=loadjnifti('magic10.bnii');
76+
```
77+
### `savejnifti` - To write a JNIfTI structure into a file (.jnii or .bnii)
78+
Example:
79+
```
80+
jnii=jnifticreate(uint8(magic(10)),'Name','10x10 magic matrix');
81+
savejnifti(jnii, 'magic10.jnii');
82+
savejnifti(jnii, 'magic10_debug.bnii','Compression','gzip');
83+
```

loadnifti.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function varargout = loadnifti (varargin)
2+
%
3+
% jnii=loadnifti(filename)
4+
% or
5+
% nii=loadnifti(filename,option)
6+
%
7+
% Read a NIfTI-1/2 (*.nii/.nii.gz) or Analyze 7.5 (*.hdr/*.img/.hdr.gz/.img.gz)
8+
% image file.
9+
%
10+
% author: Qianqian Fang (q.fang <at> neu.edu)
11+
%
12+
% Please run `help nii2jnii` to see the input output outputs.
13+
% This function is an alias to nii2jnii
14+
%
15+
%
16+
% this file is part of JNIfTI specification: https://github.com/fangq/jnifti
17+
%
18+
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
19+
%
20+
21+
[varargout{1:nargout}]=nii2jnii(varargin{:});

nifticreate.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
2121
%
2222

23-
if(nargin<1)
23+
if(nargin<2)
2424
format='nifti1';
2525
end
2626

@@ -38,7 +38,11 @@
3838
header.dim(1:ndims(img)+1)=cast([ndims(img),size(img)], class(header.dim));
3939
header.pixdim(1:ndims(img)+1)=cast(1, class(header.pixdim));
4040
header.vox_offset=cast(headerlen+4, class(header.vox_offset));
41-
header.magic(1:3)=cast('ni1',class(header.magic));
41+
if(header.sizeof_hdr==540)
42+
header.magic(1:3)=cast('ni2',class(header.magic));
43+
else
44+
header.magic(1:3)=cast('ni1',class(header.magic));
45+
end
4246
header.srow_x(1)=cast(1, class(header.srow_x));
4347
header.srow_y(2)=cast(1, class(header.srow_y));
4448
header.srow_z(3)=cast(1, class(header.srow_z));

nii2jnii.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
%
1717
% input:
1818
% fname: the file name to the .nii, .nii.gz, .hdr/.img or .hdr.gz/.img.gz file
19-
% format:'nii' for reading the NIfTI-1/2/Analyze files; 'jnii' to
20-
% convert the nii data into an in-memory JNIfTI structure.
19+
% format:'nii' for reading the NIfTI-1/2/Analyze files;
20+
% 'jnii' to convert the nii data into an in-memory JNIfTI structure.
21+
% 'niiheader' return only the nii header without the image data
2122
%
22-
% if format is not 'nii' or 'jnii' and nii2jnii is called without
23+
% if format is not listed above and nii2jnii is called without
2324
% an output, format must be a string specifying the output JNIfTI
2425
% file name - *.jnii for text-based JNIfTI, or *.bnii for the
2526
% binary version

savenifti.m

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
function bytestream=savenifti(img, filename, varargin)
2+
%
3+
% savenifti(img, filename)
4+
% or
5+
% savenifti(img, filename, rawhdr)
6+
% savenifti(img, filename, 'nifti2')
7+
% bytestream=savenifti(img)
8+
%
9+
% Write an image to a NIfTI (*.nii) or compressed NIfTI file (.nii.gz)
10+
%
11+
% author: Qianqian Fang (q.fang <at> neu.edu)
12+
%
13+
% input:
14+
% img: this is a numerical array to be stored in the NIfTI file
15+
% filename: output file name, can have a suffix of '.nii' or '.nii.gz'
16+
% if a .gz suffix is used, this function needs the JSONLab
17+
% (http://gitlab.com/fangq/jsonlab) and ZMat (http://gitlab.com/fangq/zmat)
18+
% to perform the compression.
19+
% rawhdr (optional): a struct, as a pre-created/loaded NIfTI header data structure
20+
% if rawhdr is 'nifti1' or 'nifti2', this function calls
21+
% nifticreate to create a default header.
22+
% output:
23+
% bytestream (optional): the output file byte stream. it only returns this output if
24+
% no filename is given.
25+
%
26+
% example:
27+
% a=single(rand(10,20,30));
28+
% savenifti(a,'randnii.nii');
29+
% savenifti(a,'randnii2.nii.gz','nifti2'); % needs zmat
30+
%
31+
%
32+
% this file is part of JNIfTI specification: https://github.com/fangq/jnifti
33+
%
34+
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
35+
%
36+
37+
38+
if(~isempty(varargin))
39+
if(isstruct(varargin{1}))
40+
header=varargin{1};
41+
elseif(ischar(varargin{1}))
42+
header=nifticreate(img,varargin{1});
43+
end
44+
else
45+
header=nifticreate(img);
46+
end
47+
48+
names=fieldnames(header);
49+
buf=[];
50+
for i=1:length(names)
51+
buf=[buf,typecast(header.(names{i}),'uint8')];
52+
end
53+
54+
if(length(buf)~=352 && length(buf)~=544)
55+
error('incorrect nifti-1/2 header %d',length(buf));
56+
end
57+
58+
buf=[buf,typecast(img(:)','uint8')];
59+
60+
if(nargout>1 && nargin<2)
61+
bytestream=buf;
62+
return;
63+
end
64+
65+
if(regexp(filename,'\.[Gg][Zz]$'))
66+
buf=gzipencode(buf);
67+
end
68+
69+
fid=fopen(filename,'wb');
70+
if(fid==0)
71+
error('can not write to the specified file');
72+
end
73+
fwrite(fid,buf);
74+
fclose(fid);

0 commit comments

Comments
 (0)