Skip to content

Commit f5025a4

Browse files
authored
Polish JPEG chroma subsampling handling (#4167)
Non-functional change.
1 parent 2d40c7a commit f5025a4

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

libvips/foreign/vips2jpeg.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -667,30 +667,21 @@ set_cinfo(struct jpeg_compress_struct *cinfo,
667667
if (progressive)
668668
jpeg_simple_progression(cinfo);
669669

670-
if (in->Bands != 3 ||
671-
subsample_mode == VIPS_FOREIGN_SUBSAMPLE_OFF ||
672-
(subsample_mode == VIPS_FOREIGN_SUBSAMPLE_AUTO &&
673-
qfac >= 90))
674-
/* No chroma subsample.
675-
*/
676-
for (int i = 0; i < in->Bands; i++) {
677-
cinfo->comp_info[i].h_samp_factor = 1;
678-
cinfo->comp_info[i].v_samp_factor = 1;
679-
}
680-
else {
681-
/* Use 4:2:0 subsampling, we must set this explicitly, since some
682-
* jpeg libraries do not enable chroma subsample by default.
683-
*/
684-
cinfo->comp_info[0].h_samp_factor = 2;
685-
cinfo->comp_info[0].v_samp_factor = 2;
670+
/* We must set chroma subsampling explicitly since some libjpegs do not
671+
* enable this by default.
672+
*/
673+
if (in->Bands == 3 &&
674+
(subsample_mode == VIPS_FOREIGN_SUBSAMPLE_ON ||
675+
(subsample_mode == VIPS_FOREIGN_SUBSAMPLE_AUTO &&
676+
qfac < 90)))
677+
cinfo->comp_info[0].h_samp_factor = cinfo->comp_info[0].v_samp_factor = 2;
678+
else
679+
cinfo->comp_info[0].h_samp_factor = cinfo->comp_info[0].v_samp_factor = 1;
686680

687-
/* Rest should have sampling factors 1,1.
688-
*/
689-
for (int i = 1; i < in->Bands; i++) {
690-
cinfo->comp_info[i].h_samp_factor = 1;
691-
cinfo->comp_info[i].v_samp_factor = 1;
692-
}
693-
}
681+
/* Rest should have sampling factors 1,1.
682+
*/
683+
for (int i = 1; i < in->Bands; i++)
684+
cinfo->comp_info[i].h_samp_factor = cinfo->comp_info[i].v_samp_factor = 1;
694685

695686
/* Only write the JFIF headers if we have no EXIF.
696687
* Some readers get confused if you set both.

libvips/foreign/vips2tiff.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,15 +691,16 @@ wtiff_compress_jpeg_header(Wtiff *wtiff,
691691
/* We must set chroma subsampling explicitly since some libjpegs do not
692692
* enable this by default.
693693
*/
694-
for (int i = 0; i < image->Bands; i++) {
695-
cinfo->comp_info[i].h_samp_factor = 1;
696-
cinfo->comp_info[i].v_samp_factor = 1;
697-
}
698694
if (image->Bands == 3 &&
699-
wtiff->Q < 90) {
700-
cinfo->comp_info[0].h_samp_factor = 2;
701-
cinfo->comp_info[0].v_samp_factor = 2;
702-
}
695+
wtiff->Q < 90)
696+
cinfo->comp_info[0].h_samp_factor = cinfo->comp_info[0].v_samp_factor = 2;
697+
else
698+
cinfo->comp_info[0].h_samp_factor = cinfo->comp_info[0].v_samp_factor = 1;
699+
700+
/* Rest should have sampling factors 1,1.
701+
*/
702+
for (int i = 1; i < image->Bands; i++)
703+
cinfo->comp_info[i].h_samp_factor = cinfo->comp_info[i].v_samp_factor = 1;
703704

704705
/* For low Q, we write YCbCr, for high Q, RGB. The jpeg coeffs don't
705706
* encode the photometric interpretation, the tiff header does that,

0 commit comments

Comments
 (0)