Skip to content

Commit

Permalink
Merge branch 'upstream-nifti' into nifti-2023-10-25
Browse files Browse the repository at this point in the history
# By NIFTI Upstream
* upstream-nifti:
  nifti 2023-09-11 (75180f77)
  • Loading branch information
seanm committed Oct 25, 2023
2 parents 4f71b7d + 6a88f61 commit 06fd913
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 55 deletions.
60 changes: 36 additions & 24 deletions Modules/ThirdParty/NIFTI/src/nifti/niftilib/nifti1_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,38 +658,38 @@ int nifti_update_dims_from_array( nifti_image * nim )
nim->dx = nim->pixdim[1];

/* if undefined, or less than 1, set to 1 */
if(nim->dim[0] < 2 || (nim->dim[0] >= 2 && nim->dim[2] < 1))
if(nim->dim[0] < 2 || nim->dim[2] < 1)
nim->ny = nim->dim[2] = 1;
else
nim->ny = nim->dim[2];
/* copy delta values, in any case */
nim->dy = nim->pixdim[2];

if(nim->dim[0] < 3 || (nim->dim[0] >= 3 && nim->dim[3] < 1))
if(nim->dim[0] < 3 || nim->dim[3] < 1)
nim->nz = nim->dim[3] = 1;
else /* just copy vals from arrays */
nim->nz = nim->dim[3];
nim->dz = nim->pixdim[3];

if(nim->dim[0] < 4 || (nim->dim[0] >= 4 && nim->dim[4] < 1))
if(nim->dim[0] < 4 || nim->dim[4] < 1)
nim->nt = nim->dim[4] = 1;
else /* just copy vals from arrays */
nim->nt = nim->dim[4];
nim->dt = nim->pixdim[4];

if(nim->dim[0] < 5 || (nim->dim[0] >= 5 && nim->dim[5] < 1))
if(nim->dim[0] < 5 || nim->dim[5] < 1)
nim->nu = nim->dim[5] = 1;
else /* just copy vals from arrays */
nim->nu = nim->dim[5];
nim->du = nim->pixdim[5];

if(nim->dim[0] < 6 || (nim->dim[0] >= 6 && nim->dim[6] < 1))
if(nim->dim[0] < 6 || nim->dim[6] < 1)
nim->nv = nim->dim[6] = 1;
else /* just copy vals from arrays */
nim->nv = nim->dim[6];
nim->dv = nim->pixdim[6];

if(nim->dim[0] < 7 || (nim->dim[0] >= 7 && nim->dim[7] < 1))
if(nim->dim[0] < 7 || nim->dim[7] < 1)
nim->nw = nim->dim[7] = 1;
else /* just copy vals from arrays */
nim->nw = nim->dim[7];
Expand Down Expand Up @@ -2527,7 +2527,7 @@ int nifti_is_complete_filename(const char* fname)
return 0;
}

