2
0

TIFFTechNote2.html 35 KB

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