From 463058c149cdca7a9dc9b4eea317c9e012907144 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Tue, 7 Aug 2012 14:10:19 +0100 Subject: [PATCH] note and use res unit on jpg load/save previously, jpeg save always used pixels/inch and jpeg load converted to vips pixels/mm now on jpg load the image's res unit is recorded in VIPS_META_RESOLUTION_UNIT, and on jpg save the res unit is set from VIPS_META_RESOLUTION_UNIT (or defaults to inches). you can now copy a cm-preferring tiff to a jpg and the unit is preserved --- ChangeLog | 1 + libvips/foreign/jpeg2vips.c | 6 ++++++ libvips/foreign/vips2jpeg.c | 28 +++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 42ca4b7b..60c67162 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ - so affinei and affinei_all appear in Python - be more cautious enabling YCbCr mode in tiff write - add "DEPRECATED" flag to arguments +- jpeg load/save note and use the preferred resolution unit 20/7/12 started 7.30.0 - support "rs" mode in vips7 diff --git a/libvips/foreign/jpeg2vips.c b/libvips/foreign/jpeg2vips.c index 6166dad6..9a105d74 100644 --- a/libvips/foreign/jpeg2vips.c +++ b/libvips/foreign/jpeg2vips.c @@ -45,6 +45,8 @@ * - read jfif resolution as well as exif * 19/2/12 * - switch to lazy reading + * 7/8/12 + * - note EXIF resolution unit in VIPS_META_RESOLUTION_UNIT */ /* @@ -493,6 +495,8 @@ set_vips_resolution( VipsImage *im, ExifData *ed ) */ xres /= 25.4; yres /= 25.4; + vips_image_set_string( im, + VIPS_META_RESOLUTION_UNIT, "in" ); break; case 3: @@ -500,6 +504,8 @@ set_vips_resolution( VipsImage *im, ExifData *ed ) */ xres /= 10.0; yres /= 10.0; + vips_image_set_string( im, + VIPS_META_RESOLUTION_UNIT, "cm" ); break; default: diff --git a/libvips/foreign/vips2jpeg.c b/libvips/foreign/vips2jpeg.c index 33f5f49b..06398901 100644 --- a/libvips/foreign/vips2jpeg.c +++ b/libvips/foreign/vips2jpeg.c @@ -48,6 +48,8 @@ * - rebuild exif tags from coded metadata values * 24/11/11 * - turn into a set of write fns ready to be called from a class + * 7/8/12 + * - use VIPS_META_RESOLUTION_UNIT to select resoltuion unit */ /* @@ -345,13 +347,33 @@ static int set_exif_resolution( ExifData *ed, VipsImage *im ) { double xres, yres; + char *p; int unit; - /* Always save as inches - more progs support it for read. + /* Default to inches, more progs support it. */ - xres = im->Xres * 25.4; - yres = im->Yres * 25.4; unit = 2; + if( vips_image_get_typeof( im, VIPS_META_RESOLUTION_UNIT ) && + !vips_image_get_string( im, VIPS_META_RESOLUTION_UNIT, &p ) && + vips_isprefix( "cm", p ) ) + unit = 3; + + switch( unit ) { + case 2: + xres = im->Xres * 25.4; + yres = im->Yres * 25.4; + break; + + case 3: + xres = im->Xres * 10.0; + yres = im->Yres * 10.0; + break; + + default: + vips_warn( "VipsJpeg", + "%s", _( "unknown EXIF resolution unit" ) ); + return; + } if( write_tag( ed, EXIF_TAG_X_RESOLUTION, EXIF_FORMAT_RATIONAL, vips_exif_set_double, (void *) &xres ) ||