|
| 1 | +/* load nifti images |
| 2 | + * |
| 3 | + * 10/9/18 |
| 4 | + * - from im_openslide2vips |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | +
|
| 9 | + This file is part of VIPS. |
| 10 | + |
| 11 | + VIPS is free software; you can redistribute it and/or modify |
| 12 | + it under the terms of the GNU Lesser General Public License as published by |
| 13 | + the Free Software Foundation; either version 2 of the License, or |
| 14 | + (at your option) any later version. |
| 15 | +
|
| 16 | + This program is distributed in the hope that it will be useful, |
| 17 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + GNU Lesser General Public License for more details. |
| 20 | +
|
| 21 | + You should have received a copy of the GNU Lesser General Public License |
| 22 | + along with this program; if not, write to the Free Software |
| 23 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 24 | + 02110-1301 USA |
| 25 | +
|
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | +
|
| 30 | + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk |
| 31 | +
|
| 32 | + */ |
| 33 | + |
| 34 | +/* |
| 35 | +#define DEBUG |
| 36 | + */ |
| 37 | + |
| 38 | +#ifdef HAVE_CONFIG_H |
| 39 | +#include <config.h> |
| 40 | +#endif /*HAVE_CONFIG_H*/ |
| 41 | +#include <vips/intl.h> |
| 42 | + |
| 43 | +#include <stdio.h> |
| 44 | +#include <assert.h> |
| 45 | +#include <stdlib.h> |
| 46 | +#include <string.h> |
| 47 | + |
| 48 | +#include <vips/vips.h> |
| 49 | +#include <vips/vips7compat.h> |
| 50 | +#include <vips/thread.h> |
| 51 | +#include <vips/internal.h> |
| 52 | + |
| 53 | +static int |
| 54 | +im_nifti2vips( const char *name, IMAGE *out ) |
| 55 | +{ |
| 56 | + VipsImage *t; |
| 57 | + |
| 58 | + if( vips_niftiload( name, &t, NULL ) ) |
| 59 | + return( -1 ); |
| 60 | + if( vips_image_write( t, out ) ) { |
| 61 | + g_object_unref( t ); |
| 62 | + return( -1 ); |
| 63 | + } |
| 64 | + g_object_unref( t ); |
| 65 | + |
| 66 | + return( 0 ); |
| 67 | +} |
| 68 | + |
| 69 | +static const char *nifti_suffs[] = { |
| 70 | + ".nii", ".nii.gz", |
| 71 | + ".hdr", ".hdr.gz", |
| 72 | + ".img", ".img.gz", |
| 73 | + ".nia", ".nia.gz", |
| 74 | + NULL |
| 75 | +}; |
| 76 | + |
| 77 | +static VipsFormatFlags |
| 78 | +nifti_flags( const char *name ) |
| 79 | +{ |
| 80 | + char filename[FILENAME_MAX]; |
| 81 | + char mode[FILENAME_MAX]; |
| 82 | + |
| 83 | + im_filename_split( name, filename, mode ); |
| 84 | + |
| 85 | + return( (VipsFormatFlags) |
| 86 | + vips_foreign_flags( "niftiload", filename ) ); |
| 87 | +} |
| 88 | + |
| 89 | +static int |
| 90 | +isnifti( const char *name ) |
| 91 | +{ |
| 92 | + char filename[FILENAME_MAX]; |
| 93 | + char mode[FILENAME_MAX]; |
| 94 | + |
| 95 | + im_filename_split( name, filename, mode ); |
| 96 | + |
| 97 | + return( vips_foreign_is_a( "niftiload", filename ) ); |
| 98 | +} |
| 99 | + |
| 100 | +/* nifti format adds no new members. |
| 101 | + */ |
| 102 | +typedef VipsFormat VipsFormatNifti; |
| 103 | +typedef VipsFormatClass VipsFormatNiftiClass; |
| 104 | + |
| 105 | +static void |
| 106 | +vips_format_nifti_class_init( VipsFormatNiftiClass *class ) |
| 107 | +{ |
| 108 | + VipsObjectClass *object_class = (VipsObjectClass *) class; |
| 109 | + VipsFormatClass *format_class = (VipsFormatClass *) class; |
| 110 | + |
| 111 | + object_class->nickname = "im_nifti"; |
| 112 | + object_class->description = _( "NIfTI" ); |
| 113 | + |
| 114 | + format_class->priority = 100; |
| 115 | + format_class->is_a = isnifti; |
| 116 | + format_class->load = im_nifti2vips; |
| 117 | + format_class->get_flags = nifti_flags; |
| 118 | + format_class->suffs = nifti_suffs; |
| 119 | +} |
| 120 | + |
| 121 | +static void |
| 122 | +vips_format_nifti_init( VipsFormatNifti *object ) |
| 123 | +{ |
| 124 | +} |
| 125 | + |
| 126 | +G_DEFINE_TYPE( VipsFormatNifti, vips_format_nifti, VIPS_TYPE_FORMAT ); |
| 127 | + |
0 commit comments