1<HTML> 2<HEAD> 3<TITLE> 4Using The TIFF Library 5</TITLE> 6</HEAD> 7<BODY BGCOLOR=WHITE> 8<FONT FACE="Arial, Helvetica, Sans"> 9<H1> 10<IMG SRC=images/cat.gif WIDTH=113 HEIGHT=146 BORDER=2 ALIGN=left HSPACE=6> 11Using The TIFF Library 12</H1> 13 14<P> 15<TT>libtiff</TT> is a set of C functions (a library) that support 16the manipulation of TIFF image files. 17The library requires an ANSI C compilation environment for building 18and presumes an ANSI C environment for use. 19 20<P> 21<TT>libtiff</TT> 22provides interfaces to image data at several layers of abstraction (and cost). 23At the highest level image data can be read into an 8-bit/sample, 24ABGR pixel raster format without regard for the underlying data organization, 25colorspace, or compression scheme. Below this high-level interface 26the library provides scanline-, strip-, and tile-oriented interfaces that 27return data decompressed but otherwise untransformed. These interfaces 28require that the application first identify the organization of stored 29data and select either a strip-based or tile-based API for manipulating 30data. At the lowest level the library 31provides access to the raw uncompressed strips or tiles, 32returning the data exactly as it appears in the file. 33 34<P> 35The material presented in this chapter is a basic introduction 36to the capabilities of the library; it is not an attempt to describe 37everything a developer needs to know about the library or about TIFF. 38Detailed information on the interfaces to the library are given in 39the <A HREF="http://www.remotesensing.org/libtiff/man/index.html"> 40UNIX manual pages</A> that accompany this software. 41 42<P>Michael Still has also written a useful introduction to libtiff for the 43IBM DeveloperWorks site available at 44<a href="http://www.ibm.com/developerworks/linux/library/l-libtiff"> 45http://www.ibm.com/developerworks/linux/library/l-libtiff</a>. 46 47<P> 48The following sections are found in this chapter: 49 50<UL> 51<LI><A HREF=#Version>How to tell which version you have</A> 52<LI><A HREF=#Typedefs>Library Datatypes</A> 53<LI><A HREF=#Mman>Memory Management</A> 54<LI><A HREF=#Errors>Error Handling</A> 55<LI><A HREF=#FIO>Basic File Handling</A> 56<LI><A HREF=#Dirs>TIFF Directories</A> 57<LI><A HREF=#Tags>TIFF Tags</A> 58<LI><A HREF=#Compression>TIFF Compression Schemes</A> 59<LI><A HREF=#ByteOrder>Byte Order</A> 60<LI><A HREF=#DataPlacement>Data Placement</A> 61<LI><A HREF=#TIFFRGBAImage>TIFFRGBAImage Support</A> 62<LI><A HREF=#Scanlines>Scanline-based Image I/O</A> 63<LI><A HREF=#Strips>Strip-oriented Image I/O</A> 64<LI><A HREF=#Tiles>Tile-oriented Image I/O</A> 65<LI><A HREF=#Other>Other Stuff</A> 66</UL> 67 68 69<A NAME="Version"><P><HR WIDTH=65% ALIGN=right><H3>How to tell which version you have</H3></A> 70 71The software version can be found by looking at the file named 72<TT>VERSION</TT> 73that is located at the top of the source tree; the precise alpha number 74is given in the file <TT>dist/tiff.alpha</TT>. 75If you have need to refer to this 76specific software, you should identify it as: 77 78<PRE> 79 TIFF <<I>version</I>> <<I>alpha</I>> 80</PRE> 81 82where <<I>version</I>> is whatever you get from 83<KBD>"cat VERSION"</KBD> and <<I>alpha</I>> is 84what you get from <KBD>"cat dist/tiff.alpha"</KBD>. 85 86<P> 87Within an application that uses <TT>libtiff</TT> the <TT>TIFFGetVersion</TT> 88routine will return a pointer to a string that contains software version 89information. 90The library include file <TT><tiffio.h></TT> contains a C pre-processor 91define <TT>TIFFLIB_VERSION</TT> that can be used to check library 92version compatiblity at compile time. 93 94<A NAME="Typedefs"><P><HR WIDTH=65% ALIGN=right><H3>Library Datatypes</H3></A> 95 96<TT>libtiff</TT> defines a portable programming interface through the 97use of a set of C type definitions. 98These definitions, defined in in the files <B>tiff.h</B> and 99<B>tiffio.h</B>, 100isolate the <TT>libtiff</TT> API from the characteristics 101of the underlying machine. 102To insure portable code and correct operation, applications that use 103<TT>libtiff</TT> should use the typedefs and follow the function 104prototypes for the library API. 105 106<A NAME="Mman"><P><HR WIDTH=65% ALIGN=right><H3>Memory Management</H3></A> 107 108<TT>libtiff</TT> uses a machine-specific set of routines for managing 109dynamically allocated memory. 110<TT>_TIFFmalloc</TT>, <TT>_TIFFrealloc</TT>, and <TT>_TIFFfree</TT> 111mimic the normal ANSI C routines. 112Any dynamically allocated memory that is to be passed into the library 113should be allocated using these interfaces in order to insure pointer 114compatibility on machines with a segmented architecture. 115(On 32-bit UNIX systems these routines just call the normal <TT>malloc</TT>, 116<TT>realloc</TT>, and <TT>free</TT> routines in the C library.) 117 118<P> 119To deal with segmented pointer issues <TT>libtiff</TT> also provides 120<TT>_TIFFmemcpy</TT>, <TT>_TIFFmemset</TT>, and <TT>_TIFFmemmove</TT> 121routines that mimic the equivalent ANSI C routines, but that are 122intended for use with memory allocated through <TT>_TIFFmalloc</TT> 123and <TT>_TIFFrealloc</TT>. 124 125<A NAME="Errors"><P><HR WIDTH=65% ALIGN=right><H3>Error Handling</H3></A> 126 127<TT>libtiff</TT> handles most errors by returning an invalid/erroneous 128value when returning from a function call. 129Various diagnostic messages may also be generated by the library. 130All error messages are directed to a single global error handler 131routine that can be specified with a call to <TT>TIFFSetErrorHandler</TT>. 132Likewise warning messages are directed to a single handler routine 133that can be specified with a call to <TT>TIFFSetWarningHandler</TT> 134 135<A NAME="FIO"><P><HR WIDTH=65% ALIGN=right><H3>Basic File Handling</H3></A> 136 137The library is modeled after the normal UNIX stdio library. 138For example, to read from an existing TIFF image the 139file must first be opened: 140 141<UL><PRE> 142#include "tiffio.h" 143main() 144{ 145 TIFF* tif = TIFFOpen("foo.tif", "r"); 146 ... do stuff ... 147 TIFFClose(tif); 148} 149</PRE></UL> 150 151The handle returned by <TT>TIFFOpen</TT> is <I>opaque</I>, that is 152the application is not permitted to know about its contents. 153All subsequent library calls for this file must pass the handle 154as an argument. 155 156<P> 157To create or overwrite a TIFF image the file is also opened, but with 158a <TT>"w"</TT> argument: 159 160<UL><PRE> 161#include "tiffio.h" 162main() 163{ 164 TIFF* tif = TIFFOpen("foo.tif", "w"); 165 ... do stuff ... 166 TIFFClose(tif); 167} 168</PRE></UL> 169 170If the file already exists it is first truncated to zero length. 171 172<P> 173<IMG SRC=images/warning.gif ALIGN=left HSPACE=6> 174<EM>Note that unlike the stdio library TIFF image files may not be 175opened for both reading and writing; 176there is no support for altering the contents of a TIFF file. 177</EM> 178 179<P> 180<TT>libtiff</TT> buffers much information associated with writing a 181valid TIFF image. Consequently, when writing a TIFF image it is necessary 182to always call <TT>TIFFClose</TT> or <TT>TIFFFlush</TT> to flush any 183buffered information to a file. Note that if you call <TT>TIFFClose</TT> 184you do not need to call <TT>TIFFFlush</TT>. 185 186<A NAME="Dirs"><P><HR WIDTH=65% ALIGN=right><H3>TIFF Directories</H3></A> 187 188TIFF supports the storage of multiple images in a single file. 189Each image has an associated data structure termed a <I>directory</I> 190that houses all the information about the format and content of the 191image data. 192Images in a file are usually related but they do not need to be; it 193is perfectly alright to store a color image together with a black and 194white image. 195Note however that while images may be related their directories are 196not. 197That is, each directory stands on its own; their is no need to read 198an unrelated directory in order to properly interpret the contents 199of an image. 200 201<P> 202<TT>libtiff</TT> provides several routines for reading and writing 203directories. In normal use there is no need to explicitly 204read or write a directory: the library automatically reads the first 205directory in a file when opened for reading, and directory information 206to be written is automatically accumulated and written when writing 207(assuming <TT>TIFFClose</TT> or <TT>TIFFFlush</TT> are called). 208 209<P> 210For a file open for reading the <TT>TIFFSetDirectory</TT> routine can 211be used to select an arbitrary directory; directories are referenced by 212number with the numbering starting at 0. Otherwise the 213<TT>TIFFReadDirectory</TT> and <TT>TIFFWriteDirectory</TT> routines can 214be used for sequential access to directories. 215For example, to count the number of directories in a file the following 216code might be used: 217 218<UL><PRE> 219#include "tiffio.h" 220main(int argc, char* argv[]) 221{ 222 TIFF* tif = TIFFOpen(argv[1], "r"); 223 if (tif) { 224 int dircount = 0; 225 do { 226 dircount++; 227 } while (TIFFReadDirectory(tif)); 228 printf("%d directories in %s\n", dircount, argv[1]); 229 TIFFClose(tif); 230 } 231 exit(0); 232} 233</PRE></UL> 234 235<P> 236Finally, note that there are several routines for querying the 237directory status of an open file: 238<TT>TIFFCurrentDirectory</TT> returns the index of the current 239directory and 240<TT>TIFFLastDirectory</TT> returns an indication of whether the 241current directory is the last directory in a file. 242There is also a routine, <TT>TIFFPrintDirectory</TT>, that can 243be called to print a formatted description of the contents of 244the current directory; consult the manual page for complete details. 245 246<A NAME="Tags"><P><HR WIDTH=65% ALIGN=right><H3>TIFF Tags</H3></A> 247 248Image-related information such as the image width and height, number 249of samples, orientation, colorimetric information, etc. 250are stored in each image 251directory in <I>fields</I> or <I>tags</I>. 252Tags are identified by a number that is usually a value registered 253with the Aldus (now Adobe) Corporation. 254Beware however that some vendors write 255TIFF images with tags that are unregistered; in this case interpreting 256their contents is usually a waste of time. 257 258<P> 259<TT>libtiff</TT> reads the contents of a directory all at once 260and converts the on-disk information to an appropriate in-memory 261form. While the TIFF specification permits an arbitrary set of 262tags to be defined and used in a file, the library only understands 263a limited set of tags. 264Any unknown tags that are encountered in a file are ignored. 265There is a mechanism to extend the set of tags the library handles 266without modifying the library itself; 267this is described <A HREF=../contrib/tags/README>elsewhere</A>. 268 269<P> 270<TT>libtiff</TT> provides two interfaces for getting and setting tag 271values: <TT>TIFFGetField</TT> and <TT>TIFFSetField</TT>. 272These routines use a variable argument list-style interface to pass 273parameters of different type through a single function interface. 274The <I>get interface</I> takes one or more pointers to memory locations 275where the tag values are to be returned and also returns one or 276zero according to whether the requested tag is defined in the directory. 277The <I>set interface</I> takes the tag values either by-reference or 278by-value. 279The TIFF specification defines 280<I>default values</I> for some tags. 281To get the value of a tag, or its default value if it is undefined, 282the <TT>TIFFGetFieldDefaulted</TT> interface may be used. 283 284<P> 285The manual pages for the tag get and set routines specifiy the exact data types 286and calling conventions required for each tag supported by the library. 287 288<A NAME="Compression"><P><HR WIDTH=65% ALIGN=right><H3>TIFF Compression Schemes</H3></A> 289 290<TT>libtiff</TT> includes support for a wide variety of 291data compression schemes. 292In normal operation a compression scheme is automatically used when 293the TIFF <TT>Compression</TT> tag is set, either by opening a file 294for reading, or by setting the tag when writing. 295 296<P> 297Compression schemes are implemented by software modules termed <I>codecs</I> 298that implement decoder and encoder routines that hook into the 299core library i/o support. 300Codecs other than those bundled with the library can be registered 301for use with the <TT>TIFFRegisterCODEC</TT> routine. 302This interface can also be used to override the core-library 303implementation for a compression scheme. 304 305<A NAME="ByteOrder"><P><HR WIDTH=65% ALIGN=right><H3>Byte Order</H3></A> 306 307The TIFF specification says, and has always said, that 308<EM>a correct TIFF 309reader must handle images in big-endian and little-endian byte order</EM>. 310<TT>libtiff</TT> conforms in this respect. 311Consequently there is no means to force a specific 312byte order for the data written to a TIFF image file (data is 313written in the native order of the host CPU unless appending to 314an existing file, in which case it is written in the byte order 315specified in the file). 316 317 318<A NAME="DataPlacement"><P><HR WIDTH=65% ALIGN=right><H3>Data Placement</H3></A> 319 320The TIFF specification requires that all information except an 3218-byte header can be placed anywhere in a file. 322In particular, it is perfectly legitimate for directory information 323to be written after the image data itself. 324Consequently TIFF is inherently not suitable for passing through a 325stream-oriented mechanism such as UNIX pipes. 326Software that require that data be organized in a file in a particular 327order (e.g. directory information before image data) does not 328correctly support TIFF. 329<TT>libtiff</TT> provides no mechanism for controlling the placement 330of data in a file; image data is typically written before directory 331information. 332 333<A NAME="TIFFRGBAImage"><P><HR WIDTH=65% ALIGN=right><H3>TIFFRGBAImage Support</H3></A> 334 335<TT>libtiff</TT> provides a high-level interface for reading image 336data from a TIFF file. This interface handles the details of 337data organization and format for a wide variety of TIFF files; 338at least the large majority of those files that one would normally 339encounter. Image data is, by default, returned as ABGR 340pixels packed into 32-bit words (8 bits per sample). Rectangular 341rasters can be read or data can be intercepted at an intermediate 342level and packed into memory in a format more suitable to the 343application. 344The library handles all the details of the format of data stored on 345disk and, in most cases, if any colorspace conversions are required: 346bilevel to RGB, greyscale to RGB, CMYK to RGB, YCbCr to RGB, 16-bit 347samples to 8-bit samples, associated/unassociated alpha, etc. 348 349<P> 350There are two ways to read image data using this interface. If 351all the data is to be stored in memory and manipulated at once, 352then the routine <TT>TIFFReadRGBAImage</TT> can be used: 353 354<UL><PRE> 355#include "tiffio.h" 356main(int argc, char* argv[]) 357{ 358 TIFF* tif = TIFFOpen(argv[1], "r"); 359 if (tif) { 360 uint32 w, h; 361 size_t npixels; 362 uint32* raster; 363 364 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w); 365 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h); 366 npixels = w * h; 367 raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32)); 368 if (raster != NULL) { 369 if (TIFFReadRGBAImage(tif, w, h, raster, 0)) { 370 ...process raster data... 371 } 372 _TIFFfree(raster); 373 } 374 TIFFClose(tif); 375 } 376 exit(0); 377} 378</PRE></UL> 379 380Note above that <TT>_TIFFmalloc</TT> is used to allocate memory for 381the raster passed to <TT>TIFFReadRGBAImage</TT>; this is important 382to insure the ``appropriate type of memory'' is passed on machines 383with segmented architectures. 384 385<P> 386Alternatively, <TT>TIFFReadRGBAImage</TT> can be replaced with a 387more low-level interface that permits an application to have more 388control over this reading procedure. The equivalent to the above 389is: 390 391<UL><PRE> 392#include "tiffio.h" 393main(int argc, char* argv[]) 394{ 395 TIFF* tif = TIFFOpen(argv[1], "r"); 396 if (tif) { 397 TIFFRGBAImage img; 398 char emsg[1024]; 399 400 if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) { 401 size_t npixels; 402 uint32* raster; 403 404 npixels = img.width * img.height; 405 raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32)); 406 if (raster != NULL) { 407 if (TIFFRGBAImageGet(&img, raster, img.width, img.height)) { 408 ...process raster data... 409 } 410 _TIFFfree(raster); 411 } 412 TIFFRGBAImageEnd(&img); 413 } else 414 TIFFError(argv[1], emsg); 415 TIFFClose(tif); 416 } 417 exit(0); 418} 419</PRE></UL> 420 421However this usage does not take advantage of the more fine-grained 422control that's possible. That is, by using this interface it is 423possible to: 424 425<UL> 426<LI>repeatedly fetch (and manipulate) an image without opening 427 and closing the file 428<LI>interpose a method for packing raster pixel data according to 429 application-specific needs (or write the data at all) 430<LI>interpose methods that handle TIFF formats that are not already 431 handled by the core library 432</UL> 433 434The first item means that, for example, image viewers that want to 435handle multiple files can cache decoding information in order to 436speedup the work required to display a TIFF image. 437 438<P> 439The second item is the main reason for this interface. By interposing 440a ``put method'' (the routine that is called to pack pixel data in 441the raster) it is possible share the core logic that understands how 442to deal with TIFF while packing the resultant pixels in a format that 443is optimized for the application. This alternate format might be very 444different than the 8-bit per sample ABGR format the library writes by 445default. For example, if the application is going to display the image 446on an 8-bit colormap display the put routine might take the data and 447convert it on-the-fly to the best colormap indices for display. 448 449<P> 450The last item permits an application to extend the library 451without modifying the core code. 452By overriding the code provided an application might add support 453for some esoteric flavor of TIFF that it needs, or it might 454substitute a packing routine that is able to do optimizations 455using application/environment-specific information. 456 457<P> 458The TIFF image viewer found in <B>tools/sgigt.c</B> is an example 459of an application that makes use of the <TT>TIFFRGBAImage</TT> 460support. 461 462<A NAME="Scanlines"><P><HR WIDTH=65% ALIGN=right><H3>Scanline-based Image I/O</H3></A> 463 464The simplest interface provided by <TT>libtiff</TT> is a 465scanline-oriented interface that can be used to read TIFF 466images that have their image data organized in strips 467(trying to use this interface to read data written in tiles 468will produce errors.) 469A scanline is a one pixel high row of image data whose width 470is the width of the image. 471Data is returned packed if the image data is stored with samples 472packed together, or as arrays of separate samples if the data 473is stored with samples separated. 474The major limitation of the scanline-oriented interface, other 475than the need to first identify an existing file as having a 476suitable organization, is that random access to individual 477scanlines can only be provided when data is not stored in a 478compressed format, or when the number of rows in a strip 479of image data is set to one (<TT>RowsPerStrip</TT> is one). 480 481<P> 482Two routines are provided for scanline-based i/o: 483<TT>TIFFReadScanline</TT> 484and 485<TT>TIFFWriteScanline</TT>. 486For example, to read the contents of a file that 487is assumed to be organized in strips, the following might be used: 488 489<UL><PRE> 490#include "tiffio.h" 491main() 492{ 493 TIFF* tif = TIFFOpen("myfile.tif", "r"); 494 if (tif) { 495 uint32 imagelength; 496 tdata_t buf; 497 uint32 row; 498 499 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength); 500 buf = _TIFFmalloc(TIFFScanlineSize(tif)); 501 for (row = 0; row < imagelength; row++) 502 TIFFReadScanline(tif, buf, row); 503 _TIFFfree(buf); 504 TIFFClose(tif); 505 } 506} 507</PRE></UL> 508 509<TT>TIFFScanlineSize</TT> returns the number of bytes in 510a decoded scanline, as returned by <TT>TIFFReadScanline</TT>. 511Note however that if the file had been create with samples 512written in separate planes, then the above code would only 513read data that contained the first sample of each pixel; 514to handle either case one might use the following instead: 515 516<UL><PRE> 517#include "tiffio.h" 518main() 519{ 520 TIFF* tif = TIFFOpen("myfile.tif", "r"); 521 if (tif) { 522 uint32 imagelength; 523 tdata_t buf; 524 uint32 row; 525 526 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength); 527 TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config); 528 buf = _TIFFmalloc(TIFFScanlineSize(tif)); 529 if (config == PLANARCONFIG_CONTIG) { 530 for (row = 0; row < imagelength; row++) 531 TIFFReadScanline(tif, buf, row); 532 } else if (config == PLANARCONFIG_SEPARATE) { 533 uint16 s, nsamples; 534 535 TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples); 536 for (s = 0; s < nsamples; s++) 537 for (row = 0; row < imagelength; row++) 538 TIFFReadScanline(tif, buf, row, s); 539 } 540 _TIFFfree(buf); 541 TIFFClose(tif); 542 } 543} 544</PRE></UL> 545 546Beware however that if the following code were used instead to 547read data in the case <TT>PLANARCONFIG_SEPARATE</TT>, 548 549<UL><PRE> 550 for (row = 0; row < imagelength; row++) 551 for (s = 0; s < nsamples; s++) 552 TIFFReadScanline(tif, buf, row, s); 553</PRE></UL> 554 555then problems would arise if <TT>RowsPerStrip</TT> was not one 556because the order in which scanlines are requested would require 557random access to data within strips (something that is not supported 558by the library when strips are compressed). 559 560<A NAME="Strips"><P><HR WIDTH=65% ALIGN=right><H3>Strip-oriented Image I/O</H3></A> 561 562The strip-oriented interfaces provided by the library provide 563access to entire strips of data. Unlike the scanline-oriented 564calls, data can be read or written compressed or uncompressed. 565Accessing data at a strip (or tile) level is often desirable 566because there are no complications with regard to random access 567to data within strips. 568 569<P> 570A simple example of reading an image by strips is: 571 572<UL><PRE> 573#include "tiffio.h" 574main() 575{ 576 TIFF* tif = TIFFOpen("myfile.tif", "r"); 577 if (tif) { 578 tdata_t buf; 579 tstrip_t strip; 580 581 buf = _TIFFmalloc(TIFFStripSize(tif)); 582 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) 583 TIFFReadEncodedStrip(tif, strip, buf, (tsize_t) -1); 584 _TIFFfree(buf); 585 TIFFClose(tif); 586 } 587} 588</PRE></UL> 589 590Notice how a strip size of <TT>-1</TT> is used; <TT>TIFFReadEncodedStrip</TT> 591will calculate the appropriate size in this case. 592 593<P> 594The above code reads strips in the order in which the 595data is physically stored in the file. If multiple samples 596are present and data is stored with <TT>PLANARCONFIG_SEPARATE</TT> 597then all the strips of data holding the first sample will be 598read, followed by strips for the second sample, etc. 599 600<P> 601Finally, note that the last strip of data in an image may have fewer 602rows in it than specified by the <TT>RowsPerStrip</TT> tag. A 603reader should not assume that each decoded strip contains a full 604set of rows in it. 605 606<P> 607The following is an example of how to read raw strips of data from 608a file: 609 610<UL><PRE> 611#include "tiffio.h" 612main() 613{ 614 TIFF* tif = TIFFOpen("myfile.tif", "r"); 615 if (tif) { 616 tdata_t buf; 617 tstrip_t strip; 618 uint32* bc; 619 uint32 stripsize; 620 621 TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc); 622 stripsize = bc[0]; 623 buf = _TIFFmalloc(stripsize); 624 for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) { 625 if (bc[strip] > stripsize) { 626 buf = _TIFFrealloc(buf, bc[strip]); 627 stripsize = bc[strip]; 628 } 629 TIFFReadRawStrip(tif, strip, buf, bc[strip]); 630 } 631 _TIFFfree(buf); 632 TIFFClose(tif); 633 } 634} 635</PRE></UL> 636 637As above the strips are read in the order in which they are 638physically stored in the file; this may be different from the 639logical ordering expected by an application. 640 641<A NAME="Tiles"><P><HR WIDTH=65% ALIGN=right><H3>Tile-oriented Image I/O</H3></A> 642 643Tiles of data may be read and written in a manner similar to strips. 644With this interface, an image is 645broken up into a set of rectangular areas that may have dimensions 646less than the image width and height. All the tiles 647in an image have the same size, and the tile width and length must each 648be a multiple of 16 pixels. Tiles are ordered left-to-right and 649top-to-bottom in an image. As for scanlines, samples can be packed 650contiguously or separately. When separated, all the tiles for a sample 651are colocated in the file. That is, all the tiles for sample 0 appear 652before the tiles for sample 1, etc. 653 654<P> 655Tiles and strips may also be extended in a z dimension to form 656volumes. Data volumes are organized as "slices". That is, all the 657data for a slice is colocated. Volumes whose data is organized in 658tiles can also have a tile depth so that data can be organized in 659cubes. 660 661<P> 662There are actually two interfaces for tiles. 663One interface is similar to scanlines, to read a tiled image, 664code of the following sort might be used: 665 666<UL><PRE> 667main() 668{ 669 TIFF* tif = TIFFOpen("myfile.tif", "r"); 670 if (tif) { 671 uint32 imageWidth, imageLength; 672 uint32 tileWidth, tileLength; 673 uint32 x, y; 674 tdata_t buf; 675 676 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth); 677 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength); 678 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth); 679 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileLength); 680 buf = _TIFFmalloc(TIFFTileSize(tif)); 681 for (y = 0; y < imageLength; y += tileLength) 682 for (x = 0; x < imageWidth; x += tileWidth) 683 TIFFReadTile(tif, buf, x, y, 0); 684 _TIFFfree(buf); 685 TIFFClose(tif); 686 } 687} 688</PRE></UL> 689 690(once again, we assume samples are packed contiguously.) 691 692<P> 693Alternatively a direct interface to the low-level data is provided 694a la strips. Tiles can be read with 695<TT>TIFFReadEncodedTile</TT> or 696<TT>TIFFReadRawTile</TT>, 697and written with 698<TT>TIFFWriteEncodedTile</TT> or 699<TT>TIFFWriteRawTile</TT>. 700For example, to read all the tiles in an image: 701 702<UL><PRE> 703#include "tiffio.h" 704main() 705{ 706 TIFF* tif = TIFFOpen("myfile.tif", "r"); 707 if (tif) { 708 tdata_t buf; 709 ttile_t tile; 710 711 buf = _TIFFmalloc(TIFFTileSize(tif)); 712 for (tile = 0; tile < TIFFNumberOfTiles(tif); tile++) 713 TIFFReadEncodedTile(tif, tile, buf, (tsize_t) -1); 714 _TIFFfree(buf); 715 TIFFClose(tif); 716 } 717} 718</PRE></UL> 719 720 721 722<A NAME="Other"><P><HR WIDTH=65% ALIGN=right><H3>Other Stuff</H3></A> 723 724<P> 725<I>Some other stuff will almost certainly go here...</I> 726 727<P> 728<HR> 729 730Last updated: $Date: 2004-07-11 15:35:30 $ 731 732</BODY> 733</HTML> 734