Friday 22 April 2011

What is best default value for JPEG image quality (as of popular ⟨libjpeg⟩ quality scale)?

So, it's (a) 75 for being that "the resulting reconstructed image is usually nearly indistinguishable from the source image"; (b) 50 is "based on psychovisual thresholding" and "have been used with good results".
So, why is that:
Let's cite.

From "ISO/IEC 10918-1 1993(E) - JPEG Standard recommendation"

(Same text as in "CCITT Recommendation T.81")

K.1 Quantization tables for luminance and chrominance components

Two examples of quantization tables are given in Tables K.1 and K.2. These are based on psychovisual thresholding and are derived empirically using luminance and chrominance and 2:1 horizontal subsampling. These tables are provided as examples only and are not necessarily suitable for any particular application. These quantization values have been used with good results on 8-bit per sample luminance and chrominance images of the format illustrated in Figure 13. Note that these quantization values are appropriate for the DCT normalization defined in A.3.3. If these quantization values are divided by 2, the resulting reconstructed image is usually nearly indistinguishable from the source image.
So, for (b) usual compression we should use values in these "quantization tables" (give up what's that; only some tables) and for (a) "nearly undistinguishable from the source" images, these values divided by 2. Note conditions when that's applicable highlited by bold.

From "libjpeg.txt" in ⟨libjpeg⟩ package

Documentation for function from C language programming interface:
jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor, boolean force_baseline)
Same as jpeg_set_quality() except that the generated tables are the sample tables given in the JPEC spec section K.1, multiplied by the specified scale factor (which is expressed as a percentage; thus scale_factor = 100 reproduces the spec's tables). Note that larger scale factors give lower quality. This entry point is useful for conforming to the Adobe PostScript DCT conventions, but we do not recommend linear scaling as a user-visible quality scale otherwise. force_baseline again constrains the computed table entries to 1..255.
So, for (a) we need 50 on "linear quality" scale and 100 for (b). There is note about "Adobe PostScript DCT conventions" that uses such linear quality scale.

From ⟨libjpeg⟩ source code ("jcparam.c" file)

 1 GLOBAL(void)
 2 jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
 3 /* Set or change the 'quality' (quantization) setting, using default tables.
 4 * This is the standard quality-adjusting entry point for typical user
 5 * interfaces; only those who want detailed control over quantization tables
 6 * would use the preceding three routines directly.
 7 */
 8 {
 9     /* Convert user 0-100 rating to percentage scaling */
10     quality = jpeg_quality_scaling(quality);
11
12     /* Set up standard quality tables */
13     jpeg_set_linear_quality(cinfo, quality, force_baseline);
14 }
15
16 GLOBAL(int)
17 jpeg_quality_scaling (int quality)
18 /* Convert a user-specified quality rating to a percentage scaling factor
19 * for an underlying quantization table, using our recommended scaling curve.
20 * The input 'quality' factor should be 0 (terrible) to 100 (very good).
21 */
22 {
23     /* Safety limit on quality factor.  Convert 0 to 1 to avoid zero divide. */
24     if (quality <= 0) quality = 1;
25     if (quality > 100) quality = 100;
26
27     /* The basic table is used as-is (scaling 100) for a quality of 50.
28     * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
29     * note that at Q=100 the scaling is 0, which will cause jpeg_add_quant_table
30     * to make all the table entries 1 (hence, minimum quantization loss).
31     * Qualities 1..50 are converted to scaling percentage 5000/Q.
32     */
33     if (quality < 50)
34         quality = 5000 / quality;
35     else
36         quality = 200 - quality*2;
37
38     return quality;
39 }
40
Here "user-specified quality rating" means default ⟨libjpeg⟩ quality scale; that which we are most interested ;-) in. So, as we see from code, to have (a) 50 on linear scale, we should supply 75 on ⟨libjpeg⟩ quality scale in input (50 = 200-75*2) and to have (b) 100 on linear scale, supply 50 (100 = 200-50*2).

And yes, default value for ⟨libjpeg⟩ is 75, (a). (As says same source code "jcparam.c" file).
That's it.
I'm nerd )

No comments:

Post a Comment