diff --git a/cplusplus/include/vips/VImage8.h b/cplusplus/include/vips/VImage8.h index 2577dfad..ee96ac60 100644 --- a/cplusplus/include/vips/VImage8.h +++ b/cplusplus/include/vips/VImage8.h @@ -917,6 +917,22 @@ public: return( new_from_image( to_vectorv( 1, pixel ) ) ); } + static VImage + new_from_memory_steal( void *data, size_t size, + int width, int height, int bands, VipsBandFormat format ) + { + VipsImage *image; + + if( !(image = vips_image_new_from_memory( data, size, + width, height, bands, format )) ) + throw( VError() ); + + g_signal_connect( image, "postclose", + G_CALLBACK(vips_image_free_buffer), data); + + return( VImage( image ) ); + } + /** * Make a new image by rendering self to a large memory area, * wrapping a VImage around it, and copying all metadata over from diff --git a/libvips/include/vips/image.h b/libvips/include/vips/image.h index a73f565b..483cdeee 100644 --- a/libvips/include/vips/image.h +++ b/libvips/include/vips/image.h @@ -524,6 +524,8 @@ int vips_reorder_prepare_many( VipsImage *image, VipsRegion **regions, VipsRect *r ); void vips_reorder_margin_hint( VipsImage *image, int margin ); +void vips_image_free_buffer( VipsImage *image, void *buffer ); + #ifdef __cplusplus } #endif /*__cplusplus*/ diff --git a/libvips/iofuncs/image.c b/libvips/iofuncs/image.c index 2516351d..893768a7 100644 --- a/libvips/iofuncs/image.c +++ b/libvips/iofuncs/image.c @@ -3872,6 +3872,20 @@ vips_band_format_iscomplex( VipsBandFormat format ) } } +/** + * vips_image_free_buffer: + * @image: the image that contains the buffer + * @buffer: the orignal buffer that was stolen + * + * Free the externally allocated buffer found in the input image. This function + * is intened to be used with g_signal_connect. + */ +void +vips_image_free_buffer( VipsImage *image, void *buffer ) +{ + free( buffer ); +} + /* Handy for debugging: view an image in nip2. */ int