4:2:0 JPEG chroma subsampling

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

4:2:0 JPEG chroma subsampling

Post by NicolasRobidoux »

Does anyone know how the chroma subsampling for JPEG 4:2:0 compression is done within IM?

Are the chroma channels averaged one group of four (2x2) pixels at a time? Or is some variant of nearest neighbour used? Is box filtering part of the standard?

I am wondering if using another low-pass filter than box filtering with the CbCr channels (Cubic or Quadratic (cubic or quadratic B-spline smoothing or its distort variant) or gaussian blur) and then using 4:4:4 (no chroma subsampling) instead of 4:2:0 but with a lower quality level would give better results (for a given file size).

The logic is that the Discrete Cosine Transform "converges" the fastest when the input is smoothest (including, unfortunately, the "periodic extension" of the input data). Given that box filtering is not that good of a low-pass filter, the gain in convergence rate obtained by a better chroma "smoother" may beat the fact that box filtering can be used to halve the sampling rate, and further more lead to more localized artifacts (8x8 blocks in chroma instead of 16x16).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 4:2:0 JPEG chroma subsampling

Post by anthony »

Im does not handle that at all. It is controlled by a delegate JPEG library.

The JPEG coder module will pass some controls to the library..
http://www.imagemagick.org/Usage/formats/#jpg_write

"2x2, 1x1, 1x1" is IM's standard subsampling method and corresponds to 4:2:0 Chroma sub-sampling.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: 4:2:0 JPEG chroma subsampling

Post by NicolasRobidoux »

Note sure I am controlling the chroma subsampling correctly, but here
is my first attempt. At this point, it appears that prefiltering the
chroma is not worth the bother.

(Code removed because outdated.)
Last edited by NicolasRobidoux on 2011-04-14T11:33:14-07:00, edited 4 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 4:2:0 JPEG chroma subsampling

Post by anthony »

The default is the standard
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: 4:2:0 JPEG chroma subsampling

Post by glennrp »

When "quality" is 90 or higher, the channels are not subsampled when writing a JPEG.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: 4:2:0 JPEG chroma subsampling

Post by NicolasRobidoux »

glennrp wrote:When "quality" is 90 or higher, the channels are not subsampled when writing a JPEG.
Thank you Glenn (and Anthony).

This explains why the file size gain is larger when the quality is >= 90.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: 4:2:0 JPEG chroma subsampling

Post by NicolasRobidoux »

NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: 4:2:0 JPEG chroma subsampling

Post by NicolasRobidoux »

It appears to me that if you don't explicitly specify the -sampling-factor, you do NOT get chroma subsampling at any quality level.

It is my opinioni that this is NOT a good thing.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 4:2:0 JPEG chroma subsampling

Post by anthony »

glennrp wrote:When "quality" is 90 or higher, the channels are not subsampled when writing a JPEG.
I noted that in IM examples on that jpeg define.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 4:2:0 JPEG chroma subsampling

Post by anthony »

NicolasRobidoux wrote:It appears to me that if you don't explicitly specify the -sampling-factor, you do NOT get chroma subsampling at any quality level.

It is my opinioni that this is NOT a good thing.

You could have a look at the coder source.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply