Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/doc/specification/technote2.rst

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