From 8dc2db978958bc7e3e68fe087c31c9834a1b8c9b Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Sat, 20 Feb 2021 04:52:02 +0000 Subject: [PATCH] add gif background colour as metadata --- ChangeLog | 5 +-- libvips/foreign/gifload.c | 15 +++++++++ libvips/foreign/spngload.c | 11 ++----- libvips/foreign/vipspng.c | 11 ++----- libvips/include/vips/header.h | 4 +++ libvips/iofuncs/header.c | 58 +++++++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2609a35c..63558dab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,8 +17,9 @@ - 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()m vips_niftiload_source() -- png load notes background colour as metadata [781545872] +- add vips_fitsload_source(), vips_niftiload_source() +- png and gif load note background colour as metadata [781545872] +- add vips_image_[set|get]_array_double() 22/12/20 start 8.10.6 - don't seek on bad file descriptors [kleisauke] diff --git a/libvips/foreign/gifload.c b/libvips/foreign/gifload.c index 73210a4c..4507393c 100644 --- a/libvips/foreign/gifload.c +++ b/libvips/foreign/gifload.c @@ -42,6 +42,8 @@ * 2/7/20 * - clip out of bounds images against canvas * - fix PREVIOUS handling, again + * 19/2/21 781545872 + * - read out background, if we can */ /* @@ -701,6 +703,7 @@ static int vips_foreign_load_gif_set_header( VipsForeignLoadGif *gif, VipsImage *image ) { const gint64 total_height = (gint64) gif->file->SHeight * gif->n; + ColorMapObject *map = gif->file->SColorMap; if( total_height <= 0 || total_height > VIPS_MAX_COORD ) { @@ -745,6 +748,18 @@ vips_foreign_load_gif_set_header( VipsForeignLoadGif *gif, VipsImage *image ) if( gif->comment ) vips_image_set_string( image, "gif-comment", gif->comment ); + if( map && + gif->file->SBackGroundColor >= 0 && + gif->file->SBackGroundColor < map->ColorCount ) { + double array[3]; + + array[0] = map->Colors[gif->file->SBackGroundColor].Red; + array[1] = map->Colors[gif->file->SBackGroundColor].Green; + array[2] = map->Colors[gif->file->SBackGroundColor].Blue; + + vips_image_set_array_double( image, "background", array, 3 ); + } + return( 0 ); } diff --git a/libvips/foreign/spngload.c b/libvips/foreign/spngload.c index 0357498b..044110ff 100644 --- a/libvips/foreign/spngload.c +++ b/libvips/foreign/spngload.c @@ -302,14 +302,9 @@ vips_foreign_load_png_set_header( VipsForeignLoadPng *png, VipsImage *image ) break; } - if( n > 0 ) { - GValue value = { 0 }; - - g_value_init( &value, VIPS_TYPE_ARRAY_DOUBLE ); - vips_value_set_array_double( &value, array, n ); - vips_image_set( image, "background", &value ); - g_value_unset( &value ); - } + if( n > 0 ) + vips_image_set_array_double( image, "background", + array, n ); } } diff --git a/libvips/foreign/vipspng.c b/libvips/foreign/vipspng.c index 4540a69a..cce9a112 100644 --- a/libvips/foreign/vipspng.c +++ b/libvips/foreign/vipspng.c @@ -597,14 +597,9 @@ png2vips_header( Read *read, VipsImage *out ) break; } - if( n > 0 ) { - GValue value = { 0 }; - - g_value_init( &value, VIPS_TYPE_ARRAY_DOUBLE ); - vips_value_set_array_double( &value, array, n ); - vips_image_set( out, "background", &value ); - g_value_unset( &value ); - } + if( n > 0 ) + vips_image_set_array_double( out, "background", + array, n ); } } #endif diff --git a/libvips/include/vips/header.h b/libvips/include/vips/header.h index 11e9ea3f..1bf0c579 100644 --- a/libvips/include/vips/header.h +++ b/libvips/include/vips/header.h @@ -230,6 +230,10 @@ void vips_image_set_array_int( VipsImage *image, const char *name, const int *array, int n ); int vips_image_get_array_int( VipsImage *image, const char *name, int **out, int *n ); +int vips_image_get_array_double( VipsImage *image, const char *name, + double **out, int *n ); +void vips_image_set_array_double( VipsImage *image, const char *name, + const double *array, int n ); int vips_image_history_printf( VipsImage *image, const char *format, ... ) __attribute__((format(printf, 2, 3))); diff --git a/libvips/iofuncs/header.c b/libvips/iofuncs/header.c index 3e1e8ca8..fdecbf03 100644 --- a/libvips/iofuncs/header.c +++ b/libvips/iofuncs/header.c @@ -1959,6 +1959,64 @@ vips_image_set_array_int( VipsImage *image, const char *name, g_value_unset( &value ); } +/** + * vips_image_get_array_double: (method) + * @image: image to get the metadata from + * @name: metadata name + * @out: (transfer none): return pointer to array + * @n: (allow-none): return the number of elements here, optionally + * + * Gets @out from @im under the name @name. + * The field must be of type + * #VIPS_TYPE_ARRAY_INT. + * + * Do not free @out. @out is valid as long as @image is valid. + * + * Use vips_image_get_typeof() to test for the + * existence of a piece of metadata. + * + * See also: vips_image_get(), vips_image_set_image() + * + * Returns: 0 on success, -1 otherwise. + */ +int +vips_image_get_array_double( VipsImage *image, const char *name, + double **out, int *n ) +{ + GValue value = { 0 }; + + if( meta_get_value( image, name, VIPS_TYPE_ARRAY_DOUBLE, &value ) ) + return( -1 ); + *out = vips_value_get_array_double( &value, n ); + g_value_unset( &value ); + + return( 0 ); +} + +/** + * vips_image_set_array_double: (method) + * @image: image to attach the metadata to + * @name: metadata name + * @array: (array length=n) (allow-none): array of doubles + * @n: the number of elements + * + * Attaches @array as a metadata item on @image as @name. + * A convenience function over vips_image_set(). + * + * See also: vips_image_get_image(), vips_image_set(). + */ +void +vips_image_set_array_double( VipsImage *image, const char *name, + const double *array, int n ) +{ + GValue value = { 0 }; + + g_value_init( &value, VIPS_TYPE_ARRAY_DOUBLE ); + vips_value_set_array_double( &value, array, n ); + vips_image_set( image, name, &value ); + g_value_unset( &value ); +} + /** * vips_image_history_printf: (method) * @image: add history line to this image