Skip to content

Commit

Permalink
jpegsave: explicitly use 4:2:0 subsampling by default
Browse files Browse the repository at this point in the history
Unless saving with a quality setting of 90 or higher, or when chroma
subsampling is explicitly disabled (`subsample_mode=off`).

This ensures compatibility with jpegli, which uses 4:4:4 by default.

See: libvips#3922.
  • Loading branch information
kleisauke committed Apr 7, 2024
1 parent cf23591 commit 9953a19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 18 additions & 4 deletions libvips/foreign/vips2jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9953a19

Please sign in to comment.