Page 1 of 1

4:2:0 JPEG chroma subsampling

Posted: 2011-04-13T13:18:33-07:00
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).

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-13T19:25:41-07:00
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.

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-13T19:59:01-07:00
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.)

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-13T21:09:46-07:00
by anthony
The default is the standard

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T08:27:35-07:00
by glennrp
When "quality" is 90 or higher, the channels are not subsampled when writing a JPEG.

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T11:30:49-07:00
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.

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T11:32:15-07:00
by NicolasRobidoux

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T12:15:09-07:00
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.

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T19:04:25-07:00
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.

Re: 4:2:0 JPEG chroma subsampling

Posted: 2011-04-14T19:05:28-07:00
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.