Skip to content

Commit

Permalink
update jsonlab to support lz4, update jnifti toolbox to v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Sep 16, 2019
1 parent 903791b commit 7b7385c
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 131 deletions.
2 changes: 1 addition & 1 deletion jdatadecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
if(~isempty(strmatch(N_('_ArrayZipType_'),fn)))
zipmethod=data(j).(N_('_ArrayZipType_'));
end
if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip'})))
if(~isempty(strmatch(zipmethod,{'zlib','gzip','lzma','lzip','lz4','lz4hc'})))
decompfun=str2func([zipmethod 'decode']);
if(needbase64)
ndata=reshape(typecast(decompfun(base64decode(data(j).(N_('_ArrayZipData_')))),data(j).(N_('_ArrayType_'))),data(j).(N_('_ArrayZipSize_'))(:)');
Expand Down
14 changes: 12 additions & 2 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
% for output format, it is incompatible with all
% previous releases; if old output is desired,
% please set FormatVersion to 1.9 or earlier.
% opt.Encoding ['']: json file encoding. Support all encodings of
% fopen() function
%
% output:
% dat: a cell array, where {...} blocks are converted into cell arrays,
Expand All @@ -69,11 +71,20 @@
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

opt=varargin2struct(varargin{:});

if(regexp(fname,'^\s*(?:\[.*\])|(?:\{.*\})\s*$','once'))
string=fname;
elseif(exist(fname,'file'))
try
string = fileread(fname);
encoding = jsonopt('Encoding','',opt);
if(isempty(encoding))
string = fileread(fname);
else
fid = fopen(fname,'r','n',encoding);
string = fread(fid,'*char')';
fclose(fid);
end
catch
try
string = urlread(['file://',fname]);
Expand All @@ -93,7 +104,6 @@
esc = find(inputstr=='"' | inputstr=='\' ); % comparable to: regexp(inputstr, '["\\]');
index_esc = 1;

opt=varargin2struct(varargin{:});
opt.arraytoken_=arraytoken;
opt.arraytokenidx_=arraytokenidx;

Expand Down
21 changes: 21 additions & 0 deletions loadnifti.m
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{:});
23 changes: 23 additions & 0 deletions lz4decode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function output = lz4decode(input)
%LZ4DECODE Decompress input bytes using lz4.
%
% output = lz4decode(input)
%
% The function takes a compressed byte array INPUT and returns inflated
% bytes OUTPUT. The INPUT is a result of LZ4DECODE function. The OUTPUT
% is always an 1-by-N uint8 array.
%
% See also lz4encode typecast
%
% License : BSD, see LICENSE_*.txt
%

if(nargin==0)
error('you must provide at least 1 input');
end
if(exist('zmat','file')==2 || exist('zmat','file')==3)
output=zmat(uint8(input),0,'lz4');
return;
else
error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
end
23 changes: 23 additions & 0 deletions lz4encode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function output = lz4encode(input)
%LZ4ENCODE Compress input bytes with lz4.
%
% output = lz4encode(input)
%
% The function takes a char, int8, or uint8 array INPUT and returns
% compressed bytes OUTPUT as a uint8 array. Note that the compression
% doesn't preserve input dimensions.
%
% See also lz4decode
%
% License : BSD, see LICENSE_*.txt
%

if(nargin==0)
error('you must provide at least 1 input');
end
if(exist('zmat','file')==2 || exist('zmat','file')==3)
output=zmat(uint8(input),1,'lz4');
return;
else
error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
end
23 changes: 23 additions & 0 deletions lz4hcdecode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function output = lz4hcdecode(input)
%LZ4HCDECODE Decompress input bytes using lz4hc.
%
% output = lz4hcdecode(input)
%
% The function takes a compressed byte array INPUT and returns inflated
% bytes OUTPUT. The INPUT is a result of LZ4HCDECODE function. The OUTPUT
% is always an 1-by-N uint8 array.
%
% See also lz4hcencode typecast
%
% License : BSD, see LICENSE_*.txt
%

if(nargin==0)
error('you must provide at least 1 input');
end
if(exist('zmat','file')==2 || exist('zmat','file')==3)
output=zmat(uint8(input),0,'lz4hc');
return;
else
error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
end
23 changes: 23 additions & 0 deletions lz4hcencode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function output = lz4hcencode(input)
%LZ4HCENCODE Compress input bytes with lz4hc.
%
% output = lz4hcencode(input)
%
% The function takes a char, int8, or uint8 array INPUT and returns
% compressed bytes OUTPUT as a uint8 array. Note that the compression
% doesn't preserve input dimensions.
%
% See also lz4hcdecode
%
% License : BSD, see LICENSE_*.txt
%

if(nargin==0)
error('you must provide at least 1 input');
end
if(exist('zmat','file')==2 || exist('zmat','file')==3)
output=zmat(uint8(input),1,'lz4hc');
return;
else
error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
end
70 changes: 70 additions & 0 deletions memmapstream.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function outstruct=memmapstream(bytes, format)
%
% outstruct=memmapstream(bytes, format)
%
% Map a byte-array (in char array or uint8/int8 array) into a structure
% using a dictionary (format is compatible with memmapfile in MATLAB)
%
% This function is compatible with both MATLAB and GNU Octave.
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% bytes: a char, int8 or uint8 vector or array
% format: a 3-column cell array in the format compatible with the
% 'Format' parameter of memmapfile in MATLAB. It has the
% following structure
%
% column 1: data type string, it can be one of the following
% 'int8','int16','int32','int64',
% 'uint8','uint16','uint32','uint64',
% 'single','double','logical'
% column 2: an integer vector denoting the size of the data
% column 3: a string denoting the fieldname in the output struct
%
% For example format={'int8',[1,8],'key'; 'float',[1,1],'value'}
% reads the first 8 bytes from 'bytes' as the first subfield
% 'key' and the following 4 bytes as the floating point 'value'
% subfield.
%
% output:
% outstruct: a structure containing the required field
%
% example:
% bytestream=['Andy' 5 'JT'];
% format={'uint8', [1,4], 'name',
% 'uint8', [1,1], 'age',
% 'uint8', [1,2], 'school'};
% data=memmapstream(bytestream,format);
%
% 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(nargin<2)
error('must provide bytes and format as inputs');
end

if(~ischar(bytes) && ~isa(bytes,'int8') && ~isa(bytes,'uint8') || isempty(bytes))
error('first input, bytes, must be a char-array or uint8/int8 vector');
end

if(~iscell(format) || size(format,2)<3 || size(format,1)==0 || ~ischar(format{1,1}))
error('second input, format, must be a 3-column cell array, in a format described by the memmapfile Format field.');
end

bytes=bytes(:)';

datatype=struct('int8',1,'int16',2,'int32',4,'int64',8,'uint8',1,'uint16',2,'uint32',4,'uint64',8,'single',4,'double',8);

outstruct=struct();
len=1;
for i=1:size(format,1)
bytelen=datatype.(format{i,1})*prod(format{i,2});
outstruct.(format{i,3})=reshape(typecast(uint8(bytes(len:bytelen+len-1)),format{i,1}),format{i,2});
len=len+bytelen;
if(len>length(bytes))
break;
end
end
49 changes: 49 additions & 0 deletions nifticreate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function header=nifticreate(img,format)
%
% header=nifticreate(img)
% or
% header=nifticreate(img,format)
%
% Create a default NIfTI header
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% img: the image data matching the header
% format: can only be 'nifti1'. can be ignored
%
% output:
% header: a struct that is byte-wise compatible with NIfTI-1
%
% 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(nargin<2)
format='nifti1';
end

datatype=struct('int8',256,'int16',4,'int32',8,'int64',1024,'uint8',2,'uint16',512,'uint32',768,'uint64',1280,'single',16,'double',64);

if(strcmp(format,'nifti1'))
headerlen=348;
else
headerlen=540;
end

header=memmapstream(uint8(zeros(1,headerlen+4)),niiformat(format));
header.sizeof_hdr=cast(headerlen, class(header.sizeof_hdr));
header.datatype=cast(datatype.(class(img)), class(header.datatype));
header.dim(1:ndims(img)+1)=cast([ndims(img),size(img)], class(header.dim));
header.pixdim(1:ndims(img)+1)=cast(1, class(header.pixdim));
header.vox_offset=cast(headerlen+4, class(header.vox_offset));
if(header.sizeof_hdr==540)
header.magic(1:3)=cast('ni2',class(header.magic));
else
header.magic(1:3)=cast('ni1',class(header.magic));
end
header.srow_x(1)=cast(1, class(header.srow_x));
header.srow_y(2)=cast(1, class(header.srow_y));
header.srow_z(3)=cast(1, class(header.srow_z));
header.sform_code=cast(1, class(header.sform_code));
Loading

0 comments on commit 7b7385c

Please sign in to comment.