Blame html/TIFFTechNote2.html

Packit Service 2594b8
Packit Service 2594b8
DRAFT TIFF Technical Note #2				17-Mar-95
Packit Service 2594b8
============================
Packit Service 2594b8
Packit Service 2594b8
This Technical Note describes serious problems that have been found in
Packit Service 2594b8
TIFF 6.0's design for embedding JPEG-compressed data in TIFF (Section 22
Packit Service 2594b8
of the TIFF 6.0 spec of 3 June 1992).  A replacement TIFF/JPEG
Packit Service 2594b8
specification is given.  Some corrections to Section 21 are also given.
Packit Service 2594b8
Packit Service 2594b8
To permit TIFF implementations to continue to read existing files, the 6.0
Packit Service 2594b8
JPEG fields and tag values will remain reserved indefinitely.  However,
Packit Service 2594b8
TIFF writers are strongly discouraged from using the 6.0 JPEG design.  It
Packit Service 2594b8
is expected that the next full release of the TIFF specification will not
Packit Service 2594b8
describe the old design at all, except to note that certain tag numbers
Packit Service 2594b8
are reserved.  The existing Section 22 will be replaced by the
Packit Service 2594b8
specification text given in the second part of this Tech Note.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Problems in TIFF 6.0 JPEG
Packit Service 2594b8
=========================
Packit Service 2594b8
Packit Service 2594b8
Abandoning a published spec is not a step to be taken lightly.  This
Packit Service 2594b8
section summarizes the reasons that have forced this decision.
Packit Service 2594b8
TIFF 6.0's JPEG design suffers from design errors and limitations,
Packit Service 2594b8
ambiguities, and unnecessary complexity.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Design errors and limitations
Packit Service 2594b8
-----------------------------
Packit Service 2594b8
Packit Service 2594b8
The fundamental design error in the existing Section 22 is that JPEG's
Packit Service 2594b8
various tables and parameters are broken out as separate fields which the
Packit Service 2594b8
TIFF control logic must manage.  This is bad software engineering: that
Packit Service 2594b8
information should be treated as private to the JPEG codec
Packit Service 2594b8
(compressor/decompressor).  Worse, the fields themselves are specified
Packit Service 2594b8
without sufficient thought for future extension and without regard to
Packit Service 2594b8
well-established TIFF conventions.  Here are some of the significant
Packit Service 2594b8
problems:
Packit Service 2594b8
Packit Service 2594b8
* The JPEGxxTable fields do not store the table data directly in the
Packit Service 2594b8
IFD/field structure; rather, the fields hold pointers to information
Packit Service 2594b8
elsewhere in the file.  This requires special-purpose code to be added to
Packit Service 2594b8
*every* TIFF-manipulating application, whether it needs to decode JPEG
Packit Service 2594b8
image data or not.  Even a trivial TIFF editor, for example a program to
Packit Service 2594b8
add an ImageDescription field to a TIFF file, must be explicitly aware of
Packit Service 2594b8
the internal structure of the JPEG-related tables, or else it will probably
Packit Service 2594b8
break the file.  Every other auxiliary field in the TIFF spec contains
Packit Service 2594b8
data, not pointers, and can be copied or relocated by standard code that
Packit Service 2594b8
doesn't know anything about the particular field.  This is a crucial
Packit Service 2594b8
property of the TIFF format that must not be given up.
Packit Service 2594b8
Packit Service 2594b8
* To manipulate these fields, the TIFF control logic is required to know a
Packit Service 2594b8
great deal about JPEG details, for example such arcana as how to compute
Packit Service 2594b8
the length of a Huffman code table --- the length is not supplied in the
Packit Service 2594b8
field structure and can only be found by inspecting the table contents.
Packit Service 2594b8
This is again a violation of good software practice.  Moreover, it will
Packit Service 2594b8
prevent easy adoption of future JPEG extensions that might change these
Packit Service 2594b8
low-level details.
Packit Service 2594b8
Packit Service 2594b8
* The design neglects the fact that baseline JPEG codecs support only two
Packit Service 2594b8
sets of Huffman tables: it specifies a separate table for each color
Packit Service 2594b8
component.  This implies that encoders must waste space (by storing
Packit Service 2594b8
duplicate Huffman tables) or else violate the well-founded TIFF convention
Packit Service 2594b8
that prohibits duplicate pointers.  Furthermore, baseline decoders must
Packit Service 2594b8
test to find out which tables are identical, a waste of time and code
Packit Service 2594b8
space.
Packit Service 2594b8
Packit Service 2594b8
* The JPEGInterchangeFormat field also violates TIFF's proscription against
Packit Service 2594b8
duplicate pointers: the normal strip/tile pointers are expected to point
Packit Service 2594b8
into the larger data area pointed to by JPEGInterchangeFormat.  All TIFF
Packit Service 2594b8
editing applications must be specifically aware of this relationship, since
Packit Service 2594b8
they must maintain it or else delete the JPEGInterchangeFormat field.  The
Packit Service 2594b8
JPEGxxTables fields are also likely to point into the JPEGInterchangeFormat
Packit Service 2594b8
area, creating additional pointer relationships that must be maintained.
Packit Service 2594b8
Packit Service 2594b8
* The JPEGQTables field is fixed at a byte per table entry; there is no
Packit Service 2594b8
way to support 16-bit quantization values.  This is a serious impediment
Packit Service 2594b8
to extending TIFF to use 12-bit JPEG.
Packit Service 2594b8
Packit Service 2594b8
* The 6.0 design cannot support using different quantization tables in
Packit Service 2594b8
different strips/tiles of an image (so as to encode some areas at higher
Packit Service 2594b8
quality than others).  Furthermore, since quantization tables are tied
Packit Service 2594b8
one-for-one to color components, the design cannot support table switching
Packit Service 2594b8
options that are likely to be added in future JPEG revisions.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Ambiguities
Packit Service 2594b8
-----------
Packit Service 2594b8
Packit Service 2594b8
Several incompatible interpretations are possible for 6.0's treatment of
Packit Service 2594b8
JPEG restart markers:
Packit Service 2594b8
Packit Service 2594b8
  * It is unclear whether restart markers must be omitted at TIFF segment
