xref: /aosp_15_r20/external/libjpeg-turbo/wizard.txt (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard WorkerAdvanced usage instructions for the Independent JPEG Group's JPEG software
2*dfc6aa5cSAndroid Build Coastguard Worker==========================================================================
3*dfc6aa5cSAndroid Build Coastguard Worker
4*dfc6aa5cSAndroid Build Coastguard WorkerThis file describes cjpeg's "switches for wizards".
5*dfc6aa5cSAndroid Build Coastguard Worker
6*dfc6aa5cSAndroid Build Coastguard WorkerThe "wizard" switches are intended for experimentation with JPEG by persons
7*dfc6aa5cSAndroid Build Coastguard Workerwho are reasonably knowledgeable about the JPEG standard.  If you don't know
8*dfc6aa5cSAndroid Build Coastguard Workerwhat you are doing, DON'T USE THESE SWITCHES.  You'll likely produce files
9*dfc6aa5cSAndroid Build Coastguard Workerwith worse image quality and/or poorer compression than you'd get from the
10*dfc6aa5cSAndroid Build Coastguard Workerdefault settings.  Furthermore, these switches must be used with caution
11*dfc6aa5cSAndroid Build Coastguard Workerwhen making files intended for general use, because not all JPEG decoders
12*dfc6aa5cSAndroid Build Coastguard Workerwill support unusual JPEG parameter settings.
13*dfc6aa5cSAndroid Build Coastguard Worker
14*dfc6aa5cSAndroid Build Coastguard Worker
15*dfc6aa5cSAndroid Build Coastguard WorkerQuantization Table Adjustment
16*dfc6aa5cSAndroid Build Coastguard Worker-----------------------------
17*dfc6aa5cSAndroid Build Coastguard Worker
18*dfc6aa5cSAndroid Build Coastguard WorkerOrdinarily, cjpeg starts with a default set of tables (the same ones given
19*dfc6aa5cSAndroid Build Coastguard Workeras examples in the JPEG standard) and scales them up or down according to
20*dfc6aa5cSAndroid Build Coastguard Workerthe -quality setting.  The details of the scaling algorithm can be found in
21*dfc6aa5cSAndroid Build Coastguard Workerjcparam.c.  At very low quality settings, some quantization table entries
22*dfc6aa5cSAndroid Build Coastguard Workercan get scaled up to values exceeding 255.  Although 2-byte quantization
23*dfc6aa5cSAndroid Build Coastguard Workervalues are supported by the IJG software, this feature is not in baseline
24*dfc6aa5cSAndroid Build Coastguard WorkerJPEG and is not supported by all implementations.  If you need to ensure
25*dfc6aa5cSAndroid Build Coastguard Workerwide compatibility of low-quality files, you can constrain the scaled
26*dfc6aa5cSAndroid Build Coastguard Workerquantization values to no more than 255 by giving the -baseline switch.
27*dfc6aa5cSAndroid Build Coastguard WorkerNote that use of -baseline will result in poorer quality for the same file
28*dfc6aa5cSAndroid Build Coastguard Workersize, since more bits than necessary are expended on higher AC coefficients.
29*dfc6aa5cSAndroid Build Coastguard Worker
30*dfc6aa5cSAndroid Build Coastguard WorkerYou can substitute a different set of quantization values by using the
31*dfc6aa5cSAndroid Build Coastguard Worker-qtables switch:
32*dfc6aa5cSAndroid Build Coastguard Worker
33*dfc6aa5cSAndroid Build Coastguard Worker        -qtables file   Use the quantization tables given in the named file.
34*dfc6aa5cSAndroid Build Coastguard Worker
35*dfc6aa5cSAndroid Build Coastguard WorkerThe specified file should be a text file containing decimal quantization
36*dfc6aa5cSAndroid Build Coastguard Workervalues.  The file should contain one to four tables, each of 64 elements.
37*dfc6aa5cSAndroid Build Coastguard WorkerThe tables are implicitly numbered 0,1,etc. in order of appearance.  Table
38*dfc6aa5cSAndroid Build Coastguard Workerentries appear in normal array order (NOT in the zigzag order in which they
39*dfc6aa5cSAndroid Build Coastguard Workerwill be stored in the JPEG file).
40*dfc6aa5cSAndroid Build Coastguard Worker
41*dfc6aa5cSAndroid Build Coastguard WorkerQuantization table files are free format, in that arbitrary whitespace can
42*dfc6aa5cSAndroid Build Coastguard Workerappear between numbers.  Also, comments can be included: a comment starts
43*dfc6aa5cSAndroid Build Coastguard Workerwith '#' and extends to the end of the line.  Here is an example file that
44*dfc6aa5cSAndroid Build Coastguard Workerduplicates the default quantization tables:
45*dfc6aa5cSAndroid Build Coastguard Worker
46*dfc6aa5cSAndroid Build Coastguard Worker        # Quantization tables given in Annex K (Clause K.1) of
47*dfc6aa5cSAndroid Build Coastguard Worker        # Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
48*dfc6aa5cSAndroid Build Coastguard Worker
49*dfc6aa5cSAndroid Build Coastguard Worker        # This is table 0 (the luminance table):
50*dfc6aa5cSAndroid Build Coastguard Worker          16  11  10  16  24  40  51  61
51*dfc6aa5cSAndroid Build Coastguard Worker          12  12  14  19  26  58  60  55
52*dfc6aa5cSAndroid Build Coastguard Worker          14  13  16  24  40  57  69  56
53*dfc6aa5cSAndroid Build Coastguard Worker          14  17  22  29  51  87  80  62
54*dfc6aa5cSAndroid Build Coastguard Worker          18  22  37  56  68 109 103  77
55*dfc6aa5cSAndroid Build Coastguard Worker          24  35  55  64  81 104 113  92
56*dfc6aa5cSAndroid Build Coastguard Worker          49  64  78  87 103 121 120 101
57*dfc6aa5cSAndroid Build Coastguard Worker          72  92  95  98 112 100 103  99
58*dfc6aa5cSAndroid Build Coastguard Worker
59*dfc6aa5cSAndroid Build Coastguard Worker        # This is table 1 (the chrominance table):
60*dfc6aa5cSAndroid Build Coastguard Worker          17  18  24  47  99  99  99  99
61*dfc6aa5cSAndroid Build Coastguard Worker          18  21  26  66  99  99  99  99
62*dfc6aa5cSAndroid Build Coastguard Worker          24  26  56  99  99  99  99  99
63*dfc6aa5cSAndroid Build Coastguard Worker          47  66  99  99  99  99  99  99
64*dfc6aa5cSAndroid Build Coastguard Worker          99  99  99  99  99  99  99  99
65*dfc6aa5cSAndroid Build Coastguard Worker          99  99  99  99  99  99  99  99
66*dfc6aa5cSAndroid Build Coastguard Worker          99  99  99  99  99  99  99  99
67*dfc6aa5cSAndroid Build Coastguard Worker          99  99  99  99  99  99  99  99
68*dfc6aa5cSAndroid Build Coastguard Worker
69*dfc6aa5cSAndroid Build Coastguard WorkerIf the -qtables switch is used without -quality, then the specified tables
70*dfc6aa5cSAndroid Build Coastguard Workerare used exactly as-is.  If both -qtables and -quality are used, then the
71*dfc6aa5cSAndroid Build Coastguard Workertables taken from the file are scaled in the same fashion that the default
72*dfc6aa5cSAndroid Build Coastguard Workertables would be scaled for that quality setting.  If -baseline appears, then
73*dfc6aa5cSAndroid Build Coastguard Workerthe quantization values are constrained to the range 1-255.
74*dfc6aa5cSAndroid Build Coastguard Worker
75*dfc6aa5cSAndroid Build Coastguard WorkerBy default, cjpeg will use quantization table 0 for luminance components and
76*dfc6aa5cSAndroid Build Coastguard Workertable 1 for chrominance components.  To override this choice, use the -qslots
77*dfc6aa5cSAndroid Build Coastguard Workerswitch:
78*dfc6aa5cSAndroid Build Coastguard Worker
79*dfc6aa5cSAndroid Build Coastguard Worker        -qslots N[,...]         Select which quantization table to use for
80*dfc6aa5cSAndroid Build Coastguard Worker                                each color component.
81*dfc6aa5cSAndroid Build Coastguard Worker
82*dfc6aa5cSAndroid Build Coastguard WorkerThe -qslots switch specifies a quantization table number for each color
83*dfc6aa5cSAndroid Build Coastguard Workercomponent, in the order in which the components appear in the JPEG SOF marker.
84*dfc6aa5cSAndroid Build Coastguard WorkerFor example, to create a separate table for each of Y,Cb,Cr, you could
85*dfc6aa5cSAndroid Build Coastguard Workerprovide a -qtables file that defines three quantization tables and say
86*dfc6aa5cSAndroid Build Coastguard Worker"-qslots 0,1,2".  If -qslots gives fewer table numbers than there are color
87*dfc6aa5cSAndroid Build Coastguard Workercomponents, then the last table number is repeated as necessary.
88*dfc6aa5cSAndroid Build Coastguard Worker
89*dfc6aa5cSAndroid Build Coastguard Worker
90*dfc6aa5cSAndroid Build Coastguard WorkerSampling Factor Adjustment
91*dfc6aa5cSAndroid Build Coastguard Worker--------------------------
92*dfc6aa5cSAndroid Build Coastguard Worker
93*dfc6aa5cSAndroid Build Coastguard WorkerBy default, cjpeg uses 2:1 horizontal and vertical downsampling when
94*dfc6aa5cSAndroid Build Coastguard Workercompressing YCbCr data, and no downsampling for all other color spaces.
95*dfc6aa5cSAndroid Build Coastguard WorkerYou can override this default with the -sample switch:
96*dfc6aa5cSAndroid Build Coastguard Worker
97*dfc6aa5cSAndroid Build Coastguard Worker        -sample HxV[,...]       Set JPEG sampling factors for each color
98*dfc6aa5cSAndroid Build Coastguard Worker                                component.
99*dfc6aa5cSAndroid Build Coastguard Worker
100*dfc6aa5cSAndroid Build Coastguard WorkerThe -sample switch specifies the JPEG sampling factors for each color
101*dfc6aa5cSAndroid Build Coastguard Workercomponent, in the order in which they appear in the JPEG SOF marker.
102*dfc6aa5cSAndroid Build Coastguard WorkerIf you specify fewer HxV pairs than there are components, the remaining
103*dfc6aa5cSAndroid Build Coastguard Workercomponents are set to 1x1 sampling.  For example, the default YCbCr setting
104*dfc6aa5cSAndroid Build Coastguard Workeris equivalent to "-sample 2x2,1x1,1x1", which can be abbreviated to
105*dfc6aa5cSAndroid Build Coastguard Worker"-sample 2x2".
106*dfc6aa5cSAndroid Build Coastguard Worker
107*dfc6aa5cSAndroid Build Coastguard WorkerThere are still some JPEG decoders in existence that support only 2x1
108*dfc6aa5cSAndroid Build Coastguard Workersampling (also called 4:2:2 sampling).  Compatibility with such decoders can
109*dfc6aa5cSAndroid Build Coastguard Workerbe achieved by specifying "-sample 2x1".  This is not recommended unless
110*dfc6aa5cSAndroid Build Coastguard Workerreally necessary, since it increases file size and encoding/decoding time
111*dfc6aa5cSAndroid Build Coastguard Workerwith very little quality gain.
112*dfc6aa5cSAndroid Build Coastguard Worker
113*dfc6aa5cSAndroid Build Coastguard Worker
114*dfc6aa5cSAndroid Build Coastguard WorkerMultiple Scan / Progression Control
115*dfc6aa5cSAndroid Build Coastguard Worker-----------------------------------
116*dfc6aa5cSAndroid Build Coastguard Worker
117*dfc6aa5cSAndroid Build Coastguard WorkerBy default, cjpeg emits a single-scan sequential JPEG file.  The
118*dfc6aa5cSAndroid Build Coastguard Worker-progressive switch generates a progressive JPEG file using a default series
119*dfc6aa5cSAndroid Build Coastguard Workerof progression parameters.  You can create multiple-scan sequential JPEG
120*dfc6aa5cSAndroid Build Coastguard Workerfiles or progressive JPEG files with custom progression parameters by using
121*dfc6aa5cSAndroid Build Coastguard Workerthe -scans switch:
122*dfc6aa5cSAndroid Build Coastguard Worker
123*dfc6aa5cSAndroid Build Coastguard Worker        -scans file     Use the scan sequence given in the named file.
124*dfc6aa5cSAndroid Build Coastguard Worker
125*dfc6aa5cSAndroid Build Coastguard WorkerThe specified file should be a text file containing a "scan script".
126*dfc6aa5cSAndroid Build Coastguard WorkerThe script specifies the contents and ordering of the scans to be emitted.
127*dfc6aa5cSAndroid Build Coastguard WorkerEach entry in the script defines one scan.  A scan definition specifies
128*dfc6aa5cSAndroid Build Coastguard Workerthe components to be included in the scan, and for progressive JPEG it also
129*dfc6aa5cSAndroid Build Coastguard Workerspecifies the progression parameters Ss,Se,Ah,Al for the scan.  Scan
130*dfc6aa5cSAndroid Build Coastguard Workerdefinitions are separated by semicolons (';').  A semicolon after the last
131*dfc6aa5cSAndroid Build Coastguard Workerscan definition is optional.
132*dfc6aa5cSAndroid Build Coastguard Worker
133*dfc6aa5cSAndroid Build Coastguard WorkerEach scan definition contains one to four component indexes, optionally
134*dfc6aa5cSAndroid Build Coastguard Workerfollowed by a colon (':') and the four progressive-JPEG parameters.  The
135*dfc6aa5cSAndroid Build Coastguard Workercomponent indexes denote which color component(s) are to be transmitted in
136*dfc6aa5cSAndroid Build Coastguard Workerthe scan.  Components are numbered in the order in which they appear in the
137*dfc6aa5cSAndroid Build Coastguard WorkerJPEG SOF marker, with the first component being numbered 0.  (Note that these
138*dfc6aa5cSAndroid Build Coastguard Workerindexes are not the "component ID" codes assigned to the components, just
139*dfc6aa5cSAndroid Build Coastguard Workerpositional indexes.)
140*dfc6aa5cSAndroid Build Coastguard Worker
141*dfc6aa5cSAndroid Build Coastguard WorkerThe progression parameters for each scan are:
142*dfc6aa5cSAndroid Build Coastguard Worker        Ss      Zigzag index of first coefficient included in scan
143*dfc6aa5cSAndroid Build Coastguard Worker        Se      Zigzag index of last coefficient included in scan
144*dfc6aa5cSAndroid Build Coastguard Worker        Ah      Zero for first scan of a coefficient, else Al of prior scan
145*dfc6aa5cSAndroid Build Coastguard Worker        Al      Successive approximation low bit position for scan
146*dfc6aa5cSAndroid Build Coastguard WorkerIf the progression parameters are omitted, the values 0,63,0,0 are used,
147*dfc6aa5cSAndroid Build Coastguard Workerproducing a sequential JPEG file.  cjpeg automatically determines whether
148*dfc6aa5cSAndroid Build Coastguard Workerthe script represents a progressive or sequential file, by observing whether
149*dfc6aa5cSAndroid Build Coastguard WorkerSs and Se values other than 0 and 63 appear.  (The -progressive switch is
150*dfc6aa5cSAndroid Build Coastguard Workernot needed to specify this; in fact, it is ignored when -scans appears.)
151*dfc6aa5cSAndroid Build Coastguard WorkerThe scan script must meet the JPEG restrictions on progression sequences.
152*dfc6aa5cSAndroid Build Coastguard Worker(cjpeg checks that the spec's requirements are obeyed.)  More specifically:
153*dfc6aa5cSAndroid Build Coastguard Worker
154*dfc6aa5cSAndroid Build Coastguard Worker    * An AC scan cannot include coefficients from more than one component.
155*dfc6aa5cSAndroid Build Coastguard Worker
156*dfc6aa5cSAndroid Build Coastguard Worker    * An AC scan for a particular component must be preceded by a DC scan
157*dfc6aa5cSAndroid Build Coastguard Worker      that includes the same component.
158*dfc6aa5cSAndroid Build Coastguard Worker
159*dfc6aa5cSAndroid Build Coastguard Worker    * Only the first AC scan that includes a particular coefficient for a
160*dfc6aa5cSAndroid Build Coastguard Worker      particular component can include more than one bit from that coefficient.
161*dfc6aa5cSAndroid Build Coastguard Worker
162*dfc6aa5cSAndroid Build Coastguard WorkerScan script files are free format, in that arbitrary whitespace can appear
163*dfc6aa5cSAndroid Build Coastguard Workerbetween numbers and around punctuation.  Also, comments can be included: a
164*dfc6aa5cSAndroid Build Coastguard Workercomment starts with '#' and extends to the end of the line.  For additional
165*dfc6aa5cSAndroid Build Coastguard Workerlegibility, commas or dashes can be placed between values.  (Actually, any
166*dfc6aa5cSAndroid Build Coastguard Workersingle punctuation character other than ':' or ';' can be inserted.)  For
167*dfc6aa5cSAndroid Build Coastguard Workerexample, the following two scan definitions are equivalent:
168*dfc6aa5cSAndroid Build Coastguard Worker        0 1 2: 0 63 0 0;
169*dfc6aa5cSAndroid Build Coastguard Worker        0,1,2 : 0-63, 0,0 ;
170*dfc6aa5cSAndroid Build Coastguard Worker
171*dfc6aa5cSAndroid Build Coastguard WorkerHere is an example of a scan script that generates a partially interleaved
172*dfc6aa5cSAndroid Build Coastguard Workersequential JPEG file:
173*dfc6aa5cSAndroid Build Coastguard Worker
174*dfc6aa5cSAndroid Build Coastguard Worker        0;                      # Y only in first scan
175*dfc6aa5cSAndroid Build Coastguard Worker        1 2;                    # Cb and Cr in second scan
176*dfc6aa5cSAndroid Build Coastguard Worker
177*dfc6aa5cSAndroid Build Coastguard WorkerHere is an example of a progressive scan script using only spectral selection
178*dfc6aa5cSAndroid Build Coastguard Worker(no successive approximation):
179*dfc6aa5cSAndroid Build Coastguard Worker
180*dfc6aa5cSAndroid Build Coastguard Worker        # Interleaved DC scan for Y,Cb,Cr:
181*dfc6aa5cSAndroid Build Coastguard Worker        0,1,2: 0-0,   0, 0 ;
182*dfc6aa5cSAndroid Build Coastguard Worker        # AC scans:
183*dfc6aa5cSAndroid Build Coastguard Worker        0:     1-2,   0, 0 ;    # First two Y AC coefficients
184*dfc6aa5cSAndroid Build Coastguard Worker        0:     3-5,   0, 0 ;    # Three more
185*dfc6aa5cSAndroid Build Coastguard Worker        1:     1-63,  0, 0 ;    # All AC coefficients for Cb
186*dfc6aa5cSAndroid Build Coastguard Worker        2:     1-63,  0, 0 ;    # All AC coefficients for Cr
187*dfc6aa5cSAndroid Build Coastguard Worker        0:     6-9,   0, 0 ;    # More Y coefficients
188*dfc6aa5cSAndroid Build Coastguard Worker        0:     10-63, 0, 0 ;    # Remaining Y coefficients
189*dfc6aa5cSAndroid Build Coastguard Worker
190*dfc6aa5cSAndroid Build Coastguard WorkerHere is an example of a successive-approximation script.  This is equivalent
191*dfc6aa5cSAndroid Build Coastguard Workerto the default script used by "cjpeg -progressive" for YCbCr images:
192*dfc6aa5cSAndroid Build Coastguard Worker
193*dfc6aa5cSAndroid Build Coastguard Worker        # Initial DC scan for Y,Cb,Cr (lowest bit not sent)
194*dfc6aa5cSAndroid Build Coastguard Worker        0,1,2: 0-0,   0, 1 ;
195*dfc6aa5cSAndroid Build Coastguard Worker        # First AC scan: send first 5 Y AC coefficients, minus 2 lowest bits:
196*dfc6aa5cSAndroid Build Coastguard Worker        0:     1-5,   0, 2 ;
197*dfc6aa5cSAndroid Build Coastguard Worker        # Send all Cr,Cb AC coefficients, minus lowest bit:
198*dfc6aa5cSAndroid Build Coastguard Worker        # (chroma data is usually too small to be worth subdividing further;
199*dfc6aa5cSAndroid Build Coastguard Worker        #  but note we send Cr first since eye is least sensitive to Cb)
200*dfc6aa5cSAndroid Build Coastguard Worker        2:     1-63,  0, 1 ;
201*dfc6aa5cSAndroid Build Coastguard Worker        1:     1-63,  0, 1 ;
202*dfc6aa5cSAndroid Build Coastguard Worker        # Send remaining Y AC coefficients, minus 2 lowest bits:
203*dfc6aa5cSAndroid Build Coastguard Worker        0:     6-63,  0, 2 ;
204*dfc6aa5cSAndroid Build Coastguard Worker        # Send next-to-lowest bit of all Y AC coefficients:
205*dfc6aa5cSAndroid Build Coastguard Worker        0:     1-63,  2, 1 ;
206*dfc6aa5cSAndroid Build Coastguard Worker        # At this point we've sent all but the lowest bit of all coefficients.
207*dfc6aa5cSAndroid Build Coastguard Worker        # Send lowest bit of DC coefficients
208*dfc6aa5cSAndroid Build Coastguard Worker        0,1,2: 0-0,   1, 0 ;
209*dfc6aa5cSAndroid Build Coastguard Worker        # Send lowest bit of AC coefficients
210*dfc6aa5cSAndroid Build Coastguard Worker        2:     1-63,  1, 0 ;
211*dfc6aa5cSAndroid Build Coastguard Worker        1:     1-63,  1, 0 ;
212*dfc6aa5cSAndroid Build Coastguard Worker        # Y AC lowest bit scan is last; it's usually the largest scan
213*dfc6aa5cSAndroid Build Coastguard Worker        0:     1-63,  1, 0 ;
214*dfc6aa5cSAndroid Build Coastguard Worker
215*dfc6aa5cSAndroid Build Coastguard WorkerIt may be worth pointing out that this script is tuned for quality settings
216*dfc6aa5cSAndroid Build Coastguard Workerof around 50 to 75.  For lower quality settings, you'd probably want to use
217*dfc6aa5cSAndroid Build Coastguard Workera script with fewer stages of successive approximation (otherwise the
218*dfc6aa5cSAndroid Build Coastguard Workerinitial scans will be really bad).  For higher quality settings, you might
219*dfc6aa5cSAndroid Build Coastguard Workerwant to use more stages of successive approximation (so that the initial
220*dfc6aa5cSAndroid Build Coastguard Workerscans are not too large).
221