-
Notifications
You must be signed in to change notification settings - Fork 27
/
fix_qform.m
33 lines (32 loc) · 986 Bytes
/
fix_qform.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function fix_qform (fnm)
%set qform to zero https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h
% fnm : nifti image to fix
%Examples
% fix_qform; %use graphical interface
% fix_qform('test.nii');
if ~exist('fnm','var') %fnm not specified
[A,Apth] = uigetfile({'*.nii;';'*.*'},'Select image to patch');
fnm = fullfile(Apth, A);
end;
[pth,nam,ext] = fileparts( fnm);
if ~strcmpi(ext,'.nii'), error('Requires uncompressed .nii files'); end;
qOffsetBytes = 252;
%check that file needs fixing
fid = fopen(fnm);
fseek(fid,qOffsetBytes,'bof');
qform_code = fread(fid,1,'int16');
fclose(fid);
if qform_code == 0, error('qform already set to zero %s', fnm); end;
%create copy of image and modify
outnm = fullfile(pth,['z',nam,ext]);
copyfile(fnm,outnm);
fid = fopen(fnm);
[data,count]=fread(fid, 'uint8');
fclose(fid);
fid = fopen(outnm,'w');
%modify both bytes of 16-bit qform_code
data(qOffsetBytes) = 0;
data(qOffsetBytes+1) = 0;
fwrite(fid,data);
fclose(fid);
%end fix_qform()