Page 1 of 1

Better JPEG luma quantization table

Posted: 2012-02-29T15:39:12-07:00
by NicolasRobidoux
The following is unquestionably (to me) a better luma quantization table than the stock one:

Code: Select all

######################################################################
# Nicolas Robidoux's better luma quantization table v2012.02.29
#
# To be used with, e.g.,
# 
#   cjpeg -qtables ThisFile.txt -qslots 0,1 \
#         -quality YourChosenQuality \
#         -dct float -baseline -optimize \
#         -outfile OutputImage.jpg InputImage.ppm
#
# The author (Nicolas Robidoux) consults for a living and can dial
# JPEG artifact mixes in a controlled manner, customize jpegrescan,
# etc. Email: FiRsTnAmE dOt LaStNaMe HaT gMaIl DoT cOm.
#
# Suggested improvements and comments are welcome.
# 
# The new luma table is a variant of the standard one, first described
# in an annex of ITU-T Recommendation T.81 (the original "JPEG
# standard"). For convenience, the standard chroma table (from the
# same annex) is copied below.
######################################################################

# The new luma table:

16 11 12 15 21  32  50  66
11 12 13 18 24  46  62  73
12 13 16 23 38  56  73  75
15 18 23 29 53  75  83  83
21 24 38 53 68  95  103 103
32 46 56 75 95  104 117 117
50 62 73 83 103 117 120 120
66 73 75 83 103 117 120 120

# The standard chroma table:

17 18 24 47 99 99 99 99
18 21 26 66 99 99 99 99
24 26 56 99 99 99 99 99
47 66 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
This new luma quantization table is obtained from the standard luma quantization table as follows:
  • First, symmetrize, that is, replace the entry at position (i,j) by the average of the standard entry at the same position and the standard entry at position (j,i), rounding down.
  • Then, make the result monotone, that is, fix things so that a higher frequency mode never has a smaller entry than a lower frequency mode. Specifically, this means that the list of entries should be nondecreasing both column wise and row wise.
With this quantization table, you can expect files about 1% smaller (this is highly dependent on the image content as well as its dimensions) with an equivalent quality.

In following posts, I'll explain the heuristics behind this modification, as well as give code that allows use this customized quantization table with JPEG Group's cjpeg as well as ImageMagick's convert (I am told ImageMagick will be able to use custom qtables soon).

As of now, I issue a
Challenge: Show me a noncontrived image for which the standard luma qtable gives a smaller file with as good or better image quality than the above new one.
The noncontrived clause has to do with that someone who understands JPEG compression can construct an image specifically to make the standard table win.

Note that recompressing a very low quality JPEG image and similar endeavors does not really count as a proper response to the challenge. So is recompressing a JPEG produced with the standard table at essentially the same quality level, using unreasonable quality levels, and other splitting hair tasks.

Being smaller by a trivial amount does not really count either.

This challenge has to do with a reasonable "real world" case.

(To those who followed my attempts at improving qtables: The above new luma one is most likely the best I have come up with for all purpose use. I don't think I'll be able to put together a qtable which is significantly better for all purpose use. Exploiting progressive encoding is another story.)

Re: Better JPEG luma quantization table

Posted: 2012-02-29T17:37:28-07:00
by NicolasRobidoux
It turns out that if I was to respond to my own challenge (which I won't), I would know how, but my counterexample would not prove anything.

So, let me state the spirit of the challenge: Prove to me that my new quantization table is not better than the standard one.

If you succeed, you'll get my respect and thanks. Not much of a prize, but they will be heartfelt.

Re: Better JPEG luma quantization table

Posted: 2012-03-04T17:15:11-07:00
by NicolasRobidoux
Possibly a minute improvement:

Code: Select all

######################################################################
# Nicolas Robidoux's better luma quantization table v2012.03.04
#
# To be used with, e.g.,
# 
#   cjpeg -qtables ThisFile.txt -qslots 0,1 \
#         -quality YourChosenQuality \
#         -dct float -baseline -optimize \
#         -outfile OutputImage.jpg InputImage.ppm
#
# The author (Nicolas Robidoux) consults for a living and can dial
# JPEG artifact mixes in a controlled manner, customize jpegrescan,
# etc. Email: FiRsTnAmE dOt LaStNaMe HaT gMaIl DoT cOm.
#
# Suggested improvements and comments are welcome.
# 
# The new luma table is a variant of the standard one, first described
# in an annex of ITU-T Recommendation T.81 (the original "JPEG
# standard"). For convenience, the standard chroma table (from the
# same annex) is copied below.
######################################################################

# The new luma table:

15 11 12 15 21  32  50  66
11 11 13 18 24  46  62  73
12 13 15 23 38  56  73  75
15 18 23 28 53  75  83  83
21 24 38 53 67  95  103 103
32 46 56 75 95  103 117 117
50 62 73 83 103 117 119 119
66 73 75 83 103 117 119 119

# The standard chroma table:

17 18 24 47 99 99 99 99
18 21 26 66 99 99 99 99
24 26 56 99 99 99 99 99
47 66 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99

Re: Better JPEG luma quantization table

Posted: 2012-03-04T17:28:39-07:00
by NicolasRobidoux
When I have a bit of time, I'll have another go at chroma.