diff --git a/CMakeLists.txt b/CMakeLists.txt index 713b8733bdf..4fe425b15eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ include(CMakeDependentOption) if("${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.13") set(CMAKE_VER_AT_LEAST_3_13 1 CACHE BOOL "Is set for cmake >=3.13") +mark_as_advanced(CMAKE_VER_AT_LEAST_3_13) endif() # add option to build shared libraries. The default is OFF to maintain the diff --git a/cmake/nifti_macros.cmake b/cmake/nifti_macros.cmake index 6e32fe917af..5d66b9f3b02 100644 --- a/cmake/nifti_macros.cmake +++ b/cmake/nifti_macros.cmake @@ -130,10 +130,12 @@ function(install_nifti_target target_name) LIBRARY DESTINATION ${NIFTI_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries - COMPONENT Development - PUBLIC_HEADER DESTINATION ${NIFTI_INSTALL_INCLUDE_DIR} - COMPONENT Development - INCLUDES DESTINATION ${NIFTI_INSTALL_INCLUDE_DIR} + PUBLIC_HEADER + DESTINATION ${NIFTI_INSTALL_INCLUDE_DIR} + COMPONENT Development + INCLUDES + DESTINATION ${NIFTI_INSTALL_INCLUDE_DIR} + COMPONENT Development ) endfunction() diff --git a/niftilib/Makefile b/niftilib/Makefile index 14e5bd29bf5..172176c1cd5 100644 --- a/niftilib/Makefile +++ b/niftilib/Makefile @@ -30,8 +30,8 @@ libniftiio.a: $(OBJS) $(AR) -r libniftiio.a $(OBJS) $(RANLIB) $@ -nifti_tool: nifti_tool.c nifti_tool.h - $(CC) $(CFLAGS) -Wall -o nifti_tool nifti_tool.c $(NIFTI_INCS) $(ZNZ_INCS) $(NIFTI_LIBS) $(ZNZ_LIBS) +nifti1_tool: nifti1_tool.c nifti1_tool.h + $(CC) $(CFLAGS) -Wall -o nifti1_tool nifti1_tool.c $(NIFTI_INCS) $(ZNZ_INCS) $(NIFTI_LIBS) $(ZNZ_LIBS) nifti1_test: nifti1_test.c $(CC) $(CFLAGS) -o nifti1_test nifti1_test.c $(NIFTI_INCS) $(ZNZ_INCS) $(NIFTI_LIBS) $(ZNZ_LIBS) diff --git a/niftilib/nifti1_io.c b/niftilib/nifti1_io.c index f04908052bc..57564537040 100644 --- a/niftilib/nifti1_io.c +++ b/niftilib/nifti1_io.c @@ -343,9 +343,11 @@ static char const * const gni_history[] = " - nifti_read_ascii_image no longer closes fp or free's fname\n", "2.1.0 18 Jun 2020 [leej3,hmjohnson,rickr]:\n" " - big version jump - changed to more formal library versioning\n", + "2.1.0.1 - non-release update - 16 Jun 2022 [rickr]:\n" + " - add nifti_image_write_status\n", "----------------------------------------------------------------------\n" }; -static const char gni_version[] = NIFTI1_IO_SOURCE_VERSION " (18 Jun, 2020)"; +static const char gni_version[] = NIFTI1_IO_SOURCE_VERSION " (16 Jun, 2022)"; /*! global nifti options structure - init with defaults */ static nifti_global_options g_opts = { @@ -446,6 +448,8 @@ static int unescape_string (char *str); /* string utility functions */ static char *escapize_string (const char *str); /* internal I/O routines */ +static int nifti_image_write_engine(nifti_image *nim, int write_opts, + const char *opts, znzFile *imgfile, const nifti_brick_list *NBL); static znzFile nifti_image_load_prep( nifti_image *nim ); static int has_ascii_header(znzFile fp); /*---------------------------------------------------------------------------*/ @@ -1681,7 +1685,7 @@ mat44 nifti_mat44_inverse( mat44 R ) v1 = R.m[0][3]; v2 = R.m[1][3]; v3 = R.m[2][3]; /* [ 0 0 0 1 ] */ deti = r11*r22*r33-r11*r32*r23-r21*r12*r33 - +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; + +r21*r32*r13+r31*r12*r23-r31*r22*r13 ; /* determinant */ if( deti != 0.0l ) deti = 1.0l / deti ; @@ -1689,19 +1693,19 @@ mat44 nifti_mat44_inverse( mat44 R ) Q.m[0][1] = (float)( deti*(-r12*r33+r32*r13) ) ; Q.m[0][2] = (float)( deti*( r12*r23-r22*r13) ) ; Q.m[0][3] = (float)( deti*(-r12*r23*v3+r12*v2*r33+r22*r13*v3 - -r22*v1*r33-r32*r13*v2+r32*v1*r23) ) ; + -r22*v1*r33-r32*r13*v2+r32*v1*r23) ) ; Q.m[1][0] = (float)( deti*(-r21*r33+r31*r23) ) ; Q.m[1][1] = (float)( deti*( r11*r33-r31*r13) ) ; Q.m[1][2] = (float)( deti*(-r11*r23+r21*r13) ) ; Q.m[1][3] = (float)( deti*( r11*r23*v3-r11*v2*r33-r21*r13*v3 - +r21*v1*r33+r31*r13*v2-r31*v1*r23) ) ; + +r21*v1*r33+r31*r13*v2-r31*v1*r23) ) ; Q.m[2][0] = (float)( deti*( r21*r32-r31*r22) ) ; Q.m[2][1] = (float)( deti*(-r11*r32+r31*r12) ) ; Q.m[2][2] = (float)( deti*( r11*r22-r21*r12) ) ; Q.m[2][3] = (float)( deti*(-r11*r22*v3+r11*r32*v2+r21*r12*v3 - -r21*r32*v1-r31*r12*v2+r31*r22*v1) ) ; + -r21*r32*v1-r31*r12*v2+r31*r22*v1) ) ; Q.m[3][0] = Q.m[3][1] = Q.m[3][2] = 0.0l ; Q.m[3][3] = (deti == 0.0l) ? 0.0l : 1.0l ; /* failure flag if deti == 0 */ @@ -2122,7 +2126,8 @@ void nifti_mat44_to_orientation( mat44 R , int *icod, int *jcod, int *kcod ) case -3: k = NIFTI_S2I ; break ; } - *icod = i ; *jcod = j ; *kcod = k ; } + *icod = i ; *jcod = j ; *kcod = k ; +} /*---------------------------------------------------------------------------*/ /* Routines to swap byte arrays in various ways: @@ -2151,7 +2156,7 @@ void nifti_swap_2bytes( size_t n , void *ar ) /* 2 bytes at a time */ tval = *cp1; *cp1 = *cp2; *cp2 = tval; cp1 += 2; } - } +} /*----------------------------------------------------------------------*/ /*! swap 4 bytes at a time from the given list of n sets of 4 bytes @@ -2169,7 +2174,7 @@ void nifti_swap_4bytes( size_t n , void *ar ) /* 4 bytes at a time */ tval = *cp1; *cp1 = *cp2; *cp2 = tval; cp0 += 4; } - } +} /*----------------------------------------------------------------------*/ /*! swap 8 bytes at a time from the given list of n sets of 8 bytes @@ -2191,7 +2196,7 @@ void nifti_swap_8bytes( size_t n , void *ar ) /* 8 bytes at a time */ } cp0 += 8; } - } +} /*----------------------------------------------------------------------*/ /*! swap 16 bytes at a time from the given list of n sets of 16 bytes @@ -2211,7 +2216,7 @@ void nifti_swap_16bytes( size_t n , void *ar ) /* 16 bytes at a time */ } cp0 += 16; } - } +} #if 0 /* not important: save for version update 6 Jul 2010 [rickr] */ @@ -2253,7 +2258,7 @@ void nifti_swap_Nbytes( size_t n , int siz , void *ar ) /* subsuming case */ fprintf(stderr,"** NIfTI: cannot swap in %d byte blocks\n", siz); break ; } - } +} /*-------------------------------------------------------------------------*/ @@ -2409,7 +2414,7 @@ void old_swap_nifti_header( struct nifti_1_header *h , int is_nifti ) nifti_swap_4bytes(4,h->srow_y); nifti_swap_4bytes(4,h->srow_z); } - } +} #define USE_STAT @@ -5682,25 +5687,42 @@ znzFile nifti_image_write_hdr_img( nifti_image *nim , int write_data , return nifti_image_write_hdr_img2(nim,write_data,opts,NULL,NULL); } +/*----------------------------------------------------------------------*/ +/*! This writes the header (and optionally the image data) to file. + * + * This is now just a front-end for nifti_image_write_engine, but the + * engine will return a status (for success of write), which is promptly + * ignored by this function. + * + * \sa nifti_image_write_engine +*//*--------------------------------------------------------------------*/ +znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, + const char * opts, znzFile imgfile, const nifti_brick_list * NBL) +{ + znzFile loc_img = imgfile; /* might be NULL, might point to open struct */ + (void)nifti_image_write_engine(nim, write_opts, opts, &loc_img, NBL); + return loc_img; +} #undef ERREX -#define ERREX(msg) \ - do{ fprintf(stderr,"** ERROR: nifti_image_write_hdr_img: %s\n",(msg)) ; \ - return fp ; } while(0) +#define ERREX(msg) \ + do{ fprintf(stderr,"** ERROR: nifti_image_write_engine: %s\n",(msg)) ; \ + if( imgfile ) *imgfile = fp; \ + return 1 ; } while(0) /* ----------------------------------------------------------------------*/ /*! This writes the header (and optionally the image data) to file * - * If the image data file is left open it returns a valid znzFile handle. - * It also uses imgfile as the open image file is not null, and modifies - * it inside. + * If imgfile points to a NULL znzFile, it modifies it to a valid and open + * handle. If it points to an non-NULL znzFile, it uses that as the open + * image and simply modifies that structure. This also depends on write_opts. * * \param nim nifti_image to write to disk * \param write_opts flags whether to write data and/or close file (see below) * \param opts file-open options, probably "wb" from nifti_image_write() - * \param imgfile optional open znzFile struct, for writing image data - (may be NULL) + * \param imgfile pointer to optionally open znzFile, for writing image data + (must not be NULL, contents might be NULL) * \param NBL optional nifti_brick_list, containing the image data (may be NULL) * @@ -5714,19 +5736,19 @@ znzFile nifti_image_write_hdr_img( nifti_image *nim , int write_data , * \sa nifti_image_write, nifti_image_write_hdr_img, nifti_image_free, * nifti_set_filenames *//*---------------------------------------------------------------------*/ -znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, - const char * opts, znzFile imgfile, const nifti_brick_list * NBL) +static int nifti_image_write_engine(nifti_image *nim, int write_opts, + const char *opts, znzFile *imgfile, const nifti_brick_list *NBL) { struct nifti_1_header nhdr ; znzFile fp=NULL; size_t ss ; int write_data, leave_open; - char func[] = { "nifti_image_write_hdr_img2" }; + char func[] = { "nifti_image_write_engine" }; write_data = write_opts & 1; /* just separate the bits now */ leave_open = write_opts & 2; - if( ! nim ) ERREX("NULL input") ; + if( ! nim || ! imgfile ) ERREX("NULL input") ; if( ! nifti_validfilename(nim->fname) ) ERREX("bad fname input") ; if( write_data && ! nim->data && ! NBL ) ERREX("no image data") ; @@ -5735,6 +5757,7 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, nifti_set_iname_offset(nim); + /* chit-chat */ if( g_opts.debug > 1 ){ fprintf(stderr,"-d writing nifti file '%s'...\n", nim->fname); if( g_opts.debug > 2 ) @@ -5742,8 +5765,13 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, nim->nifti_type, nim->iname_offset); } - if( nim->nifti_type == NIFTI_FTYPE_ASCII ) /* non-standard case */ - return nifti_write_ascii_image(nim,NBL,opts,write_data,leave_open); + /* get to work */ + + /* if non-standard ASCII, just write out and return */ + if( nim->nifti_type == NIFTI_FTYPE_ASCII ) { + *imgfile = nifti_write_ascii_image(nim,NBL,opts,write_data,leave_open); + return 0; /* write_ascii has no status */ + } nhdr = nifti_convert_nim2nhdr(nim); /* create the nifti1_header struct */ @@ -5754,22 +5782,27 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, } if( nim->iname == NULL ){ /* then make a new one */ nim->iname = nifti_makeimgname(nim->fname,nim->nifti_type,0,0); - if( nim->iname == NULL ) return NULL; + if( nim->iname == NULL ) { + *imgfile = NULL; + return 1; + } } } /* if we have an imgfile and will write the header there, use it */ - if( ! znz_isnull(imgfile) && nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ){ + if( ! znz_isnull(*imgfile) && nim->nifti_type == NIFTI_FTYPE_NIFTI1_1 ){ if( g_opts.debug > 2 ) fprintf(stderr,"+d using passed file for hdr\n"); - fp = imgfile; + fp = *imgfile; } else { + /* we will write the header to a new file */ if( g_opts.debug > 2 ) fprintf(stderr,"+d opening output file %s [%s]\n",nim->fname,opts); fp = znzopen( nim->fname , opts , nifti_is_gzfile(nim->fname) ) ; if( znz_isnull(fp) ){ LNI_FERR(func,"cannot open output file",nim->fname); - return fp; + *imgfile = fp; + return 1; } } @@ -5778,24 +5811,27 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, ss = znzwrite(&nhdr , 1 , sizeof(nhdr) , fp); /* write header */ if( ss < sizeof(nhdr) ){ LNI_FERR(func,"bad header write to output file",nim->fname); - znzclose(fp); return fp; + znzclose(fp); *imgfile = fp; return 1; } - /* partial file exists, and errors have been printed, so ignore return */ + /* write extensions; any errors will be printed */ if( nim->nifti_type != NIFTI_FTYPE_ANALYZE ) - (void)nifti_write_extensions(fp,nim); + if( nifti_write_extensions(fp,nim) < 0 ) { + znzclose(fp); *imgfile = fp; return 1; + } /* if the header is all we want, we are done */ if( ! write_data && ! leave_open ){ if( g_opts.debug > 2 ) fprintf(stderr,"-d header is all we want: done\n"); - znzclose(fp); return(fp); + znzclose(fp); *imgfile = fp; return 0; } + /* if multiple files (hdr/img), close fp and use (any) *imgfile for data */ if( nim->nifti_type != NIFTI_FTYPE_NIFTI1_1 ){ /* get a new file pointer */ znzclose(fp); /* first, close header file */ - if( ! znz_isnull(imgfile) ){ + if( ! znz_isnull(*imgfile) ){ if(g_opts.debug > 2) fprintf(stderr,"+d using passed file for img\n"); - fp = imgfile; + fp = *imgfile; } else { if( g_opts.debug > 2 ) @@ -5810,7 +5846,9 @@ znzFile nifti_image_write_hdr_img2(nifti_image *nim, int write_opts, if( write_data ) nifti_write_all_data(fp,nim,NBL); if( ! leave_open ) znzclose(fp); - return fp; + *imgfile = fp; + + return 0; } @@ -5865,33 +5903,81 @@ znzFile nifti_write_ascii_image(nifti_image *nim, const nifti_brick_list * NBL, fields from the qto_xyz matrix, you can use the utility function nifti_mat44_to_quatern() + \return 0 on success, -1 on error + \sa nifti_image_write_bricks, nifti_image_free, nifti_set_filenames, nifti_image_write_hdr_img *//*------------------------------------------------------------------------*/ void nifti_image_write( nifti_image *nim ) { - znzFile fp = nifti_image_write_hdr_img(nim,1,"wb"); + znzFile fp=NULL; + int rv; + + rv = nifti_image_write_engine(nim, 1, "wb", &fp, NULL); if( fp ){ if( g_opts.debug > 2 ) fprintf(stderr,"-d niw: done with znzFile\n"); free(fp); } - if( g_opts.debug > 1 ) fprintf(stderr,"-d nifti_image_write: done\n"); + if( g_opts.debug > 1 ) + fprintf(stderr,"-d nifti_image_write: done, status %d\n", rv); +} + + +/*--------------------------------------------------------------------------*/ +/*! Write a nifti_image to disk, returning 0 on success, else failure. + + This simple write function takes a nifti_image as input and returns + the status of the operation. It is akin to nifti_image_write, but + returns the status. Changing nifti_image_write from void to int + would have backward compatibility ramifications. + + \sa nifti_image_write_bricks, nifti_image_free, nifti_set_filenames, + nifti_image_write_engine, nifti_image_write +*//*------------------------------------------------------------------------*/ +int nifti_image_write_status( nifti_image *nim ) +{ + znzFile fp=NULL; /* required for _engine, but promptly ignored */ + int rv; + + rv = nifti_image_write_engine(nim, 1, "wb", &fp, NULL); + if( g_opts.debug > 1 ) + fprintf(stderr,"-d nifti_image_write_status: done, status %d\n", rv); + return rv; } /*----------------------------------------------------------------------*/ /*! similar to nifti_image_write, but data is in NBL struct, not nim->data + \return 0 on success, 1 on error + \sa nifti_image_write, nifti_image_free, nifti_set_filenames, nifti_free_NBL *//*--------------------------------------------------------------------*/ -void nifti_image_write_bricks( nifti_image *nim, const nifti_brick_list * NBL ) +int nifti_image_write_bricks_status( nifti_image *nim, + const nifti_brick_list * NBL ) { - znzFile fp = nifti_image_write_hdr_img2(nim,1,"wb",NULL,NBL); + znzFile fp=NULL; + int rv; + + rv = nifti_image_write_engine(nim, 1, "wb", &fp, NBL); if( fp ){ if( g_opts.debug > 2 ) fprintf(stderr,"-d niwb: done with znzFile\n"); free(fp); } - if( g_opts.debug > 1 ) fprintf(stderr,"-d niwb: done writing bricks\n"); + if( g_opts.debug > 1 ) + fprintf(stderr,"-d niwb: done writing bricks, status %d\n", rv); + return rv; +} + + +/*----------------------------------------------------------------------*/ +/*! similar to nifti_image_write, but data is in NBL struct, not nim->data + + \sa nifti_image_write, nifti_image_free, nifti_set_filenames, nifti_free_NBL +*//*--------------------------------------------------------------------*/ +void nifti_image_write_bricks( nifti_image *nim, const nifti_brick_list * NBL ) +{ + (void)nifti_image_write_bricks_status(nim, NBL); } @@ -7180,7 +7266,7 @@ static int make_pivot_list(nifti_image * nim, const int dims[], int pivots[], dim_index = nim->dim[0]; while( dim_index > 0 ){ prods[len] = 1; - while( dim_index > 0 && + while( dim_index > 0 && (nim->dim[dim_index] == 1 || dims[dim_index] == -1) ){ prods[len] *= nim->dim[dim_index]; dim_index--; diff --git a/niftilib/nifti1_io.h b/niftilib/nifti1_io.h index c9619e8a6a2..5abee12eb40 100644 --- a/niftilib/nifti1_io.h +++ b/niftilib/nifti1_io.h @@ -17,7 +17,7 @@ #endif #include "nifti1.h" /*** NIFTI-1 header specification ***/ -#include +#include "znzlib.h" /*=================*/ #ifdef __cplusplus @@ -299,8 +299,12 @@ int nifti_read_subregion_image( nifti_image * nim, void ** data ); void nifti_image_write ( nifti_image * nim ) ; +int nifti_image_write_status( nifti_image *nim ); + void nifti_image_write_bricks(nifti_image * nim, const nifti_brick_list * NBL); +int nifti_image_write_bricks_status(nifti_image * nim, + const nifti_brick_list * NBL); void nifti_image_infodump( const nifti_image * nim ) ; void nifti_disp_lib_hist( void ) ; /* to display library history */ diff --git a/niftilib/nifti1_test.c b/niftilib/nifti1_test.c index 515dad8e382..e057500084f 100644 --- a/niftilib/nifti1_test.c +++ b/niftilib/nifti1_test.c @@ -97,7 +97,12 @@ int main( int argc , char *argv[] ) strcat(nim->fname,".gz"); strcat(nim->iname,".gz"); } - nifti_image_write( nim ) ; + if( nifti_image_write_status( nim ) ) { + fprintf(stderr, "** failed to write nifti_image\n"); + nifti_image_free( nim ) ; + return EXIT_FAILURE; + } + if ( ! local_fileexists( nim->fname ) ) { return EXIT_FAILURE; diff --git a/niftilib/nifti1_tool.c b/niftilib/nifti1_tool.c index 8f8ffb505bc..f5e2c65b763 100644 --- a/niftilib/nifti1_tool.c +++ b/niftilib/nifti1_tool.c @@ -1877,7 +1877,13 @@ int act_add_exts( nt_opts * opts ) fprintf(stderr,"+d writing %s with %d new extension(s)\n", opts->infiles.list[fc], opts->elist.len); - nifti_image_write(nim); + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", + opts->infiles.list[fc]); + nifti_image_free(nim); + return 1; + } + nifti_image_free(nim); } @@ -1990,6 +1996,11 @@ int act_strip( nt_opts * opts ) nim->fname); nifti_image_write(nim); + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } if( g_debug > 3 ) nifti_image_infodump(nim); nifti_image_free(nim); @@ -2063,7 +2074,12 @@ int act_rm_ext( nt_opts * opts ) fprintf(stderr,"+d writing %s with %d fewer extension(s)\n", nim->fname, ext_ind == -1 ? num_ext : opts->elist.len); - nifti_image_write(nim); + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } + nifti_image_free(nim); } @@ -2590,7 +2606,13 @@ int act_mod_hdrs( nt_opts * opts ) } dupname = nifti_strdup(nim->fname); /* so we know to free it */ fname = dupname; - nifti_image_write(nim); /* create the duplicate file */ + /* create the duplicate file */ + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } + /* if we added a history note, get the new offset into the header */ /* mod: if the new offset is valid, use it 31 Jan 2006 [rickr] */ if( nim->iname_offset >= 348 ) nhdr->vox_offset = nim->iname_offset; @@ -2705,7 +2727,13 @@ int act_swap_hdrs( nt_opts * opts ) } dupname = nifti_strdup(nim->fname); /* so we know to free it */ fname = dupname; - nifti_image_write(nim); /* create the duplicate file */ + /* create the duplicate file */ + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } + /* if we added a history note, get the new offset into the header */ /* mod: if the new offset is valid, use it 31 Jan 2006 [rickr] */ if( nim->iname_offset >= 348 ) nhdr->vox_offset = nim->iname_offset; @@ -2766,7 +2794,13 @@ int act_mod_nims( nt_opts * opts ) return 1; } - nifti_image_write(nim); /* and write it out, piece of cake :) */ + /* and write it out, piece of cake :) */ + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } + nifti_image_free(nim); } @@ -4020,7 +4054,13 @@ int act_cci( nt_opts * opts ) if(g_debug>2) disp_field("new nim:\n",g_nim_fields,nim,NT_NIM_NUM_FIELDS,1); /* and finally, write out results */ - if( nifti_nim_is_valid(nim, g_debug) ) nifti_image_write(nim); + if( nifti_nim_is_valid(nim, g_debug) ) { + if( nifti_image_write_status(nim) ) { + fprintf(stderr,"** failed to write image %s\n", nim->fname); + nifti_image_free(nim); + return 1; + } + } nifti_image_free(nim); diff --git a/niftilib/nifti_tester001.c b/niftilib/nifti_tester001.c index 916eb9654f2..ffd659ad6a0 100644 --- a/niftilib/nifti_tester001.c +++ b/niftilib/nifti_tester001.c @@ -226,7 +226,14 @@ int main (int argc, char *argv[]) NIFTITEST_FALSE,&Errors); } PrintTest("Create reference image",reference_image==0,NIFTITEST_TRUE,&Errors); - nifti_image_write ( reference_image ) ; + if( nifti_image_write_status( reference_image ) ) + { + printf("ERROR: failed to write nifti_image."); + fflush(stdout); + nifti_image_free(reference_image); + return EXIT_FAILURE; + } + /* * test nifti_copy_extension */ @@ -351,7 +358,12 @@ int main (int argc, char *argv[]) nifti_image * reference_image = generate_reference_image("TestAsciiImage.nia",&Errors); reference_image->nifti_type = 3; - nifti_image_write(reference_image); + if( nifti_image_write_status( reference_image ) ) + { + printf("ERROR: failed to write nifti_image."); + fflush(stdout); + } + nifti_image * reloaded_image = nifti_image_read("TestAsciiImage.nia",1); PrintTest("Read/Write Ascii image", reloaded_image == 0,NIFTITEST_FALSE,&Errors);