From 54b203b9e7b74d9251088527129c2fe26ee57592 Mon Sep 17 00:00:00 2001 From: Ruven Date: Sun, 6 Oct 2024 22:43:11 +0200 Subject: [PATCH 1/3] Modified TIFF reader to set resolution to zero if no resolution set within TIFF file. TIFF writer now only sets resolution if this is really known, rather than setting a default value. The resolution of sub-resolution layers within a pyramid TIFF are now scaled depending on the sub-sampling of that layer. --- libvips/foreign/tiff2vips.c | 4 ++-- libvips/foreign/vips2tiff.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libvips/foreign/tiff2vips.c b/libvips/foreign/tiff2vips.c index 9b553d9d80..b3f6b9b459 100644 --- a/libvips/foreign/tiff2vips.c +++ b/libvips/foreign/tiff2vips.c @@ -541,8 +541,8 @@ get_resolution(TIFF *tiff, VipsImage *out) /* We used to warn about missing res data, but it happens so * often and is so harmless, why bother. */ - x = 1.0; - y = 1.0; + x = 0.0; + y = 0.0; } out->Xres = x; diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index e80fc3db6d..82e332de01 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -885,13 +885,15 @@ wtiff_write_header(Wtiff *wtiff, Layer *layer) !wtiff->tile) wtiff->we_compress = FALSE; - /* Don't write mad resolutions (eg. zero), it confuses some programs. + /* Only set resolution if we actually have one. Also, mad resolutions (eg. zero) confuse some programs. */ - TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, wtiff->resunit); - TIFFSetField(tif, TIFFTAG_XRESOLUTION, - VIPS_FCLIP(0.01, wtiff->xres, 1000000)); - TIFFSetField(tif, TIFFTAG_YRESOLUTION, - VIPS_FCLIP(0.01, wtiff->yres, 1000000)); + if(wtiff->xres || wtiff->yres){ + TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, wtiff->resunit); + /* Scale resolution by the subsampling factor of the current level + */ + if(wtiff->xres) TIFFSetField(tif, TIFFTAG_XRESOLUTION, (wtiff->xres/layer->sub)); + if(wtiff->yres) TIFFSetField(tif, TIFFTAG_YRESOLUTION, (wtiff->yres/layer->sub)); + } if (wtiff_embed_xmp(wtiff, tif) || wtiff_embed_iptc(wtiff, tif) || From 0034ff886ae84c47c1229927b720dca8817d4f68 Mon Sep 17 00:00:00 2001 From: Ruven Date: Sun, 6 Oct 2024 22:46:13 +0200 Subject: [PATCH 2/3] Resolution only set when saving as PNG if resolution is really known --- libvips/foreign/vipspng.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libvips/foreign/vipspng.c b/libvips/foreign/vipspng.c index b5dc7a8bae..2e02552482 100644 --- a/libvips/foreign/vipspng.c +++ b/libvips/foreign/vipspng.c @@ -1163,11 +1163,13 @@ write_vips(Write *write, in->Xsize, in->Ysize, bitdepth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - /* Set resolution. libpng uses pixels per meter. + /* Set resolution if known. libpng uses pixels per meter. */ - png_set_pHYs(write->pPng, write->pInfo, - VIPS_RINT(in->Xres * 1000), VIPS_RINT(in->Yres * 1000), - PNG_RESOLUTION_METER); + if(in->Xres && in->Yres){ + png_set_pHYs(write->pPng, write->pInfo, + VIPS_RINT(in->Xres * 1000), VIPS_RINT(in->Yres * 1000), + PNG_RESOLUTION_METER); + } /* Metadata */ From 86c49c3c62ebe9a459202f9295875f2c02b88c2c Mon Sep 17 00:00:00 2001 From: Ruven Date: Sun, 6 Oct 2024 22:47:20 +0200 Subject: [PATCH 3/3] Made default X/Y resolution zero for Vips images rather than 1.0 --- libvips/iofuncs/image.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libvips/iofuncs/image.c b/libvips/iofuncs/image.c index 16a8bb632a..807ca40ba5 100644 --- a/libvips/iofuncs/image.c +++ b/libvips/iofuncs/image.c @@ -1344,8 +1344,8 @@ vips_image_init(VipsImage *image) image->Ysize = 1; image->Bands = 1; - image->Xres = 1.0; - image->Yres = 1.0; + image->Xres = 0.0; + image->Yres = 0.0; image->fd = -1; /* since 0 is stdout */ image->sslock = vips_g_mutex_new();