diff --git a/ChangeLog b/ChangeLog index acaad39cd..7c5e73ddf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 26/3/24 8.15.3 - fix dzsave of >8-bit images to JPEG +- jpegsave: fix chrominance subsampling mode with jpegli [kleisauke] 12/3/24 8.15.2 diff --git a/libvips/foreign/vips2jpeg.c b/libvips/foreign/vips2jpeg.c index 3de84f888..db6ff29ed 100644 --- a/libvips/foreign/vips2jpeg.c +++ b/libvips/foreign/vips2jpeg.c @@ -667,12 +667,26 @@ set_cinfo(struct jpeg_compress_struct *cinfo, if (progressive) jpeg_simple_progression(cinfo); - if (subsample_mode == VIPS_FOREIGN_SUBSAMPLE_OFF || + if (in->Bands != 3 || + subsample_mode == VIPS_FOREIGN_SUBSAMPLE_OFF || (subsample_mode == VIPS_FOREIGN_SUBSAMPLE_AUTO && - qfac >= 90)) { - int i; + qfac >= 90)) + /* No chroma subsample. + */ + for (int i = 0; i < in->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; - for (i = 0; i < in->Bands; i++) { + /* Rest should have sampling factors 1,1. + */ + for (int i = 1; i < in->Bands; i++) { cinfo->comp_info[i].h_samp_factor = 1; cinfo->comp_info[i].v_samp_factor = 1; }