From 0deb640bc43318f0b07474bfd5ae3c8362d96fea Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Mon, 21 Dec 2015 13:53:33 +0000 Subject: [PATCH] load and save TIFFTAG_IMAGEDESCRIPTION this often has useful metadata in, for example the OME spec has all the metadata in there as an XML document see https://github.com/jcupitt/libvips/issues/358 --- ChangeLog | 1 + libvips/foreign/tiff2vips.c | 12 ++++++++++++ libvips/foreign/vips2tiff.c | 33 +++++++++++++++++++++++++++++---- libvips/include/vips/header.h | 7 +++++++ libvips/iofuncs/type.c | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 71052126..9da7707c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,6 +23,7 @@ - oop, removed a DEBUG from buffer.c, vips is 30% faster - faster and lower-mem TIFF read - faster bilinear interpolator +- TIFF loads and saves IMAGEDESCRIPTION 7/5/15 started 8.1.1 - oop, vips-8.0 wrapper script should be vips-8.1, thanks Danilo diff --git a/libvips/foreign/tiff2vips.c b/libvips/foreign/tiff2vips.c index 43807f92..74a1ab5a 100644 --- a/libvips/foreign/tiff2vips.c +++ b/libvips/foreign/tiff2vips.c @@ -160,6 +160,8 @@ * 29/9/15 * - load IPCT metadata * - load photoshop metadata + * 21/12/15 + * - load TIFFTAG_IMAGEDESCRIPTION */ /* @@ -1224,6 +1226,16 @@ parse_header( ReadTiff *rtiff, VipsImage *out ) (VipsCallbackFn) vips_free, data_copy, data_length ); } + /* IMAGEDESCRIPTION often has useful metadata. + */ + if( TIFFGetField( rtiff->tiff, TIFFTAG_IMAGEDESCRIPTION, &data ) ) { + /* libtiff makes sure that data is null-terminated and contains + * no embedded null characters. + */ + vips_image_set_string( out, + VIPS_META_IMAGEDESCRIPTION, (char *) data ); + } + return( 0 ); } diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index a0f8b15a..a21e74df 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -158,6 +158,8 @@ * - try to write photoshop metadata * 11/11/15 * - better alpha handling, thanks sadaqatullahn + * 21/12/15 + * - write TIFFTAG_IMAGEDESCRIPTION */ /* @@ -496,8 +498,8 @@ write_embed_photoshop( Write *write, TIFF *tif ) if( !vips_image_get_typeof( write->im, VIPS_META_PHOTOSHOP_NAME ) ) return( 0 ); - if( vips_image_get_blob( write->im, VIPS_META_PHOTOSHOP_NAME, - &data, &data_length ) ) + if( vips_image_get_blob( write->im, + VIPS_META_PHOTOSHOP_NAME, &data, &data_length ) ) return( -1 ); TIFFSetField( tif, TIFFTAG_PHOTOSHOP, data_length, data ); @@ -508,6 +510,27 @@ write_embed_photoshop( Write *write, TIFF *tif ) return( 0 ); } +/* Set IMAGEDESCRIPTION, if it's there. + */ +static int +write_embed_imagedescription( Write *write, TIFF *tif ) +{ + const char *imagedescription; + + if( !vips_image_get_typeof( write->im, VIPS_META_IMAGEDESCRIPTION ) ) + return( 0 ); + if( vips_image_get_string( write->im, + VIPS_META_IMAGEDESCRIPTION, &imagedescription ) ) + return( -1 ); + TIFFSetField( tif, TIFFTAG_IMAGEDESCRIPTION, imagedescription ); + +#ifdef DEBUG + printf( "vips2tiff: attached imagedescription from meta\n" ); +#endif /*DEBUG*/ + + return( 0 ); +} + /* Write a TIFF header. width and height are the size of the VipsImage we are * writing (it may have been shrunk). */ @@ -543,7 +566,8 @@ write_tiff_header( Write *write, Layer *layer ) if( write_embed_profile( write, tif ) || write_embed_xmp( write, tif ) || write_embed_ipct( write, tif ) || - write_embed_photoshop( write, tif ) ) + write_embed_photoshop( write, tif ) || + write_embed_imagedescription( write, tif ) ) return( -1 ); /* And colour fields. @@ -1497,7 +1521,8 @@ write_copy_tiff( Write *write, TIFF *out, TIFF *in ) if( write_embed_profile( write, out ) || write_embed_xmp( write, out ) || write_embed_ipct( write, out ) || - write_embed_photoshop( write, out ) ) + write_embed_photoshop( write, out ) || + write_embed_imagedescription( write, out ) ) return( -1 ); buf = vips_malloc( NULL, TIFFTileSize( in ) ); diff --git a/libvips/include/vips/header.h b/libvips/include/vips/header.h index 45e8163b..43bd0f3d 100644 --- a/libvips/include/vips/header.h +++ b/libvips/include/vips/header.h @@ -85,6 +85,13 @@ extern "C" { */ #define VIPS_META_XML "xml-header" +/** + * VIPS_META_IMAGEDESCRIPTION: + * + * The IMAGEDESCRIPTION tag. Often has useful metadata. + */ +#define VIPS_META_IMAGEDESCRIPTION "image-description" + /** * VIPS_META_RESOLUTION_UNIT: * diff --git a/libvips/iofuncs/type.c b/libvips/iofuncs/type.c index ee76916f..192e86ea 100644 --- a/libvips/iofuncs/type.c +++ b/libvips/iofuncs/type.c @@ -1413,7 +1413,7 @@ vips_value_get_ref_string( const GValue *value, size_t *length ) * Copies the C string @str into @value. * * vips_ref_string are immutable C strings that are copied between images by - * copying reference-counted pointers, making the much more efficient than + * copying reference-counted pointers, making them much more efficient than * regular %GValue strings. */ void