Dear FS developers,
Firstly, let me thank you for FreeSurfer: I use FS almost daily, and it is fantastic.
Now, onto the bug report:
When reading an analyze header with a modified hdr.hist.originator field, load_analyze_hdr.m sometimes “runs out” of bytes and leaves the last field (hdr.hist.smin) empty. When I pass the hdr struct to write_analyze_hdr.m, an invalid, 344 byte header is written (due to this empty hdr.hist.smin field). I will explain how to replicate the issue after first explaining what I think is happening and how I think it can be fixed.
I notice that on line 119 of load_analyze_hdr.m, the comment reads “Is this correct? Should be unsigned char”; so, perhaps someone had this issue previously, or hand an inkling that the code was problematic. Because of varying character encodings, fscanf(fp,’%c’,10) may read more than 10 bytes, which means all later fields will not be set properly.
The fix is simple, and there are a few options: 1) When opening the hdr file, force ‘US-ASCII’ encoding so each character is precisely 1 byte: (lines 59 and 63) fp = fopen(hdrfile,’r’,’b’,’US-ASCII’);
(line 75) fp = fopen(hdrfile,’r’,’l’,’US-ASCII’);
*This is the preferred solution as it preserves the “readability” of the field. It also would apply to all the other instances of fscanf.*
2) use fread with uchar
(line 119) hdr.hist.originator = fread(fp,10,’uchar’);
3) use fread with char*1 (line 119) hdr.hist.originator = fread(fp,10,’char*1’);
I noticed this bug in the following context. I received a nifti file with an incorrect resolution in the header. When I corrected the resolution using matlab (load_nifti.m -> correct nii.pixdim -> save_nifti.m), converted the nifti image to analyze (mri_convert -ot analyze in.nii out.img), loaded the resulting analyze header in matlab with hdr = load_analyze_hdr(‘out.hdr’), and tried to run write_analyze_hdr(hdr,’new.hdr’), the new.hdr file was an invalid, 344 byte header file. I would be happy to send along sample images if needed.
I am hoping that you will add this fix to later versions of FreeSurfer, and perhaps consider updating 5.3.0, as well.
Thanks and best wishes, Kate
*Kathryn I. Alpert* Software Developer, Neuroimaging and Applied Computational Anatomy Lab Northwestern University Feinberg School of Medicine 312.725.2832 k-alpert@northwestern.edu
[image: Feinberg School of Medicine] *Educate, Discover, Improve Health*