From ea99beb7276e39f4bc860fc4c87fef7b52c10799 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 6 Jan 2011 12:18:40 +0000 Subject: [PATCH] get/set resolution in png files --- ChangeLog | 1 + libvips/format/im_png2vips.c | 26 +++++++++++++++++++++++++- libvips/format/im_vips2png.c | 8 ++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index e318ef92..116035bf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ - faster tiff read for some common cases - faster im_tile_cache() - add -lstdc++ to vips-7.xx.pc, if we used it +- im_vips2png() / im_png2vips() set / get png resolution (thanks Zhiyu Wu) 30/11/10 started 7.24.0 - bump for new stable diff --git a/libvips/format/im_png2vips.c b/libvips/format/im_png2vips.c index 5f454785..de311442 100644 --- a/libvips/format/im_png2vips.c +++ b/libvips/format/im_png2vips.c @@ -16,6 +16,8 @@ * - small cleanups * 4/2/10 * - gtkdoc + * 8/1/11 + * - get png resolution (thanks Zhiyu Wu) */ /* @@ -229,6 +231,9 @@ static int png2vips( Read *read, int header_only ) { int bands, bpp, type; + png_uint_32 res_x, res_y; + int unit_type; + double Xres, Yres; if( setjmp( read->pPng->jmpbuf ) ) return( -1 ); @@ -296,13 +301,32 @@ png2vips( Read *read, int header_only ) if( read->pInfo->bit_depth > 8 && !im_amiMSBfirst() ) png_set_swap( read->pPng ); + /* Get resolution. I'm not sure what we should do for UNKNOWN, since + * vips is always pixels/mm. + */ + png_get_pHYs( read->pPng, read->pInfo, + &res_x, &res_y, &unit_type ); + switch( unit_type ) { + case PNG_RESOLUTION_METER: + Xres = res_x / 1000.0; + Yres = res_y / 1000.0; + break; + + default: + Xres = res_x; + Yres = res_y; + break; + } + /* Set VIPS header. */ im_initdesc( read->out, read->pInfo->width, read->pInfo->height, bands, bpp == 1 ? IM_BBITS_BYTE : IM_BBITS_SHORT, bpp == 1 ? IM_BANDFMT_UCHAR : IM_BANDFMT_USHORT, - IM_CODING_NONE, type, 1.0, 1.0, 0, 0 ); + IM_CODING_NONE, type, + Xres, Yres, + 0, 0 ); if( !header_only ) { if( png_set_interlace_handling( read->pPng ) > 1 ) { diff --git a/libvips/format/im_vips2png.c b/libvips/format/im_vips2png.c index 37d62005..b8076bad 100644 --- a/libvips/format/im_vips2png.c +++ b/libvips/format/im_vips2png.c @@ -19,6 +19,8 @@ * - lololo but broke 8-bit save, fixed again * 20/7/10 Tim Elliott * - added im_vips2bufpng() + * 8/1/11 + * - set png resolution (thanks Zhiyu Wu) */ /* @@ -255,6 +257,12 @@ write_vips( Write *write, int compress, int interlace ) g_assert( 0 ); } + /* Set resolution. libpnbg uses pixels per meter. + */ + png_set_pHYs( write->pPng, write->pInfo, + IM_RINT( in->Xres * 1000 ), IM_RINT( in->Yres * 1000 ), + PNG_RESOLUTION_METER ); + png_write_info( write->pPng, write->pInfo ); /* If we're an intel byte order CPU and this is a 16bit image, we need