| 1 |
Modifying The TIFF Library |
| 2 |
========================== |
| 3 |
|
| 4 |
.. image:: images/dave.gif |
| 5 |
:width: 107 |
| 6 |
:alt: dave |
| 7 |
|
| 8 |
This chapter provides information about the internal structure of |
| 9 |
the library, how to control the configuration when building it, and |
| 10 |
how to add new support to the library. |
| 11 |
The following sections are found in this chapter: |
| 12 |
|
| 13 |
Library Configuration |
| 14 |
--------------------- |
| 15 |
|
| 16 |
Information on compiling the library is given :doc:`build` |
| 17 |
elsewhere in this documentation. |
| 18 |
This section describes the low-level mechanisms used to control |
| 19 |
the optional parts of the library that are configured at build |
| 20 |
time. Control is based on |
| 21 |
a collection of C defines that are specified either on the compiler |
| 22 |
command line or in a configuration file such as :file:`port.h` |
| 23 |
(as generated by the :program:`configure` script for UNIX systems) |
| 24 |
or :file:`tiffconf.h`. |
| 25 |
|
| 26 |
Configuration defines are split into three areas: |
| 27 |
|
| 28 |
* those that control which compression schemes are |
| 29 |
configured as part of the builtin codecs, |
| 30 |
* those that control support for groups of tags that |
| 31 |
are considered optional, and |
| 32 |
* those that control operating system or machine-specific support. |
| 33 |
|
| 34 |
If the define :c:macro:`COMPRESSION_SUPPORT` is **not defined** |
| 35 |
then a default set of compression schemes is automatically |
| 36 |
configured: |
| 37 |
|
| 38 |
* CCITT Group 3 and 4 algorithms (compression codes 2, 3, 4, and 32771), |
| 39 |
* the Macintosh PackBits algorithm (compression 32773), |
| 40 |
* a 4-bit run-length encoding scheme from ThunderScan (compression 32809), |
| 41 |
* a 2-bit encoding scheme used by NeXT (compression 32766), and |
| 42 |
* two experimental schemes intended for images with high dynamic range |
| 43 |
(compression 34676 and 34677). |
| 44 |
|
| 45 |
To override the default compression behaviour define |
| 46 |
:c:macro:`COMPRESSION_SUPPORT` and then one or more additional defines |
| 47 |
to enable configuration of the appropriate codecs (see the table |
| 48 |
below); e.g. |
| 49 |
|
| 50 |
.. highlight:: c |
| 51 |
|
| 52 |
:: |
| 53 |
|
| 54 |
#define COMPRESSION_SUPPORT |
| 55 |
#define CCITT_SUPPORT |
| 56 |
#define PACKBITS_SUPPORT |
| 57 |
|
| 58 |
Several other compression schemes are configured separately from |
| 59 |
the default set because they depend on ancillary software |
| 60 |
packages that are not distributed with LibTIFF. |
| 61 |
|
| 62 |
Support for JPEG compression is controlled by :c:macro:`JPEG_SUPPORT`. |
| 63 |
The JPEG codec that comes with LibTIFF is designed for |
| 64 |
use with release 5 or later of the Independent JPEG Group's freely |
| 65 |
available software distribution. |
| 66 |
This software can be retrieved from the directory |
| 67 |
`<ftp://ftp.uu.net/graphics/jpeg>`_. |
| 68 |
|
| 69 |
.. note:: |
| 70 |
|
| 71 |
Enabling JPEG support automatically enables support for |
| 72 |
the TIFF 6.0 colorimetry and YCbCr-related tags. |
| 73 |
|
| 74 |
Experimental support for the deflate algorithm is controlled by |
| 75 |
:c:macro:`DEFLATE_SUPPORT`. |
| 76 |
The deflate codec that comes with LibTIFF is designed |
| 77 |
for use with version 0.99 or later of the freely available |
| 78 |
``libz`` library written by Jean-loup Gailly and Mark Adler. |
| 79 |
The data format used by this library is described |
| 80 |
in the files |
| 81 |
`<ftp://ftp.uu.net/pub/archiving/zip/doc/zlib-3.1.doc>`_, |
| 82 |
and |
| 83 |
`<ftp://ftp.uu.net/pub/archiving/zip/doc/deflate-1.1.doc>`_, |
| 84 |
available in the directory |
| 85 |
`<ftp://ftp.uu.net/pub/archiving/zip/doc>`_.. |
| 86 |
The library can be retried from the directory |
| 87 |
`<ftp://ftp.uu.net/pub/archiving/zip/zlib>`_ |
| 88 |
(or try `<ftp://quest.jpl.nasa.gov/beta/zlib/>`_). |
| 89 |
|
| 90 |
.. warning:: |
| 91 |
|
| 92 |
**The deflate algorithm is experimental. Do not expect |
| 93 |
to exchange files using this compression scheme; |
| 94 |
it is included only because the similar, and more common, |
| 95 |
LZW algorithm is claimed to be governed by licensing restrictions.** |
| 96 |
|
| 97 |
By default :file:`tiffconf.h` defines |
| 98 |
:c:macro:`COLORIMETRY_SUPPORT`, |
| 99 |
:c:macro:`YCBCR_SUPPORT`, |
| 100 |
and |
| 101 |
:c:macro:`CMYK_SUPPORT`. |
| 102 |
|
| 103 |
|
| 104 |
.. list-table:: :file:`tiffconf.h` defines |
| 105 |
:widths: 5 20 |
| 106 |
:header-rows: 1 |
| 107 |
|
| 108 |
* - Define |
| 109 |
- Description |
| 110 |
|
| 111 |
* - :c:macro:`CCITT_SUPPORT` |
| 112 |
- CCITT Group 3 and 4 algorithms (compression codes 2, 3, 4, and 32771) |
| 113 |
|
| 114 |
* - :c:macro:`PACKBITS_SUPPORT` |
| 115 |
- Macintosh PackBits algorithm (compression 32773) |
| 116 |
|
| 117 |
* - :c:macro:`LZW_SUPPORT` |
| 118 |
- Lempel-Ziv & Welch (LZW) algorithm (compression 5) |
| 119 |
|
| 120 |
* - :c:macro:`THUNDER_SUPPORT` |
| 121 |
- 4-bit run-length encoding scheme from ThunderScan (compression 32809) |
| 122 |
|
| 123 |
* - :c:macro:`NEXT_SUPPORT` |
| 124 |
- 2-bit encoding scheme used by NeXT (compression 32766) |
| 125 |
|
| 126 |
* - :c:macro:`OJPEG_SUPPORT` |
| 127 |
- obsolete JPEG scheme defined in the 6.0 spec (compression 6) |
| 128 |
|
| 129 |
* - :c:macro:`JPEG_SUPPORT` |
| 130 |
- current JPEG scheme defined in TTN2 (compression 7) |
| 131 |
|
| 132 |
* - :c:macro:`ZIP_SUPPORT` |
| 133 |
- experimental Deflate scheme (compression 32946) |
| 134 |
|
| 135 |
* - :c:macro:`PIXARLOG_SUPPORT` |
| 136 |
- Pixar's compression scheme for high-resolution color images (compression 32909) |
| 137 |
|
| 138 |
* - :c:macro:`SGILOG_SUPPORT` |
| 139 |
- SGI's compression scheme for high-resolution color images (compression 34676 and 34677) |
| 140 |
|
| 141 |
* - :c:macro:`COLORIMETRY_SUPPORT` |
| 142 |
- support for the TIFF 6.0 colorimetry tags |
| 143 |
|
| 144 |
* - :c:macro:`YCBCR_SUPPORT` |
| 145 |
- support for the TIFF 6.0 YCbCr-related tags |
| 146 |
|
| 147 |
* - :c:macro:`CMYK_SUPPORT` |
| 148 |
- support for the TIFF 6.0 CMYK-related tags |
| 149 |
|
| 150 |
* - :c:macro:`ICC_SUPPORT` |
| 151 |
- support for the ICC Profile tag; see |
| 152 |
*The ICC Profile Format Specification*, |
| 153 |
Annex B.3 "Embedding ICC Profiles in TIFF Files"; |
| 154 |
available at |
| 155 |
`<http://www.color.org/>`_ |
| 156 |
|
| 157 |
General Portability Comments |
| 158 |
---------------------------- |
| 159 |
|
| 160 |
This software is developed on Silicon Graphics UNIX |
| 161 |
systems (big-endian, MIPS CPU, 32-bit ints, |
| 162 |
IEEE floating point). |
| 163 |
The :program:`configure` shell script generates the appropriate |
| 164 |
include files and make files for UNIX systems. |
| 165 |
Makefiles exist for non-UNIX platforms that the |
| 166 |
code runs on---this work has mostly been done by other people. |
| 167 |
|
| 168 |
In general, the code is guaranteed to work only on SGI machines. |
| 169 |
In practice it is highly portable to any 32-bit or 64-bit system and much |
| 170 |
work has been done to insure portability to 16-bit systems. |
| 171 |
If you encounter portability problems please return fixes so |
| 172 |
that future distributions can be improved. |
| 173 |
|
| 174 |
The software is written to assume an ANSI C compilation environment. |
| 175 |
If your compiler does not support ANSI function prototypes, ``const``, |
| 176 |
and :file:`<stdarg.h>` then you will have to make modifications to the |
| 177 |
software. In the past I have tried to support compilers without ``const`` |
| 178 |
and systems without :file:`<stdarg.h>`, but I am |
| 179 |
**no longer interested in these |
| 180 |
antiquated environments**. With the general availability of |
| 181 |
the freely available GCC compiler, I |
| 182 |
see no reason to incorporate modifications to the software for these |
| 183 |
purposes. |
| 184 |
|
| 185 |
An effort has been made to isolate as many of the |
| 186 |
operating system-dependencies |
| 187 |
as possible in two files: :file:`tiffcomp.h` and |
| 188 |
:file:`libtiff/tif_<os>.c`. The latter file contains |
| 189 |
operating system-specific routines to do I/O and I/O-related operations. |
| 190 |
The UNIX (:file:`tif_unix.c`) code has had the most use. |
| 191 |
|
| 192 |
Native CPU byte order is determined through the :c:macro:`WORDS_BIGENDIAN` |
| 193 |
definition, which must be set to the appropriate value in :file:`tif_config.h` |
| 194 |
by the build system. |
| 195 |
|
| 196 |
The following defines control general portability: |
| 197 |
|
| 198 |
.. list-table:: Portability defines |
| 199 |
:widths: 5 20 |
| 200 |
:header-rows: 1 |
| 201 |
|
| 202 |
* - Define |
| 203 |
- Description |
| 204 |
|
| 205 |
* - :c:macro:`BSDTYPES` |
| 206 |
- Define this if your system does **not** define the |
| 207 |
usual BSD typedefs: :c:type:`u_char`, |
| 208 |
:c:type:`u_short`, :c:type:`u_int`, :c:type:`u_long`. |
| 209 |
|
| 210 |
* - :c:macro:`HAVE_IEEEFP` |
| 211 |
- Define this as 0 or 1 according to the floating point |
| 212 |
format supported by the machine. If your machine does |
| 213 |
not support IEEE floating point then you will need to |
| 214 |
add support to tif_machdep.c to convert between the |
| 215 |
native format and IEEE format. |
| 216 |
|
| 217 |
* - :c:macro:`HAVE_MMAP` |
| 218 |
- Define this if there is *mmap-style* support for |
| 219 |
mapping files into memory (used only to read data). |
| 220 |
|
| 221 |
* - :c:macro:`HOST_FILLORDER` |
| 222 |
- Define the native CPU bit order: one of :c:macro:`FILLORDER_MSB2LSB` |
| 223 |
or :c:macro:`FILLORDER_LSB2MSB` |
| 224 |
|
| 225 |
* - :c:macro:`HOST_BIGENDIAN` |
| 226 |
- Define the native CPU byte order: 1 if big-endian (Motorola) |
| 227 |
or 0 if little-endian (Intel); this may be used |
| 228 |
in codecs to optimize code |
| 229 |
|
| 230 |
On UNIX systems :c:macro:`HAVE_MMAP` is defined through the running of |
| 231 |
the :program:`configure` script; otherwise support for memory-mapped |
| 232 |
files is disabled. |
| 233 |
Note that :file:`tiffcomp.h` defines :c:macro:`HAVE_IEEEFP` to be |
| 234 |
1 (:c:macro:`BSDTYPES` is not defined). |
| 235 |
|
| 236 |
Types and Portability |
| 237 |
--------------------- |
| 238 |
|
| 239 |
The software makes extensive use of C typedefs to promote portability. |
| 240 |
Two sets of typedefs are used, one for communication with clients |
| 241 |
of the library and one for internal data structures and parsing of the |
| 242 |
TIFF format. There are interactions between these two to be careful |
| 243 |
of, but for the most part you should be able to deal with portability |
| 244 |
purely by fiddling with the following machine-dependent typedefs: |
| 245 |
|
| 246 |
.. list-table:: Portability typedefs |
| 247 |
:widths: 5 15 5 |
| 248 |
:header-rows: 1 |
| 249 |
|
| 250 |
* - Typedef |
| 251 |
- Description |
| 252 |
- Header |
| 253 |
|
| 254 |
* - :c:type:`uint8_t` |
| 255 |
- 8-bit unsigned integer |
| 256 |
- :file:`tiff.h` |
| 257 |
|
| 258 |
* - :c:type:`int8_t` |
| 259 |
- 8-bit signed integer |
| 260 |
- :file:`tiff.h` |
| 261 |
|
| 262 |
* - :c:type:`uint16_t` |
| 263 |
- 16-bit unsigned integer |
| 264 |
- :file:`tiff.h` |
| 265 |
|
| 266 |
* - :c:type:`int16_t` |
| 267 |
- 16-bit signed integer |
| 268 |
- :file:`tiff.h` |
| 269 |
|
| 270 |
* - :c:type:`uint32_t` |
| 271 |
- 32-bit unsigned integer |
| 272 |
- :file:`tiff.h` |
| 273 |
|
| 274 |
* - :c:type:`int32_t` |
| 275 |
- 32-bit signed integer |
| 276 |
- :file:`tiff.h` |
| 277 |
|
| 278 |
* - :c:type:`dblparam_t` |
| 279 |
- promoted type for floats |
| 280 |
- :file:`tiffcomp.h` |
| 281 |
|
| 282 |
(to clarify :c:type:`dblparam_t`, it is the type that float parameters are |
| 283 |
promoted to when passed by value in a function call.) |
| 284 |
|
| 285 |
The following typedefs are used throughout the library and interfaces |
| 286 |
to refer to certain objects whose size is dependent on the TIFF image |
| 287 |
structure: |
| 288 |
|
| 289 |
.. list-table:: TIFF image typedefs |
| 290 |
:widths: 10 10 10 |
| 291 |
:header-rows: 1 |
| 292 |
|
| 293 |
* - Typedef |
| 294 |
- Type |
| 295 |
- Description |
| 296 |
|
| 297 |
|
| 298 |
* - :c:type:`ttag_t` |
| 299 |
- :c:expr:`unsigned int` |
| 300 |
- directory tag |
| 301 |
|
| 302 |
* - :c:type:`tdir_t` |
| 303 |
- :c:type:`uint16_t` |
| 304 |
- directory index |
| 305 |
|
| 306 |
* - :c:type:`tsample_t` |
| 307 |
- :c:type:`uint16_t` |
| 308 |
- sample number |
| 309 |
|
| 310 |
* - :c:type:`tstrip_t` |
| 311 |
- :c:type:`uint32_t` |
| 312 |
- strip number |
| 313 |
|
| 314 |
* - :c:type:`ttile_t` |
| 315 |
- :c:type:`uint32_t` |
| 316 |
- tile number |
| 317 |
|
| 318 |
* - :c:type:`tsize_t` |
| 319 |
- :c:type:`int32_t` |
| 320 |
- i/o size in bytes |
| 321 |
|
| 322 |
* - :c:type:`tdata_t` |
| 323 |
- :c:expr:`void *` |
| 324 |
- image data ref |
| 325 |
|
| 326 |
* - :c:type:`thandle_t` |
| 327 |
- :c:expr:`void *` |
| 328 |
- client data handle |
| 329 |
|
| 330 |
* - :c:type:`toff_t` |
| 331 |
- :c:type:`int32_t` |
| 332 |
- file offset (should be off_t) |
| 333 |
|
| 334 |
* - :c:type:`tidata_t` |
| 335 |
- :c:expr:`unsigned char *` |
| 336 |
- internal image data |
| 337 |
|
| 338 |
Note that :c:type:`tstrip_t`, :c:type:`ttile_t`, and :c:type:`tsize_t` |
| 339 |
are constrained to be |
| 340 |
no more than 32-bit quantities by 32-bit fields they are stored |
| 341 |
in in the TIFF image. Likewise :c:type:`tsample_t` is limited by the 16-bit |
| 342 |
field used to store the ``SamplesPerPixel`` tag. :c:type:`tdir_t` |
| 343 |
constrains |
| 344 |
the maximum number of IFDs that may appear in an image and may |
| 345 |
be an arbitrary size (without penalty). :c:type:`ttag_t` must be either |
| 346 |
:c:expr:`int`, :c:expr:`unsigned int`, pointer, or :c:expr:`double` |
| 347 |
because the library uses a varargs |
| 348 |
interface and ANSI C restricts the type of the parameter before an |
| 349 |
ellipsis to be a promoted type. :c:type:`toff_t` is defined as |
| 350 |
:c:type:`int32_t` because |
| 351 |
TIFF file offsets are (unsigned) 32-bit quantities. A signed |
| 352 |
value is used because some interfaces return -1 on error (sigh). |
| 353 |
Finally, note that :c:type:`tidata_t` is used internally to the library to |
| 354 |
manipulate internal data. User-specified data references are |
| 355 |
passed as opaque handles and only cast at the lowest layers where |
| 356 |
their type is presumed. |
| 357 |
|
| 358 |
|
| 359 |
General Comments |
| 360 |
---------------- |
| 361 |
|
| 362 |
The library is designed to hide as much of the details of TIFF from |
| 363 |
applications as |
| 364 |
possible. In particular, TIFF directories are read in their entirety |
| 365 |
into an internal format. Only the tags known by the library are |
| 366 |
available to a user and certain tag data may be maintained that a user |
| 367 |
does not care about (e.g. transfer function tables). |
| 368 |
|
| 369 |
Adding New Builtin Codecs |
| 370 |
------------------------- |
| 371 |
|
| 372 |
To add builtin support for a new compression algorithm, you can either |
| 373 |
use the "tag-extension" trick to override the handling of the |
| 374 |
TIFF Compression tag (see :doc:`addingtags`), |
| 375 |
or do the following to add support directly to the core library: |
| 376 |
|
| 377 |
* Define the tag value in :file:`tiff.h`. |
| 378 |
* Edit the file :file:`tif_codec.c` to add an entry to the |
| 379 |
:c:var:`_TIFFBuiltinCODECS` array (see how other algorithms are handled). |
| 380 |
* Add the appropriate function prototype declaration to |
| 381 |
:file:`tiffiop.h` (close to the bottom). |
| 382 |
* Create a file with the compression scheme code, by convention files |
| 383 |
are named :file:`tif_*.c` (except perhaps on some systems where the |
| 384 |
``tif_`` prefix pushes some filenames over 14 chars. |
| 385 |
* Update build configuration to include new source file. |
| 386 |
|
| 387 |
A codec, say ``foo``, can have many different entry points: |
| 388 |
|
| 389 |
:: |
| 390 |
|
| 391 |
TIFFInitfoo(tif, scheme) /* initialize scheme and setup entry points in tif */ |
| 392 |
fooSetupDecode(tif) /* called once per IFD after tags has been frozen */ |
| 393 |
fooPreDecode(tif, sample) /* called once per strip/tile, after data is read, |
| 394 |
but before the first row is decoded */ |
| 395 |
fooDecode*(tif, bp, cc, sample) /* decode cc bytes of data into the buffer */ |
| 396 |
fooDecodeRow(...) /* called to decode a single scanline */ |
| 397 |
fooDecodeStrip(...) /* called to decode an entire strip */ |
| 398 |
fooDecodeTile(...) /* called to decode an entire tile */ |
| 399 |
fooSetupEncode(tif) /* called once per IFD after tags has been frozen */ |
| 400 |
fooPreEncode(tif, sample) /* called once per strip/tile, before the first row in |
| 401 |
a strip/tile is encoded */ |
| 402 |
fooEncode*(tif, bp, cc, sample)/* encode cc bytes of user data (bp) */ |
| 403 |
fooEncodeRow(...) /* called to decode a single scanline */ |
| 404 |
fooEncodeStrip(...) /* called to decode an entire strip */ |
| 405 |
fooEncodeTile(...) /* called to decode an entire tile */ |
| 406 |
fooPostEncode(tif) /* called once per strip/tile, just before data is written */ |
| 407 |
fooSeek(tif, row) /* seek forwards row scanlines from the beginning |
| 408 |
of a strip (row will always be <0 and >rows/strip */ |
| 409 |
fooCleanup(tif) /* called when compression scheme is replaced by user */ |
| 410 |
|
| 411 |
Note that the encoding and decoding variants are only needed when |
| 412 |
a compression algorithm is dependent on the structure of the data. |
| 413 |
For example, Group 3 2D encoding and decoding maintains a reference |
| 414 |
scanline. The sample parameter identifies which sample is to be |
| 415 |
encoded or decoded if the image is organized with ``PlanarConfig=2`` |
| 416 |
(separate planes). This is important for algorithms such as JPEG. |
| 417 |
If ``PlanarConfig=1`` (interleaved), then sample will always be 0. |
| 418 |
|
| 419 |
Other Comments |
| 420 |
-------------- |
| 421 |
|
| 422 |
The library handles most I/O buffering. There are two data buffers |
| 423 |
when decoding data: a raw data buffer that holds all the data in a |
| 424 |
strip, and a user-supplied scanline buffer that compression schemes |
| 425 |
place decoded data into. When encoding data the data in the |
| 426 |
user-supplied scanline buffer is encoded into the raw data buffer (from |
| 427 |
where it is written). Decoding routines should never have to explicitly |
| 428 |
read data -- a full strip/tile's worth of raw data is read and scanlines |
| 429 |
never cross strip boundaries. Encoding routines must be cognizant of |
| 430 |
the raw data buffer size and call :c:func:`TIFFFlushData1` when necessary. |
| 431 |
Note that any pending data is automatically flushed when a new strip/tile is |
| 432 |
started, so there's no need do that in the tif_postencode routine (if |
| 433 |
one exists). Bit order is automatically handled by the library when |
| 434 |
a raw strip or tile is filled. If the decoded samples are interpreted |
| 435 |
by the decoding routine before they are passed back to the user, then |
| 436 |
the decoding logic must handle byte-swapping by overriding the |
| 437 |
:c:member:`tif_postdecode` |
| 438 |
routine (set it to :c:func:`TIFFNoPostDecode`) and doing the required work |
| 439 |
internally. For an example of doing this look at the horizontal |
| 440 |
differencing code in the routines in :file:`tif_predict.c`. |
| 441 |
|
| 442 |
The variables :c:member:`tif_rawcc`, :c:member:`tif_rawdata`, and |
| 443 |
:c:member:`tif_rawcp` in a :c:struct:`TIFF` structure |
| 444 |
are associated with the raw data buffer. :c:member:`tif_rawcc` must be non-zero |
| 445 |
for the library to automatically flush data. The variable |
| 446 |
:c:member:`tif_scanlinesize` is the size a user's scanline buffer should be. The |
| 447 |
variable :c:member:`tif_tilesize` is the size of a tile for tiled images. This |
| 448 |
should not normally be used by compression routines, except where it |
| 449 |
relates to the compression algorithm. That is, the ``cc`` parameter to the |
| 450 |
:c:expr:`tif_decode*` and :c:expr:`tif_encode*` |
| 451 |
routines should be used in terminating |
| 452 |
decompression/compression. This ensures these routines can be used, |
| 453 |
for example, to decode/encode entire strips of data. |
| 454 |
|
| 455 |
In general, if you have a new compression algorithm to add, work from |
| 456 |
the code for an existing routine. In particular, |
| 457 |
:file:`tif_dumpmode.c` |
| 458 |
has the trivial code for the "nil" compression scheme, |
| 459 |
:file:`tif_packbits.c` is a |
| 460 |
simple byte-oriented scheme that has to watch out for buffer |
| 461 |
boundaries, and :file:`tif_lzw.c` has the LZW scheme that has the most |
| 462 |
complexity -- it tracks the buffer boundary at a bit level. |
| 463 |
Of course, using a private compression scheme (or private tags) limits |
| 464 |
the portability of your TIFF files. |