Blame html/TIFFTechNote2.html

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