1<HTML> 2<HEAD> 3<TITLE> 4Modifying The TIFF Library 5</TITLE> 6</HEAD> 7<BODY BGCOLOR=white> 8<FONT FACE="Arial, Helvetica, Sans"> 9<H1> 10<IMG SRC=images/dave.gif WIDTH=107 HEIGHT=148 BORDER=2 ALIGN=left HSPACE=6> 11Modifying The TIFF Library 12</H1> 13 14 15<P> 16This chapter provides information about the internal structure of 17the library, how to control the configuration when building it, and 18how to add new support to the library. 19The following sections are found in this chapter: 20 21<UL> 22<LI><A HREF=#Config>Library Configuration</A> 23<LI><A HREF=#Portability>General Portability Comments</A> 24<LI><A HREF="#Types">Types and Portability</A> 25<LI><A HREF=#AddingTags>Adding New Tags</A> 26<LI><A HREF=#AddingCODECS>Adding New Builtin Codecs</A> 27<LI><A HREF=#AddingCODECTags>Adding New Codec-private Tags</A> 28<LI><A HREF=#Other>Other Comments</A> 29</UL> 30 31 32<A NAME="Config"><P><HR WIDTH=65% ALIGN=right><H3>Library Configuration</H3></A> 33 34Information on compiling the library is given 35<A HREF=build.html>elsewhere in this documentation</A>. 36This section describes the low-level mechanisms used to control 37the optional parts of the library that are configured at build 38time. Control is based on 39a collection of C defines that are specified either on the compiler 40command line or in a configuration file such as <TT>port.h</TT> 41(as generated by the <TT>configure</TT> script for UNIX systems) 42or <B>tiffconf.h</B>. 43 44<P> 45Configuration defines are split into three areas: 46<UL> 47<LI>those that control which compression schemes are 48 configured as part of the builtin codecs, 49<LI>those that control support for groups of tags that 50 are considered optional, and 51<LI>those that control operating system or machine-specific support. 52</UL> 53 54<P> 55If the define <TT>COMPRESSION_SUPPORT</TT> is <STRONG>not defined</STRONG> 56then a default set of compression schemes is automatically 57configured: 58<UL> 59<LI>CCITT Group 3 and 4 algorithms (compression codes 2, 3, 4, and 32771), 60<LI>the Macintosh PackBits algorithm (compression 32773), 61<LI>a Lempel-Ziv & Welch (LZW) algorithm (compression 5), 62<LI>a 4-bit run-length encoding scheme from ThunderScan (compression 32809), 63<LI>a 2-bit encoding scheme used by NeXT (compression 32766), and 64<LI>two experimental schemes intended for images with high dynamic range 65(compression 34676 and 34677). 66</UL> 67 68To override this behaviour define <TT>COMPRESSION_SUPPORT</TT> 69and then one or more additional defines to enable configuration of 70the appropriate codecs (see the table below); e.g. 71 72<UL><PRE> 73#define COMPRESSION_SUPPORT 74#define CCITT_SUPPORT 75#define PACKBITS_SUPPORT 76</PRE></UL> 77 78Several other compression schemes are configured separately from 79the default set because they depend on ancillary software 80packages that are not distributed with <TT>libtiff</TT>. 81 82<P> 83Support for JPEG compression is controlled by <TT>JPEG_SUPPORT</TT>. 84The JPEG codec that comes with <TT>libtiff</TT> is designed for 85use with release 5 or later of the Independent JPEG Group's freely 86available software distribution. 87This software can be retrieved from the directory 88<A HREF=ftp://ftp.uu.net/graphics/jpeg>ftp.uu.net:/graphics/jpeg/</A>. 89 90 91<P> 92<IMG SRC="images/info.gif" ALT="NOTE: " ALIGN=left HSPACE=8> 93<EM>Enabling JPEG support automatically enables support for 94the TIFF 6.0 colorimetry and YCbCr-related tags.</EM> 95 96<P> 97Experimental support for the deflate algorithm is controlled by 98<TT>DEFLATE_SUPPORT</TT>. 99The deflate codec that comes with <TT>libtiff</TT> is designed 100for use with version 0.99 or later of the freely available 101<TT>libz</TT> library written by Jean-loup Gailly and Mark Adler. 102The data format used by this library is described 103in the files 104<A HREF=ftp://ftp.uu.net/pub/archiving/zip/doc/zlib-3.1.doc>zlib-3.1.doc</A>, 105and 106<A HREF=ftp://ftp.uu.net/pub/archiving/zip/doc/deflate-1.1.doc>deflate-1.1.doc</A>, 107available in the directory 108<A HREF=ftp://ftp.uu.net/pub/archiving/zip/doc>ftp.uu.net:/pub/archiving/zip/doc</A>.</EM> 109The library can be retried from the directory 110<A HREF=ftp://ftp.uu.net/pub/archiving/zip/zlib/>ftp.uu.net:/pub/archiving/zip/zlib/</A> 111(or try <A HREF=ftp://quest.jpl.nasa.gov/beta/zlib/>quest.jpl.nasa.gov:/beta/zlib/</A>). 112 113<P> 114<IMG SRC="images/warning.gif" ALT="NOTE: " ALIGN=left HSPACE=8 VSPACE=6> 115<EM>The deflate algorithm is experimental. Do not expect 116to exchange files using this compression scheme; 117it is included only because the similar, and more common, 118LZW algorithm is claimed to be governed by licensing restrictions.</EM> 119 120 121<P> 122By default <B>tiffconf.h</B> defines 123<TT>COLORIMETRY_SUPPORT</TT>, 124<TT>YCBCR_SUPPORT</TT>, 125and 126<TT>CMYK_SUPPORT</TT>. 127 128<P> 129<TABLE BORDER CELLPADDING=3> 130 131<TR><TH ALIGN=left>Define</TH><TH ALIGN=left>Description</TH></TR> 132 133<TR> 134<TD VALIGN=top><TT>CCITT_SUPPORT</TT></TD> 135<TD>CCITT Group 3 and 4 algorithms (compression codes 2, 3, 4, 136 and 32771)</TD> 137</TR> 138 139<TR> 140<TD VALIGN=top><TT>PACKBITS_SUPPORT</TT></TD> 141<TD>Macintosh PackBits algorithm (compression 32773)</TD> 142</TR> 143 144<TR> 145<TD VALIGN=top><TT>LZW_SUPPORT</TT></TD> 146<TD>Lempel-Ziv & Welch (LZW) algorithm (compression 5)</TD> 147</TR> 148 149<TR> 150<TD VALIGN=top><TT>THUNDER_SUPPORT</TT></TD> 151<TD>4-bit 152run-length encoding scheme from ThunderScan (compression 32809)</TD> 153</TR> 154 155<TR> 156<TD VALIGN=top><TT>NEXT_SUPPORT</TT></TD> 157<TD>2-bit encoding scheme used by NeXT (compression 32766)</TD> 158</TR> 159 160<TR> 161<TD VALIGN=top><TT>OJPEG_SUPPORT</TT></TD> 162<TD>obsolete JPEG scheme defined in the 6.0 spec (compression 6)</TD> 163</TR> 164 165<TR> 166<TD VALIGN=top><TT>JPEG_SUPPORT</TT></TD> 167<TD>current JPEG scheme defined in TTN2 (compression 7)</TD> 168</TR> 169 170<TR> 171<TD VALIGN=top><TT>ZIP_SUPPORT</TT></TD> 172<TD>experimental Deflate scheme (compression 32946)</TD> 173</TR> 174 175<TR> 176<TD VALIGN=top><TT>PIXARLOG_SUPPORT</TT></TD> 177<TD>Pixar's compression scheme for high-resolution color images (compression 32909)</TD> 178</TR> 179 180<TR> 181<TD VALIGN=top><TT>SGILOG_SUPPORT</TT></TD> 182<TD>SGI's compression scheme for high-resolution color images (compression 34676 and 34677)</TD> 183</TR> 184 185<TR> 186<TD VALIGN=top><TT>COLORIMETRY_SUPPORT</TT></TD> 187<TD>support for the TIFF 6.0 colorimetry tags</TD> 188</TR> 189 190<TR> 191<TD VALIGN=top><TT>YCBCR_SUPPORT</TT></TD> 192<TD>support for the TIFF 6.0 YCbCr-related tags</TD> 193</TR> 194 195<TR> 196<TD VALIGN=top><TT>CMYK_SUPPORT</TT></TD> 197<TD>support for the TIFF 6.0 CMYK-related tags</TD> 198</TR> 199 200<TR> 201<TD VALIGN=top><TT>ICC_SUPPORT</TT></TD> 202<TD>support for the ICC Profile tag; see 203<I>The ICC Profile Format Specification</I>, 204Annex B.3 "Embedding ICC Profiles in TIFF Files"; 205available at 206<A HREF=http://www.color.org>http://www.color.org</A> 207</TD> 208</TR> 209 210</TABLE> 211 212 213<A NAME="Portability"><P><HR WIDTH=65% ALIGN=right><H3>General Portability Comments</H3></A> 214 215This software is developed on Silicon Graphics UNIX 216systems (big-endian, MIPS CPU, 32-bit ints, 217IEEE floating point). 218The <TT>configure</TT> shell script generates the appropriate 219include files and make files for UNIX systems. 220Makefiles exist for non-UNIX platforms that the 221code runs on -- this work has mostly been done by other people. 222 223<P> 224In general, the code is guaranteed to work only on SGI machines. 225In practice it is highly portable to any 32-bit or 64-bit system and much 226work has been done to insure portability to 16-bit systems. 227If you encounter portability problems please return fixes so 228that future distributions can be improved. 229 230<P> 231The software is written to assume an ANSI C compilation environment. 232If your compiler does not support ANSI function prototypes, <TT>const</TT>, 233and <TT><stdarg.h></TT> then you will have to make modifications to the 234software. In the past I have tried to support compilers without <TT>const</TT> 235and systems without <TT><stdarg.h></TT>, but I am 236<EM>no longer interested in these 237antiquated environments</EM>. With the general availability of 238the freely available GCC compiler, I 239see no reason to incorporate modifications to the software for these 240purposes. 241 242<P> 243An effort has been made to isolate as many of the 244operating system-dependencies 245as possible in two files: <B>tiffcomp.h</B> and 246<B>libtiff/tif_<os>.c</B>. The latter file contains 247operating system-specific routines to do I/O and I/O-related operations. 248The UNIX (<B>tif_unix.c</B>), 249Macintosh (<B>tif_apple.c</B>), 250and VMS (<B>tif_vms.c</B>) 251code has had the most use; 252the MS/DOS support (<B>tif_msdos.c</B>) assumes 253some level of UNIX system call emulation (i.e. 254<TT>open</TT>, 255<TT>read</TT>, 256<TT>write</TT>, 257<TT>fstat</TT>, 258<TT>malloc</TT>, 259<TT>free</TT>). 260 261<P> 262Native CPU byte order is determined on the fly by 263the library and does not need to be specified. 264The <TT>HOST_FILLORDER</TT> and <TT>HOST_BIGENDIAN</TT> 265definitions are not currently used, but may be employed by 266codecs for optimization purposes. 267 268<P> 269The following defines control general portability: 270 271<P> 272<TABLE BORDER CELLPADDING=3 WIDTH=100%> 273 274<TR> 275<TD VALIGN=top><TT>BSDTYPES</TT></TD> 276<TD>Define this if your system does NOT define the 277 usual BSD typedefs: <TT>u_char</TT>, 278 <TT>u_short</TT>, <TT>u_int</TT>, <TT>u_long</TT>.</TD> 279</TR> 280 281<TR> 282<TD VALIGN=top><TT>HAVE_IEEEFP</TT></TD> 283<TD>Define this as 0 or 1 according to the floating point 284 format suported by the machine. If your machine does 285 not support IEEE floating point then you will need to 286 add support to tif_machdep.c to convert between the 287 native format and IEEE format.</TD> 288</TR> 289 290<TR> 291<TD VALIGN=top><TT>HAVE_MMAP</TT></TD> 292<TD>Define this if there is <I>mmap-style</I> support for 293mapping files into memory (used only to read data).</TD> 294</TR> 295 296<TR> 297<TD VALIGN=top><TT>HOST_FILLORDER</TT></TD> 298<TD>Define the native CPU bit order: one of <TT>FILLORDER_MSB2LSB</TT> 299 or <TT>FILLORDER_LSB2MSB</TT></TD> 300</TR> 301 302<TR> 303<TD VALIGN=top><TT>HOST_BIGENDIAN</TT></TD> 304<TD>Define the native CPU byte order: 1 if big-endian (Motorola) 305 or 0 if little-endian (Intel); this may be used 306 in codecs to optimize code</TD> 307</TR> 308</TABLE> 309 310<P> 311On UNIX systems <TT>HAVE_MMAP</TT> is defined through the running of 312the <TT>configure</TT> script; otherwise support for memory-mapped 313files is disabled. 314Note that <B>tiffcomp.h</B> defines <TT>HAVE_IEEEFP</TT> to be 3151 (<TT>BSDTYPES</TT> is not defined). 316 317 318<A NAME="Types"><P><HR WIDTH=65% ALIGN=right><H3>Types and Portability</H3></A> 319 320The software makes extensive use of C typedefs to promote portability. 321Two sets of typedefs are used, one for communication with clients 322of the library and one for internal data structures and parsing of the 323TIFF format. There are interactions between these two to be careful 324of, but for the most part you should be able to deal with portability 325purely by fiddling with the following machine-dependent typedefs: 326 327 328<P> 329<TABLE BORDER CELLPADDING=3 WIDTH=100%> 330 331<TR> 332<TD>uint8</TD> 333<TD>8-bit unsigned integer</TD> 334<TD>tiff.h</TD> 335</TR> 336 337<TR> 338<TD>int8</TD> 339<TD>8-bit signed integer</TD> 340<TD>tiff.h</TD> 341</TR> 342 343<TR> 344<TD>uint16</TD> 345<TD>16-bit unsigned integer</TD> 346<TD>tiff.h</TD> 347</TR> 348 349<TR> 350<TD>int16</TD> 351<TD>16-bit signed integer</TD> 352<TD>tiff.h</TD> 353</TR> 354 355<TR> 356<TD>uint32</TD> 357<TD>32-bit unsigned integer</TD> 358<TD>tiff.h</TD> 359</TR> 360 361<TR> 362<TD>int32</TD> 363<TD>32-bit signed integer</TD> 364<TD>tiff.h</TD> 365</TR> 366 367<TR> 368<TD>dblparam_t</TD> 369<TD>promoted type for floats</TD> 370<TD>tiffcomp.h</TD> 371</TR> 372 373</TABLE> 374 375<P> 376(to clarify <TT>dblparam_t</TT>, it is the type that float parameters are 377promoted to when passed by value in a function call.) 378 379<P> 380The following typedefs are used throughout the library and interfaces 381to refer to certain objects whose size is dependent on the TIFF image 382structure: 383 384 385<P> 386<TABLE BORDER CELLPADDING=3 WIDTH=100%> 387 388<TR> 389<TD WIDTH=25%>typedef unsigned int ttag_t;</TD> <TD>directory tag</TD> 390</TR> 391 392<TR> 393<TD>typedef uint16 tdir_t;</TD> <TD>directory index</TD> 394</TR> 395 396<TR> 397<TD>typedef uint16 tsample_t;</TD> <TD>sample number</TD> 398</TR> 399 400<TR> 401<TD>typedef uint32 tstrip_t;</TD> <TD>strip number</TD> 402</TR> 403 404<TR> 405<TD>typedef uint32 ttile_t;</TD> <TD>tile number</TD> 406</TR> 407 408<TR> 409<TD>typedef int32 tsize_t;</TD> <TD>i/o size in bytes</TD> 410</TR> 411 412<TR> 413<TD>typedef void* tdata_t;</TD> <TD>image data ref</TD> 414</TR> 415 416<TR> 417<TD>typedef void* thandle_t;</TD> <TD>client data handle</TD> 418</TR> 419 420<TR> 421<TD>typedef int32 toff_t;</TD> <TD>file offset (should be off_t)</TD> 422</TR> 423 424<TR> 425<TD>typedef unsigned char* tidata_t;</TD> <TD>internal image data</TD> 426</TR> 427 428</TABLE> 429 430<P> 431Note that <TT>tstrip_t</TT>, <TT>ttile_t</TT>, and <TT>tsize_t</TT> 432are constrained to be 433no more than 32-bit quantities by 32-bit fields they are stored 434in in the TIFF image. Likewise <TT>tsample_t</TT> is limited by the 16-bit 435field used to store the <TT>SamplesPerPixel</TT> tag. <TT>tdir_t</TT> 436constrains 437the maximum number of IFDs that may appear in an image and may 438be an arbitrary size (without penalty). <TT>ttag_t</TT> must be either 439<TT>int</TT>, <TT>unsigned int</TT>, pointer, or <TT>double</TT> 440because the library uses a varargs 441interface and ANSI C restricts the type of the parameter before an 442ellipsis to be a promoted type. <TT>toff_t</TT> is defined as 443<TT>int32</TT> because 444TIFF file offsets are (unsigned) 32-bit quantities. A signed 445value is used because some interfaces return -1 on error (sigh). 446Finally, note that <TT>tidata_t</TT> is used internally to the library to 447manipulate internal data. User-specified data references are 448passed as opaque handles and only cast at the lowest layers where 449their type is presumed. 450 451 452<P><HR WIDTH=65% ALIGN=right><H3>General Comments</H3></A> 453 454The library is designed to hide as much of the details of TIFF from 455applications as 456possible. In particular, TIFF directories are read in their entirety 457into an internal format. Only the tags known by the library are 458available to a user and certain tag data may be maintained that a user 459does not care about (e.g. transfer function tables). 460 461<A NAME=AddingTags><P><HR WIDTH=65% ALIGN=right><H3>Adding New Tags</H3></A> 462 463To add support for a new directory tag you have three options. If your 464tag is specific to a compression algorithm, see below. If you have a lot 465of tags you may want to try using Niles Ritter's runtime tag-extension 466scheme in the "contrib/tags" directory, which makes the changes 467orthogonal to the main libtiff code. Otherwise use 468the following guidelines to add support to the ``core library''. 469 470<OL> 471<LI>Define the tag in <B>tiff.h</B>. 472<LI>Add a field to the directory structure in <B>tif_dir.h</B> 473 and define a <TT>FIELD_*</TT> bit (also update the definition of 474 <TT>FIELD_CODEC</TT> to reflect your addition). 475<LI>Add an entry in the <TT>TIFFFieldInfo</TT> array defined at the top of 476 <B>tif_dirinfo.c</B>. 477 Note that you must keep this array sorted by tag 478 number and that the widest variant entry for a tag should come 479 first (e.g. <TT>LONG</TT> before <TT>SHORT</TT>). 480<LI>Add entries in <TT>_TIFFVSetField()</TT> and <TT>_TIFFVGetField()</TT> 481 for the new tag. 482<LI>(<I>optional</I>) If the value associated with the tag is not a scalar value 483 (e.g. the array for <TT>TransferFunction</TT>) and requires 484 special processing, 485 then add the appropriate code to <TT>TIFFReadDirectory()</TT> and 486 <TT>TIFFWriteDirectory()</TT>. You're best off finding a similar tag and 487 cribbing code. 488<LI>Add support to <TT>TIFFPrintDirectory()</TT> in <B>tif_print.c</B> 489 to print the tag's value. 490</OL> 491 492<P> 493If you want to maintain portability, beware of making assumptions 494about data types. Use the typedefs (<TT>uint16</TT>, etc. when dealing with 495data on disk and <TT>t*_t</TT> when stuff is in memory) and be careful about 496passing items through printf or similar vararg interfaces. 497 498<A NAME=AddingCODECS><P><HR WIDTH=65% ALIGN=right><H3>Adding New Builtin Codecs</H3></A> 499 500To add builtin support for a new compression algorithm, you can either 501use the "tag-extension" trick to override the handling of the 502TIFF Compression tag (see <A HREF=#AddingTags>Adding New Tags</A>, above), 503or do the following to add support directly to the core library: 504 505<OL> 506<LI>Define the tag value in <B>tiff.h</B>. 507<LI>Edit the file <B>tif_codec.c</B> to add an entry to the 508 _TIFFBuiltinCODECS array (see how other algorithms are handled). 509<LI>Add the appropriate function prototype declaration to 510 <B>tiffiop.h</B> (close to the bottom). 511<LI>Create a file with the compression scheme code, by convention files 512 are named <B>tif_*.c</B> (except perhaps on some systems where the 513 tif_ prefix pushes some filenames over 14 chars. 514<LI>Edit <B>Makefile.in</B> (and any other Makefiles) 515 to include the new source file. 516</OL> 517 518<P> 519A codec, say <TT>foo</TT>, can have many different entry points: 520 521<PRE> 522TIFFInitfoo(tif, scheme)/* initialize scheme and setup entry points in tif */ 523fooSetupDecode(tif) /* called once per IFD after tags has been frozen */ 524fooPreDecode(tif, sample)/* called once per strip/tile, after data is read, 525 but before the first row is decoded */ 526fooDecode*(tif, bp, cc, sample)/* decode cc bytes of data into the buffer */ 527 fooDecodeRow(...) /* called to decode a single scanline */ 528 fooDecodeStrip(...) /* called to decode an entire strip */ 529 fooDecodeTile(...) /* called to decode an entire tile */ 530fooSetupEncode(tif) /* called once per IFD after tags has been frozen */ 531fooPreEncode(tif, sample)/* called once per strip/tile, before the first row in 532 a strip/tile is encoded */ 533fooEncode*(tif, bp, cc, sample)/* encode cc bytes of user data (bp) */ 534 fooEncodeRow(...) /* called to decode a single scanline */ 535 fooEncodeStrip(...) /* called to decode an entire strip */ 536 fooEncodeTile(...) /* called to decode an entire tile */ 537fooPostEncode(tif) /* called once per strip/tile, just before data is written */ 538fooSeek(tif, row) /* seek forwards row scanlines from the beginning 539 of a strip (row will always be >0 and <rows/strip */ 540fooCleanup(tif) /* called when compression scheme is replaced by user */ 541</PRE> 542 543<P> 544Note that the encoding and decoding variants are only needed when 545a compression algorithm is dependent on the structure of the data. 546For example, Group 3 2D encoding and decoding maintains a reference 547scanline. The sample parameter identifies which sample is to be 548encoded or decoded if the image is organized with <TT>PlanarConfig</TT>=2 549(separate planes). This is important for algorithms such as JPEG. 550If <TT>PlanarConfig</TT>=1 (interleaved), then sample will always be 0. 551 552 553<A NAME=AddingCODECTags><P><HR WIDTH=65% ALIGN=right><H3>Adding New Codec-private Tags</H3></A> 554 555To add tags that are meaningful <EM>only when a particular compression 556algorithm is used</EM> follow these steps: 557 558<OL> 559<LI>Define the tag in <B>tiff.h</B>. 560<LI>Allocate storage for the tag values in the private state block of 561 the codec. 562<LI>Insure the state block is created when the codec is initialized. 563<LI>At <TT>TIFFInitfoo</TT> time override the method pointers in the 564 TIFF structure 565 for getting, setting and printing tag values. For example, 566<PRE> 567 sp->vgetparent = tif->tif_vgetfield; 568 tif->tif_vgetfield = fooVGetField; /* hook for codec tags */ 569 sp->vsetparent = tif->tif_vsetfield; 570 tif->tif_vsetfield = fooVSetField; /* hook for codec tags */ 571 tif->tif_printdir = fooPrintDir; /* hook for codec tags */ 572</PRE> 573 (Actually you may decide not to override the 574 <TT>tif_printdir</TT> method, but rather just specify it). 575<LI>Create a private <TT>TIFFFieldInfo</TT> array for your tags and 576 merge them into the core tags at initialization time using 577 <TT>_TIFFMergeFieldInfo</TT>; e.g. 578<PRE> 579 _TIFFMergeFieldInfo(tif, fooFieldInfo, N(fooFieldInfo)); 580</PRE> 581 (where <TT>N</TT> is a macro used liberaly throughout the distributed code). 582<LI>Fill in the get and set routines. Be sure to call the parent method 583 for tags that you are not handled directly. Also be sure to set the 584 <TT>FIELD_*</TT> bits for tags that are to be written to the file. Note that 585 you can create ``pseudo-tags'' by defining tags that are processed 586 exclusively in the get/set routines and never written to file (see 587 the handling of <TT>TIFFTAG_FAXMODE</TT> in <B>tif_fax3.c</B> 588 for an example of this). 589<LI>Fill in the print routine, if appropriate. 590</OL> 591 592Note that space has been allocated in the <TT>FIELD_*</TT> bit space for 593codec-private tags. Define your bits as <TT>FIELD_CODEC+<offset></TT> to 594keep them away from the core tags. If you need more tags than there 595is room for, just increase <TT>FIELD_SETLONGS</TT> at the top of 596<B>tiffiop.h</B>. 597 598 599<A NAME=Other><P><HR WIDTH=65% ALIGN=right><H3>Other Comments</H3></A> 600 601The library handles most I/O buffering. There are two data buffers 602when decoding data: a raw data buffer that holds all the data in a 603strip, and a user-supplied scanline buffer that compression schemes 604place decoded data into. When encoding data the data in the 605user-supplied scanline buffer is encoded into the raw data buffer (from 606where it is written). Decoding routines should never have to explicitly 607read data -- a full strip/tile's worth of raw data is read and scanlines 608never cross strip boundaries. Encoding routines must be cognizant of 609the raw data buffer size and call <TT>TIFFFlushData1()</TT> when necessary. 610Note that any pending data is automatically flushed when a new strip/tile is 611started, so there's no need do that in the tif_postencode routine (if 612one exists). Bit order is automatically handled by the library when 613a raw strip or tile is filled. If the decoded samples are interpreted 614by the decoding routine before they are passed back to the user, then 615the decoding logic must handle byte-swapping by overriding the 616<TT>tif_postdecode</TT> 617routine (set it to <TT>TIFFNoPostDecode</TT>) and doing the required work 618internally. For an example of doing this look at the horizontal 619differencing code in the routines in <B>tif_predict.c</TT>. 620 621<P> 622The variables <TT>tif_rawcc</TT>, <TT>tif_rawdata</TT>, and 623<TT>tif_rawcp</TT> in a <TT>TIFF</TT> structure 624are associated with the raw data buffer. <TT>tif_rawcc</TT> must be non-zero 625for the library to automatically flush data. The variable 626<TT>tif_scanlinesize</TT> is the size a user's scanline buffer should be. The 627variable <TT>tif_tilesize</TT> is the size of a tile for tiled images. This 628should not normally be used by compression routines, except where it 629relates to the compression algorithm. That is, the <TT>cc</TT> parameter to the 630<TT>tif_decode*</TT> and <TT>tif_encode*</TT> 631routines should be used in terminating 632decompression/compression. This ensures these routines can be used, 633for example, to decode/encode entire strips of data. 634 635<P> 636In general, if you have a new compression algorithm to add, work from 637the code for an existing routine. In particular, 638<B>tif_dumpmode.c</B> 639has the trivial code for the "nil" compression scheme, 640<B>tif_packbits.c</B> is a 641simple byte-oriented scheme that has to watch out for buffer 642boundaries, and <B>tif_lzw.c</B> has the LZW scheme that has the most 643complexity -- it tracks the buffer boundary at a bit level. 644Of course, using a private compression scheme (or private tags) limits 645the portability of your TIFF files. 646 647<P> 648<HR> 649 650Last updated: $Date: 1999-08-13 19:30:57 $ 651 652</BODY> 653 654</HTML> 655