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