verify_nii_ext.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. % Verify NIFTI header extension to make sure that each extension section
  2. % must be an integer multiple of 16 byte long that includes the first 8
  3. % bytes of esize and ecode. If the length of extension section is not the
  4. % above mentioned case, edata should be padded with all 0.
  5. %
  6. % Usage: [ext, esize_total] = verify_nii_ext(ext)
  7. %
  8. % ext - Structure of NIFTI header extension, which includes num_ext,
  9. % and all the extended header sections in the header extension.
  10. % Each extended header section will have its esize, ecode, and
  11. % edata, where edata can be plain text, xml, or any raw data
  12. % that was saved in the extended header section.
  13. %
  14. % esize_total - Sum of all esize variable in all header sections.
  15. %
  16. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  17. %
  18. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  19. %
  20. function [ext, esize_total] = verify_nii_ext(ext)
  21. if ~isfield(ext, 'section')
  22. error('Incorrect NIFTI header extension structure.');
  23. elseif ~isfield(ext, 'num_ext')
  24. ext.num_ext = length(ext.section);
  25. elseif ~isfield(ext, 'extension')
  26. ext.extension = [1 0 0 0];
  27. end
  28. esize_total = 0;
  29. for i=1:ext.num_ext
  30. if ~isfield(ext.section(i), 'ecode') | ~isfield(ext.section(i), 'edata')
  31. error('Incorrect NIFTI header extension structure.');
  32. end
  33. ext.section(i).esize = ceil((length(ext.section(i).edata)+8)/16)*16;
  34. ext.section(i).edata = ...
  35. [ext.section(i).edata ...
  36. zeros(1,ext.section(i).esize-length(ext.section(i).edata)-8)];
  37. esize_total = esize_total + ext.section(i).esize;
  38. end
  39. return % verify_nii_ext