Blame html/TIFFTechNote2.html

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