Packit Service 2594b8
    (strip/tile) boundaries, or whether they are optional.
Packit Service 2594b8
Packit Service 2594b8
  * It is unclear whether the segment size is required to be chosen as
Packit Service 2594b8
    a multiple of the specified restart interval (if any); perhaps the
Packit Service 2594b8
    JPEG codec is supposed to be reset at each segment boundary as if
Packit Service 2594b8
    there were a restart marker there, even if the boundary does not fall
Packit Service 2594b8
    at a multiple of the nominal restart interval.
Packit Service 2594b8
Packit Service 2594b8
  * The spec fails to address the question of restart marker numbering:
Packit Service 2594b8
    do the numbers begin again within each segment, or not?
Packit Service 2594b8
Packit Service 2594b8
That last point is particularly nasty.  If we make numbering begin again
Packit Service 2594b8
within each segment, we give up the ability to impose a TIFF strip/tile
Packit Service 2594b8
structure on an existing JPEG datastream with restarts (which was clearly a
Packit Service 2594b8
goal of Section 22's authors).  But the other choice interferes with random
Packit Service 2594b8
access to the image segments: a reader must compute the first restart
Packit Service 2594b8
number to be expected within a segment, and must have a way to reset its
Packit Service 2594b8
JPEG decoder to expect a nonzero restart number first.  This may not even
Packit Service 2594b8
be possible with some JPEG chips.
Packit Service 2594b8
Packit Service 2594b8
The tile height restriction found on page 104 contradicts Section 15's
Packit Service 2594b8
general description of tiles.  For an image that is not vertically
Packit Service 2594b8
downsampled, page 104 specifies a tile height of one MCU or 8 pixels; but
Packit Service 2594b8
Section 15 requires tiles to be a multiple of 16 pixels high.
Packit Service 2594b8
Packit Service 2594b8
This Tech Note does not attempt to resolve these ambiguities, so
Packit Service 2594b8
implementations that follow the 6.0 design should be aware that
Packit Service 2594b8
inter-application compatibility problems are likely to arise.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Unnecessary complexity
Packit Service 2594b8
----------------------
Packit Service 2594b8
Packit Service 2594b8
The 6.0 design creates problems for implementations that need to keep the
Packit Service 2594b8
JPEG codec separate from the TIFF control logic --- for example, consider
Packit Service 2594b8
using a JPEG chip that was not designed specifically for TIFF.  JPEG codecs
Packit Service 2594b8
generally want to produce or consume a standard ISO JPEG datastream, not
Packit Service 2594b8
just raw compressed data.  (If they were to handle raw data, a separate
Packit Service 2594b8
out-of-band mechanism would be needed to load tables into the codec.)
Packit Service 2594b8
With such a codec, the TIFF control logic must parse JPEG markers emitted
Packit Service 2594b8
by the codec to create the TIFF table fields (when writing) or synthesize
Packit Service 2594b8
JPEG markers from the TIFF fields to feed the codec (when reading).  This
Packit Service 2594b8
means that the control logic must know a great deal more about JPEG details
Packit Service 2594b8
than we would like.  The parsing and reconstruction of the markers also
Packit Service 2594b8
represents a fair amount of unnecessary work.
Packit Service 2594b8
Packit Service 2594b8
Quite a few implementors have proposed writing "TIFF/JPEG" files in which
Packit Service 2594b8
a standard JPEG datastream is simply dumped into the file and pointed to
Packit Service 2594b8
by JPEGInterchangeFormat.  To avoid parsing the JPEG datastream, they
Packit Service 2594b8
suggest not writing the JPEG auxiliary fields (JPEGxxTables etc) nor even
Packit Service 2594b8
the basic TIFF strip/tile data pointers.  This approach is incompatible
Packit Service 2594b8
with implementations that handle the full TIFF 6.0 JPEG design, since they
Packit Service 2594b8
will expect to find strip/tile pointers and auxiliary fields.  Indeed this
Packit Service 2594b8
is arguably not TIFF at all, since *all* TIFF-reading applications expect
Packit Service 2594b8
to find strip or tile pointers.  A subset implementation that is not
Packit Service 2594b8
upward-compatible with the full spec is clearly unacceptable.  However,
Packit Service 2594b8
the frequency with which this idea has come up makes it clear that
Packit Service 2594b8
implementors find the existing Section 22 too complex.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Overview of the solution
Packit Service 2594b8
========================
Packit Service 2594b8
Packit Service 2594b8
To solve these problems, we adopt a new design for embedding
Packit Service 2594b8
JPEG-compressed data in TIFF files.  The new design uses only complete,
Packit Service 2594b8
uninterpreted ISO JPEG datastreams, so it should be much more forgiving of
Packit Service 2594b8
extensions to the ISO standard.  It should also be far easier to implement
Packit Service 2594b8
using unmodified JPEG codecs.
Packit Service 2594b8
Packit Service 2594b8
To reduce overhead in multi-segment TIFF files, we allow JPEG overhead
Packit Service 2594b8
tables to be stored just once in a JPEGTables auxiliary field.  This
Packit Service 2594b8
feature does not violate the integrity of the JPEG datastreams, because it
Packit Service 2594b8
uses the notions of "tables-only datastreams" and "abbreviated image
Packit Service 2594b8
datastreams" as defined by the ISO standard.
Packit Service 2594b8
Packit Service 2594b8
To prevent confusion with the old design, the new design is given a new
Packit Service 2594b8
Compression tag value, Compression=7.  Readers that need to handle
Packit Service 2594b8
existing 6.0 JPEG files may read both old and new files, using whatever
Packit Service 2594b8
interpretation of the 6.0 spec they did before.  Compression tag value 6
Packit Service 2594b8
and the field tag numbers defined by 6.0 section 22 will remain reserved
Packit Service 2594b8
indefinitely, even though detailed descriptions of them will be dropped
Packit Service 2594b8
from future editions of the TIFF specification.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Replacement TIFF/JPEG specification
Packit Service 2594b8
===================================
Packit Service 2594b8
Packit Service 2594b8
[This section of the Tech Note is expected to replace Section 22 in the
Packit Service 2594b8
next release of the TIFF specification.]
Packit Service 2594b8
Packit Service 2594b8
This section describes TIFF compression scheme 7, a high-performance
Packit Service 2594b8
compression method for continuous-tone images.
Packit Service 2594b8
Packit Service 2594b8
Introduction
Packit Service 2594b8
------------
Packit Service 2594b8
Packit Service 2594b8
This TIFF compression method uses the international standard for image
Packit Service 2594b8
compression ISO/IEC 10918-1, usually known as "JPEG" (after the original
Packit Service 2594b8
name of the standards committee, Joint Photographic Experts Group).  JPEG
Packit Service 2594b8
is a joint ISO/CCITT standard for compression of continuous-tone images.
Packit Service 2594b8
Packit Service 2594b8
The JPEG committee decided that because of the broad scope of the standard,
Packit Service 2594b8
no one algorithmic procedure was able to satisfy the requirements of all
Packit Service 2594b8
applications.  Instead, the JPEG standard became a "toolkit" of multiple
Packit Service 2594b8
algorithms and optional capabilities.  Individual applications may select
Packit Service 2594b8
a subset of the JPEG standard that meets their requirements.
Packit Service 2594b8
Packit Service 2594b8
The most important distinction among the JPEG processes is between lossy
Packit Service 2594b8
and lossless compression.  Lossy compression methods provide high
Packit Service 2594b8
compression but allow only approximate reconstruction of the original
Packit Service 2594b8
image.  JPEG's lossy processes allow the encoder to trade off compressed
Packit Service 2594b8
file size against reconstruction fidelity over a wide range.  Typically,
Packit Service 2594b8
10:1 or more compression of full-color data can be obtained while keeping
Packit Service 2594b8
the reconstructed image visually indistinguishable from the original.  Much
Packit Service 2594b8
higher compression ratios are possible if a low-quality reconstructed image
Packit Service 2594b8
is acceptable.  Lossless compression provides exact reconstruction of the
Packit Service 2594b8
source data, but the achievable compression ratio is much lower than for
Packit Service 2594b8
the lossy processes; JPEG's rather simple lossless process typically
Packit Service 2594b8
achieves around 2:1 compression of full-color data.
Packit Service 2594b8
Packit Service 2594b8
The most widely implemented JPEG subset is the "baseline" JPEG process.
Packit Service 2594b8
This provides lossy compression of 8-bit-per-channel data.  Optional
Packit Service 2594b8
extensions include 12-bit-per-channel data, arithmetic entropy coding for
Packit Service 2594b8
better compression, and progressive/hierarchical representations.  The
Packit Service 2594b8
lossless process is an independent algorithm that has little in
Packit Service 2594b8
common with the lossy processes.
Packit Service 2594b8
Packit Service 2594b8
It should be noted that the optional arithmetic-coding extension is subject
Packit Service 2594b8
to several US and Japanese patents.  To avoid patent problems, use of
Packit Service 2594b8
arithmetic coding processes in TIFF files intended for inter-application
Packit Service 2594b8
interchange is discouraged.
Packit Service 2594b8
Packit Service 2594b8
All of the JPEG processes are useful only for "continuous tone" data,
Packit Service 2594b8
in which the difference between adjacent pixel values is usually small.
Packit Service 2594b8
Low-bit-depth source data is not appropriate for JPEG compression, nor
Packit Service 2594b8
are palette-color images good candidates.  The JPEG processes work well
Packit Service 2594b8
on grayscale and full-color data.
Packit Service 2594b8
Packit Service 2594b8
Describing the JPEG compression algorithms in sufficient detail to permit
Packit Service 2594b8
implementation would require more space than we have here.  Instead, we
Packit Service 2594b8
refer the reader to the References section.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
What data is being compressed?
Packit Service 2594b8
------------------------------
Packit Service 2594b8
Packit Service 2594b8
In lossy JPEG compression, it is customary to convert color source data
Packit Service 2594b8
to YCbCr and then downsample it before JPEG compression.  This gives
Packit Service 2594b8
2:1 data compression with hardly any visible image degradation, and it
Packit Service 2594b8
permits additional space savings within the JPEG compression step proper.
Packit Service 2594b8
However, these steps are not considered part of the ISO JPEG standard.
Packit Service 2594b8
The ISO standard is "color blind": it accepts data in any color space.
Packit Service 2594b8
Packit Service 2594b8
For TIFF purposes, the JPEG compression tag is considered to represent the
Packit Service 2594b8
ISO JPEG compression standard only.  The ISO standard is applied to the
Packit Service 2594b8
same data that would be stored in the TIFF file if no compression were
Packit Service 2594b8
used.  Therefore, if color conversion or downsampling are used, they must
Packit Service 2594b8
be reflected in the regular TIFF fields; these steps are not considered to
Packit Service 2594b8
be implicit in the JPEG compression tag value.  PhotometricInterpretation
Packit Service 2594b8
and related fields shall describe the color space actually stored in the
Packit Service 2594b8
file.  With the TIFF 6.0 field definitions, downsampling is permissible
Packit Service 2594b8
only for YCbCr data, and it must correspond to the YCbCrSubSampling field.
Packit Service 2594b8
(Note that the default value for this field is not 1,1; so the default for
Packit Service 2594b8
YCbCr is to apply downsampling!)  It is likely that future versions of TIFF
Packit Service 2594b8
will provide additional PhotometricInterpretation values and a more general
Packit Service 2594b8
way of defining subsampling, so as to allow more flexibility in
Packit Service 2594b8
JPEG-compressed files.  But that issue is not addressed in this Tech Note.
Packit Service 2594b8
Packit Service 2594b8
Implementors should note that many popular JPEG codecs
Packit Service 2594b8
(compressor/decompressors) provide automatic color conversion and
Packit Service 2594b8
downsampling, so that the application may supply full-size RGB data which
Packit Service 2594b8
is nonetheless converted to downsampled YCbCr.  This is an implementation
Packit Service 2594b8
convenience which does not excuse the TIFF control layer from its
Packit Service 2594b8
responsibility to know what is really going on.  The
Packit Service 2594b8
PhotometricInterpretation and subsampling fields written to the file must
Packit Service 2594b8
describe what is actually in the file.
Packit Service 2594b8
Packit Service 2594b8
A JPEG-compressed TIFF file will typically have PhotometricInterpretation =
Packit Service 2594b8
YCbCr and YCbCrSubSampling = [2,1] or [2,2], unless the source data was
Packit Service 2594b8
grayscale or CMYK.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Basic representation of JPEG-compressed images
Packit Service 2594b8
----------------------------------------------
Packit Service 2594b8
Packit Service 2594b8
JPEG compression works in either strip-based or tile-based TIFF files.
Packit Service 2594b8
Rather than repeating "strip or tile" constantly, we will use the term
Packit Service 2594b8
"segment" to mean either a strip or a tile.
Packit Service 2594b8
Packit Service 2594b8
When the Compression field has the value 7, each image segment contains
Packit Service 2594b8
a complete JPEG datastream which is valid according to the ISO JPEG
Packit Service 2594b8
standard (ISO/IEC 10918-1).  Any sequential JPEG process can be used,
Packit Service 2594b8
including lossless JPEG, but progressive and hierarchical processes are not
Packit Service 2594b8
supported.  Since JPEG is useful only for continuous-tone images, the
Packit Service 2594b8
PhotometricInterpretation of the image shall not be 3 (palette color) nor
Packit Service 2594b8
4 (transparency mask).  The bit depth of the data is also restricted as
Packit Service 2594b8
specified below.
Packit Service 2594b8
Packit Service 2594b8
Each image segment in a JPEG-compressed TIFF file shall contain a valid
Packit Service 2594b8
JPEG datastream according to the ISO JPEG standard's rules for
Packit Service 2594b8
interchange-format or abbreviated-image-format data.  The datastream shall
Packit Service 2594b8
contain a single JPEG frame storing that segment of the image.  The
Packit Service 2594b8
required JPEG markers within a segment are:
Packit Service 2594b8
	SOI	(must appear at very beginning of segment)
Packit Service 2594b8
	SOFn
Packit Service 2594b8
	SOS	(one for each scan, if there is more than one scan)
Packit Service 2594b8
	EOI	(must appear at very end of segment)
Packit Service 2594b8
The actual compressed data follows SOS; it may contain RSTn markers if DRI
Packit Service 2594b8
is used.
Packit Service 2594b8
Packit Service 2594b8
Additional JPEG "tables and miscellaneous" markers may appear between SOI
Packit Service 2594b8
and SOFn, between SOFn and SOS, and before each subsequent SOS if there is
Packit Service 2594b8
more than one scan.  These markers include:
Packit Service 2594b8
	DQT
Packit Service 2594b8
	DHT
Packit Service 2594b8
	DAC	(not to appear unless arithmetic coding is used)
Packit Service 2594b8
	DRI
Packit Service 2594b8
	APPn	(shall be ignored by TIFF readers)
Packit Service 2594b8
	COM	(shall be ignored by TIFF readers)
Packit Service 2594b8
DNL markers shall not be used in TIFF files.  Readers should abort if any
Packit Service 2594b8
other marker type is found, especially the JPEG reserved markers;
Packit Service 2594b8
occurrence of such a marker is likely to indicate a JPEG extension.
Packit Service 2594b8
Packit Service 2594b8
The tables/miscellaneous markers may appear in any order.  Readers are
Packit Service 2594b8
cautioned that although the SOFn marker refers to DQT tables, JPEG does not
Packit Service 2594b8
require those tables to precede the SOFn, only the SOS.  Missing-table
Packit Service 2594b8
checks should be made when SOS is reached.
Packit Service 2594b8
Packit Service 2594b8
If no JPEGTables field is used, then each image segment shall be a complete
Packit Service 2594b8
JPEG interchange datastream.  Each segment must define all the tables it
Packit Service 2594b8
references.  To allow readers to decode segments in any order, no segment
Packit Service 2594b8
may rely on tables being carried over from a previous segment.
Packit Service 2594b8
Packit Service 2594b8
When a JPEGTables field is used, image segments may omit tables that have
Packit Service 2594b8
been specified in the JPEGTables field.  Further details appear below.
Packit Service 2594b8
Packit Service 2594b8
The SOFn marker shall be of type SOF0 for strict baseline JPEG data, of
Packit Service 2594b8
type SOF1 for non-baseline lossy JPEG data, or of type SOF3 for lossless
Packit Service 2594b8
JPEG data.  (SOF9 or SOF11 would be used for arithmetic coding.)  All
Packit Service 2594b8
segments of a JPEG-compressed TIFF image shall use the same JPEG
Packit Service 2594b8
compression process, in particular the same SOFn type.
Packit Service 2594b8
Packit Service 2594b8
The data precision field of the SOFn marker shall agree with the TIFF
Packit Service 2594b8
BitsPerSample field.  (Note that when PlanarConfiguration=1, this implies
Packit Service 2594b8
that all components must have the same BitsPerSample value; when
Packit Service 2594b8
PlanarConfiguration=2, different components could have different bit
Packit Service 2594b8
depths.)  For SOF0 only precision 8 is permitted; for SOF1, precision 8 or
Packit Service 2594b8
12 is permitted; for SOF3, precisions 2 to 16 are permitted.
Packit Service 2594b8
Packit Service 2594b8
The image dimensions given in the SOFn marker shall agree with the logical
Packit Service 2594b8
dimensions of that particular strip or tile.  For strip images, the SOFn
Packit Service 2594b8
image width shall equal ImageWidth and the height shall equal RowsPerStrip,
Packit Service 2594b8
except in the last strip; its SOFn height shall equal the number of rows
Packit Service 2594b8
remaining in the ImageLength.  (In other words, no padding data is counted
Packit Service 2594b8
in the SOFn dimensions.)  For tile images, each SOFn shall have width
Packit Service 2594b8
TileWidth and height TileHeight; adding and removing any padding needed in
Packit Service 2594b8
the edge tiles is the concern of some higher level of the TIFF software.
Packit Service 2594b8
(The dimensional rules are slightly different when PlanarConfiguration=2,
Packit Service 2594b8
as described below.)
Packit Service 2594b8
Packit Service 2594b8
The ISO JPEG standard only permits images up to 65535 pixels in width or
Packit Service 2594b8
height, due to 2-byte fields in the SOFn markers.  In TIFF, this limits
Packit Service 2594b8
the size of an individual JPEG-compressed strip or tile, but the total
Packit Service 2594b8
image size can be greater.
Packit Service 2594b8
Packit Service 2594b8
The number of components in the JPEG datastream shall equal SamplesPerPixel
Packit Service 2594b8
for PlanarConfiguration=1, and shall be 1 for PlanarConfiguration=2.  The
Packit Service 2594b8
components shall be stored in the same order as they are described at the
Packit Service 2594b8
TIFF field level.  (This applies both to their order in the SOFn marker,
Packit Service 2594b8
and to the order in which they are scanned if multiple JPEG scans are
Packit Service 2594b8
used.)  The component ID bytes are arbitrary so long as each component
Packit Service 2594b8
within an image segment is given a distinct ID.  To avoid any possible
Packit Service 2594b8
confusion, we require that all segments of a TIFF image use the same ID
Packit Service 2594b8
code for a given component.
Packit Service 2594b8
Packit Service 2594b8
In PlanarConfiguration 1, the sampling factors given in SOFn markers shall
Packit Service 2594b8
agree with the sampling factors defined by the related TIFF fields (or with
Packit Service 2594b8
the default values that are specified in the absence of those fields).
Packit Service 2594b8
Packit Service 2594b8
When DCT-based JPEG is used in a strip TIFF file, RowsPerStrip is required
Packit Service 2594b8
to be a multiple of 8 times the largest vertical sampling factor, i.e., a
Packit Service 2594b8
multiple of the height of an interleaved MCU.  (For simplicity of
Packit Service 2594b8
specification, we require this even if the data is not actually
Packit Service 2594b8
interleaved.)  For example, if YCbCrSubSampling = [2,2] then RowsPerStrip
Packit Service 2594b8
must be a multiple of 16.  An exception to this rule is made for
Packit Service 2594b8
single-strip images (RowsPerStrip >= ImageLength): the exact value of
Packit Service 2594b8
RowsPerStrip is unimportant in that case.  This rule ensures that no data
Packit Service 2594b8
padding is needed at the bottom of a strip, except perhaps the last strip.
Packit Service 2594b8
Any padding required at the right edge of the image, or at the bottom of
Packit Service 2594b8
the last strip, is expected to occur internally to the JPEG codec.
Packit Service 2594b8
Packit Service 2594b8
When DCT-based JPEG is used in a tiled TIFF file, TileLength is required
Packit Service 2594b8
to be a multiple of 8 times the largest vertical sampling factor, i.e.,
Packit Service 2594b8
a multiple of the height of an interleaved MCU; and TileWidth is required
Packit Service 2594b8
to be a multiple of 8 times the largest horizontal sampling factor, i.e.,
Packit Service 2594b8
a multiple of the width of an interleaved MCU.  (For simplicity of
Packit Service 2594b8
specification, we require this even if the data is not actually
Packit Service 2594b8
interleaved.)  All edge padding required will therefore occur in the course
Packit Service 2594b8
of normal TIFF tile padding; it is not special to JPEG.
Packit Service 2594b8
Packit Service 2594b8
Lossless JPEG does not impose these constraints on strip and tile sizes,
Packit Service 2594b8
since it is not DCT-based.
Packit Service 2594b8
Packit Service 2594b8
Note that within JPEG datastreams, multibyte values appear in the MSB-first
Packit Service 2594b8
order specified by the JPEG standard, regardless of the byte ordering of
Packit Service 2594b8
the surrounding TIFF file.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
JPEGTables field
Packit Service 2594b8
----------------
Packit Service 2594b8
Packit Service 2594b8
The only auxiliary TIFF field added for Compression=7 is the optional
Packit Service 2594b8
JPEGTables field.  The purpose of JPEGTables is to predefine JPEG
Packit Service 2594b8
quantization and/or Huffman tables for subsequent use by JPEG image
Packit Service 2594b8
segments.  When this is done, these rather bulky tables need not be
Packit Service 2594b8
duplicated in each segment, thus saving space and processing time.
Packit Service 2594b8
JPEGTables may be used even in a single-segment file, although there is no
Packit Service 2594b8
space savings in that case.
Packit Service 2594b8
Packit Service 2594b8
JPEGTables:
Packit Service 2594b8
	Tag = 347 (15B.H)
Packit Service 2594b8
	Type = UNDEFINED
Packit Service 2594b8
	N = number of bytes in tables datastream, typically a few hundred
Packit Service 2594b8
JPEGTables provides default JPEG quantization and/or Huffman tables which
Packit Service 2594b8
are used whenever a segment datastream does not contain its own tables, as
Packit Service 2594b8
specified below.
Packit Service 2594b8
Packit Service 2594b8
Notice that the JPEGTables field is required to have type code UNDEFINED,
Packit Service 2594b8
not type code BYTE.  This is to cue readers that expanding individual bytes
Packit Service 2594b8
to short or long integers is not appropriate.  A TIFF reader will generally
Packit Service 2594b8
need to store the field value as an uninterpreted byte sequence until it is
Packit Service 2594b8
fed to the JPEG decoder.
Packit Service 2594b8
Packit Service 2594b8
Multibyte quantities within the tables follow the ISO JPEG convention of
Packit Service 2594b8
MSB-first storage, regardless of the byte ordering of the surrounding TIFF
Packit Service 2594b8
file.
Packit Service 2594b8
Packit Service 2594b8
When the JPEGTables field is present, it shall contain a valid JPEG
Packit Service 2594b8
"abbreviated table specification" datastream.  This datastream shall begin
Packit Service 2594b8
with SOI and end with EOI.  It may contain zero or more JPEG "tables and
Packit Service 2594b8
miscellaneous" markers, namely:
Packit Service 2594b8
	DQT
Packit Service 2594b8
	DHT
Packit Service 2594b8
	DAC	(not to appear unless arithmetic coding is used)
Packit Service 2594b8
	DRI
Packit Service 2594b8
	APPn	(shall be ignored by TIFF readers)
Packit Service 2594b8
	COM	(shall be ignored by TIFF readers)
Packit Service 2594b8
Since JPEG defines the SOI marker to reset the DAC and DRI state, these two
Packit Service 2594b8
markers' values cannot be carried over into any image datastream, and thus
Packit Service 2594b8
they are effectively no-ops in the JPEGTables field.  To avoid confusion,
Packit Service 2594b8
it is recommended that writers not place DAC or DRI markers in JPEGTables.
Packit Service 2594b8
However readers must properly skip over them if they appear.
Packit Service 2594b8
Packit Service 2594b8
When JPEGTables is present, readers shall load the table specifications
Packit Service 2594b8
contained in JPEGTables before processing image segment datastreams.
Packit Service 2594b8
Image segments may simply refer to these preloaded tables without defining
Packit Service 2594b8
them.  An image segment can still define and use its own tables, subject to
Packit Service 2594b8
the restrictions below.
Packit Service 2594b8
Packit Service 2594b8
An image segment may not redefine any table defined in JPEGTables.  (This
Packit Service 2594b8
restriction is imposed to allow readers to process image segments in random
Packit Service 2594b8
order without having to reload JPEGTables between segments.)  Therefore, use
Packit Service 2594b8
of JPEGTables divides the available table slots into two groups: "global"
Packit Service 2594b8
slots are defined in JPEGTables and may be used but not redefined by
Packit Service 2594b8
segments; "local" slots are available for local definition and use in each
Packit Service 2594b8
segment.  To permit random access, a segment may not reference any local
Packit Service 2594b8
tables that it does not itself define.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Special considerations for PlanarConfiguration 2
Packit Service 2594b8
------------------------------------------------
Packit Service 2594b8
Packit Service 2594b8
In PlanarConfiguration 2, each image segment contains data for only one
Packit Service 2594b8
color component.  To avoid confusing the JPEG codec, we wish the segments
Packit Service 2594b8
to look like valid single-channel (i.e., grayscale) JPEG datastreams.  This
Packit Service 2594b8
means that different rules must be used for the SOFn parameters.
Packit Service 2594b8
Packit Service 2594b8
In PlanarConfiguration 2, the dimensions given in the SOFn of a subsampled
Packit Service 2594b8
component shall be scaled down by the sampling factors compared to the SOFn
Packit Service 2594b8
dimensions that would be used in PlanarConfiguration 1.  This is necessary
Packit Service 2594b8
to match the actual number of samples stored in that segment, so that the
Packit Service 2594b8
JPEG codec doesn't complain about too much or too little data.  In strip
Packit Service 2594b8
TIFF files the computed dimensions may need to be rounded up to the next
Packit Service 2594b8
integer; in tiled files, the restrictions on tile size make this case
Packit Service 2594b8
impossible.
Packit Service 2594b8
Packit Service 2594b8
Furthermore, all SOFn sampling factors shall be given as 1.  (This is
Packit Service 2594b8
merely to avoid confusion, since the sampling factors in a single-channel
Packit Service 2594b8
JPEG datastream have no real effect.)
Packit Service 2594b8
Packit Service 2594b8
Any downsampling will need to happen externally to the JPEG codec, since
Packit Service 2594b8
JPEG sampling factors are defined with reference to the full-precision
Packit Service 2594b8
component.  In PlanarConfiguration 2, the JPEG codec will be working on
Packit Service 2594b8
only one component at a time and thus will have no reference component to
Packit Service 2594b8
downsample against.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Minimum requirements for TIFF/JPEG
Packit Service 2594b8
----------------------------------
Packit Service 2594b8
Packit Service 2594b8
ISO JPEG is a large and complex standard; most implementations support only
Packit Service 2594b8
a subset of it.  Here we define a "core" subset of TIFF/JPEG which readers
Packit Service 2594b8
must support to claim TIFF/JPEG compatibility.  For maximum
Packit Service 2594b8
cross-application compatibility, we recommend that writers confine
Packit Service 2594b8
themselves to this subset unless there is very good reason to do otherwise.
Packit Service 2594b8
Packit Service 2594b8
Use the ISO baseline JPEG process: 8-bit data precision, Huffman coding,
Packit Service 2594b8
with no more than 2 DC and 2 AC Huffman tables.  Note that this implies
Packit Service 2594b8
BitsPerSample = 8 for each component.  We recommend deviating from baseline
Packit Service 2594b8
JPEG only if 12-bit data precision or lossless coding is required.
Packit Service 2594b8
Packit Service 2594b8
Use no subsampling (all JPEG sampling factors = 1) for color spaces other
Packit Service 2594b8
than YCbCr.  (This is, in fact, required with the TIFF 6.0 field
Packit Service 2594b8
definitions, but may not be so in future revisions.)  For YCbCr, use one of
Packit Service 2594b8
the following choices:
Packit Service 2594b8
	YCbCrSubSampling field		JPEG sampling factors
Packit Service 2594b8
	1,1				1h1v, 1h1v, 1h1v
Packit Service 2594b8
	2,1				2h1v, 1h1v, 1h1v
Packit Service 2594b8
	2,2  (default value)		2h2v, 1h1v, 1h1v
Packit Service 2594b8
We recommend that RGB source data be converted to YCbCr for best compression
Packit Service 2594b8
results.  Other source data colorspaces should probably be left alone.
Packit Service 2594b8
Minimal readers need not support JPEG images with colorspaces other than
Packit Service 2594b8
YCbCr and grayscale (PhotometricInterpretation = 6 or 1).
Packit Service 2594b8
Packit Service 2594b8
A minimal reader also need not support JPEG YCbCr images with nondefault
Packit Service 2594b8
values of YCbCrCoefficients or YCbCrPositioning, nor with values of
Packit Service 2594b8
ReferenceBlackWhite other than [0,255,128,255,128,255].  (These values
Packit Service 2594b8
correspond to the RGB<=>YCbCr conversion specified by JFIF, which is widely
Packit Service 2594b8
implemented in JPEG codecs.)
Packit Service 2594b8
Packit Service 2594b8
Writers are reminded that a ReferenceBlackWhite field *must* be included
Packit Service 2594b8
when PhotometricInterpretation is YCbCr, because the default
Packit Service 2594b8
ReferenceBlackWhite values are inappropriate for YCbCr.
Packit Service 2594b8
Packit Service 2594b8
If any subsampling is used, PlanarConfiguration=1 is preferred to avoid the
Packit Service 2594b8
possibly-confusing requirements of PlanarConfiguration=2.  In any case,
Packit Service 2594b8
readers are not required to support PlanarConfiguration=2.
Packit Service 2594b8
Packit Service 2594b8
If possible, use a single interleaved scan in each image segment.  This is
Packit Service 2594b8
not legal JPEG if there are more than 4 SamplesPerPixel or if the sampling
Packit Service 2594b8
factors are such that more than 10 blocks would be needed per MCU; in that
Packit Service 2594b8
case, use a separate scan for each component.  (The recommended color
Packit Service 2594b8
spaces and sampling factors will not run into that restriction, so a
Packit Service 2594b8
minimal reader need not support more than one scan per segment.)
Packit Service 2594b8
Packit Service 2594b8
To claim TIFF/JPEG compatibility, readers shall support multiple-strip TIFF
Packit Service 2594b8
files and the optional JPEGTables field; it is not acceptable to read only
Packit Service 2594b8
single-datastream files.  Support for tiled TIFF files is strongly
Packit Service 2594b8
recommended but not required.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Other recommendations for implementors
Packit Service 2594b8
--------------------------------------
Packit Service 2594b8
Packit Service 2594b8
The TIFF tag Compression=7 guarantees only that the compressed data is
Packit Service 2594b8
represented as ISO JPEG datastreams.  Since JPEG is a large and evolving
Packit Service 2594b8
standard, readers should apply careful error checking to the JPEG markers
Packit Service 2594b8
to ensure that the compression process is within their capabilities.  In
Packit Service 2594b8
particular, to avoid being confused by future extensions to the JPEG
Packit Service 2594b8
standard, it is important to abort if unknown marker codes are seen.
Packit Service 2594b8
Packit Service 2594b8
The point of requiring that all image segments use the same JPEG process is
Packit Service 2594b8
to ensure that a reader need check only one segment to determine whether it
Packit Service 2594b8
can handle the image.  For example, consider a TIFF reader that has access
Packit Service 2594b8
to fast but restricted JPEG hardware, as well as a slower, more general
Packit Service 2594b8
software implementation.  It is desirable to check only one image segment
Packit Service 2594b8
to find out whether the fast hardware can be used.  Thus, writers should
Packit Service 2594b8
try to ensure that all segments of an image look as much "alike" as
Packit Service 2594b8
possible: there should be no variation in scan layout, use of options such
Packit Service 2594b8
as DRI, etc.  Ideally, segments will be processed identically except
Packit Service 2594b8
perhaps for using different local quantization or entropy-coding tables.
Packit Service 2594b8
Packit Service 2594b8
Writers should avoid including "noise" JPEG markers (COM and APPn markers).
Packit Service 2594b8
Standard TIFF fields provide a better way to transport any non-image data.
Packit Service 2594b8
Some JPEG codecs may change behavior if they see an APPn marker they
Packit Service 2594b8
think they understand; since the TIFF spec requires these markers to be
Packit Service 2594b8
ignored, this behavior is undesirable.
Packit Service 2594b8
Packit Service 2594b8
It is possible to convert an interchange-JPEG file (e.g., a JFIF file) to
Packit Service 2594b8
TIFF simply by dropping the interchange datastream into a single strip.
Packit Service 2594b8
(However, designers are reminded that the TIFF spec discourages huge
Packit Service 2594b8
strips; splitting the image is somewhat more work but may give better
Packit Service 2594b8
results.)  Conversion from TIFF to interchange JPEG is more complex.  A
Packit Service 2594b8
strip-based TIFF/JPEG file can be converted fairly easily if all strips use
Packit Service 2594b8
identical JPEG tables and no RSTn markers: just delete the overhead markers
Packit Service 2594b8
and insert RSTn markers between strips.  Converting tiled images is harder,
Packit Service 2594b8
since the data will usually not be in the right order (unless the tiles are
Packit Service 2594b8
only one MCU high).  This can still be done losslessly, but it will require
Packit Service 2594b8
undoing and redoing the entropy coding so that the DC coefficient
Packit Service 2594b8
differences can be updated.
Packit Service 2594b8
Packit Service 2594b8
There is no default value for JPEGTables: standard TIFF files must define all
Packit Service 2594b8
tables that they reference.  For some closed systems in which many files will
Packit Service 2594b8
have identical tables, it might make sense to define a default JPEGTables
Packit Service 2594b8
value to avoid actually storing the tables.  Or even better, invent a
Packit Service 2594b8
private field selecting one of N default JPEGTables settings, so as to allow
Packit Service 2594b8
for future expansion.  Either of these must be regarded as a private
Packit Service 2594b8
extension that will render the files unreadable by other applications.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
References
Packit Service 2594b8
----------
Packit Service 2594b8
Packit Service 2594b8
[1] Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
Packit Service 2594b8
Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
Packit Service 2594b8
Packit Service 2594b8
This is the best short technical introduction to the JPEG algorithms.
Packit Service 2594b8
It is a good overview but does not provide sufficiently detailed
Packit Service 2594b8
information to write an implementation.
Packit Service 2594b8
Packit Service 2594b8
[2] Pennebaker, William B. and Mitchell, Joan L.  "JPEG Still Image Data
Packit Service 2594b8
Compression Standard", Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
Packit Service 2594b8
638pp.
Packit Service 2594b8
Packit Service 2594b8
This textbook is by far the most complete exposition of JPEG in existence.
Packit Service 2594b8
It includes the full text of the ISO JPEG standards (DIS 10918-1 and draft
Packit Service 2594b8
DIS 10918-2).  No would-be JPEG implementor should be without it.
Packit Service 2594b8
Packit Service 2594b8
[3] ISO/IEC IS 10918-1, "Digital Compression and Coding of Continuous-tone
Packit Service 2594b8
Still Images, Part 1: Requirements and guidelines", February 1994.
Packit Service 2594b8
ISO/IEC DIS 10918-2, "Digital Compression and Coding of Continuous-tone
Packit Service 2594b8
Still Images, Part 2: Compliance testing", final approval expected 1994.
Packit Service 2594b8
Packit Service 2594b8
These are the official standards documents.  Note that the Pennebaker and
Packit Service 2594b8
Mitchell textbook is likely to be cheaper and more useful than the official
Packit Service 2594b8
standards.
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
Changes to Section 21: YCbCr Images
Packit Service 2594b8
===================================
Packit Service 2594b8
Packit Service 2594b8
[This section of the Tech Note clarifies section 21 to make clear the
Packit Service 2594b8
interpretation of image dimensions in a subsampled image.  Furthermore,
Packit Service 2594b8
the section is changed to allow the original image dimensions not to be
Packit Service 2594b8
multiples of the sampling factors.  This change is necessary to support use
Packit Service 2594b8
of JPEG compression on odd-size images.]
Packit Service 2594b8
Packit Service 2594b8
Add the following paragraphs to the Section 21 introduction (p. 89),
Packit Service 2594b8
just after the paragraph beginning "When a Class Y image is subsampled":
Packit Service 2594b8
Packit Service 2594b8
	In a subsampled image, it is understood that all TIFF image
Packit Service 2594b8
	dimensions are measured in terms of the highest-resolution
Packit Service 2594b8
	(luminance) component.  In particular, ImageWidth, ImageLength,
Packit Service 2594b8
	RowsPerStrip, TileWidth, TileLength, XResolution, and YResolution
Packit Service 2594b8
	are measured in luminance samples.
Packit Service 2594b8
Packit Service 2594b8
	RowsPerStrip, TileWidth, and TileLength are constrained so that
Packit Service 2594b8
	there are an integral number of samples of each component in a
Packit Service 2594b8
	complete strip or tile.  However, ImageWidth/ImageLength are not
Packit Service 2594b8
	constrained.  If an odd-size image is to be converted to subsampled
Packit Service 2594b8
	format, the writer should pad the source data to a multiple of the
Packit Service 2594b8
	sampling factors by replication of the last column and/or row, then
Packit Service 2594b8
	downsample.  The number of luminance samples actually stored in the
Packit Service 2594b8
	file will be a multiple of the sampling factors.  Conversely,
Packit Service 2594b8
	readers must ignore any extra data (outside the specified image
Packit Service 2594b8
	dimensions) after upsampling.
Packit Service 2594b8
Packit Service 2594b8
	When PlanarConfiguration=2, each strip or tile covers the same
Packit Service 2594b8
	image area despite subsampling; that is, the total number of strips
Packit Service 2594b8
	or tiles in the image is the same for each component.  Therefore
Packit Service 2594b8
	strips or tiles of the subsampled components contain fewer samples
Packit Service 2594b8
	than strips or tiles of the luminance component.
Packit Service 2594b8
Packit Service 2594b8
	If there are extra samples per pixel (see field ExtraSamples),
Packit Service 2594b8
	these data channels have the same number of samples as the
Packit Service 2594b8
	luminance component.
Packit Service 2594b8
Packit Service 2594b8
Rewrite the YCbCrSubSampling field description (pp 91-92) as follows
Packit Service 2594b8
(largely to eliminate possibly-misleading references to
Packit Service 2594b8
ImageWidth/ImageLength of the subsampled components):
Packit Service 2594b8
Packit Service 2594b8
	(first paragraph unchanged)
Packit Service 2594b8
Packit Service 2594b8
	The two elements of this field are defined as follows:
Packit Service 2594b8
Packit Service 2594b8
	Short 0: ChromaSubsampleHoriz:
Packit Service 2594b8
Packit Service 2594b8
	1 = there are equal numbers of luma and chroma samples horizontally.
Packit Service 2594b8
Packit Service 2594b8
	2 = there are twice as many luma samples as chroma samples
Packit Service 2594b8
	horizontally.
Packit Service 2594b8
Packit Service 2594b8
	4 = there are four times as many luma samples as chroma samples
Packit Service 2594b8
	horizontally.
Packit Service 2594b8
Packit Service 2594b8
	Short 1: ChromaSubsampleVert:
Packit Service 2594b8
Packit Service 2594b8
	1 = there are equal numbers of luma and chroma samples vertically.
Packit Service 2594b8
Packit Service 2594b8
	2 = there are twice as many luma samples as chroma samples
Packit Service 2594b8
	vertically.
Packit Service 2594b8
Packit Service 2594b8
	4 = there are four times as many luma samples as chroma samples
Packit Service 2594b8
	vertically.
Packit Service 2594b8
Packit Service 2594b8
	ChromaSubsampleVert shall always be less than or equal to
Packit Service 2594b8
	ChromaSubsampleHoriz.  Note that Cb and Cr have the same sampling
Packit Service 2594b8
	ratios.
Packit Service 2594b8
Packit Service 2594b8
	In a strip TIFF file, RowsPerStrip is required to be an integer
Packit Service 2594b8
	multiple of ChromaSubSampleVert (unless RowsPerStrip >=
Packit Service 2594b8
	ImageLength, in which case its exact value is unimportant).
Packit Service 2594b8
	If ImageWidth and ImageLength are not multiples of
Packit Service 2594b8
	ChromaSubsampleHoriz and ChromaSubsampleVert respectively, then the
Packit Service 2594b8
	source data shall be padded to the next integer multiple of these
Packit Service 2594b8
	values before downsampling.
Packit Service 2594b8
Packit Service 2594b8
	In a tiled TIFF file, TileWidth must be an integer multiple of
Packit Service 2594b8
	ChromaSubsampleHoriz and TileLength must be an integer multiple of
Packit Service 2594b8
	ChromaSubsampleVert.  Padding will occur to tile boundaries.
Packit Service 2594b8
Packit Service 2594b8
	The default values of this field are [ 2,2 ].  Thus, YCbCr data is
Packit Service 2594b8
	downsampled by default!
Packit Service 2594b8