nifti2_data_format > Proposal : Cleaning up nifticlib file i/o
Mar 1, 2011  03:03 PM | Cinly Ooi
Proposal : Cleaning up nifticlib file i/o
Dear All,

If nifticlib is to be reworked, can I propose contemplating the clean up file i/o section of nifticlib?

The current solution works, so I am not sure whether it is a good idea to clean up file i/o. But since the worst I will get is someone flaming me and that is unlikely on this forum, I will say my piece and leave it at it.

The issue is the physical file read/write part of nifticlib has to deal with four different formats (hdr/img, nii and their gzip counterpart). The current system effectively requires the niftilib read write function to deal with it themselves. This results in quite a lot of code looking like this:

  if (filetype==hdr) then writeHdrImg()
  else if (filetype==hdr.gz) then writeHdrImgGz()
  else if (filetype==nii) then writeNii()
  else if (filetype==nii.gz) then writeNiiGZ()

As each function has to implement this, it becomes a nightmare to introduce new read/write as all functions doing read/write has to be updated. If nifticlib maintainer wants to introduce the ability to read and write nifti1 and nifti2 simultaneously in this update, another 4 formats (for nifti 2) has to be supported.

I think a better solution worth considering is to use function pointers. This way, say we simply need one read and one write function. We modify nifti_image struct to carry function pointers for the two, e.g.

  struct nifti_image {
    ...
    *(fn_read)(...)
    *(fn_write)(...)
  }

and each of the file format supported will define its own fn_read and fn_write function, e.g.
  * hdr     : fn_hdr_read(...)/fn_hdr_write(...)
  * hdr.gz: fn_hdrgz_read(...)/fn_hdrgz_write(...)
  * nii     : fn_nii_read(...)/fn_nii_write(...)
  * nii.gz: fn_niigz_read(...)/fn_niigz_write(...)

Then, a function will be responsible for attaching the correct set of function pointers to nifti_image, say
  function setIOPointers(nim) {
      if (hdr) { nim->fn_read = fn_hdr_read; nim->fn_write=fn_hdr_write; }
      ...
  }

This way, any function that needs to read/write simply has to call
    nim->fn_read()
or
   nim->fn_write()
instead of the if-else statement in the first example

The advantage is we centralize all read/write function maintenance at one place and make it easier to introduce new format or take old format out.

Thanks and HTH
Cinly

Threaded View

TitleAuthorDate
Mark Jenkinson Feb 28, 2011
Mark Jenkinson Mar 15, 2011
Cinly Ooi Mar 15, 2011
Cinly Ooi Mar 2, 2011
Ged Ridgway Mar 7, 2011
Jon Clayden Mar 5, 2011
Cinly Ooi Mar 1, 2011
Andrew Janke Mar 1, 2011
Cinly Ooi Mar 2, 2011
Satrajit Ghosh Mar 5, 2011
Cinly Ooi Mar 1, 2011
Proposal : Cleaning up nifticlib file i/o
Cinly Ooi Mar 1, 2011
Cinly Ooi Mar 1, 2011
Cinly Ooi Mar 1, 2011
Denis Rivière Feb 28, 2011
Cinly Ooi Mar 1, 2011
Brandon Whitcher Mar 1, 2011
Satrajit Ghosh Feb 28, 2011
Jonas Larsson Mar 1, 2011
Mark Horsfield Mar 1, 2011
Andrew Janke Mar 1, 2011
Jochen Weber Feb 28, 2011
Randall Frank Mar 1, 2011
Michael Martinez Feb 28, 2011
Cinly Ooi Feb 28, 2011
Chris Rorden Feb 28, 2011