if ( ext && ext == fname ) { /* then no filename prefix */
if ( ext == fname ) { /* then no filename prefix */
if ( g_opts.debug > 0 )
fprintf(stderr,"-- no prefix for filename '%s'\n", fname);
return 0;
Expand Down Expand Up @@ -3227,13 +3227,18 @@ static int fileext_compare(const char * test_ext, const char * known_ext)
{
char caps[8] = "";
size_t c,len;

/* Pointer-equal is equal, including both NULL. */
if( test_ext == known_ext ) return 0;

/* If one is NULL consider it as less than the other. */
if( !test_ext ) return -1;
if( !known_ext ) return 1;

/* if equal, don't need to check case (store to avoid multiple calls) */
const int cmp = strcmp(test_ext, known_ext);
if( cmp == 0 ) return cmp;

/* if anything odd, use default */
if( !test_ext || !known_ext ) return cmp;

len = strlen(known_ext);
if( len > 7 ) return cmp;

Expand All @@ -3252,13 +3257,18 @@ static int fileext_n_compare(const char * test_ext,
{
char caps[8] = "";
size_t c,len;

/* Pointer-equal is equal, including both NULL. */
if( test_ext == known_ext ) return 0;

/* If one is NULL consider it as less than the other. */
if( !test_ext ) return -1;
if( !known_ext ) return 1;

/* if equal, don't need to check case (store to avoid multiple calls) */
const int cmp = strncmp(test_ext, known_ext, maxlen);
if( cmp == 0 ) return cmp;

/* if anything odd, use default */
if( !test_ext || !known_ext ) return cmp;

len = strlen(known_ext);
if( len > maxlen ) len = maxlen; /* ignore anything past maxlen */
if( len > 7 ) return cmp;
Expand Down Expand Up @@ -4263,7 +4273,7 @@ nifti_image *nifti_image_read( const char *hname , int read_data )

if( g_opts.debug > 3 ){
fprintf(stderr,"+d nifti_image_read(), have nifti image:\n");
if( g_opts.debug > 2 ) nifti_image_infodump(nim);
nifti_image_infodump(nim);
}

/**- check for extensions (any errors here means no extensions) */
Expand Down Expand Up @@ -4536,8 +4546,8 @@ static int nifti_add_exten_to_list( nifti1_extension * new_ext,

/* check for failure first */
if( ! *list ){
fprintf(stderr,"** failed to alloc %d extension structs (%d bytes)\n",
new_length, new_length*(int)sizeof(nifti1_extension));
fprintf(stderr,"** failed to alloc %d extension structs (%zu bytes)\n",
new_length, new_length*sizeof(nifti1_extension));
if( !tmplist ) return -1; /* no old list to lose */

*list = tmplist; /* reset list to old one */
Expand Down Expand Up @@ -4858,7 +4868,7 @@ static znzFile nifti_image_load_prep( nifti_image *nim )
return NULL;
}
ii = nifti_get_filesize( nim->iname ) ;
if( ii <= 0 ){
if( ii == 0 ){
if( g_opts.debug > 0 ) LNI_FERR(fname,"empty data file",nim->iname);
znzclose(fp);
return NULL;
Expand Down Expand Up @@ -5213,7 +5223,7 @@ static int nifti_write_extensions(znzFile fp, nifti_image *nim)
{
nifti1_extension * list;
char extdr[4] = { 0, 0, 0, 0 };
int c, size, ok = 1;
int c, size, ok;

if( znz_isnull(fp) || !nim || nim->num_ext < 0 ){
if( g_opts.debug > 0 )
Expand Down Expand Up @@ -6193,7 +6203,7 @@ static char *escapize_string( const char * str )
*//*-------------------------------------------------------------------------*/
char *nifti_image_to_ascii( const nifti_image *nim )
{
char *buf , *ebuf ; int nbuf ;
char *buf , *ebuf , *newbuf; int nbuf ;

if( nim == NULL ) return NULL ; /* stupid caller */

Expand Down Expand Up @@ -6430,9 +6440,12 @@ char *nifti_image_to_ascii( const nifti_image *nim )
snprintf( buf+strlen(buf) , bufLen-strlen(buf) , "/>\n" ) ; /* XML-ish closer */

nbuf = (int)strlen(buf) ;
buf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */
if( !buf ) fprintf(stderr,"** NITA: failed to realloc %d bytes\n",nbuf+1);
return buf ;
newbuf = (char *)realloc((void *)buf, nbuf+1); /* cut back to proper length */
if( !newbuf ){
free(buf);
fprintf(stderr,"** NITA: failed to realloc %d bytes\n",nbuf+1);
}
return newbuf ;
}

/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -6721,7 +6734,6 @@ int nifti_nim_has_valid_dims(nifti_image * nim, int complain)

/**- start with dim[0]: failure here is considered terminal */
if( nim->dim[0] <= 0 || nim->dim[0] > 7 ){
errs++;
if( complain )
fprintf(stderr,"** NVd: dim[0] (%d) out of range [1,7]\n",nim->dim[0]);
return 0;
Expand Down Expand Up @@ -6763,7 +6775,7 @@ int nifti_nim_has_valid_dims(nifti_image * nim, int complain)
for( c = 1; c <= nim->dim[0]; c++ ){
if( nim->dim[c] > 0)
prod *= nim->dim[c];
else if( nim->dim[c] <= 0 ){
else {
if( !complain ) return 0;
fprintf(stderr,"** NVd: dim[%d] (=%d) <= 0\n",c, nim->dim[c]);
errs++;
Expand Down
6 changes: 3 additions & 3 deletions Modules/ThirdParty/NIFTI/src/nifti/niftilib/nifti1_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int local_fileexists(const char* fname)
int main( int argc , char *argv[] )
{
nifti_image *nim ;
int iarg=1 , outmode=1 , argn=1, usegzip=0;
int iarg=1 , outmode=1 , argn, usegzip=0;
char *tmpstr;
size_t ll;

Expand Down Expand Up @@ -76,8 +76,8 @@ int main( int argc , char *argv[] )
if( iarg >= argc ){ nifti_image_infodump(nim); exit(0); }

nim->nifti_type = outmode ;
if( nim->fname != NULL ) free(nim->fname) ;
if( nim->iname != NULL ) free(nim->iname) ;
free(nim->fname) ;
free(nim->iname) ;

ll = strlen(argv[iarg]) ;
tmpstr = nifti_makebasename(argv[iarg]);
Expand Down
55 changes: 34 additions & 21 deletions Modules/ThirdParty/NIFTI/src/nifti/niftilib/nifti1_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,8 @@ int act_add_exts( nt_opts * opts )
}

/* if extension came from file, free the data */
if( edata ){ free(edata); edata = NULL; }
free(edata);
edata = NULL;
}

if( opts->keep_hist && nifti_add_extension(nim, opts->command,
Expand Down Expand Up @@ -2167,7 +2168,7 @@ int remove_ext_list( nifti_image * nim, const char ** elist, int len )
disp_nifti1_extension("+d removing ext: ",nim->ext_list+ec,-1);

/* delete this data, and shift the list down (yeah, inefficient) */
if( nim->ext_list[ec].edata ) free( nim->ext_list[ec].edata );
free( nim->ext_list[ec].edata );

/* move anything above down one */
for( c = ec+1; c < nim->num_ext; c++ )
Expand Down Expand Up @@ -2631,7 +2632,7 @@ int act_mod_hdrs( nt_opts * opts )
/* if all is well, overwrite header in fname dataset */
(void)write_hdr_to_file(nhdr, fname); /* errors printed in function */

if( dupname ) free(dupname);
free(dupname);
free(nhdr);
}

Expand Down Expand Up @@ -2750,7 +2751,7 @@ int act_swap_hdrs( nt_opts * opts )
/* if all is well, overwrite header in fname dataset */
(void)write_hdr_to_file(nhdr, fname); /* errors printed in function */

if( dupname ) free(dupname);
free(dupname);
free(nhdr);
}

Expand Down Expand Up @@ -3550,8 +3551,7 @@ int diff_field(field_s *fieldp, void * str0, void * str1, int nfields)

if( ! ext0 && ! ext1 ) break; /* continue on */

if( ext0 && ! ext1 ) return 1; /* pointer diff is diff */
if( ! ext0 && ext1 ) return 1;
if( !(ext0 && ext1) ) return 1; /* pointer diff is diff */

/* just check size and type for a single extension */
if( ext0->esize != ext1->esize ) return 1;
Expand Down Expand Up @@ -3662,8 +3662,11 @@ int diff_hdrs( nifti_1_header * s0, nifti_1_header * s1, int display )
for( c = 0; c < NT_HDR_NUM_FIELDS; c++, fp++ )
if( diff_field(fp, s0, s1, 1) )
{
if( display ) disp_field(NULL, fp, s0, 1, ndiff == 0);
if( display ) disp_field(NULL, fp, s1, 1, 0);
if( display )
{
disp_field(NULL, fp, s0, 1, ndiff == 0);
disp_field(NULL, fp, s1, 1, 0);
}
ndiff++;
}

Expand All @@ -3682,8 +3685,11 @@ int diff_nims( nifti_image * s0, nifti_image * s1, int display )
for( c = 0; c < NT_NIM_NUM_FIELDS; c++, fp++ )
if( diff_field(fp, s0, s1, 1) )
{
if( display ) disp_field(NULL, fp, s0, 1, ndiff == 0);
if( display ) disp_field(NULL, fp, s1, 1, 0);
if( display )
{
disp_field(NULL, fp, s0, 1, ndiff == 0);
disp_field(NULL, fp, s1, 1, 0);
}
ndiff++;
}

Expand All @@ -3707,8 +3713,11 @@ int diff_hdrs_list( nifti_1_header * s0, nifti_1_header * s1, str_list * slist,
fp = get_hdr_field(*sptr, 1); /* "not found" displayed in func */
if( fp && diff_field(fp, s0, s1, 1) )
{
if( display ) disp_field(NULL, fp, s0, 1, ndiff == 0);
if( display ) disp_field(NULL, fp, s1, 1, 0);
if( display )
{
disp_field(NULL, fp, s0, 1, ndiff == 0);
disp_field(NULL, fp, s1, 1, 0);
}
ndiff++;
}
sptr++;
Expand All @@ -3734,8 +3743,11 @@ int diff_nims_list( nifti_image * s0, nifti_image * s1, str_list * slist,
fp = get_nim_field(*sptr, 1); /* "not found" displayed in func */
if( fp && diff_field(fp, s0, s1, 1) )
{
if( display ) disp_field(NULL, fp, s0, 1, ndiff == 0);
if( display ) disp_field(NULL, fp, s1, 1, 0);
if( display )
{
disp_field(NULL, fp, s0, 1, ndiff == 0);
disp_field(NULL, fp, s1, 1, 0);
}
ndiff++;
}
sptr++;
Expand Down Expand Up @@ -3804,7 +3816,8 @@ int act_disp_ci( nt_opts * opts )
if( len < 0 || !data )
{
fprintf(stderr,"** FAILURE for dataset '%s'\n", nim->fname);
if( data ) { free(data); data = NULL; }
free(data);
data = NULL;
err++;
}

Expand All @@ -3826,7 +3839,7 @@ int act_disp_ci( nt_opts * opts )
nifti_image_free(nim);
}

if( data ) free(data);
free(data);

return 0;
}
Expand Down Expand Up @@ -4106,11 +4119,11 @@ static int free_opts_mem( nt_opts * nopt )
{
if( !nopt ) return 1;

if( nopt->elist.list ) free(nopt->elist.list);
if( nopt->etypes.list ) free(nopt->etypes.list);
if( nopt->flist.list ) free(nopt->flist.list);
if( nopt->vlist.list ) free(nopt->vlist.list);
if( nopt->infiles.list ) free(nopt->infiles.list);
free(nopt->elist.list);
free(nopt->etypes.list);
free(nopt->flist.list);
free(nopt->vlist.list);
free(nopt->infiles.list);

return 0;
}
Expand Down
14 changes: 8 additions & 6 deletions Modules/ThirdParty/NIFTI/src/nifti/niftilib/nifti_tester001.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
* test program for NIFTI lib.
*/
#include <stdbool.h>
#include <nifti1_io.h>
enum NIFTITEST_BOOL {
NIFTITEST_TRUE=1,
NIFTITEST_FALSE=0
};

static void PrintTest_eng(const int line,const char * message,const int FailureOccured, const enum NIFTITEST_BOOL isFatal,int *ErrorAccum)
static void PrintTest_eng(const int line,const char * message,const int FailureOccured, const enum NIFTITEST_BOOL isFatal,int *ErrorAccum,bool verbose)
{
if(FailureOccured==NIFTITEST_TRUE) /* This line can be commented out for a more verbose output */
if(verbose || FailureOccured==NIFTITEST_TRUE)
{
char const * const PREFIX= (FailureOccured)?"==========ERROR":"..........SUCCESS";
char const * const ISFATALPREFIX= (isFatal && FailureOccured)?" FATAL":"";
Expand All @@ -23,8 +24,9 @@ static void PrintTest_eng(const int line,const char * message,const int FailureO
}
}
}
/* Pass true instead of false for a more verbose output */
#define PrintTest(message,failure,isfailure,errorcount) \
PrintTest_eng(__LINE__,message,failure,isfailure,errorcount)
PrintTest_eng(__LINE__,message,failure,isfailure,errorcount,false)

static nifti_image * generate_reference_image( const char * write_image_filename , int * const Errors)
{
Expand Down Expand Up @@ -167,14 +169,13 @@ static void compare_reference_image_values(nifti_image const * const reference_i
PrintTest("Checking description",(strncmp(reference_image->descrip,reloaded_image->descrip,80))!=0,NIFTITEST_FALSE,Errors);
}

int main (int argc, char *argv[])
int main (int argc, const char *argv[])
{
if (argc > 1)
{
printf("The test program takes no arguments: %s", argv[0]);
return EXIT_FAILURE;
}
char TEMP_STR[256];
nifti_set_debug_level(3);
int Errors=0;
{
Expand Down Expand Up @@ -283,7 +284,7 @@ int main (int argc, char *argv[])

nifti_image * nim_orig, * nim_select;

int blist[5] = { 7, 0, 5, 5, 9 };
const int blist[5] = { 7, 0, 5, 5, 9 };

/*
* test some error paths in the nifti_image_read_bricks
Expand Down Expand Up @@ -416,6 +417,7 @@ int main (int argc, char *argv[])
unsigned int fni;
for(fni=0;fni<NUM_FILE_NAMES;fni++)
{
char TEMP_STR[256];
printf("\nTesting \"%s\" filename\n",FILE_NAMES[fni]);
{
int KnownValid=nifti_validfilename(FILE_NAMES[fni]);
Expand Down
Loading

0 comments on commit 06fd913

Please sign in to comment.