From 8730d8ddf1f33d3ea95d3acb051d7335960bd77d Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 20 Jul 2018 18:20:26 +0100 Subject: [PATCH] add nifti ext save --- libvips/foreign/niftisave.c | 47 ++++++++++++++++++++++++++++++++-- libvips/include/vips/foreign.h | 2 ++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/libvips/foreign/niftisave.c b/libvips/foreign/niftisave.c index 9427630d..a0c618ad 100644 --- a/libvips/foreign/niftisave.c +++ b/libvips/foreign/niftisave.c @@ -235,6 +235,41 @@ vips_foreign_save_nifti_set_fields( const char *name, return( NULL ); } +static void * +vips_foreign_save_nifti_ext( VipsImage *image, + const char *field, GValue *value, void *a ) +{ + nifti_image *nim = (nifti_image *) a; + + int i; + int ecode; + char *data; + size_t length; + + if( !vips_isprefix( "nifti-ext-", field ) ) + return( NULL ); + + /* The name is "nifti-ext-N-XX" where N is the index (discard this) + * and XX is the nifti ext ecode. + */ + if( sscanf( field, "nifti-ext-%d-%d", &i, &ecode ) != 2 ) { + vips_error( "niftisave", + "%s", _( "bad nifti-ext- field name" ) ); + return( image ); + } + + if( vips_image_get_blob( image, field, (void *) &data, &length ) ) + return( image ); + + if( nifti_add_extension( nim, data, length, ecode ) ) { + vips_error( "niftisave", + "%s", _( "unable to attach nifti ext" ) ); + return( image ); + } + + return( NULL ); +} + /* Make ->nim from the nifti- fields. */ static int @@ -309,6 +344,12 @@ vips_foreign_save_nifti_header_nifti( VipsForeignSaveNifti *nifti, vips_foreign_save_nifti_set_fields, &info, NULL ) ) return( -1 ); + /* Attach any ext blocks. + */ + if( vips_image_map( image, + (VipsImageMapFn) vips_foreign_save_nifti_ext, nifti->nim ) ) + return( -1 ); + return( 0 ); } @@ -423,9 +464,11 @@ vips_foreign_save_nifti_init( VipsForeignSaveNifti *nifti ) * @filename: file to write to * @...: %NULL-terminated list of optional named arguments * - * Write a VIPS image to a file in NIFTI format. + * Write a VIPS image to a file in NIFTI format. * - * See also: vips_image_write_to_file(). + * Use the various NIFTI suffixes to pick the nifti save format. + * + * See also: vips_image_write_to_file(), vips_niftiload(). * * Returns: 0 on success, -1 on error. */ diff --git a/libvips/include/vips/foreign.h b/libvips/include/vips/foreign.h index 57d0b321..4711d448 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -562,6 +562,8 @@ int vips_gifload_buffer( void *buf, size_t len, VipsImage **out, ... ) int vips_niftiload( const char *filename, VipsImage **out, ... ) __attribute__((sentinel)); +int vips_niftisave( VipsImage *in, const char *filename, ... ) + __attribute__((sentinel)); /** * VipsForeignDzLayout: