From 6dc6b4f7224c8c3f7adfdcabbd805edaa99d13fc Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 29 Jun 2018 17:31:17 +0100 Subject: [PATCH] add niftiload skeleton --- configure.ac | 4 +- libvips/foreign/Makefile.am | 1 + libvips/foreign/foreign.c | 5 + libvips/foreign/niftiload.c | 181 +++++++++++++++++++++++++++++++++ libvips/include/vips/foreign.h | 3 + 5 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 libvips/foreign/niftiload.c diff --git a/configure.ac b/configure.ac index bb9b02a6..4fc5c6d9 100644 --- a/configure.ac +++ b/configure.ac @@ -1297,8 +1297,8 @@ do done | sort -ru` VIPS_CFLAGS=`echo $VIPS_CFLAGS` VIPS_CFLAGS="$VIPS_DEBUG_FLAGS $VIPS_CFLAGS" -VIPS_INCLUDES="$ZLIB_INCLUDES $PNG_INCLUDES $TIFF_INCLUDES $JPEG_INCLUDES" -VIPS_LIBS="$ZLIB_LIBS $MAGICK_LIBS $PNG_LIBS $IMAGEQUANT_LIBS $TIFF_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $EXPAT_LIBS $PANGOFT2_LIBS $GSF_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $GIFLIB_LIBS $RSVG_LIBS $PDFIUM_LIBS $POPPLER_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $LIBWEBP_LIBS $LIBWEBPMUX_LIBS $MATIO_LIBS $EXIF_LIBS -lm" +VIPS_INCLUDES="$ZLIB_INCLUDES $PNG_INCLUDES $TIFF_INCLUDES $JPEG_INCLUDES $NIFTI_INCLUDES" +VIPS_LIBS="$ZLIB_LIBS $MAGICK_LIBS $PNG_LIBS $IMAGEQUANT_LIBS $TIFF_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $EXPAT_LIBS $PANGOFT2_LIBS $GSF_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $GIFLIB_LIBS $RSVG_LIBS $NIFTI_LIBS $PDFIUM_LIBS $POPPLER_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $LIBWEBP_LIBS $LIBWEBPMUX_LIBS $MATIO_LIBS $EXIF_LIBS -lm" AC_SUBST(VIPS_LIBDIR) diff --git a/libvips/foreign/Makefile.am b/libvips/foreign/Makefile.am index 58a33053..c74d94b3 100644 --- a/libvips/foreign/Makefile.am +++ b/libvips/foreign/Makefile.am @@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libforeign.la libforeign_la_SOURCES = \ pforeign.h \ + niftiload.c \ quantise.c \ exif.c \ gifload.c \ diff --git a/libvips/foreign/foreign.c b/libvips/foreign/foreign.c index 276edb12..74c935f2 100644 --- a/libvips/foreign/foreign.c +++ b/libvips/foreign/foreign.c @@ -1836,6 +1836,7 @@ vips_foreign_operation_init( void ) extern GType vips_foreign_load_svg_get_type( void ); extern GType vips_foreign_load_svg_file_get_type( void ); extern GType vips_foreign_load_svg_buffer_get_type( void ); + extern GType vips_foreign_load_nifti_get_type( void ); extern GType vips_foreign_load_gif_get_type( void ); extern GType vips_foreign_load_gif_file_get_type( void ); extern GType vips_foreign_load_gif_buffer_get_type( void ); @@ -1958,6 +1959,10 @@ vips_foreign_operation_init( void ) vips_foreign_load_openexr_get_type(); #endif /*HAVE_OPENEXR*/ +#ifdef HAVE_NIFTI + vips_foreign_load_nifti_get_type(); +#endif /*HAVE_NIFTI*/ + vips__foreign_load_operation = g_quark_from_static_string( "vips-foreign-load-operation" ); } diff --git a/libvips/foreign/niftiload.c b/libvips/foreign/niftiload.c new file mode 100644 index 00000000..5ff2f253 --- /dev/null +++ b/libvips/foreign/niftiload.c @@ -0,0 +1,181 @@ +/* load nifti from a file + * + * 29/6/18 + * - from niftiload.c + */ + +/* + + This file is part of VIPS. + + VIPS is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +/* +#define DEBUG + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include +#include + +#include +#include +#include + +#ifdef HAVE_NIFTI + +#include + +#include "pforeign.h" + +typedef struct _VipsForeignLoadNifti { + VipsForeignLoad parent_object; + + /* Filename for load. + */ + char *filename; + +} VipsForeignLoadNifti; + +typedef VipsForeignLoadClass VipsForeignLoadNiftiClass; + +G_DEFINE_TYPE( VipsForeignLoadNifti, vips_foreign_load_nifti, + VIPS_TYPE_FOREIGN_LOAD ); + +static int +vips_foreign_load_nifti_is_a( const char *filename ) +{ + nifti_image *nim; + + gboolean result; + + VIPS_DEBUG_MSG( "nifti_is_a: testing \"%s\"\n", filename ); + + result = FALSE; + if( (nim = nifti_image_read( filename, FALSE )) ) { + nifti_image_free( nim ); + result = TRUE; + } + + return( result ); +} + +static int +vips_foreign_load_nifti_header( VipsForeignLoad *load ) +{ + VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( load ); + VipsForeignLoadNifti *nifti = (VipsForeignLoadNifti *) load; + + nifti_image *nim; + + /* FALSE means don't read data, just the header. Use + * nifti_image_load() later to pull the data in. + */ + if( !(nim = nifti_image_read( nifti->filename, FALSE )) ) { + vips_error( class->nickname, + "%s", _( "unable to read NIFTI file" ) ); + return( 0 ); + } + + /* Set load->out. + */ + + nifti_image_free( nim ); + + VIPS_SETSTR( load->out->filename, nifti->filename ); + + return( 0 ); +} + +const char *vips__nifti_suffs[] = { ".nii", ".nii.gz", NULL }; + +static void +vips_foreign_load_nifti_class_init( VipsForeignLoadNiftiClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *object_class = (VipsObjectClass *) class; + VipsForeignClass *foreign_class = (VipsForeignClass *) class; + VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class; + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + object_class->nickname = "niftiload"; + object_class->description = _( "load a FITS image" ); + + /* is_a() is not that quick ... lower the priority. + */ + foreign_class->priority = -50; + + foreign_class->suffs = vips__nifti_suffs; + + load_class->is_a = vips_foreign_load_nifti_is_a; + load_class->header = vips_foreign_load_nifti_header; + + VIPS_ARG_STRING( class, "filename", 1, + _( "Filename" ), + _( "Filename to load from" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsForeignLoadNifti, filename ), + NULL ); +} + +static void +vips_foreign_load_nifti_init( VipsForeignLoadNifti *nifti ) +{ +} + +#endif /*HAVE_CFITSIO*/ + +/** + * vips_niftiload: + * @filename: file to load + * @out: (out): decompressed image + * @...: %NULL-terminated list of optional named arguments + * + * Read a NIFTI image file into a VIPS image. + * + * NIFTI metadata is attached with the "nifti-" prefix. + * + * See also: vips_image_new_from_file(). + * + * Returns: 0 on success, -1 on error. + */ +int +vips_niftiload( const char *filename, VipsImage **out, ... ) +{ + va_list ap; + int result; + + va_start( ap, out ); + result = vips_call_split( "niftiload", ap, filename, out ); + va_end( ap ); + + return( result ); +} diff --git a/libvips/include/vips/foreign.h b/libvips/include/vips/foreign.h index 3e7ed922..57d0b321 100644 --- a/libvips/include/vips/foreign.h +++ b/libvips/include/vips/foreign.h @@ -560,6 +560,9 @@ int vips_gifload( const char *filename, VipsImage **out, ... ) int vips_gifload_buffer( void *buf, size_t len, VipsImage **out, ... ) __attribute__((sentinel)); +int vips_niftiload( const char *filename, VipsImage **out, ... ) + __attribute__((sentinel)); + /** * VipsForeignDzLayout: * @VIPS_FOREIGN_DZ_LAYOUT_DZ: use DeepZoom directory layout