xref: /plan9/sys/src/cmd/gs/jpeg/wizard.doc (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1*7dd7cddfSDavid du ColombierAdvanced usage instructions for the Independent JPEG Group's JPEG software
2*7dd7cddfSDavid du Colombier==========================================================================
3*7dd7cddfSDavid du Colombier
4*7dd7cddfSDavid du ColombierThis file describes cjpeg's "switches for wizards".
5*7dd7cddfSDavid du Colombier
6*7dd7cddfSDavid du ColombierThe "wizard" switches are intended for experimentation with JPEG by persons
7*7dd7cddfSDavid du Colombierwho are reasonably knowledgeable about the JPEG standard.  If you don't know
8*7dd7cddfSDavid du Colombierwhat you are doing, DON'T USE THESE SWITCHES.  You'll likely produce files
9*7dd7cddfSDavid du Colombierwith worse image quality and/or poorer compression than you'd get from the
10*7dd7cddfSDavid du Colombierdefault settings.  Furthermore, these switches must be used with caution
11*7dd7cddfSDavid du Colombierwhen making files intended for general use, because not all JPEG decoders
12*7dd7cddfSDavid du Colombierwill support unusual JPEG parameter settings.
13*7dd7cddfSDavid du Colombier
14*7dd7cddfSDavid du Colombier
15*7dd7cddfSDavid du ColombierQuantization Table Adjustment
16*7dd7cddfSDavid du Colombier-----------------------------
17*7dd7cddfSDavid du Colombier
18*7dd7cddfSDavid du ColombierOrdinarily, cjpeg starts with a default set of tables (the same ones given
19*7dd7cddfSDavid du Colombieras examples in the JPEG standard) and scales them up or down according to
20*7dd7cddfSDavid du Colombierthe -quality setting.  The details of the scaling algorithm can be found in
21*7dd7cddfSDavid du Colombierjcparam.c.  At very low quality settings, some quantization table entries
22*7dd7cddfSDavid du Colombiercan get scaled up to values exceeding 255.  Although 2-byte quantization
23*7dd7cddfSDavid du Colombiervalues are supported by the IJG software, this feature is not in baseline
24*7dd7cddfSDavid du ColombierJPEG and is not supported by all implementations.  If you need to ensure
25*7dd7cddfSDavid du Colombierwide compatibility of low-quality files, you can constrain the scaled
26*7dd7cddfSDavid du Colombierquantization values to no more than 255 by giving the -baseline switch.
27*7dd7cddfSDavid du ColombierNote that use of -baseline will result in poorer quality for the same file
28*7dd7cddfSDavid du Colombiersize, since more bits than necessary are expended on higher AC coefficients.
29*7dd7cddfSDavid du Colombier
30*7dd7cddfSDavid du ColombierYou can substitute a different set of quantization values by using the
31*7dd7cddfSDavid du Colombier-qtables switch:
32*7dd7cddfSDavid du Colombier
33*7dd7cddfSDavid du Colombier	-qtables file	Use the quantization tables given in the named file.
34*7dd7cddfSDavid du Colombier
35*7dd7cddfSDavid du ColombierThe specified file should be a text file containing decimal quantization
36*7dd7cddfSDavid du Colombiervalues.  The file should contain one to four tables, each of 64 elements.
37*7dd7cddfSDavid du ColombierThe tables are implicitly numbered 0,1,etc. in order of appearance.  Table
38*7dd7cddfSDavid du Colombierentries appear in normal array order (NOT in the zigzag order in which they
39*7dd7cddfSDavid du Colombierwill be stored in the JPEG file).
40*7dd7cddfSDavid du Colombier
41*7dd7cddfSDavid du ColombierQuantization table files are free format, in that arbitrary whitespace can
42*7dd7cddfSDavid du Colombierappear between numbers.  Also, comments can be included: a comment starts
43*7dd7cddfSDavid du Colombierwith '#' and extends to the end of the line.  Here is an example file that
44*7dd7cddfSDavid du Colombierduplicates the default quantization tables:
45*7dd7cddfSDavid du Colombier
46*7dd7cddfSDavid du Colombier	# Quantization tables given in JPEG spec, section K.1
47*7dd7cddfSDavid du Colombier
48*7dd7cddfSDavid du Colombier	# This is table 0 (the luminance table):
49*7dd7cddfSDavid du Colombier	  16  11  10  16  24  40  51  61
50*7dd7cddfSDavid du Colombier	  12  12  14  19  26  58  60  55
51*7dd7cddfSDavid du Colombier	  14  13  16  24  40  57  69  56
52*7dd7cddfSDavid du Colombier	  14  17  22  29  51  87  80  62
53*7dd7cddfSDavid du Colombier	  18  22  37  56  68 109 103  77
54*7dd7cddfSDavid du Colombier	  24  35  55  64  81 104 113  92
55*7dd7cddfSDavid du Colombier	  49  64  78  87 103 121 120 101
56*7dd7cddfSDavid du Colombier	  72  92  95  98 112 100 103  99
57*7dd7cddfSDavid du Colombier
58*7dd7cddfSDavid du Colombier	# This is table 1 (the chrominance table):
59*7dd7cddfSDavid du Colombier	  17  18  24  47  99  99  99  99
60*7dd7cddfSDavid du Colombier	  18  21  26  66  99  99  99  99
61*7dd7cddfSDavid du Colombier	  24  26  56  99  99  99  99  99
62*7dd7cddfSDavid du Colombier	  47  66  99  99  99  99  99  99
63*7dd7cddfSDavid du Colombier	  99  99  99  99  99  99  99  99
64*7dd7cddfSDavid du Colombier	  99  99  99  99  99  99  99  99
65*7dd7cddfSDavid du Colombier	  99  99  99  99  99  99  99  99
66*7dd7cddfSDavid du Colombier	  99  99  99  99  99  99  99  99
67*7dd7cddfSDavid du Colombier
68*7dd7cddfSDavid du ColombierIf the -qtables switch is used without -quality, then the specified tables
69*7dd7cddfSDavid du Colombierare used exactly as-is.  If both -qtables and -quality are used, then the
70*7dd7cddfSDavid du Colombiertables taken from the file are scaled in the same fashion that the default
71*7dd7cddfSDavid du Colombiertables would be scaled for that quality setting.  If -baseline appears, then
72*7dd7cddfSDavid du Colombierthe quantization values are constrained to the range 1-255.
73*7dd7cddfSDavid du Colombier
74*7dd7cddfSDavid du ColombierBy default, cjpeg will use quantization table 0 for luminance components and
75*7dd7cddfSDavid du Colombiertable 1 for chrominance components.  To override this choice, use the -qslots
76*7dd7cddfSDavid du Colombierswitch:
77*7dd7cddfSDavid du Colombier
78*7dd7cddfSDavid du Colombier	-qslots N[,...]		Select which quantization table to use for
79*7dd7cddfSDavid du Colombier				each color component.
80*7dd7cddfSDavid du Colombier
81*7dd7cddfSDavid du ColombierThe -qslots switch specifies a quantization table number for each color
82*7dd7cddfSDavid du Colombiercomponent, in the order in which the components appear in the JPEG SOF marker.
83*7dd7cddfSDavid du ColombierFor example, to create a separate table for each of Y,Cb,Cr, you could
84*7dd7cddfSDavid du Colombierprovide a -qtables file that defines three quantization tables and say
85*7dd7cddfSDavid du Colombier"-qslots 0,1,2".  If -qslots gives fewer table numbers than there are color
86*7dd7cddfSDavid du Colombiercomponents, then the last table number is repeated as necessary.
87*7dd7cddfSDavid du Colombier
88*7dd7cddfSDavid du Colombier
89*7dd7cddfSDavid du ColombierSampling Factor Adjustment
90*7dd7cddfSDavid du Colombier--------------------------
91*7dd7cddfSDavid du Colombier
92*7dd7cddfSDavid du ColombierBy default, cjpeg uses 2:1 horizontal and vertical downsampling when
93*7dd7cddfSDavid du Colombiercompressing YCbCr data, and no downsampling for all other color spaces.
94*7dd7cddfSDavid du ColombierYou can override this default with the -sample switch:
95*7dd7cddfSDavid du Colombier
96*7dd7cddfSDavid du Colombier	-sample HxV[,...]	Set JPEG sampling factors for each color
97*7dd7cddfSDavid du Colombier				component.
98*7dd7cddfSDavid du Colombier
99*7dd7cddfSDavid du ColombierThe -sample switch specifies the JPEG sampling factors for each color
100*7dd7cddfSDavid du Colombiercomponent, in the order in which they appear in the JPEG SOF marker.
101*7dd7cddfSDavid du ColombierIf you specify fewer HxV pairs than there are components, the remaining
102*7dd7cddfSDavid du Colombiercomponents are set to 1x1 sampling.  For example, the default YCbCr setting
103*7dd7cddfSDavid du Colombieris equivalent to "-sample 2x2,1x1,1x1", which can be abbreviated to
104*7dd7cddfSDavid du Colombier"-sample 2x2".
105*7dd7cddfSDavid du Colombier
106*7dd7cddfSDavid du ColombierThere are still some JPEG decoders in existence that support only 2x1
107*7dd7cddfSDavid du Colombiersampling (also called 4:2:2 sampling).  Compatibility with such decoders can
108*7dd7cddfSDavid du Colombierbe achieved by specifying "-sample 2x1".  This is not recommended unless
109*7dd7cddfSDavid du Colombierreally necessary, since it increases file size and encoding/decoding time
110*7dd7cddfSDavid du Colombierwith very little quality gain.
111*7dd7cddfSDavid du Colombier
112*7dd7cddfSDavid du Colombier
113*7dd7cddfSDavid du ColombierMultiple Scan / Progression Control
114*7dd7cddfSDavid du Colombier-----------------------------------
115*7dd7cddfSDavid du Colombier
116*7dd7cddfSDavid du ColombierBy default, cjpeg emits a single-scan sequential JPEG file.  The
117*7dd7cddfSDavid du Colombier-progressive switch generates a progressive JPEG file using a default series
118*7dd7cddfSDavid du Colombierof progression parameters.  You can create multiple-scan sequential JPEG
119*7dd7cddfSDavid du Colombierfiles or progressive JPEG files with custom progression parameters by using
120*7dd7cddfSDavid du Colombierthe -scans switch:
121*7dd7cddfSDavid du Colombier
122*7dd7cddfSDavid du Colombier	-scans file	Use the scan sequence given in the named file.
123*7dd7cddfSDavid du Colombier
124*7dd7cddfSDavid du ColombierThe specified file should be a text file containing a "scan script".
125*7dd7cddfSDavid du ColombierThe script specifies the contents and ordering of the scans to be emitted.
126*7dd7cddfSDavid du ColombierEach entry in the script defines one scan.  A scan definition specifies
127*7dd7cddfSDavid du Colombierthe components to be included in the scan, and for progressive JPEG it also
128*7dd7cddfSDavid du Colombierspecifies the progression parameters Ss,Se,Ah,Al for the scan.  Scan
129*7dd7cddfSDavid du Colombierdefinitions are separated by semicolons (';').  A semicolon after the last
130*7dd7cddfSDavid du Colombierscan definition is optional.
131*7dd7cddfSDavid du Colombier
132*7dd7cddfSDavid du ColombierEach scan definition contains one to four component indexes, optionally
133*7dd7cddfSDavid du Colombierfollowed by a colon (':') and the four progressive-JPEG parameters.  The
134*7dd7cddfSDavid du Colombiercomponent indexes denote which color component(s) are to be transmitted in
135*7dd7cddfSDavid du Colombierthe scan.  Components are numbered in the order in which they appear in the
136*7dd7cddfSDavid du ColombierJPEG SOF marker, with the first component being numbered 0.  (Note that these
137*7dd7cddfSDavid du Colombierindexes are not the "component ID" codes assigned to the components, just
138*7dd7cddfSDavid du Colombierpositional indexes.)
139*7dd7cddfSDavid du Colombier
140*7dd7cddfSDavid du ColombierThe progression parameters for each scan are:
141*7dd7cddfSDavid du Colombier	Ss	Zigzag index of first coefficient included in scan
142*7dd7cddfSDavid du Colombier	Se	Zigzag index of last coefficient included in scan
143*7dd7cddfSDavid du Colombier	Ah	Zero for first scan of a coefficient, else Al of prior scan
144*7dd7cddfSDavid du Colombier	Al	Successive approximation low bit position for scan
145*7dd7cddfSDavid du ColombierIf the progression parameters are omitted, the values 0,63,0,0 are used,
146*7dd7cddfSDavid du Colombierproducing a sequential JPEG file.  cjpeg automatically determines whether
147*7dd7cddfSDavid du Colombierthe script represents a progressive or sequential file, by observing whether
148*7dd7cddfSDavid du ColombierSs and Se values other than 0 and 63 appear.  (The -progressive switch is
149*7dd7cddfSDavid du Colombiernot needed to specify this; in fact, it is ignored when -scans appears.)
150*7dd7cddfSDavid du ColombierThe scan script must meet the JPEG restrictions on progression sequences.
151*7dd7cddfSDavid du Colombier(cjpeg checks that the spec's requirements are obeyed.)
152*7dd7cddfSDavid du Colombier
153*7dd7cddfSDavid du ColombierScan script files are free format, in that arbitrary whitespace can appear
154*7dd7cddfSDavid du Colombierbetween numbers and around punctuation.  Also, comments can be included: a
155*7dd7cddfSDavid du Colombiercomment starts with '#' and extends to the end of the line.  For additional
156*7dd7cddfSDavid du Colombierlegibility, commas or dashes can be placed between values.  (Actually, any
157*7dd7cddfSDavid du Colombiersingle punctuation character other than ':' or ';' can be inserted.)  For
158*7dd7cddfSDavid du Colombierexample, the following two scan definitions are equivalent:
159*7dd7cddfSDavid du Colombier	0 1 2: 0 63 0 0;
160*7dd7cddfSDavid du Colombier	0,1,2 : 0-63, 0,0 ;
161*7dd7cddfSDavid du Colombier
162*7dd7cddfSDavid du ColombierHere is an example of a scan script that generates a partially interleaved
163*7dd7cddfSDavid du Colombiersequential JPEG file:
164*7dd7cddfSDavid du Colombier
165*7dd7cddfSDavid du Colombier	0;			# Y only in first scan
166*7dd7cddfSDavid du Colombier	1 2;			# Cb and Cr in second scan
167*7dd7cddfSDavid du Colombier
168*7dd7cddfSDavid du ColombierHere is an example of a progressive scan script using only spectral selection
169*7dd7cddfSDavid du Colombier(no successive approximation):
170*7dd7cddfSDavid du Colombier
171*7dd7cddfSDavid du Colombier	# Interleaved DC scan for Y,Cb,Cr:
172*7dd7cddfSDavid du Colombier	0,1,2: 0-0,   0, 0 ;
173*7dd7cddfSDavid du Colombier	# AC scans:
174*7dd7cddfSDavid du Colombier	0:     1-2,   0, 0 ;	# First two Y AC coefficients
175*7dd7cddfSDavid du Colombier	0:     3-5,   0, 0 ;	# Three more
176*7dd7cddfSDavid du Colombier	1:     1-63,  0, 0 ;	# All AC coefficients for Cb
177*7dd7cddfSDavid du Colombier	2:     1-63,  0, 0 ;	# All AC coefficients for Cr
178*7dd7cddfSDavid du Colombier	0:     6-9,   0, 0 ;	# More Y coefficients
179*7dd7cddfSDavid du Colombier	0:     10-63, 0, 0 ;	# Remaining Y coefficients
180*7dd7cddfSDavid du Colombier
181*7dd7cddfSDavid du ColombierHere is an example of a successive-approximation script.  This is equivalent
182*7dd7cddfSDavid du Colombierto the default script used by "cjpeg -progressive" for YCbCr images:
183*7dd7cddfSDavid du Colombier
184*7dd7cddfSDavid du Colombier	# Initial DC scan for Y,Cb,Cr (lowest bit not sent)
185*7dd7cddfSDavid du Colombier	0,1,2: 0-0,   0, 1 ;
186*7dd7cddfSDavid du Colombier	# First AC scan: send first 5 Y AC coefficients, minus 2 lowest bits:
187*7dd7cddfSDavid du Colombier	0:     1-5,   0, 2 ;
188*7dd7cddfSDavid du Colombier	# Send all Cr,Cb AC coefficients, minus lowest bit:
189*7dd7cddfSDavid du Colombier	# (chroma data is usually too small to be worth subdividing further;
190*7dd7cddfSDavid du Colombier	#  but note we send Cr first since eye is least sensitive to Cb)
191*7dd7cddfSDavid du Colombier	2:     1-63,  0, 1 ;
192*7dd7cddfSDavid du Colombier	1:     1-63,  0, 1 ;
193*7dd7cddfSDavid du Colombier	# Send remaining Y AC coefficients, minus 2 lowest bits:
194*7dd7cddfSDavid du Colombier	0:     6-63,  0, 2 ;
195*7dd7cddfSDavid du Colombier	# Send next-to-lowest bit of all Y AC coefficients:
196*7dd7cddfSDavid du Colombier	0:     1-63,  2, 1 ;
197*7dd7cddfSDavid du Colombier	# At this point we've sent all but the lowest bit of all coefficients.
198*7dd7cddfSDavid du Colombier	# Send lowest bit of DC coefficients
199*7dd7cddfSDavid du Colombier	0,1,2: 0-0,   1, 0 ;
200*7dd7cddfSDavid du Colombier	# Send lowest bit of AC coefficients
201*7dd7cddfSDavid du Colombier	2:     1-63,  1, 0 ;
202*7dd7cddfSDavid du Colombier	1:     1-63,  1, 0 ;
203*7dd7cddfSDavid du Colombier	# Y AC lowest bit scan is last; it's usually the largest scan
204*7dd7cddfSDavid du Colombier	0:     1-63,  1, 0 ;
205*7dd7cddfSDavid du Colombier
206*7dd7cddfSDavid du ColombierIt may be worth pointing out that this script is tuned for quality settings
207*7dd7cddfSDavid du Colombierof around 50 to 75.  For lower quality settings, you'd probably want to use
208*7dd7cddfSDavid du Colombiera script with fewer stages of successive approximation (otherwise the
209*7dd7cddfSDavid du Colombierinitial scans will be really bad).  For higher quality settings, you might
210*7dd7cddfSDavid du Colombierwant to use more stages of successive approximation (so that the initial
211*7dd7cddfSDavid du Colombierscans are not too large).
212