Skip to content

Commit

Permalink
basic miss_hit code reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Aug 5, 2022
1 parent 4ce2cca commit b89f80c
Show file tree
Hide file tree
Showing 12 changed files with 585 additions and 603 deletions.
9 changes: 9 additions & 0 deletions .miss_hit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
project_root

suppress_rule: "redundant_brackets"
indent_function_file_body: false
tab_width: 2
copyright_location: "file_header"
copyright_entity: "Qianqian Fang"
line_length: 100
octave: true
23 changes: 0 additions & 23 deletions LICENSE_GPLv3.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
===============================================================================
= Monte Carlo eXtreme (MCX) -- CUDA =
===============================================================================

Copyright (c) 2010-2019 Qianqian Fang <q.fang at neu.edu>

-------------------------------------------------------------------------------

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

------------------------------------------------------------------------

GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Expand Down
4 changes: 2 additions & 2 deletions loadnifti.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
% or
% nii=loadnifti(filename,option)
%
% Read a NIfTI-1/2 (*.nii/.nii.gz) or Analyze 7.5 (*.hdr/*.img/.hdr.gz/.img.gz)
% 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)
Expand All @@ -18,4 +18,4 @@
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
%

[varargout{1:nargout}]=nii2jnii(varargin{:});
[varargout{1:nargout}] = nii2jnii(varargin{:});
54 changes: 27 additions & 27 deletions memmapstream.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function outstruct=memmapstream(bytes, format, varargin)
function outstruct = memmapstream(bytes, format, varargin)
%
% 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.
% This function is compatible with both MATLAB and GNU Octave.
%
% author: Qianqian Fang (q.fang <at> neu.edu)
%
Expand Down Expand Up @@ -42,40 +42,40 @@
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
%

if(nargin<2)
error('must provide bytes and format as inputs');
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');
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.');
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(:)';
bytes = bytes(:)';

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

opt=varargin2struct(varargin{:});
opt.usemap=jsonopt('usemap',0,opt) && exist('containers.Map');
opt = varargin2struct(varargin{:});
opt.usemap = jsonopt('usemap', 0, opt) && exist('containers.Map');

if(opt.usemap)
outstruct=containers.Map();
if (opt.usemap)
outstruct = containers.Map();
else
outstruct=struct();
outstruct = struct();
end
len=1;
for i=1:size(format,1)
bytelen=datatype.(format{i,1})*prod(format{i,2});
if(opt.usemap)
outstruct(format{i,3})=reshape(typecast(uint8(bytes(len:bytelen+len-1)),format{i,1}),format{i,2});
else
outstruct.(format{i,3})=reshape(typecast(uint8(bytes(len:bytelen+len-1)),format{i,1}),format{i,2});
end
len=len+bytelen;
if(len>length(bytes))
break;
end
len = 1;
for i = 1:size(format, 1)
bytelen = datatype.(format{i, 1}) * prod(format{i, 2});
if (opt.usemap)
outstruct(format{i, 3}) = reshape(typecast(uint8(bytes(len:bytelen + len - 1)), format{i, 1}), format{i, 2});
else
outstruct.(format{i, 3}) = reshape(typecast(uint8(bytes(len:bytelen + len - 1)), format{i, 1}), format{i, 2});
end
len = len + bytelen;
if (len > length(bytes))
break
end
end
42 changes: 21 additions & 21 deletions nifticreate.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function header=nifticreate(img,format)
function header = nifticreate(img, format)
%
% header=nifticreate(img)
% or
Expand All @@ -20,31 +20,31 @@
% License: Apache 2.0, see https://github.com/fangq/jnifti for details
%

if(nargin<2)
format='nifti1';
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);
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;
if (strcmp(format, 'nifti1'))
headerlen = 348;
else
headerlen=540;
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:end)=cast(1,class(header.dim));
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));
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:end) = cast(1, class(header.dim));
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));
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));
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));
151 changes: 75 additions & 76 deletions niftiinfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,99 +17,98 @@
%% @deftypefn {Function File} {} niftiinfo (@var{filename})
%% Read the metadata from a NIfTI-1/2 and Analyze7.5 formatted image file
%%
%% Parsing the metadata in a NIfTI-1/2 file or a two-part NIfTI
%% or Analyze7.5 header file specified by @var{filename}. The
%% Parsing the metadata in a NIfTI-1/2 file or a two-part NIfTI
%% or Analyze7.5 header file specified by @var{filename}. The
%% accepted file suffixes include .nii, .nii.gz, .hdr, and .hdr.gz.
%%
%% @seealso{niftiread, niftiwrite}
%% @end deftypefn

function nii = niftiinfo (filename, varargin)

if(nargin<1)
error('must provide the file name');
if (nargin < 1)
error('must provide the file name');
end

try
header=nii2jnii(filename,'niiheader');
header = nii2jnii(filename, 'niiheader');

fileinfo=dir(filename);
nii=struct('Filename',which(filename),...
'Filemoddate',fileinfo.date,...
'Filesize',fileinfo.bytes,...
'Version','NIfTI1',...
'Description',deblank(char(header.hdr.descrip)),...
'ImageSize',header.hdr.dim(2:header.hdr.dim(1)+1),...
'PixelDimensions',header.hdr.pixdim(2:header.hdr.dim(1)+1),...
'Datatype',header.datatype,...
'BitsPerPixel',header.hdr.bitpix,...
'SpaceUnits',niicodemap('unit',bitand(header.hdr.xyzt_units, 7)),...
'TimeUnits',niicodemap('unit',bitand(header.hdr.xyzt_units, 56)),...
'AdditiveOffset',header.hdr.scl_inter,...
'MultiplicativeScaling',header.hdr.scl_slope,...
'TimeOffset',header.hdr.toffset,...
'SliceCode',niicodemap('slicetype',header.hdr.slice_code),...
'FrequencyDimension',bitand(header.hdr.dim_info,7),...
'PhaseDimension',bitand(bitshift(header.hdr.dim_info,-3),7),...
'SpatialDimension',0,...
'DisplayIntensityRange',[header.hdr.cal_min,header.hdr.cal_max],...
'TransformName',0,...
'Transform',0,...
'Qfactor',header.hdr.pixdim(1),...
'raw',header.hdr ...
);
if(header.hdr.sizeof_hdr==540)
nii.Version='NIfTI2';
end
if(header.hdr.sform_code>0)
nii.TransformName='Sform';
Aaffine=[header.hdr.srow_x;header.hdr.srow_y;header.hdr.srow_z]';
Aaffine(4,4)=1;
elseif(header.hdr.qform_code>0)
nii.TransformName='Qform';
b=header.hdr.quatern_b;
c=header.hdr.quatern_c;
d=header.hdr.quatern_d;
a=sqrt(1.0-(b*b+c*c+d*d));
R=[a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c
2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b
2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b ];
if(det(R)==0)
Aaffine=[R [nii0.hdr.qoffset_x,nii0.hdr.qoffset_y,nii0.hdr.qoffset_z]']';
else
Aaffine=[R*diag([header.hdr.pixdim(2:3),header.hdr.pixdim(1)*header.hdr.pixdim(4)]) ...
[nii0.hdr.qoffset_x,nii0.hdr.qoffset_y,nii0.hdr.qoffset_z]']';
end
Aaffine(4,4)=1;
fileinfo = dir(filename);
nii = struct('Filename', which(filename), ...
'Filemoddate', fileinfo.date, ...
'Filesize', fileinfo.bytes, ...
'Version', 'NIfTI1', ...
'Description', deblank(char(header.hdr.descrip)), ...
'ImageSize', header.hdr.dim(2:header.hdr.dim(1) + 1), ...
'PixelDimensions', header.hdr.pixdim(2:header.hdr.dim(1) + 1), ...
'Datatype', header.datatype, ...
'BitsPerPixel', header.hdr.bitpix, ...
'SpaceUnits', niicodemap('unit', bitand(header.hdr.xyzt_units, 7)), ...
'TimeUnits', niicodemap('unit', bitand(header.hdr.xyzt_units, 56)), ...
'AdditiveOffset', header.hdr.scl_inter, ...
'MultiplicativeScaling', header.hdr.scl_slope, ...
'TimeOffset', header.hdr.toffset, ...
'SliceCode', niicodemap('slicetype', header.hdr.slice_code), ...
'FrequencyDimension', bitand(header.hdr.dim_info, 7), ...
'PhaseDimension', bitand(bitshift(header.hdr.dim_info, -3), 7), ...
'SpatialDimension', 0, ...
'DisplayIntensityRange', [header.hdr.cal_min, header.hdr.cal_max], ...
'TransformName', 0, ...
'Transform', 0, ...
'Qfactor', header.hdr.pixdim(1), ...
'raw', header.hdr ...
);
if (header.hdr.sizeof_hdr == 540)
nii.Version = 'NIfTI2';
end
if (header.hdr.sform_code > 0)
nii.TransformName = 'Sform';
Aaffine = [header.hdr.srow_x; header.hdr.srow_y; header.hdr.srow_z]';
Aaffine(4, 4) = 1;
elseif (header.hdr.qform_code > 0)
nii.TransformName = 'Qform';
b = header.hdr.quatern_b;
c = header.hdr.quatern_c;
d = header.hdr.quatern_d;
a = sqrt(1.0 - (b * b + c * c + d * d));
R = [a * a + b * b - c * c - d * d 2 * b * c - 2 * a * d 2 * b * d + 2 * a * c
2 * b * c + 2 * a * d a * a + c * c - b * b - d * d 2 * c * d - 2 * a * b
2 * b * d - 2 * a * c 2 * c * d + 2 * a * b a * a + d * d - c * c - b * b];
if (det(R) == 0)
Aaffine = [R [nii0.hdr.qoffset_x, nii0.hdr.qoffset_y, nii0.hdr.qoffset_z]']';
else
nii.TransformName='Old';
Aaffine=diag(header.hdr.pixdim(2:4));
Aaffine(4,4)=1;
Aaffine = [R * diag([header.hdr.pixdim(2:3), header.hdr.pixdim(1) * header.hdr.pixdim(4)]) ...
[nii0.hdr.qoffset_x, nii0.hdr.qoffset_y, nii0.hdr.qoffset_z]']';
end
if(exist('Aaffine','var'))
if(exist('affine3d'))
nii.Transform=affine3d(Aaffine);
else
nii.Transform=Aaffine;
end
Aaffine(4, 4) = 1;
else
nii.TransformName = 'Old';
Aaffine = diag(header.hdr.pixdim(2:4));
Aaffine(4, 4) = 1;
end
if (exist('Aaffine', 'var'))
if (exist('affine3d'))
nii.Transform = affine3d(Aaffine);
else
nii.Transform=diag([1,1,1,1]);
nii.Transform = Aaffine;
end
else
nii.Transform = diag([1, 1, 1, 1]);
end
catch ME
rethrow(ME);
rethrow(ME);
end

endfunction

endfunction;

%!demo
%! %% Reading a
%! urlwrite('https://nifti.nimh.nih.gov/nifti-1/data/minimal.nii.gz','minimal.nii.gz')
%! gunzip ('minimal.nii.gz');
%! header=niftiinfo('minimal.nii');
% !demo
% ! %% Reading a
% ! urlwrite('https://nifti.nimh.nih.gov/nifti-1/data/minimal.nii.gz','minimal.nii.gz')
% ! gunzip ('minimal.nii.gz');
% ! header=niftiinfo('minimal.nii');

%!test
%! urlwrite('https://nifti.nimh.nih.gov/nifti-1/data/minimal.nii.gz','minimal.nii.gz')
%! gunzip ('minimal.nii.gz');
%! header=niftiinfo('minimal.nii');
%! assert (header.ImageSize,[64 64 10]);
% !test
% ! urlwrite('https://nifti.nimh.nih.gov/nifti-1/data/minimal.nii.gz','minimal.nii.gz')
% ! gunzip ('minimal.nii.gz');
% ! header=niftiinfo('minimal.nii');
% ! assert (header.ImageSize,[64 64 10]);
Loading

0 comments on commit b89f80c

Please sign in to comment.