add fitsload_source

and fix some small fits loading issues
This commit is contained in:
John Cupitt 2021-02-11 16:44:53 +00:00
parent 15f4d935aa
commit 498e9c2829
5 changed files with 277 additions and 25 deletions

View File

@ -17,6 +17,7 @@
- hist_find outputs a double histogram for large images [erdmann]
- fix ref leaks in mosaicing package
- run libvips leak test in CI
- add vips_fitsload_source()
22/12/20 start 8.10.6
- don't seek on bad file descriptors [kleisauke]

View File

@ -616,11 +616,22 @@ vips_foreign_load_csv_source_build( VipsObject *object )
return( 0 );
}
static gboolean
vips_foreign_load_csv_source_is_a_source( VipsSource *source )
{
/* Detecting CSV files automatically is tricky. Define this method to
* prevent a warning, but users will need to run the csv loader
* explicitly.
*/
return( FALSE );
}
static void
vips_foreign_load_csv_source_class_init( VipsForeignLoadCsvFileClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
@ -628,6 +639,8 @@ vips_foreign_load_csv_source_class_init( VipsForeignLoadCsvFileClass *class )
object_class->nickname = "csvload_source";
object_class->build = vips_foreign_load_csv_source_build;
load_class->is_a_source = vips_foreign_load_csv_source_is_a_source;
VIPS_ARG_OBJECT( class, "source", 1,
_( "Source" ),
_( "Source to load from" ),

View File

@ -217,7 +217,9 @@ vips_fits_get_header( VipsFits *fits, VipsImage *out )
int status;
int bitpix;
int width, height, bands, format, type;
int width, height, bands;
VipsBandFormat format;
VipsInterpretation interpretation;
int keysexist;
int i;
@ -318,24 +320,24 @@ vips_fits_get_header( VipsFits *fits, VipsImage *out )
if( bands == 1 ) {
if( format == VIPS_FORMAT_USHORT )
type = VIPS_INTERPRETATION_GREY16;
interpretation = VIPS_INTERPRETATION_GREY16;
else
type = VIPS_INTERPRETATION_B_W;
interpretation = VIPS_INTERPRETATION_B_W;
}
else if( bands == 3 ) {
if( format == VIPS_FORMAT_USHORT )
type = VIPS_INTERPRETATION_RGB16;
interpretation = VIPS_INTERPRETATION_RGB16;
else
type = VIPS_INTERPRETATION_sRGB;
interpretation = VIPS_INTERPRETATION_sRGB;
}
else
type = VIPS_INTERPRETATION_MULTIBAND;
interpretation = VIPS_INTERPRETATION_MULTIBAND;
vips_image_pipelinev( out, VIPS_DEMAND_STYLE_SMALLTILE, NULL );
vips_image_init_fields( out,
width, height, bands,
format,
VIPS_CODING_NONE, type, 1.0, 1.0 );
VIPS_CODING_NONE, interpretation, 1.0, 1.0 );
/* Read all keys into meta.
*/
@ -517,7 +519,8 @@ int
vips__fits_read( const char *filename, VipsImage *out )
{
VipsImage *t;
int n_bands;
int bands;
VipsInterpretation interpretation;
VIPS_DEBUG_MSG( "fits2vips: reading \"%s\"\n", filename );
@ -531,22 +534,26 @@ vips__fits_read( const char *filename, VipsImage *out )
g_object_unref( t );
return( -1 );
}
n_bands = t->Bands;
bands = t->Bands;
interpretation = t->Type;
g_object_unref( t );
if( n_bands == 1 ) {
if( bands == 1 ) {
if( fits2vips( filename, out, 0 ) )
return( -1 );
}
else {
VipsImage **x;
VipsImage **y;
int i;
t = vips_image_new();
x = (VipsImage **) vips_object_local_array( VIPS_OBJECT( t ),
n_bands + 1 );
bands );
y = (VipsImage **) vips_object_local_array( VIPS_OBJECT( t ),
3 );
for( i = 0; i < n_bands; i++ ) {
for( i = 0; i < bands; i++ ) {
x[i] = vips_image_new();
if( fits2vips( filename, x[i], i ) ) {
g_object_unref( t );
@ -554,8 +561,11 @@ vips__fits_read( const char *filename, VipsImage *out )
}
}
if( vips_bandjoin( x, &x[n_bands], n_bands, NULL ) ||
vips_image_write( x[n_bands], out ) ) {
if( vips_bandjoin( x, &y[0], bands, NULL ) ||
vips_copy( y[0], &y[1],
"interpretation", interpretation,
NULL ) ||
vips_image_write( y[1], out ) ) {
g_object_unref( t );
return( -1 );
}

View File

@ -55,17 +55,85 @@
typedef struct _VipsForeignLoadFits {
VipsForeignLoad parent_object;
/* Filename for load.
/* Set by subclasses.
*/
char *filename;
VipsSource *source;
/* Filename from source.
*/
const char *filename;
} VipsForeignLoadFits;
typedef VipsForeignLoadClass VipsForeignLoadFitsClass;
G_DEFINE_TYPE( VipsForeignLoadFits, vips_foreign_load_fits,
G_DEFINE_ABSTRACT_TYPE( VipsForeignLoadFits, vips_foreign_load_fits,
VIPS_TYPE_FOREIGN_LOAD );
static void
vips_foreign_load_fits_dispose( GObject *gobject )
{
VipsForeignLoadFits *fits = (VipsForeignLoadFits *) gobject;
VIPS_UNREF( fits->source );
G_OBJECT_CLASS( vips_foreign_load_fits_parent_class )->
dispose( gobject );
}
static int
vips_foreign_load_fits_build( VipsObject *object )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
VipsForeignLoadFits *fits =
(VipsForeignLoadFits *) object;
/* We can only open source which have an associated filename, since
* the fits library works in terms of filenames.
*/
if( fits->source ) {
fits->filename = vips_connection_filename( VIPS_CONNECTION(
fits->source ) );
if( !fits->filename ) {
vips_error( class->nickname, "%s",
_( "no filename available" ) );
return( -1 );
}
}
if( VIPS_OBJECT_CLASS( vips_foreign_load_fits_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static VipsForeignFlags
vips_foreign_load_fits_get_flags_source( VipsSource *source )
{
return( VIPS_FOREIGN_PARTIAL );
}
static VipsForeignFlags
vips_foreign_load_fits_get_flags( VipsForeignLoad *load )
{
return( VIPS_FOREIGN_PARTIAL );
}
static VipsForeignFlags
vips_foreign_load_fits_get_flags_filename( const char *filename )
{
VipsSource *source;
VipsForeignFlags flags;
if( !(source = vips_source_new_from_file( filename )) )
return( 0 );
flags = vips_foreign_load_fits_get_flags_source( source );
VIPS_UNREF( source );
return( flags );
}
static int
vips_foreign_load_fits_header( VipsForeignLoad *load )
{
@ -103,32 +171,167 @@ vips_foreign_load_fits_class_init( VipsForeignLoadFitsClass *class )
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->dispose = vips_foreign_load_fits_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "fitsload";
object_class->description = _( "load a FITS image" );
object_class->nickname = "fitsload_base";
object_class->description = _( "FITS loader base class" );
object_class->build = vips_foreign_load_fits_build;
/* is_a() is not that quick ... lower the priority.
*/
foreign_class->priority = -50;
foreign_class->suffs = vips__fits_suffs;
load_class->get_flags_filename =
vips_foreign_load_fits_get_flags_filename;
load_class->get_flags = vips_foreign_load_fits_get_flags;
load_class->is_a = vips__fits_isfits;
load_class->header = vips_foreign_load_fits_header;
load_class->load = vips_foreign_load_fits_load;
}
static void
vips_foreign_load_fits_init( VipsForeignLoadFits *fits )
{
}
typedef struct _VipsForeignLoadFitsFile {
VipsForeignLoadFits parent_object;
/* Filename for load.
*/
char *filename;
} VipsForeignLoadFitsFile;
typedef VipsForeignLoadFitsClass VipsForeignLoadFitsFileClass;
G_DEFINE_TYPE( VipsForeignLoadFitsFile, vips_foreign_load_fits_file,
vips_foreign_load_fits_get_type() );
static int
vips_foreign_load_fits_file_build( VipsObject *object )
{
VipsForeignLoadFits *fits = (VipsForeignLoadFits *) object;
VipsForeignLoadFitsFile *file = (VipsForeignLoadFitsFile *) object;
if( file->filename &&
!(fits->source = vips_source_new_from_file( file->filename )) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_foreign_load_fits_file_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_load_fits_file_class_init( VipsForeignLoadFitsFileClass *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 = "fitsload";
object_class->description = _( "load a FITS image" );
object_class->build = vips_foreign_load_fits_file_build;
foreign_class->suffs = vips__fits_suffs;
load_class->is_a = vips__fits_isfits;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadFits, filename ),
G_STRUCT_OFFSET( VipsForeignLoadFitsFile, filename ),
NULL );
}
static void
vips_foreign_load_fits_init( VipsForeignLoadFits *fits )
vips_foreign_load_fits_file_init( VipsForeignLoadFitsFile *file )
{
}
typedef struct _VipsForeignLoadFitsSource {
VipsForeignLoadFits parent_object;
/* Load from a source.
*/
VipsSource *source;
} VipsForeignLoadFitsSource;
typedef VipsForeignLoadFitsClass VipsForeignLoadFitsSourceClass;
G_DEFINE_TYPE( VipsForeignLoadFitsSource, vips_foreign_load_fits_source,
vips_foreign_load_fits_get_type() );
static int
vips_foreign_load_fits_source_build( VipsObject *object )
{
VipsForeignLoadFits *fits = (VipsForeignLoadFits *) object;
VipsForeignLoadFitsSource *source =
(VipsForeignLoadFitsSource *) object;
if( source->source ) {
fits->source = source->source;
g_object_ref( fits->source );
}
if( VIPS_OBJECT_CLASS( vips_foreign_load_fits_source_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static gboolean
vips_foreign_load_fits_source_is_a_source( VipsSource *source )
{
const char *filename;
return( (filename =
vips_connection_filename( VIPS_CONNECTION( source ) )) &&
vips__fits_isfits( filename ) );
}
static void
vips_foreign_load_fits_source_class_init(
VipsForeignLoadFitsSourceClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) 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 = "fitsload_source";
object_class->description = _( "load FITS from a source" );
object_class->build = vips_foreign_load_fits_source_build;
load_class->is_a_source =
vips_foreign_load_fits_source_is_a_source;
VIPS_ARG_OBJECT( class, "source", 1,
_( "Source" ),
_( "Source to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadFitsSource, source ),
VIPS_TYPE_SOURCE );
}
static void
vips_foreign_load_fits_source_init( VipsForeignLoadFitsSource *fits )
{
}
@ -166,3 +369,26 @@ vips_fitsload( const char *filename, VipsImage **out, ... )
return( result );
}
/**
* vips_fitsload_source:
* @source: source to load from
* @out: (out): decompressed image
* @...: %NULL-terminated list of optional named arguments
*
* Exactly as vips_fitsload(), but read from a source.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_fitsload_source( VipsSource *source, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "fitsload_source", ap, source, out );
va_end( ap );
return( result );
}

View File

@ -2117,7 +2117,8 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_save_matrix_target_get_type( void );
extern GType vips_foreign_print_matrix_get_type( void );
extern GType vips_foreign_load_fits_get_type( void );
extern GType vips_foreign_load_fits_file_get_type( void );
extern GType vips_foreign_load_fits_source_get_type( void );
extern GType vips_foreign_save_fits_get_type( void );
extern GType vips_foreign_load_analyze_get_type( void );
@ -2324,7 +2325,8 @@ vips_foreign_operation_init( void )
#endif /*ENABLE_MAGICKSAVE*/
#ifdef HAVE_CFITSIO
vips_foreign_load_fits_get_type();
vips_foreign_load_fits_file_get_type();
vips_foreign_load_fits_source_get_type();
vips_foreign_save_fits_get_type();
#endif /*HAVE_CFITSIO*/