xref: /libtiff-4.0.7/html/TIFFTechNote2.html (revision 5c402d22)
1*5c402d22SFrank Warmerdam<pre>
2*5c402d22SFrank WarmerdamDRAFT TIFF Technical Note #2				17-Mar-95
3*5c402d22SFrank Warmerdam============================
4*5c402d22SFrank Warmerdam
5*5c402d22SFrank WarmerdamThis Technical Note describes serious problems that have been found in
6*5c402d22SFrank WarmerdamTIFF 6.0's design for embedding JPEG-compressed data in TIFF (Section 22
7*5c402d22SFrank Warmerdamof the TIFF 6.0 spec of 3 June 1992).  A replacement TIFF/JPEG
8*5c402d22SFrank Warmerdamspecification is given.  Some corrections to Section 21 are also given.
9*5c402d22SFrank Warmerdam
10*5c402d22SFrank WarmerdamTo permit TIFF implementations to continue to read existing files, the 6.0
11*5c402d22SFrank WarmerdamJPEG fields and tag values will remain reserved indefinitely.  However,
12*5c402d22SFrank WarmerdamTIFF writers are strongly discouraged from using the 6.0 JPEG design.  It
13*5c402d22SFrank Warmerdamis expected that the next full release of the TIFF specification will not
14*5c402d22SFrank Warmerdamdescribe the old design at all, except to note that certain tag numbers
15*5c402d22SFrank Warmerdamare reserved.  The existing Section 22 will be replaced by the
16*5c402d22SFrank Warmerdamspecification text given in the second part of this Tech Note.
17*5c402d22SFrank Warmerdam
18*5c402d22SFrank Warmerdam
19*5c402d22SFrank WarmerdamProblems in TIFF 6.0 JPEG
20*5c402d22SFrank Warmerdam=========================
21*5c402d22SFrank Warmerdam
22*5c402d22SFrank WarmerdamAbandoning a published spec is not a step to be taken lightly.  This
23*5c402d22SFrank Warmerdamsection summarizes the reasons that have forced this decision.
24*5c402d22SFrank WarmerdamTIFF 6.0's JPEG design suffers from design errors and limitations,
25*5c402d22SFrank Warmerdamambiguities, and unnecessary complexity.
26*5c402d22SFrank Warmerdam
27*5c402d22SFrank Warmerdam
28*5c402d22SFrank WarmerdamDesign errors and limitations
29*5c402d22SFrank Warmerdam-----------------------------
30*5c402d22SFrank Warmerdam
31*5c402d22SFrank WarmerdamThe fundamental design error in the existing Section 22 is that JPEG's
32*5c402d22SFrank Warmerdamvarious tables and parameters are broken out as separate fields which the
33*5c402d22SFrank WarmerdamTIFF control logic must manage.  This is bad software engineering: that
34*5c402d22SFrank Warmerdaminformation should be treated as private to the JPEG codec
35*5c402d22SFrank Warmerdam(compressor/decompressor).  Worse, the fields themselves are specified
36*5c402d22SFrank Warmerdamwithout sufficient thought for future extension and without regard to
37*5c402d22SFrank Warmerdamwell-established TIFF conventions.  Here are some of the significant
38*5c402d22SFrank Warmerdamproblems:
39*5c402d22SFrank Warmerdam
40*5c402d22SFrank Warmerdam* The JPEGxxTable fields do not store the table data directly in the
41*5c402d22SFrank WarmerdamIFD/field structure; rather, the fields hold pointers to information
42*5c402d22SFrank Warmerdamelsewhere in the file.  This requires special-purpose code to be added to
43*5c402d22SFrank Warmerdam*every* TIFF-manipulating application, whether it needs to decode JPEG
44*5c402d22SFrank Warmerdamimage data or not.  Even a trivial TIFF editor, for example a program to
45*5c402d22SFrank Warmerdamadd an ImageDescription field to a TIFF file, must be explicitly aware of
46*5c402d22SFrank Warmerdamthe internal structure of the JPEG-related tables, or else it will probably
47*5c402d22SFrank Warmerdambreak the file.  Every other auxiliary field in the TIFF spec contains
48*5c402d22SFrank Warmerdamdata, not pointers, and can be copied or relocated by standard code that
49*5c402d22SFrank Warmerdamdoesn't know anything about the particular field.  This is a crucial
50*5c402d22SFrank Warmerdamproperty of the TIFF format that must not be given up.
51*5c402d22SFrank Warmerdam
52*5c402d22SFrank Warmerdam* To manipulate these fields, the TIFF control logic is required to know a
53*5c402d22SFrank Warmerdamgreat deal about JPEG details, for example such arcana as how to compute
54*5c402d22SFrank Warmerdamthe length of a Huffman code table --- the length is not supplied in the
55*5c402d22SFrank Warmerdamfield structure and can only be found by inspecting the table contents.
56*5c402d22SFrank WarmerdamThis is again a violation of good software practice.  Moreover, it will
57*5c402d22SFrank Warmerdamprevent easy adoption of future JPEG extensions that might change these
58*5c402d22SFrank Warmerdamlow-level details.
59*5c402d22SFrank Warmerdam
60*5c402d22SFrank Warmerdam* The design neglects the fact that baseline JPEG codecs support only two
61*5c402d22SFrank Warmerdamsets of Huffman tables: it specifies a separate table for each color
62*5c402d22SFrank Warmerdamcomponent.  This implies that encoders must waste space (by storing
63*5c402d22SFrank Warmerdamduplicate Huffman tables) or else violate the well-founded TIFF convention
64*5c402d22SFrank Warmerdamthat prohibits duplicate pointers.  Furthermore, baseline decoders must
65*5c402d22SFrank Warmerdamtest to find out which tables are identical, a waste of time and code
66*5c402d22SFrank Warmerdamspace.
67*5c402d22SFrank Warmerdam
68*5c402d22SFrank Warmerdam* The JPEGInterchangeFormat field also violates TIFF's proscription against
69*5c402d22SFrank Warmerdamduplicate pointers: the normal strip/tile pointers are expected to point
70*5c402d22SFrank Warmerdaminto the larger data area pointed to by JPEGInterchangeFormat.  All TIFF
71*5c402d22SFrank Warmerdamediting applications must be specifically aware of this relationship, since
72*5c402d22SFrank Warmerdamthey must maintain it or else delete the JPEGInterchangeFormat field.  The
73*5c402d22SFrank WarmerdamJPEGxxTables fields are also likely to point into the JPEGInterchangeFormat
74*5c402d22SFrank Warmerdamarea, creating additional pointer relationships that must be maintained.
75*5c402d22SFrank Warmerdam
76*5c402d22SFrank Warmerdam* The JPEGQTables field is fixed at a byte per table entry; there is no
77*5c402d22SFrank Warmerdamway to support 16-bit quantization values.  This is a serious impediment
78*5c402d22SFrank Warmerdamto extending TIFF to use 12-bit JPEG.
79*5c402d22SFrank Warmerdam
80*5c402d22SFrank Warmerdam* The 6.0 design cannot support using different quantization tables in
81*5c402d22SFrank Warmerdamdifferent strips/tiles of an image (so as to encode some areas at higher
82*5c402d22SFrank Warmerdamquality than others).  Furthermore, since quantization tables are tied
83*5c402d22SFrank Warmerdamone-for-one to color components, the design cannot support table switching
84*5c402d22SFrank Warmerdamoptions that are likely to be added in future JPEG revisions.
85*5c402d22SFrank Warmerdam
86*5c402d22SFrank Warmerdam
87*5c402d22SFrank WarmerdamAmbiguities
88*5c402d22SFrank Warmerdam-----------
89*5c402d22SFrank Warmerdam
90*5c402d22SFrank WarmerdamSeveral incompatible interpretations are possible for 6.0's treatment of
91*5c402d22SFrank WarmerdamJPEG restart markers:
92*5c402d22SFrank Warmerdam
93*5c402d22SFrank Warmerdam  * It is unclear whether restart markers must be omitted at TIFF segment
94*5c402d22SFrank Warmerdam    (strip/tile) boundaries, or whether they are optional.
95*5c402d22SFrank Warmerdam
96*5c402d22SFrank Warmerdam  * It is unclear whether the segment size is required to be chosen as
97*5c402d22SFrank Warmerdam    a multiple of the specified restart interval (if any); perhaps the
98*5c402d22SFrank Warmerdam    JPEG codec is supposed to be reset at each segment boundary as if
99*5c402d22SFrank Warmerdam    there were a restart marker there, even if the boundary does not fall
100*5c402d22SFrank Warmerdam    at a multiple of the nominal restart interval.
101*5c402d22SFrank Warmerdam
102*5c402d22SFrank Warmerdam  * The spec fails to address the question of restart marker numbering:
103*5c402d22SFrank Warmerdam    do the numbers begin again within each segment, or not?
104*5c402d22SFrank Warmerdam
105*5c402d22SFrank WarmerdamThat last point is particularly nasty.  If we make numbering begin again
106*5c402d22SFrank Warmerdamwithin each segment, we give up the ability to impose a TIFF strip/tile
107*5c402d22SFrank Warmerdamstructure on an existing JPEG datastream with restarts (which was clearly a
108*5c402d22SFrank Warmerdamgoal of Section 22's authors).  But the other choice interferes with random
109*5c402d22SFrank Warmerdamaccess to the image segments: a reader must compute the first restart
110*5c402d22SFrank Warmerdamnumber to be expected within a segment, and must have a way to reset its
111*5c402d22SFrank WarmerdamJPEG decoder to expect a nonzero restart number first.  This may not even
112*5c402d22SFrank Warmerdambe possible with some JPEG chips.
113*5c402d22SFrank Warmerdam
114*5c402d22SFrank WarmerdamThe tile height restriction found on page 104 contradicts Section 15's
115*5c402d22SFrank Warmerdamgeneral description of tiles.  For an image that is not vertically
116*5c402d22SFrank Warmerdamdownsampled, page 104 specifies a tile height of one MCU or 8 pixels; but
117*5c402d22SFrank WarmerdamSection 15 requires tiles to be a multiple of 16 pixels high.
118*5c402d22SFrank Warmerdam
119*5c402d22SFrank WarmerdamThis Tech Note does not attempt to resolve these ambiguities, so
120*5c402d22SFrank Warmerdamimplementations that follow the 6.0 design should be aware that
121*5c402d22SFrank Warmerdaminter-application compatibility problems are likely to arise.
122*5c402d22SFrank Warmerdam
123*5c402d22SFrank Warmerdam
124*5c402d22SFrank WarmerdamUnnecessary complexity
125*5c402d22SFrank Warmerdam----------------------
126*5c402d22SFrank Warmerdam
127*5c402d22SFrank WarmerdamThe 6.0 design creates problems for implementations that need to keep the
128*5c402d22SFrank WarmerdamJPEG codec separate from the TIFF control logic --- for example, consider
129*5c402d22SFrank Warmerdamusing a JPEG chip that was not designed specifically for TIFF.  JPEG codecs
130*5c402d22SFrank Warmerdamgenerally want to produce or consume a standard ISO JPEG datastream, not
131*5c402d22SFrank Warmerdamjust raw compressed data.  (If they were to handle raw data, a separate
132*5c402d22SFrank Warmerdamout-of-band mechanism would be needed to load tables into the codec.)
133*5c402d22SFrank WarmerdamWith such a codec, the TIFF control logic must parse JPEG markers emitted
134*5c402d22SFrank Warmerdamby the codec to create the TIFF table fields (when writing) or synthesize
135*5c402d22SFrank WarmerdamJPEG markers from the TIFF fields to feed the codec (when reading).  This
136*5c402d22SFrank Warmerdammeans that the control logic must know a great deal more about JPEG details
137*5c402d22SFrank Warmerdamthan we would like.  The parsing and reconstruction of the markers also
138*5c402d22SFrank Warmerdamrepresents a fair amount of unnecessary work.
139*5c402d22SFrank Warmerdam
140*5c402d22SFrank WarmerdamQuite a few implementors have proposed writing "TIFF/JPEG" files in which
141*5c402d22SFrank Warmerdama standard JPEG datastream is simply dumped into the file and pointed to
142*5c402d22SFrank Warmerdamby JPEGInterchangeFormat.  To avoid parsing the JPEG datastream, they
143*5c402d22SFrank Warmerdamsuggest not writing the JPEG auxiliary fields (JPEGxxTables etc) nor even
144*5c402d22SFrank Warmerdamthe basic TIFF strip/tile data pointers.  This approach is incompatible
145*5c402d22SFrank Warmerdamwith implementations that handle the full TIFF 6.0 JPEG design, since they
146*5c402d22SFrank Warmerdamwill expect to find strip/tile pointers and auxiliary fields.  Indeed this
147*5c402d22SFrank Warmerdamis arguably not TIFF at all, since *all* TIFF-reading applications expect
148*5c402d22SFrank Warmerdamto find strip or tile pointers.  A subset implementation that is not
149*5c402d22SFrank Warmerdamupward-compatible with the full spec is clearly unacceptable.  However,
150*5c402d22SFrank Warmerdamthe frequency with which this idea has come up makes it clear that
151*5c402d22SFrank Warmerdamimplementors find the existing Section 22 too complex.
152*5c402d22SFrank Warmerdam
153*5c402d22SFrank Warmerdam
154*5c402d22SFrank WarmerdamOverview of the solution
155*5c402d22SFrank Warmerdam========================
156*5c402d22SFrank Warmerdam
157*5c402d22SFrank WarmerdamTo solve these problems, we adopt a new design for embedding
158*5c402d22SFrank WarmerdamJPEG-compressed data in TIFF files.  The new design uses only complete,
159*5c402d22SFrank Warmerdamuninterpreted ISO JPEG datastreams, so it should be much more forgiving of
160*5c402d22SFrank Warmerdamextensions to the ISO standard.  It should also be far easier to implement
161*5c402d22SFrank Warmerdamusing unmodified JPEG codecs.
162*5c402d22SFrank Warmerdam
163*5c402d22SFrank WarmerdamTo reduce overhead in multi-segment TIFF files, we allow JPEG overhead
164*5c402d22SFrank Warmerdamtables to be stored just once in a JPEGTables auxiliary field.  This
165*5c402d22SFrank Warmerdamfeature does not violate the integrity of the JPEG datastreams, because it
166*5c402d22SFrank Warmerdamuses the notions of "tables-only datastreams" and "abbreviated image
167*5c402d22SFrank Warmerdamdatastreams" as defined by the ISO standard.
168*5c402d22SFrank Warmerdam
169*5c402d22SFrank WarmerdamTo prevent confusion with the old design, the new design is given a new
170*5c402d22SFrank WarmerdamCompression tag value, Compression=7.  Readers that need to handle
171*5c402d22SFrank Warmerdamexisting 6.0 JPEG files may read both old and new files, using whatever
172*5c402d22SFrank Warmerdaminterpretation of the 6.0 spec they did before.  Compression tag value 6
173*5c402d22SFrank Warmerdamand the field tag numbers defined by 6.0 section 22 will remain reserved
174*5c402d22SFrank Warmerdamindefinitely, even though detailed descriptions of them will be dropped
175*5c402d22SFrank Warmerdamfrom future editions of the TIFF specification.
176*5c402d22SFrank Warmerdam
177*5c402d22SFrank Warmerdam
178*5c402d22SFrank WarmerdamReplacement TIFF/JPEG specification
179*5c402d22SFrank Warmerdam===================================
180*5c402d22SFrank Warmerdam
181*5c402d22SFrank Warmerdam[This section of the Tech Note is expected to replace Section 22 in the
182*5c402d22SFrank Warmerdamnext release of the TIFF specification.]
183*5c402d22SFrank Warmerdam
184*5c402d22SFrank WarmerdamThis section describes TIFF compression scheme 7, a high-performance
185*5c402d22SFrank Warmerdamcompression method for continuous-tone images.
186*5c402d22SFrank Warmerdam
187*5c402d22SFrank WarmerdamIntroduction
188*5c402d22SFrank Warmerdam------------
189*5c402d22SFrank Warmerdam
190*5c402d22SFrank WarmerdamThis TIFF compression method uses the international standard for image
191*5c402d22SFrank Warmerdamcompression ISO/IEC 10918-1, usually known as "JPEG" (after the original
192*5c402d22SFrank Warmerdamname of the standards committee, Joint Photographic Experts Group).  JPEG
193*5c402d22SFrank Warmerdamis a joint ISO/CCITT standard for compression of continuous-tone images.
194*5c402d22SFrank Warmerdam
195*5c402d22SFrank WarmerdamThe JPEG committee decided that because of the broad scope of the standard,
196*5c402d22SFrank Warmerdamno one algorithmic procedure was able to satisfy the requirements of all
197*5c402d22SFrank Warmerdamapplications.  Instead, the JPEG standard became a "toolkit" of multiple
198*5c402d22SFrank Warmerdamalgorithms and optional capabilities.  Individual applications may select
199*5c402d22SFrank Warmerdama subset of the JPEG standard that meets their requirements.
200*5c402d22SFrank Warmerdam
201*5c402d22SFrank WarmerdamThe most important distinction among the JPEG processes is between lossy
202*5c402d22SFrank Warmerdamand lossless compression.  Lossy compression methods provide high
203*5c402d22SFrank Warmerdamcompression but allow only approximate reconstruction of the original
204*5c402d22SFrank Warmerdamimage.  JPEG's lossy processes allow the encoder to trade off compressed
205*5c402d22SFrank Warmerdamfile size against reconstruction fidelity over a wide range.  Typically,
206*5c402d22SFrank Warmerdam10:1 or more compression of full-color data can be obtained while keeping
207*5c402d22SFrank Warmerdamthe reconstructed image visually indistinguishable from the original.  Much
208*5c402d22SFrank Warmerdamhigher compression ratios are possible if a low-quality reconstructed image
209*5c402d22SFrank Warmerdamis acceptable.  Lossless compression provides exact reconstruction of the
210*5c402d22SFrank Warmerdamsource data, but the achievable compression ratio is much lower than for
211*5c402d22SFrank Warmerdamthe lossy processes; JPEG's rather simple lossless process typically
212*5c402d22SFrank Warmerdamachieves around 2:1 compression of full-color data.
213*5c402d22SFrank Warmerdam
214*5c402d22SFrank WarmerdamThe most widely implemented JPEG subset is the "baseline" JPEG process.
215*5c402d22SFrank WarmerdamThis provides lossy compression of 8-bit-per-channel data.  Optional
216*5c402d22SFrank Warmerdamextensions include 12-bit-per-channel data, arithmetic entropy coding for
217*5c402d22SFrank Warmerdambetter compression, and progressive/hierarchical representations.  The
218*5c402d22SFrank Warmerdamlossless process is an independent algorithm that has little in
219*5c402d22SFrank Warmerdamcommon with the lossy processes.
220*5c402d22SFrank Warmerdam
221*5c402d22SFrank WarmerdamIt should be noted that the optional arithmetic-coding extension is subject
222*5c402d22SFrank Warmerdamto several US and Japanese patents.  To avoid patent problems, use of
223*5c402d22SFrank Warmerdamarithmetic coding processes in TIFF files intended for inter-application
224*5c402d22SFrank Warmerdaminterchange is discouraged.
225*5c402d22SFrank Warmerdam
226*5c402d22SFrank WarmerdamAll of the JPEG processes are useful only for "continuous tone" data,
227*5c402d22SFrank Warmerdamin which the difference between adjacent pixel values is usually small.
228*5c402d22SFrank WarmerdamLow-bit-depth source data is not appropriate for JPEG compression, nor
229*5c402d22SFrank Warmerdamare palette-color images good candidates.  The JPEG processes work well
230*5c402d22SFrank Warmerdamon grayscale and full-color data.
231*5c402d22SFrank Warmerdam
232*5c402d22SFrank WarmerdamDescribing the JPEG compression algorithms in sufficient detail to permit
233*5c402d22SFrank Warmerdamimplementation would require more space than we have here.  Instead, we
234*5c402d22SFrank Warmerdamrefer the reader to the References section.
235*5c402d22SFrank Warmerdam
236*5c402d22SFrank Warmerdam
237*5c402d22SFrank WarmerdamWhat data is being compressed?
238*5c402d22SFrank Warmerdam------------------------------
239*5c402d22SFrank Warmerdam
240*5c402d22SFrank WarmerdamIn lossy JPEG compression, it is customary to convert color source data
241*5c402d22SFrank Warmerdamto YCbCr and then downsample it before JPEG compression.  This gives
242*5c402d22SFrank Warmerdam2:1 data compression with hardly any visible image degradation, and it
243*5c402d22SFrank Warmerdampermits additional space savings within the JPEG compression step proper.
244*5c402d22SFrank WarmerdamHowever, these steps are not considered part of the ISO JPEG standard.
245*5c402d22SFrank WarmerdamThe ISO standard is "color blind": it accepts data in any color space.
246*5c402d22SFrank Warmerdam
247*5c402d22SFrank WarmerdamFor TIFF purposes, the JPEG compression tag is considered to represent the
248*5c402d22SFrank WarmerdamISO JPEG compression standard only.  The ISO standard is applied to the
249*5c402d22SFrank Warmerdamsame data that would be stored in the TIFF file if no compression were
250*5c402d22SFrank Warmerdamused.  Therefore, if color conversion or downsampling are used, they must
251*5c402d22SFrank Warmerdambe reflected in the regular TIFF fields; these steps are not considered to
252*5c402d22SFrank Warmerdambe implicit in the JPEG compression tag value.  PhotometricInterpretation
253*5c402d22SFrank Warmerdamand related fields shall describe the color space actually stored in the
254*5c402d22SFrank Warmerdamfile.  With the TIFF 6.0 field definitions, downsampling is permissible
255*5c402d22SFrank Warmerdamonly for YCbCr data, and it must correspond to the YCbCrSubSampling field.
256*5c402d22SFrank Warmerdam(Note that the default value for this field is not 1,1; so the default for
257*5c402d22SFrank WarmerdamYCbCr is to apply downsampling!)  It is likely that future versions of TIFF
258*5c402d22SFrank Warmerdamwill provide additional PhotometricInterpretation values and a more general
259*5c402d22SFrank Warmerdamway of defining subsampling, so as to allow more flexibility in
260*5c402d22SFrank WarmerdamJPEG-compressed files.  But that issue is not addressed in this Tech Note.
261*5c402d22SFrank Warmerdam
262*5c402d22SFrank WarmerdamImplementors should note that many popular JPEG codecs
263*5c402d22SFrank Warmerdam(compressor/decompressors) provide automatic color conversion and
264*5c402d22SFrank Warmerdamdownsampling, so that the application may supply full-size RGB data which
265*5c402d22SFrank Warmerdamis nonetheless converted to downsampled YCbCr.  This is an implementation
266*5c402d22SFrank Warmerdamconvenience which does not excuse the TIFF control layer from its
267*5c402d22SFrank Warmerdamresponsibility to know what is really going on.  The
268*5c402d22SFrank WarmerdamPhotometricInterpretation and subsampling fields written to the file must
269*5c402d22SFrank Warmerdamdescribe what is actually in the file.
270*5c402d22SFrank Warmerdam
271*5c402d22SFrank WarmerdamA JPEG-compressed TIFF file will typically have PhotometricInterpretation =
272*5c402d22SFrank WarmerdamYCbCr and YCbCrSubSampling = [2,1] or [2,2], unless the source data was
273*5c402d22SFrank Warmerdamgrayscale or CMYK.
274*5c402d22SFrank Warmerdam
275*5c402d22SFrank Warmerdam
276*5c402d22SFrank WarmerdamBasic representation of JPEG-compressed images
277*5c402d22SFrank Warmerdam----------------------------------------------
278*5c402d22SFrank Warmerdam
279*5c402d22SFrank WarmerdamJPEG compression works in either strip-based or tile-based TIFF files.
280*5c402d22SFrank WarmerdamRather than repeating "strip or tile" constantly, we will use the term
281*5c402d22SFrank Warmerdam"segment" to mean either a strip or a tile.
282*5c402d22SFrank Warmerdam
283*5c402d22SFrank WarmerdamWhen the Compression field has the value 7, each image segment contains
284*5c402d22SFrank Warmerdama complete JPEG datastream which is valid according to the ISO JPEG
285*5c402d22SFrank Warmerdamstandard (ISO/IEC 10918-1).  Any sequential JPEG process can be used,
286*5c402d22SFrank Warmerdamincluding lossless JPEG, but progressive and hierarchical processes are not
287*5c402d22SFrank Warmerdamsupported.  Since JPEG is useful only for continuous-tone images, the
288*5c402d22SFrank WarmerdamPhotometricInterpretation of the image shall not be 3 (palette color) nor
289*5c402d22SFrank Warmerdam4 (transparency mask).  The bit depth of the data is also restricted as
290*5c402d22SFrank Warmerdamspecified below.
291*5c402d22SFrank Warmerdam
292*5c402d22SFrank WarmerdamEach image segment in a JPEG-compressed TIFF file shall contain a valid
293*5c402d22SFrank WarmerdamJPEG datastream according to the ISO JPEG standard's rules for
294*5c402d22SFrank Warmerdaminterchange-format or abbreviated-image-format data.  The datastream shall
295*5c402d22SFrank Warmerdamcontain a single JPEG frame storing that segment of the image.  The
296*5c402d22SFrank Warmerdamrequired JPEG markers within a segment are:
297*5c402d22SFrank Warmerdam	SOI	(must appear at very beginning of segment)
298*5c402d22SFrank Warmerdam	SOFn
299*5c402d22SFrank Warmerdam	SOS	(one for each scan, if there is more than one scan)
300*5c402d22SFrank Warmerdam	EOI	(must appear at very end of segment)
301*5c402d22SFrank WarmerdamThe actual compressed data follows SOS; it may contain RSTn markers if DRI
302*5c402d22SFrank Warmerdamis used.
303*5c402d22SFrank Warmerdam
304*5c402d22SFrank WarmerdamAdditional JPEG "tables and miscellaneous" markers may appear between SOI
305*5c402d22SFrank Warmerdamand SOFn, between SOFn and SOS, and before each subsequent SOS if there is
306*5c402d22SFrank Warmerdammore than one scan.  These markers include:
307*5c402d22SFrank Warmerdam	DQT
308*5c402d22SFrank Warmerdam	DHT
309*5c402d22SFrank Warmerdam	DAC	(not to appear unless arithmetic coding is used)
310*5c402d22SFrank Warmerdam	DRI
311*5c402d22SFrank Warmerdam	APPn	(shall be ignored by TIFF readers)
312*5c402d22SFrank Warmerdam	COM	(shall be ignored by TIFF readers)
313*5c402d22SFrank WarmerdamDNL markers shall not be used in TIFF files.  Readers should abort if any
314*5c402d22SFrank Warmerdamother marker type is found, especially the JPEG reserved markers;
315*5c402d22SFrank Warmerdamoccurrence of such a marker is likely to indicate a JPEG extension.
316*5c402d22SFrank Warmerdam
317*5c402d22SFrank WarmerdamThe tables/miscellaneous markers may appear in any order.  Readers are
318*5c402d22SFrank Warmerdamcautioned that although the SOFn marker refers to DQT tables, JPEG does not
319*5c402d22SFrank Warmerdamrequire those tables to precede the SOFn, only the SOS.  Missing-table
320*5c402d22SFrank Warmerdamchecks should be made when SOS is reached.
321*5c402d22SFrank Warmerdam
322*5c402d22SFrank WarmerdamIf no JPEGTables field is used, then each image segment shall be a complete
323*5c402d22SFrank WarmerdamJPEG interchange datastream.  Each segment must define all the tables it
324*5c402d22SFrank Warmerdamreferences.  To allow readers to decode segments in any order, no segment
325*5c402d22SFrank Warmerdammay rely on tables being carried over from a previous segment.
326*5c402d22SFrank Warmerdam
327*5c402d22SFrank WarmerdamWhen a JPEGTables field is used, image segments may omit tables that have
328*5c402d22SFrank Warmerdambeen specified in the JPEGTables field.  Further details appear below.
329*5c402d22SFrank Warmerdam
330*5c402d22SFrank WarmerdamThe SOFn marker shall be of type SOF0 for strict baseline JPEG data, of
331*5c402d22SFrank Warmerdamtype SOF1 for non-baseline lossy JPEG data, or of type SOF3 for lossless
332*5c402d22SFrank WarmerdamJPEG data.  (SOF9 or SOF11 would be used for arithmetic coding.)  All
333*5c402d22SFrank Warmerdamsegments of a JPEG-compressed TIFF image shall use the same JPEG
334*5c402d22SFrank Warmerdamcompression process, in particular the same SOFn type.
335*5c402d22SFrank Warmerdam
336*5c402d22SFrank WarmerdamThe data precision field of the SOFn marker shall agree with the TIFF
337*5c402d22SFrank WarmerdamBitsPerSample field.  (Note that when PlanarConfiguration=1, this implies
338*5c402d22SFrank Warmerdamthat all components must have the same BitsPerSample value; when
339*5c402d22SFrank WarmerdamPlanarConfiguration=2, different components could have different bit
340*5c402d22SFrank Warmerdamdepths.)  For SOF0 only precision 8 is permitted; for SOF1, precision 8 or
341*5c402d22SFrank Warmerdam12 is permitted; for SOF3, precisions 2 to 16 are permitted.
342*5c402d22SFrank Warmerdam
343*5c402d22SFrank WarmerdamThe image dimensions given in the SOFn marker shall agree with the logical
344*5c402d22SFrank Warmerdamdimensions of that particular strip or tile.  For strip images, the SOFn
345*5c402d22SFrank Warmerdamimage width shall equal ImageWidth and the height shall equal RowsPerStrip,
346*5c402d22SFrank Warmerdamexcept in the last strip; its SOFn height shall equal the number of rows
347*5c402d22SFrank Warmerdamremaining in the ImageLength.  (In other words, no padding data is counted
348*5c402d22SFrank Warmerdamin the SOFn dimensions.)  For tile images, each SOFn shall have width
349*5c402d22SFrank WarmerdamTileWidth and height TileHeight; adding and removing any padding needed in
350*5c402d22SFrank Warmerdamthe edge tiles is the concern of some higher level of the TIFF software.
351*5c402d22SFrank Warmerdam(The dimensional rules are slightly different when PlanarConfiguration=2,
352*5c402d22SFrank Warmerdamas described below.)
353*5c402d22SFrank Warmerdam
354*5c402d22SFrank WarmerdamThe ISO JPEG standard only permits images up to 65535 pixels in width or
355*5c402d22SFrank Warmerdamheight, due to 2-byte fields in the SOFn markers.  In TIFF, this limits
356*5c402d22SFrank Warmerdamthe size of an individual JPEG-compressed strip or tile, but the total
357*5c402d22SFrank Warmerdamimage size can be greater.
358*5c402d22SFrank Warmerdam
359*5c402d22SFrank WarmerdamThe number of components in the JPEG datastream shall equal SamplesPerPixel
360*5c402d22SFrank Warmerdamfor PlanarConfiguration=1, and shall be 1 for PlanarConfiguration=2.  The
361*5c402d22SFrank Warmerdamcomponents shall be stored in the same order as they are described at the
362*5c402d22SFrank WarmerdamTIFF field level.  (This applies both to their order in the SOFn marker,
363*5c402d22SFrank Warmerdamand to the order in which they are scanned if multiple JPEG scans are
364*5c402d22SFrank Warmerdamused.)  The component ID bytes are arbitrary so long as each component
365*5c402d22SFrank Warmerdamwithin an image segment is given a distinct ID.  To avoid any possible
366*5c402d22SFrank Warmerdamconfusion, we require that all segments of a TIFF image use the same ID
367*5c402d22SFrank Warmerdamcode for a given component.
368*5c402d22SFrank Warmerdam
369*5c402d22SFrank WarmerdamIn PlanarConfiguration 1, the sampling factors given in SOFn markers shall
370*5c402d22SFrank Warmerdamagree with the sampling factors defined by the related TIFF fields (or with
371*5c402d22SFrank Warmerdamthe default values that are specified in the absence of those fields).
372*5c402d22SFrank Warmerdam
373*5c402d22SFrank WarmerdamWhen DCT-based JPEG is used in a strip TIFF file, RowsPerStrip is required
374*5c402d22SFrank Warmerdamto be a multiple of 8 times the largest vertical sampling factor, i.e., a
375*5c402d22SFrank Warmerdammultiple of the height of an interleaved MCU.  (For simplicity of
376*5c402d22SFrank Warmerdamspecification, we require this even if the data is not actually
377*5c402d22SFrank Warmerdaminterleaved.)  For example, if YCbCrSubSampling = [2,2] then RowsPerStrip
378*5c402d22SFrank Warmerdammust be a multiple of 16.  An exception to this rule is made for
379*5c402d22SFrank Warmerdamsingle-strip images (RowsPerStrip >= ImageLength): the exact value of
380*5c402d22SFrank WarmerdamRowsPerStrip is unimportant in that case.  This rule ensures that no data
381*5c402d22SFrank Warmerdampadding is needed at the bottom of a strip, except perhaps the last strip.
382*5c402d22SFrank WarmerdamAny padding required at the right edge of the image, or at the bottom of
383*5c402d22SFrank Warmerdamthe last strip, is expected to occur internally to the JPEG codec.
384*5c402d22SFrank Warmerdam
385*5c402d22SFrank WarmerdamWhen DCT-based JPEG is used in a tiled TIFF file, TileLength is required
386*5c402d22SFrank Warmerdamto be a multiple of 8 times the largest vertical sampling factor, i.e.,
387*5c402d22SFrank Warmerdama multiple of the height of an interleaved MCU; and TileWidth is required
388*5c402d22SFrank Warmerdamto be a multiple of 8 times the largest horizontal sampling factor, i.e.,
389*5c402d22SFrank Warmerdama multiple of the width of an interleaved MCU.  (For simplicity of
390*5c402d22SFrank Warmerdamspecification, we require this even if the data is not actually
391*5c402d22SFrank Warmerdaminterleaved.)  All edge padding required will therefore occur in the course
392*5c402d22SFrank Warmerdamof normal TIFF tile padding; it is not special to JPEG.
393*5c402d22SFrank Warmerdam
394*5c402d22SFrank WarmerdamLossless JPEG does not impose these constraints on strip and tile sizes,
395*5c402d22SFrank Warmerdamsince it is not DCT-based.
396*5c402d22SFrank Warmerdam
397*5c402d22SFrank WarmerdamNote that within JPEG datastreams, multibyte values appear in the MSB-first
398*5c402d22SFrank Warmerdamorder specified by the JPEG standard, regardless of the byte ordering of
399*5c402d22SFrank Warmerdamthe surrounding TIFF file.
400*5c402d22SFrank Warmerdam
401*5c402d22SFrank Warmerdam
402*5c402d22SFrank WarmerdamJPEGTables field
403*5c402d22SFrank Warmerdam----------------
404*5c402d22SFrank Warmerdam
405*5c402d22SFrank WarmerdamThe only auxiliary TIFF field added for Compression=7 is the optional
406*5c402d22SFrank WarmerdamJPEGTables field.  The purpose of JPEGTables is to predefine JPEG
407*5c402d22SFrank Warmerdamquantization and/or Huffman tables for subsequent use by JPEG image
408*5c402d22SFrank Warmerdamsegments.  When this is done, these rather bulky tables need not be
409*5c402d22SFrank Warmerdamduplicated in each segment, thus saving space and processing time.
410*5c402d22SFrank WarmerdamJPEGTables may be used even in a single-segment file, although there is no
411*5c402d22SFrank Warmerdamspace savings in that case.
412*5c402d22SFrank Warmerdam
413*5c402d22SFrank WarmerdamJPEGTables:
414*5c402d22SFrank Warmerdam	Tag = 347 (15B.H)
415*5c402d22SFrank Warmerdam	Type = UNDEFINED
416*5c402d22SFrank Warmerdam	N = number of bytes in tables datastream, typically a few hundred
417*5c402d22SFrank WarmerdamJPEGTables provides default JPEG quantization and/or Huffman tables which
418*5c402d22SFrank Warmerdamare used whenever a segment datastream does not contain its own tables, as
419*5c402d22SFrank Warmerdamspecified below.
420*5c402d22SFrank Warmerdam
421*5c402d22SFrank WarmerdamNotice that the JPEGTables field is required to have type code UNDEFINED,
422*5c402d22SFrank Warmerdamnot type code BYTE.  This is to cue readers that expanding individual bytes
423*5c402d22SFrank Warmerdamto short or long integers is not appropriate.  A TIFF reader will generally
424*5c402d22SFrank Warmerdamneed to store the field value as an uninterpreted byte sequence until it is
425*5c402d22SFrank Warmerdamfed to the JPEG decoder.
426*5c402d22SFrank Warmerdam
427*5c402d22SFrank WarmerdamMultibyte quantities within the tables follow the ISO JPEG convention of
428*5c402d22SFrank WarmerdamMSB-first storage, regardless of the byte ordering of the surrounding TIFF
429*5c402d22SFrank Warmerdamfile.
430*5c402d22SFrank Warmerdam
431*5c402d22SFrank WarmerdamWhen the JPEGTables field is present, it shall contain a valid JPEG
432*5c402d22SFrank Warmerdam"abbreviated table specification" datastream.  This datastream shall begin
433*5c402d22SFrank Warmerdamwith SOI and end with EOI.  It may contain zero or more JPEG "tables and
434*5c402d22SFrank Warmerdammiscellaneous" markers, namely:
435*5c402d22SFrank Warmerdam	DQT
436*5c402d22SFrank Warmerdam	DHT
437*5c402d22SFrank Warmerdam	DAC	(not to appear unless arithmetic coding is used)
438*5c402d22SFrank Warmerdam	DRI
439*5c402d22SFrank Warmerdam	APPn	(shall be ignored by TIFF readers)
440*5c402d22SFrank Warmerdam	COM	(shall be ignored by TIFF readers)
441*5c402d22SFrank WarmerdamSince JPEG defines the SOI marker to reset the DAC and DRI state, these two
442*5c402d22SFrank Warmerdammarkers' values cannot be carried over into any image datastream, and thus
443*5c402d22SFrank Warmerdamthey are effectively no-ops in the JPEGTables field.  To avoid confusion,
444*5c402d22SFrank Warmerdamit is recommended that writers not place DAC or DRI markers in JPEGTables.
445*5c402d22SFrank WarmerdamHowever readers must properly skip over them if they appear.
446*5c402d22SFrank Warmerdam
447*5c402d22SFrank WarmerdamWhen JPEGTables is present, readers shall load the table specifications
448*5c402d22SFrank Warmerdamcontained in JPEGTables before processing image segment datastreams.
449*5c402d22SFrank WarmerdamImage segments may simply refer to these preloaded tables without defining
450*5c402d22SFrank Warmerdamthem.  An image segment can still define and use its own tables, subject to
451*5c402d22SFrank Warmerdamthe restrictions below.
452*5c402d22SFrank Warmerdam
453*5c402d22SFrank WarmerdamAn image segment may not redefine any table defined in JPEGTables.  (This
454*5c402d22SFrank Warmerdamrestriction is imposed to allow readers to process image segments in random
455*5c402d22SFrank Warmerdamorder without having to reload JPEGTables between segments.)  Therefore, use
456*5c402d22SFrank Warmerdamof JPEGTables divides the available table slots into two groups: "global"
457*5c402d22SFrank Warmerdamslots are defined in JPEGTables and may be used but not redefined by
458*5c402d22SFrank Warmerdamsegments; "local" slots are available for local definition and use in each
459*5c402d22SFrank Warmerdamsegment.  To permit random access, a segment may not reference any local
460*5c402d22SFrank Warmerdamtables that it does not itself define.
461*5c402d22SFrank Warmerdam
462*5c402d22SFrank Warmerdam
463*5c402d22SFrank WarmerdamSpecial considerations for PlanarConfiguration 2
464*5c402d22SFrank Warmerdam------------------------------------------------
465*5c402d22SFrank Warmerdam
466*5c402d22SFrank WarmerdamIn PlanarConfiguration 2, each image segment contains data for only one
467*5c402d22SFrank Warmerdamcolor component.  To avoid confusing the JPEG codec, we wish the segments
468*5c402d22SFrank Warmerdamto look like valid single-channel (i.e., grayscale) JPEG datastreams.  This
469*5c402d22SFrank Warmerdammeans that different rules must be used for the SOFn parameters.
470*5c402d22SFrank Warmerdam
471*5c402d22SFrank WarmerdamIn PlanarConfiguration 2, the dimensions given in the SOFn of a subsampled
472*5c402d22SFrank Warmerdamcomponent shall be scaled down by the sampling factors compared to the SOFn
473*5c402d22SFrank Warmerdamdimensions that would be used in PlanarConfiguration 1.  This is necessary
474*5c402d22SFrank Warmerdamto match the actual number of samples stored in that segment, so that the
475*5c402d22SFrank WarmerdamJPEG codec doesn't complain about too much or too little data.  In strip
476*5c402d22SFrank WarmerdamTIFF files the computed dimensions may need to be rounded up to the next
477*5c402d22SFrank Warmerdaminteger; in tiled files, the restrictions on tile size make this case
478*5c402d22SFrank Warmerdamimpossible.
479*5c402d22SFrank Warmerdam
480*5c402d22SFrank WarmerdamFurthermore, all SOFn sampling factors shall be given as 1.  (This is
481*5c402d22SFrank Warmerdammerely to avoid confusion, since the sampling factors in a single-channel
482*5c402d22SFrank WarmerdamJPEG datastream have no real effect.)
483*5c402d22SFrank Warmerdam
484*5c402d22SFrank WarmerdamAny downsampling will need to happen externally to the JPEG codec, since
485*5c402d22SFrank WarmerdamJPEG sampling factors are defined with reference to the full-precision
486*5c402d22SFrank Warmerdamcomponent.  In PlanarConfiguration 2, the JPEG codec will be working on
487*5c402d22SFrank Warmerdamonly one component at a time and thus will have no reference component to
488*5c402d22SFrank Warmerdamdownsample against.
489*5c402d22SFrank Warmerdam
490*5c402d22SFrank Warmerdam
491*5c402d22SFrank WarmerdamMinimum requirements for TIFF/JPEG
492*5c402d22SFrank Warmerdam----------------------------------
493*5c402d22SFrank Warmerdam
494*5c402d22SFrank WarmerdamISO JPEG is a large and complex standard; most implementations support only
495*5c402d22SFrank Warmerdama subset of it.  Here we define a "core" subset of TIFF/JPEG which readers
496*5c402d22SFrank Warmerdammust support to claim TIFF/JPEG compatibility.  For maximum
497*5c402d22SFrank Warmerdamcross-application compatibility, we recommend that writers confine
498*5c402d22SFrank Warmerdamthemselves to this subset unless there is very good reason to do otherwise.
499*5c402d22SFrank Warmerdam
500*5c402d22SFrank WarmerdamUse the ISO baseline JPEG process: 8-bit data precision, Huffman coding,
501*5c402d22SFrank Warmerdamwith no more than 2 DC and 2 AC Huffman tables.  Note that this implies
502*5c402d22SFrank WarmerdamBitsPerSample = 8 for each component.  We recommend deviating from baseline
503*5c402d22SFrank WarmerdamJPEG only if 12-bit data precision or lossless coding is required.
504*5c402d22SFrank Warmerdam
505*5c402d22SFrank WarmerdamUse no subsampling (all JPEG sampling factors = 1) for color spaces other
506*5c402d22SFrank Warmerdamthan YCbCr.  (This is, in fact, required with the TIFF 6.0 field
507*5c402d22SFrank Warmerdamdefinitions, but may not be so in future revisions.)  For YCbCr, use one of
508*5c402d22SFrank Warmerdamthe following choices:
509*5c402d22SFrank Warmerdam	YCbCrSubSampling field		JPEG sampling factors
510*5c402d22SFrank Warmerdam	1,1				1h1v, 1h1v, 1h1v
511*5c402d22SFrank Warmerdam	2,1				2h1v, 1h1v, 1h1v
512*5c402d22SFrank Warmerdam	2,2  (default value)		2h2v, 1h1v, 1h1v
513*5c402d22SFrank WarmerdamWe recommend that RGB source data be converted to YCbCr for best compression
514*5c402d22SFrank Warmerdamresults.  Other source data colorspaces should probably be left alone.
515*5c402d22SFrank WarmerdamMinimal readers need not support JPEG images with colorspaces other than
516*5c402d22SFrank WarmerdamYCbCr and grayscale (PhotometricInterpretation = 6 or 1).
517*5c402d22SFrank Warmerdam
518*5c402d22SFrank WarmerdamA minimal reader also need not support JPEG YCbCr images with nondefault
519*5c402d22SFrank Warmerdamvalues of YCbCrCoefficients or YCbCrPositioning, nor with values of
520*5c402d22SFrank WarmerdamReferenceBlackWhite other than [0,255,128,255,128,255].  (These values
521*5c402d22SFrank Warmerdamcorrespond to the RGB<=>YCbCr conversion specified by JFIF, which is widely
522*5c402d22SFrank Warmerdamimplemented in JPEG codecs.)
523*5c402d22SFrank Warmerdam
524*5c402d22SFrank WarmerdamWriters are reminded that a ReferenceBlackWhite field *must* be included
525*5c402d22SFrank Warmerdamwhen PhotometricInterpretation is YCbCr, because the default
526*5c402d22SFrank WarmerdamReferenceBlackWhite values are inappropriate for YCbCr.
527*5c402d22SFrank Warmerdam
528*5c402d22SFrank WarmerdamIf any subsampling is used, PlanarConfiguration=1 is preferred to avoid the
529*5c402d22SFrank Warmerdampossibly-confusing requirements of PlanarConfiguration=2.  In any case,
530*5c402d22SFrank Warmerdamreaders are not required to support PlanarConfiguration=2.
531*5c402d22SFrank Warmerdam
532*5c402d22SFrank WarmerdamIf possible, use a single interleaved scan in each image segment.  This is
533*5c402d22SFrank Warmerdamnot legal JPEG if there are more than 4 SamplesPerPixel or if the sampling
534*5c402d22SFrank Warmerdamfactors are such that more than 10 blocks would be needed per MCU; in that
535*5c402d22SFrank Warmerdamcase, use a separate scan for each component.  (The recommended color
536*5c402d22SFrank Warmerdamspaces and sampling factors will not run into that restriction, so a
537*5c402d22SFrank Warmerdamminimal reader need not support more than one scan per segment.)
538*5c402d22SFrank Warmerdam
539*5c402d22SFrank WarmerdamTo claim TIFF/JPEG compatibility, readers shall support multiple-strip TIFF
540*5c402d22SFrank Warmerdamfiles and the optional JPEGTables field; it is not acceptable to read only
541*5c402d22SFrank Warmerdamsingle-datastream files.  Support for tiled TIFF files is strongly
542*5c402d22SFrank Warmerdamrecommended but not required.
543*5c402d22SFrank Warmerdam
544*5c402d22SFrank Warmerdam
545*5c402d22SFrank WarmerdamOther recommendations for implementors
546*5c402d22SFrank Warmerdam--------------------------------------
547*5c402d22SFrank Warmerdam
548*5c402d22SFrank WarmerdamThe TIFF tag Compression=7 guarantees only that the compressed data is
549*5c402d22SFrank Warmerdamrepresented as ISO JPEG datastreams.  Since JPEG is a large and evolving
550*5c402d22SFrank Warmerdamstandard, readers should apply careful error checking to the JPEG markers
551*5c402d22SFrank Warmerdamto ensure that the compression process is within their capabilities.  In
552*5c402d22SFrank Warmerdamparticular, to avoid being confused by future extensions to the JPEG
553*5c402d22SFrank Warmerdamstandard, it is important to abort if unknown marker codes are seen.
554*5c402d22SFrank Warmerdam
555*5c402d22SFrank WarmerdamThe point of requiring that all image segments use the same JPEG process is
556*5c402d22SFrank Warmerdamto ensure that a reader need check only one segment to determine whether it
557*5c402d22SFrank Warmerdamcan handle the image.  For example, consider a TIFF reader that has access
558*5c402d22SFrank Warmerdamto fast but restricted JPEG hardware, as well as a slower, more general
559*5c402d22SFrank Warmerdamsoftware implementation.  It is desirable to check only one image segment
560*5c402d22SFrank Warmerdamto find out whether the fast hardware can be used.  Thus, writers should
561*5c402d22SFrank Warmerdamtry to ensure that all segments of an image look as much "alike" as
562*5c402d22SFrank Warmerdampossible: there should be no variation in scan layout, use of options such
563*5c402d22SFrank Warmerdamas DRI, etc.  Ideally, segments will be processed identically except
564*5c402d22SFrank Warmerdamperhaps for using different local quantization or entropy-coding tables.
565*5c402d22SFrank Warmerdam
566*5c402d22SFrank WarmerdamWriters should avoid including "noise" JPEG markers (COM and APPn markers).
567*5c402d22SFrank WarmerdamStandard TIFF fields provide a better way to transport any non-image data.
568*5c402d22SFrank WarmerdamSome JPEG codecs may change behavior if they see an APPn marker they
569*5c402d22SFrank Warmerdamthink they understand; since the TIFF spec requires these markers to be
570*5c402d22SFrank Warmerdamignored, this behavior is undesirable.
571*5c402d22SFrank Warmerdam
572*5c402d22SFrank WarmerdamIt is possible to convert an interchange-JPEG file (e.g., a JFIF file) to
573*5c402d22SFrank WarmerdamTIFF simply by dropping the interchange datastream into a single strip.
574*5c402d22SFrank Warmerdam(However, designers are reminded that the TIFF spec discourages huge
575*5c402d22SFrank Warmerdamstrips; splitting the image is somewhat more work but may give better
576*5c402d22SFrank Warmerdamresults.)  Conversion from TIFF to interchange JPEG is more complex.  A
577*5c402d22SFrank Warmerdamstrip-based TIFF/JPEG file can be converted fairly easily if all strips use
578*5c402d22SFrank Warmerdamidentical JPEG tables and no RSTn markers: just delete the overhead markers
579*5c402d22SFrank Warmerdamand insert RSTn markers between strips.  Converting tiled images is harder,
580*5c402d22SFrank Warmerdamsince the data will usually not be in the right order (unless the tiles are
581*5c402d22SFrank Warmerdamonly one MCU high).  This can still be done losslessly, but it will require
582*5c402d22SFrank Warmerdamundoing and redoing the entropy coding so that the DC coefficient
583*5c402d22SFrank Warmerdamdifferences can be updated.
584*5c402d22SFrank Warmerdam
585*5c402d22SFrank WarmerdamThere is no default value for JPEGTables: standard TIFF files must define all
586*5c402d22SFrank Warmerdamtables that they reference.  For some closed systems in which many files will
587*5c402d22SFrank Warmerdamhave identical tables, it might make sense to define a default JPEGTables
588*5c402d22SFrank Warmerdamvalue to avoid actually storing the tables.  Or even better, invent a
589*5c402d22SFrank Warmerdamprivate field selecting one of N default JPEGTables settings, so as to allow
590*5c402d22SFrank Warmerdamfor future expansion.  Either of these must be regarded as a private
591*5c402d22SFrank Warmerdamextension that will render the files unreadable by other applications.
592*5c402d22SFrank Warmerdam
593*5c402d22SFrank Warmerdam
594*5c402d22SFrank WarmerdamReferences
595*5c402d22SFrank Warmerdam----------
596*5c402d22SFrank Warmerdam
597*5c402d22SFrank Warmerdam[1] Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
598*5c402d22SFrank WarmerdamCommunications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
599*5c402d22SFrank Warmerdam
600*5c402d22SFrank WarmerdamThis is the best short technical introduction to the JPEG algorithms.
601*5c402d22SFrank WarmerdamIt is a good overview but does not provide sufficiently detailed
602*5c402d22SFrank Warmerdaminformation to write an implementation.
603*5c402d22SFrank Warmerdam
604*5c402d22SFrank Warmerdam[2] Pennebaker, William B. and Mitchell, Joan L.  "JPEG Still Image Data
605*5c402d22SFrank WarmerdamCompression Standard", Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
606*5c402d22SFrank Warmerdam638pp.
607*5c402d22SFrank Warmerdam
608*5c402d22SFrank WarmerdamThis textbook is by far the most complete exposition of JPEG in existence.
609*5c402d22SFrank WarmerdamIt includes the full text of the ISO JPEG standards (DIS 10918-1 and draft
610*5c402d22SFrank WarmerdamDIS 10918-2).  No would-be JPEG implementor should be without it.
611*5c402d22SFrank Warmerdam
612*5c402d22SFrank Warmerdam[3] ISO/IEC IS 10918-1, "Digital Compression and Coding of Continuous-tone
613*5c402d22SFrank WarmerdamStill Images, Part 1: Requirements and guidelines", February 1994.
614*5c402d22SFrank WarmerdamISO/IEC DIS 10918-2, "Digital Compression and Coding of Continuous-tone
615*5c402d22SFrank WarmerdamStill Images, Part 2: Compliance testing", final approval expected 1994.
616*5c402d22SFrank Warmerdam
617*5c402d22SFrank WarmerdamThese are the official standards documents.  Note that the Pennebaker and
618*5c402d22SFrank WarmerdamMitchell textbook is likely to be cheaper and more useful than the official
619*5c402d22SFrank Warmerdamstandards.
620*5c402d22SFrank Warmerdam
621*5c402d22SFrank Warmerdam
622*5c402d22SFrank WarmerdamChanges to Section 21: YCbCr Images
623*5c402d22SFrank Warmerdam===================================
624*5c402d22SFrank Warmerdam
625*5c402d22SFrank Warmerdam[This section of the Tech Note clarifies section 21 to make clear the
626*5c402d22SFrank Warmerdaminterpretation of image dimensions in a subsampled image.  Furthermore,
627*5c402d22SFrank Warmerdamthe section is changed to allow the original image dimensions not to be
628*5c402d22SFrank Warmerdammultiples of the sampling factors.  This change is necessary to support use
629*5c402d22SFrank Warmerdamof JPEG compression on odd-size images.]
630*5c402d22SFrank Warmerdam
631*5c402d22SFrank WarmerdamAdd the following paragraphs to the Section 21 introduction (p. 89),
632*5c402d22SFrank Warmerdamjust after the paragraph beginning "When a Class Y image is subsampled":
633*5c402d22SFrank Warmerdam
634*5c402d22SFrank Warmerdam	In a subsampled image, it is understood that all TIFF image
635*5c402d22SFrank Warmerdam	dimensions are measured in terms of the highest-resolution
636*5c402d22SFrank Warmerdam	(luminance) component.  In particular, ImageWidth, ImageLength,
637*5c402d22SFrank Warmerdam	RowsPerStrip, TileWidth, TileLength, XResolution, and YResolution
638*5c402d22SFrank Warmerdam	are measured in luminance samples.
639*5c402d22SFrank Warmerdam
640*5c402d22SFrank Warmerdam	RowsPerStrip, TileWidth, and TileLength are constrained so that
641*5c402d22SFrank Warmerdam	there are an integral number of samples of each component in a
642*5c402d22SFrank Warmerdam	complete strip or tile.  However, ImageWidth/ImageLength are not
643*5c402d22SFrank Warmerdam	constrained.  If an odd-size image is to be converted to subsampled
644*5c402d22SFrank Warmerdam	format, the writer should pad the source data to a multiple of the
645*5c402d22SFrank Warmerdam	sampling factors by replication of the last column and/or row, then
646*5c402d22SFrank Warmerdam	downsample.  The number of luminance samples actually stored in the
647*5c402d22SFrank Warmerdam	file will be a multiple of the sampling factors.  Conversely,
648*5c402d22SFrank Warmerdam	readers must ignore any extra data (outside the specified image
649*5c402d22SFrank Warmerdam	dimensions) after upsampling.
650*5c402d22SFrank Warmerdam
651*5c402d22SFrank Warmerdam	When PlanarConfiguration=2, each strip or tile covers the same
652*5c402d22SFrank Warmerdam	image area despite subsampling; that is, the total number of strips
653*5c402d22SFrank Warmerdam	or tiles in the image is the same for each component.  Therefore
654*5c402d22SFrank Warmerdam	strips or tiles of the subsampled components contain fewer samples
655*5c402d22SFrank Warmerdam	than strips or tiles of the luminance component.
656*5c402d22SFrank Warmerdam
657*5c402d22SFrank Warmerdam	If there are extra samples per pixel (see field ExtraSamples),
658*5c402d22SFrank Warmerdam	these data channels have the same number of samples as the
659*5c402d22SFrank Warmerdam	luminance component.
660*5c402d22SFrank Warmerdam
661*5c402d22SFrank WarmerdamRewrite the YCbCrSubSampling field description (pp 91-92) as follows
662*5c402d22SFrank Warmerdam(largely to eliminate possibly-misleading references to
663*5c402d22SFrank WarmerdamImageWidth/ImageLength of the subsampled components):
664*5c402d22SFrank Warmerdam
665*5c402d22SFrank Warmerdam	(first paragraph unchanged)
666*5c402d22SFrank Warmerdam
667*5c402d22SFrank Warmerdam	The two elements of this field are defined as follows:
668*5c402d22SFrank Warmerdam
669*5c402d22SFrank Warmerdam	Short 0: ChromaSubsampleHoriz:
670*5c402d22SFrank Warmerdam
671*5c402d22SFrank Warmerdam	1 = there are equal numbers of luma and chroma samples horizontally.
672*5c402d22SFrank Warmerdam
673*5c402d22SFrank Warmerdam	2 = there are twice as many luma samples as chroma samples
674*5c402d22SFrank Warmerdam	horizontally.
675*5c402d22SFrank Warmerdam
676*5c402d22SFrank Warmerdam	4 = there are four times as many luma samples as chroma samples
677*5c402d22SFrank Warmerdam	horizontally.
678*5c402d22SFrank Warmerdam
679*5c402d22SFrank Warmerdam	Short 1: ChromaSubsampleVert:
680*5c402d22SFrank Warmerdam
681*5c402d22SFrank Warmerdam	1 = there are equal numbers of luma and chroma samples vertically.
682*5c402d22SFrank Warmerdam
683*5c402d22SFrank Warmerdam	2 = there are twice as many luma samples as chroma samples
684*5c402d22SFrank Warmerdam	vertically.
685*5c402d22SFrank Warmerdam
686*5c402d22SFrank Warmerdam	4 = there are four times as many luma samples as chroma samples
687*5c402d22SFrank Warmerdam	vertically.
688*5c402d22SFrank Warmerdam
689*5c402d22SFrank Warmerdam	ChromaSubsampleVert shall always be less than or equal to
690*5c402d22SFrank Warmerdam	ChromaSubsampleHoriz.  Note that Cb and Cr have the same sampling
691*5c402d22SFrank Warmerdam	ratios.
692*5c402d22SFrank Warmerdam
693*5c402d22SFrank Warmerdam	In a strip TIFF file, RowsPerStrip is required to be an integer
694*5c402d22SFrank Warmerdam	multiple of ChromaSubSampleVert (unless RowsPerStrip >=
695*5c402d22SFrank Warmerdam	ImageLength, in which case its exact value is unimportant).
696*5c402d22SFrank Warmerdam	If ImageWidth and ImageLength are not multiples of
697*5c402d22SFrank Warmerdam	ChromaSubsampleHoriz and ChromaSubsampleVert respectively, then the
698*5c402d22SFrank Warmerdam	source data shall be padded to the next integer multiple of these
699*5c402d22SFrank Warmerdam	values before downsampling.
700*5c402d22SFrank Warmerdam
701*5c402d22SFrank Warmerdam	In a tiled TIFF file, TileWidth must be an integer multiple of
702*5c402d22SFrank Warmerdam	ChromaSubsampleHoriz and TileLength must be an integer multiple of
703*5c402d22SFrank Warmerdam	ChromaSubsampleVert.  Padding will occur to tile boundaries.
704*5c402d22SFrank Warmerdam
705*5c402d22SFrank Warmerdam	The default values of this field are [ 2,2 ].  Thus, YCbCr data is
706*5c402d22SFrank Warmerdam	downsampled by default!
707*5c402d22SFrank Warmerdam</pre>
708