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