Page 1 of 1

Estimating JPEG Quality

Posted: 2019-01-22T02:26:27-07:00
by whugemann
I would like to know how IM estimates the JPEG Quality.

I took a look at the code in jpeg.c, and it obviously calculates it from the DQT (around line 922). I know about the way how quality is translated into DQT as suggested by the IJG. But to my knowledge the IJG made no suugestion for the other way round. The code in jpeg.c is not commented and inunderstandable to me. Is there an explication / scientific article somewhere?

Re: Estimating JPEG Quality

Posted: 2019-01-25T05:19:02-07:00
by whugemann
I took a closer look at the code and found out myself: Basically, IM calculates the sum of all DQT entries and compares it directly to the sum of the IJG table entries, which is pre-calculated and stored in the vector sums[101]. The same is done for the sums of the two table entries no. 2 and 53, which are compared to the sum of the corresponding entries of the IJG DQT, stored in hash[101].

If this "hash" is identical to one of those stored in hash[101], the JPEG is assumed to use the IJG tables, and the guess on the quality is assumed to be exact. Otherwise it is characterised as approximate.

This approach is also found in, where the average of the entries serves the same purpose.

I cloned the code of JPEGsnoop ( from GitHub and saw that it performs the job a little more sophisticated: It calculates the ratio of each table entry to the corresponding entry in standard tables from annex K of the JPEG standard and derives the scale factor as well as a variance from that. The variance tells, how far the DQT of the JPEG is off-standard and how reliable the estimation of the quality thus is. (The code is around line 4600 of JfifDecode.cpp.)

I see that the code in IM's jpeg.c is very old and probably will not be touched anymore, but there seems to be room for future enhancement.