From d270f759b08f0ab8236cdbd9690efd91896f28d4 Mon Sep 17 00:00:00 2001 From: Guillaume Flandin Date: Wed, 8 Nov 2023 20:37:09 +0000 Subject: [PATCH] Merge with SPM changes --- @gifti/gifti.m | 4 +-- @gifti/private/Makefile | 12 ++++---- @gifti/private/Makefile.var | 16 +++++------ @gifti/private/gifti_read.m | 4 +++ @gifti/private/obj_read.m | 9 ++++-- @gifti/save.m | 57 ++++++++++++++++++++++++------------- 6 files changed, 62 insertions(+), 40 deletions(-) diff --git a/@gifti/gifti.m b/@gifti/gifti.m index 2ef6a36..b89861b 100644 --- a/@gifti/gifti.m +++ b/@gifti/gifti.m @@ -21,8 +21,8 @@ this = varargin{1}; elseif isstruct(varargin{1}) - f = {'faces', 'face', 'tri' 'vertices', 'vert', 'pnt', 'cdata', 'indices'}; - ff = {'faces', 'faces', 'faces', 'vertices', 'vertices', 'vertices', 'cdata', 'indices'}; + f = {'faces', 'face', 'tri' 'vertices', 'vert', 'pnt', 'pos', 'cdata', 'indices'}; + ff = {'faces', 'faces', 'faces', 'vertices', 'vertices', 'vertices', 'vertices', 'cdata', 'indices'}; [c, ia] = intersect(f,fieldnames(varargin{1})); if ~isempty(c) this = gifti; diff --git a/@gifti/private/Makefile b/@gifti/private/Makefile index bf9e5ab..8690bb3 100644 --- a/@gifti/private/Makefile +++ b/@gifti/private/Makefile @@ -1,15 +1,13 @@ #!/usr/bin/env make -f # GIfTI Makefile # -# Copyright (C) 2015 Wellcome Trust Centre for Neuroimaging -# -# $Id: Makefile 6704 2016-01-29 17:23:13Z guillaume $ +# Copyright (C) 2008-2023 Wellcome Centre for Human Neuroimaging include Makefile.var -SPMMEX = zstream.$(SUF) xml_parser.$(SUF) base64.$(SUF) +SPMMEX = zstream.$(MEXEXT) xml_parser.$(MEXEXT) base64.$(MEXEXT) -ifeq (mex,$(SUF)) +ifeq (mex,$(MEXEXT)) export CFLAGS = $(shell $(MEX) -p CFLAGS) -std=c99 else ifeq (windows,$(PLATFORM)) @@ -34,8 +32,8 @@ install: tarball: all $(TAR) cf spm_mex.tar $(SPMMEX) -%.$(SUF) : %.c +%.$(MEXEXT) : %.c $(MEX) $< $(MEXEND) -xml_parser.$(SUF): xml_parser.c yxml.c yxml.h +xml_parser.$(MEXEXT): xml_parser.c yxml.c yxml.h $(MEX) $< yxml.c $(MEXEND) diff --git a/@gifti/private/Makefile.var b/@gifti/private/Makefile.var index 692ed2b..64f1595 100644 --- a/@gifti/private/Makefile.var +++ b/@gifti/private/Makefile.var @@ -41,25 +41,25 @@ MOVE = mv -f TAR = tar ZIP = gzip -f -ifndef SUF +ifndef MEXEXT ifndef PLATFORM PLATFORM = $(shell $(UNAME)) endif ##### Linux ##### ifeq (Linux,$(PLATFORM)) - SUF = mexa64 + MEXEXT = mexa64 endif ##### MacOS ##### ifeq (Darwin,$(PLATFORM)) - SUF = mexmaci64 + MEXEXT = mexmaci64 endif ##### Windows ##### ifeq (MINGW32,$(word 1,$(subst _, ,$(PLATFORM)))) # MSC override PLATFORM = windows ifeq (x86,$(PROCESSOR_ARCHITECTURE)) - SUF = mexw32 + MEXEXT = mexw32 else - SUF = mexw64 + MEXEXT = mexw64 endif MEXBIN = cmd /c "mex.bat MEXOPTS += -DSPM_WIN32 @@ -68,7 +68,7 @@ ifndef SUF AR = lib.exe /out: endif ifeq (MSYS,$(word 1,$(subst _, ,$(PLATFORM)))) # GCC - SUF = mexw64 + MEXEXT = mexw64 MEXOPTS += -DSPM_WIN32 MOSUF = obj endif @@ -76,11 +76,11 @@ ifndef SUF ifeq (octave,$(PLATFORM)) MEXBIN = mkoctfile MEXOPTS = --mex - SUF = mex + MEXEXT = mex override PLATFORM = $(shell $(UNAME)) endif #### Otherwise #### - ifndef SUF + ifndef MEXEXT $(error Unknowm platform $(PLATFORM)) endif endif diff --git a/@gifti/private/gifti_read.m b/@gifti/private/gifti_read.m index 1211db3..1d7a158 100644 --- a/@gifti/private/gifti_read.m +++ b/@gifti/private/gifti_read.m @@ -203,6 +203,10 @@ % special case that does not require permuting d = reshape(d,s.Dim); else + if isa(d,'file_array') + %warning('Memory-mapped data are loaded into memory'); + d = subsref(d,substruct('()',repmat({':'},1,numel(d.dim)))); + end d = permute(reshape(d,fliplr(s.Dim)),length(s.Dim):-1:1); end case 'ColumnMajorOrder' diff --git a/@gifti/private/obj_read.m b/@gifti/private/obj_read.m index 6fd9a71..5d00e37 100644 --- a/@gifti/private/obj_read.m +++ b/@gifti/private/obj_read.m @@ -40,7 +40,7 @@ otherwise v = sscanf(l(2:end),'%f %f %f'); if numel(v) > 3, v = v(1:3); end - M.vertices(size(M.vertices,1)+1,:) = v; + M.vertices(:,size(M.vertices,2)+1) = v; end case 'f' f = sscanf(l(2:end),'%d %d %d'); @@ -64,8 +64,8 @@ end end i = find(f<0); - if isempty(i), f(i) = size(M.vertices,1) + f(i); end - M.faces(size(M.faces,1)+1,:) = f; + if isempty(i), f(i) = size(M.vertices,2) + f(i); end + M.faces(:,size(M.faces,2)+1) = f; case 'o' fprintf('Ignoring named objects.\n'); case 'g' @@ -82,3 +82,6 @@ end fclose(fid); + +M.vertices = M.vertices'; +M.faces = M.faces'; diff --git a/@gifti/save.m b/@gifti/save.m index 2f3668f..9e3bb7a 100644 --- a/@gifti/save.m +++ b/@gifti/save.m @@ -1,4 +1,4 @@ -function save(this,filename,encoding) +function save(this,filename,encoding,ordering) % Save GIfTI object in a GIfTI format file % FORMAT save(this,filename,encoding) % this - GIfTI object @@ -6,6 +6,9 @@ function save(this,filename,encoding) % encoding - optional argument to specify encoding format, among % ASCII, Base64Binary, GZipBase64Binary, ExternalFileBinary. % [Default: 'GZipBase64Binary'] +% ordering - optional argument to specify array element ordering, among +% ColumnMajorOrder, RowMajorOrder +% [Default: 'ColumnMajorOrder'] %__________________________________________________________________________ % Guillaume Flandin @@ -24,6 +27,24 @@ function save(this,filename,encoding) end filename = fullfile(p,[f e]); +% Check encoding +%-------------------------------------------------------------------------- +if nargin < 3 || isempty(encoding), encoding = 'GZipBase64Binary'; end +switch encoding + case {'ASCII','Base64Binary','GZipBase64Binary','ExternalFileBinary'} + otherwise + error('Unknown encoding.'); +end + +% Check order +%-------------------------------------------------------------------------- +if nargin < 4 || isempty(ordering), ordering = 'ColumnMajorOrder'; end +switch ordering + case {'ColumnMajorOrder','RowMajorOrder'} + otherwise + error('Unknown ordering.'); +end + % Open file for writing %-------------------------------------------------------------------------- fid = fopen(filename,'wt'); @@ -33,13 +54,7 @@ function save(this,filename,encoding) % Write file %-------------------------------------------------------------------------- -if nargin < 3, encoding = 'GZipBase64Binary'; end -switch encoding - case {'ASCII','Base64Binary','GZipBase64Binary','ExternalFileBinary'} - otherwise - error('Unknown encoding.'); -end -fid = save_gii(fid,this,encoding); +fid = save_gii(fid,this,encoding,ordering); % Close file %-------------------------------------------------------------------------- @@ -47,9 +62,9 @@ function save(this,filename,encoding) %========================================================================== -% function fid = save_gii(fid,this,encoding) +% function fid = save_gii(fid,this,encoding,ordering) %========================================================================== -function fid = save_gii(fid,this,encoding) +function fid = save_gii(fid,this,encoding,ordering) % Defaults for DataArray's attributes %-------------------------------------------------------------------------- @@ -81,8 +96,7 @@ function save(this,filename,encoding) for j=1:length(d) this.data{i}.attributes.(sprintf('Dim%d',j-1)) = num2str(d(j)); end - % Enforce some conventions - this.data{i}.attributes.ArrayIndexingOrder = 'ColumnMajorOrder'; + this.data{i}.attributes.ArrayIndexingOrder = ordering; if ~isfield(this.data{i}.attributes,'DataType') || ... isempty(this.data{i}.attributes.DataType) warning('DataType set to default: %s', def.DataType); @@ -206,7 +220,7 @@ function save(this,filename,encoding) fprintf(fid,'%s\n',o(2)); end - % Data (saved using MATLAB's ColumnMajorOrder) + % Data %---------------------------------------------------------------------- fprintf(fid,'%s',o(2)); tp = getdict; @@ -215,21 +229,24 @@ function save(this,filename,encoding) catch error('[GIFTI] Unknown DataType.'); end + dat = this.data{i}.data; + if isa(dat,'file_array') + dat = subsref(dat,substruct('()',repmat({':'},1,numel(dat.dim)))); + end + if strcmp(this.data{i}.attributes.ArrayIndexingOrder,'RowMajorOrder') + dat = dat.'; + end switch this.data{i}.attributes.Encoding case 'ASCII' - fprintf(fid, [tp.format ' '], this.data{i}.data); + fprintf(fid, [tp.format ' '], dat); case 'Base64Binary' - fwrite(fid,base64('encode',typecast(this.data{i}.data(:),'uint8')),'uint8'); + fprintf(fid,base64encode(typecast(dat(:),'uint8'))); % uses native machine format case 'GZipBase64Binary' - fwrite(fid,base64('encode',zstream('C',typecast(this.data{i}.data(:),'uint8'))),'uint8'); + fprintf(fid,base64encode(zstream('C',typecast(dat(:),'uint8')))); % uses native machine format case 'ExternalFileBinary' extfilename = this.data{i}.attributes.ExternalFileName; - dat = this.data{i}.data; - if isa(dat,'file_array') - dat = subsref(dat,substruct('()',repmat({':'},1,numel(dat.dim)))); - end if ~def.offset fide = fopen(extfilename,'w'); % uses native machine format else