From 1ba3be81a12a7adaf3cfaa8b9f081fe2c8d2f0d8 Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Thu, 4 Apr 2024 15:44:13 +0200 Subject: [PATCH] tiffsave: sync with jpegsave --- libvips/foreign/vips2tiff.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/libvips/foreign/vips2tiff.c b/libvips/foreign/vips2tiff.c index d1309304b0..c5c265f468 100644 --- a/libvips/foreign/vips2tiff.c +++ b/libvips/foreign/vips2tiff.c @@ -682,21 +682,32 @@ wtiff_compress_jpeg_header(Wtiff *wtiff, jpeg_set_defaults(cinfo); - // disable chroma subsample for high Q - if (wtiff->Q >= 90) + /* Set compression quality. + */ + jpeg_set_quality(cinfo, wtiff->Q, TRUE); + + if (image->Bands != 3 || + wtiff->Q >= 90) + /* No chroma subsample. + */ for (int i = 0; i < image->Bands; i++) { cinfo->comp_info[i].h_samp_factor = 1; cinfo->comp_info[i].v_samp_factor = 1; } + else { + /* Use 4:2:0 subsampling, we must set this explicitly, since some + * jpeg libraries do not enable chroma subsample by default. + */ + cinfo->comp_info[0].h_samp_factor = 2; + cinfo->comp_info[0].v_samp_factor = 2; - // use RGB mode (no chroma subsample) for high Q RGB images - if (wtiff->Q >= 90 && - image->Bands == 3) - jpeg_set_colorspace(cinfo, JCS_RGB); - - /* Set compression quality. Must be called after setting params above. - */ - jpeg_set_quality(cinfo, wtiff->Q, TRUE); + /* Rest should have sampling factors 1,1. + */ + for (int i = 1; i < image->Bands; i++) { + cinfo->comp_info[i].h_samp_factor = 1; + cinfo->comp_info[i].v_samp_factor = 1; + } + } // Avoid writing the JFIF APP0 marker. cinfo->write_JFIF_header = FALSE;