roentgen b75cab
roentgen b75cab
roentgen b75cab
roentgen b75cab
  <title>Using The TIFF Library</title>
roentgen b75cab
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
roentgen b75cab
  <meta content="en" http-equiv="content-language">
roentgen b75cab
  <style type="text/css"></style>
roentgen b75cab
  
roentgen b75cab
    th {text-align: left; vertical-align: top; font-style: italic; font-weight: normal}
roentgen b75cab
  -->
roentgen b75cab
  
roentgen b75cab
roentgen b75cab
roentgen b75cab
  
roentgen b75cab
    
roentgen b75cab
      
roentgen b75cab
      
roentgen b75cab
        

Using The TIFF Library

roentgen b75cab
        

roentgen b75cab
          <tt>libtiff</tt> is a set of C functions (a library) that support
roentgen b75cab
          the manipulation of TIFF image files.
roentgen b75cab
          The library requires an ANSI C compilation environment for building
roentgen b75cab
          and presumes an ANSI C environment for use.
roentgen b75cab
        

roentgen b75cab
      
roentgen b75cab
    
roentgen b75cab
  
roentgen b75cab
  
roentgen b75cab
  

roentgen b75cab
    <tt>libtiff</tt>
roentgen b75cab
    provides interfaces to image data at several layers of abstraction (and cost).
roentgen b75cab
    At the highest level image data can be read into an 8-bit/sample,
roentgen b75cab
    ABGR pixel raster format without regard for the underlying data organization,
roentgen b75cab
    colorspace, or compression scheme.  Below this high-level interface
roentgen b75cab
    the library provides scanline-, strip-, and tile-oriented interfaces that
roentgen b75cab
    return data decompressed but otherwise untransformed.  These interfaces
roentgen b75cab
    require that the application first identify the organization of stored
roentgen b75cab
    data and select either a strip-based or tile-based API for manipulating
roentgen b75cab
    data.  At the lowest level the library
roentgen b75cab
    provides access to the raw uncompressed strips or tiles,
roentgen b75cab
    returning the data exactly as it appears in the file.
roentgen b75cab
  

roentgen b75cab
  

roentgen b75cab
    The material presented in this chapter is a basic introduction
roentgen b75cab
    to the capabilities of the library; it is not an attempt to describe
roentgen b75cab
    everything a developer needs to know about the library or about TIFF.
roentgen b75cab
    Detailed information on the interfaces to the library are given in
roentgen b75cab
    the UNIX 
roentgen b75cab
    manual pages that accompany this software.
roentgen b75cab
  

roentgen b75cab
  

roentgen b75cab
    Michael Still has also written a useful introduction to libtiff for the
roentgen b75cab
    IBM DeveloperWorks site available at
roentgen b75cab
    http://www.ibm.com/developerworks/linux/library/l-libtiff.
roentgen b75cab
  

roentgen b75cab
  

roentgen b75cab
    The following sections are found in this chapter:
roentgen b75cab
  

roentgen b75cab
  
    roentgen b75cab
        
  • How to tell which version you have
  • roentgen b75cab
        
  • Library Datatypes
  • roentgen b75cab
        
  • Memory Management
  • roentgen b75cab
        
  • Error Handling
  • roentgen b75cab
        
  • Basic File Handling
  • roentgen b75cab
        
  • TIFF Directories
  • roentgen b75cab
        
  • TIFF Tags
  • roentgen b75cab
        
  • TIFF Compression Schemes
  • roentgen b75cab
        
  • Byte Order
  • roentgen b75cab
        
  • Data Placement
  • roentgen b75cab
        
  • TIFFRGBAImage Support
  • roentgen b75cab
        
  • Scanline-based Image I/O
  • roentgen b75cab
        
  • Strip-oriented Image I/O
  • roentgen b75cab
        
  • Tile-oriented Image I/O
  • roentgen b75cab
        
  • Other Stuff
  • roentgen b75cab
      
    roentgen b75cab
      
    roentgen b75cab
      

    How to tell which version you have

    roentgen b75cab
      

    roentgen b75cab
        The software version can be found by looking at the file named
    roentgen b75cab
        <tt>VERSION</tt>
    roentgen b75cab
        that is located at the top of the source tree; the precise alpha number
    roentgen b75cab
        is given in the file <tt>dist/tiff.alpha</tt>.
    roentgen b75cab
        If you have need to refer to this
    roentgen b75cab
        specific software, you should identify it as:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>TIFF <version> <alpha></tt>
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        where <tt><version></tt> is whatever you get from
    roentgen b75cab
        <tt>"cat VERSION"</tt> and <tt><alpha></tt> is
    roentgen b75cab
        what you get from <tt>"cat dist/tiff.alpha"</tt>.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        Within an application that uses <tt>libtiff</tt> the <tt>TIFFGetVersion</tt>
    roentgen b75cab
        routine will return a pointer to a string that contains software version
    roentgen b75cab
        information.
    roentgen b75cab
        The library include file <tt><tiffio.h></tt> contains a C pre-processor
    roentgen b75cab
        define <tt>TIFFLIB_VERSION</tt> that can be used to check library
    roentgen b75cab
        version compatiblity at compile time.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Library Datatypes

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> defines a portable programming interface through the
    roentgen b75cab
        use of a set of C type definitions.
    roentgen b75cab
        These definitions, defined in in the files tiff.h and
    roentgen b75cab
        tiffio.h,
    roentgen b75cab
        isolate the <tt>libtiff</tt> API from the characteristics
    roentgen b75cab
        of the underlying machine.
    roentgen b75cab
        To insure portable code and correct operation, applications that use
    roentgen b75cab
        <tt>libtiff</tt> should use the typedefs and follow the function
    roentgen b75cab
        prototypes for the library API.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Memory Management

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> uses a machine-specific set of routines for managing
    roentgen b75cab
        dynamically allocated memory.
    roentgen b75cab
        <tt>_TIFFmalloc</tt>, <tt>_TIFFrealloc</tt>, and <tt>_TIFFfree</tt>
    roentgen b75cab
        mimic the normal ANSI C routines.
    roentgen b75cab
        Any dynamically allocated memory that is to be passed into the library
    roentgen b75cab
        should be allocated using these interfaces in order to insure pointer
    roentgen b75cab
        compatibility on machines with a segmented architecture.
    roentgen b75cab
        (On 32-bit UNIX systems these routines just call the normal <tt>malloc</tt>,
    roentgen b75cab
        <tt>realloc</tt>, and <tt>free</tt> routines in the C library.)
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        To deal with segmented pointer issues <tt>libtiff</tt> also provides
    roentgen b75cab
        <tt>_TIFFmemcpy</tt>, <tt>_TIFFmemset</tt>, and <tt>_TIFFmemmove</tt>
    roentgen b75cab
        routines that mimic the equivalent ANSI C routines, but that are
    roentgen b75cab
        intended for use with memory allocated through <tt>_TIFFmalloc</tt>
    roentgen b75cab
        and <tt>_TIFFrealloc</tt>.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Error Handling

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> handles most errors by returning an invalid/erroneous
    roentgen b75cab
        value when returning from a function call.
    roentgen b75cab
        Various diagnostic messages may also be generated by the library.
    roentgen b75cab
        All error messages are directed to a single global error handler
    roentgen b75cab
        routine that can be specified with a call to <tt>TIFFSetErrorHandler</tt>.
    roentgen b75cab
        Likewise warning messages are directed to a single handler routine
    roentgen b75cab
        that can be specified with a call to <tt>TIFFSetWarningHandler</tt>
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Basic File Handling

    roentgen b75cab
      

    roentgen b75cab
        The library is modeled after the normal UNIX stdio library.
    roentgen b75cab
        For example, to read from an existing TIFF image the
    roentgen b75cab
        file must first be opened:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>#include "tiffio.h"
    </tt>
    roentgen b75cab
        main()
    roentgen b75cab
        {
    roentgen b75cab
            TIFF* tif = TIFFOpen("foo.tif", "r");
    roentgen b75cab
            ... do stuff ...
    roentgen b75cab
            TIFFClose(tif);
    roentgen b75cab
        }
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        The handle returned by <tt>TIFFOpen</tt> is opaque, that is
    roentgen b75cab
        the application is not permitted to know about its contents.
    roentgen b75cab
        All subsequent library calls for this file must pass the handle
    roentgen b75cab
        as an argument.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        To create or overwrite a TIFF image the file is also opened, but with
    roentgen b75cab
        a <tt>"w"</tt> argument:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>#include "tiffio.h"
    </tt>
    roentgen b75cab
        main()
    roentgen b75cab
        {
    roentgen b75cab
            TIFF* tif = TIFFOpen("foo.tif", "w");
    roentgen b75cab
            ... do stuff ...
    roentgen b75cab
            TIFFClose(tif);
    roentgen b75cab
        }
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        If the file already exists it is first truncated to zero length.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
        
    roentgen b75cab
          
    roentgen b75cab
          Note that unlike the stdio library TIFF image files may not be
    roentgen b75cab
            opened for both reading and writing;
    roentgen b75cab
            there is no support for altering the contents of a TIFF file.
    roentgen b75cab
        
    roentgen b75cab
      
    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> buffers much information associated with writing a
    roentgen b75cab
        valid TIFF image.  Consequently, when writing a TIFF image it is necessary
    roentgen b75cab
        to always call <tt>TIFFClose</tt> or <tt>TIFFFlush</tt> to flush any
    roentgen b75cab
        buffered information to a file.  Note that if you call <tt>TIFFClose</tt>
    roentgen b75cab
        you do not need to call <tt>TIFFFlush</tt>.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    TIFF Directories

    roentgen b75cab
      

    roentgen b75cab
        TIFF supports the storage of multiple images in a single file.
    roentgen b75cab
        Each image has an associated data structure termed a directory
    roentgen b75cab
        that houses all the information about the format and content of the
    roentgen b75cab
        image data.
    roentgen b75cab
        Images in a file are usually related but they do not need to be; it
    roentgen b75cab
        is perfectly alright to store a color image together with a black and
    roentgen b75cab
        white image.
    roentgen b75cab
        Note however that while images may be related their directories are
    roentgen b75cab
        not.
    roentgen b75cab
        That is, each directory stands on its own; their is no need to read
    roentgen b75cab
        an unrelated directory in order to properly interpret the contents
    roentgen b75cab
        of an image.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> provides several routines for reading and writing
    roentgen b75cab
        directories.  In normal use there is no need to explicitly
    roentgen b75cab
        read or write a directory: the library automatically reads the first
    roentgen b75cab
        directory in a file when opened for reading, and directory information
    roentgen b75cab
        to be written is automatically accumulated and written when writing
    roentgen b75cab
        (assuming <tt>TIFFClose</tt> or <tt>TIFFFlush</tt> are called).
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        For a file open for reading the <tt>TIFFSetDirectory</tt> routine can
    roentgen b75cab
        be used to select an arbitrary directory; directories are referenced by
    roentgen b75cab
        number with the numbering starting at 0.  Otherwise the
    roentgen b75cab
        <tt>TIFFReadDirectory</tt> and <tt>TIFFWriteDirectory</tt> routines can
    roentgen b75cab
        be used for sequential access to directories.
    roentgen b75cab
        For example, to count the number of directories in a file the following
    roentgen b75cab
        code might be used:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>#include "tiffio.h"
    </tt>
    roentgen b75cab
        main(int argc, char* argv[])
    roentgen b75cab
        {
    roentgen b75cab
            TIFF* tif = TIFFOpen(argv[1], "r");
    roentgen b75cab
            if (tif) {
    roentgen b75cab
                int dircount = 0;
    roentgen b75cab
                do {
    roentgen b75cab
                    dircount++;
    roentgen b75cab
                } while (TIFFReadDirectory(tif));
    roentgen b75cab
                printf("%d directories in %s\n", dircount, argv[1]);
    roentgen b75cab
                TIFFClose(tif);
    roentgen b75cab
            }
    roentgen b75cab
            exit(0);
    roentgen b75cab
        }
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        Finally, note that there are several routines for querying the
    roentgen b75cab
        directory status of an open file:
    roentgen b75cab
        <tt>TIFFCurrentDirectory</tt> returns the index of the current
    roentgen b75cab
        directory and
    roentgen b75cab
        <tt>TIFFLastDirectory</tt> returns an indication of whether the
    roentgen b75cab
        current directory is the last directory in a file.
    roentgen b75cab
        There is also a routine, <tt>TIFFPrintDirectory</tt>, that can
    roentgen b75cab
        be called to print a formatted description of the contents of
    roentgen b75cab
        the current directory; consult the manual page for complete details.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    TIFF Tags

    roentgen b75cab
      

    roentgen b75cab
        Image-related information such as the image width and height, number
    roentgen b75cab
        of samples, orientation, colorimetric information, etc.
    roentgen b75cab
        are stored in each image
    roentgen b75cab
        directory in fields or tags.
    roentgen b75cab
        Tags are identified by a number that is usually a value registered
    roentgen b75cab
        with the Aldus (now Adobe) Corporation.
    roentgen b75cab
        Beware however that some vendors write
    roentgen b75cab
        TIFF images with tags that are unregistered; in this case interpreting
    roentgen b75cab
        their contents is usually a waste of time.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> reads the contents of a directory all at once
    roentgen b75cab
        and converts the on-disk information to an appropriate in-memory
    roentgen b75cab
        form.  While the TIFF specification permits an arbitrary set of
    roentgen b75cab
        tags to be defined and used in a file, the library only understands
    roentgen b75cab
        a limited set of tags.
    roentgen b75cab
        Any unknown tags that are encountered in a file are ignored.
    roentgen b75cab
        There is a mechanism to extend the set of tags the library handles
    roentgen b75cab
        without modifying the library itself;
    roentgen b75cab
        this is described elsewhere.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> provides two interfaces for getting and setting tag
    roentgen b75cab
        values: <tt>TIFFGetField</tt> and <tt>TIFFSetField</tt>.
    roentgen b75cab
        These routines use a variable argument list-style interface to pass
    roentgen b75cab
        parameters of different type through a single function interface.
    roentgen b75cab
        The get interface takes one or more pointers to memory locations
    roentgen b75cab
        where the tag values are to be returned and also returns one or
    roentgen b75cab
        zero according to whether the requested tag is defined in the directory.
    roentgen b75cab
        The set interface takes the tag values either by-reference or
    roentgen b75cab
        by-value.
    roentgen b75cab
        The TIFF specification defines
    roentgen b75cab
        default values for some tags.
    roentgen b75cab
        To get the value of a tag, or its default value if it is undefined,
    roentgen b75cab
        the <tt>TIFFGetFieldDefaulted</tt> interface may be used.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        The manual pages for the tag get and set routines specifiy the exact data types
    roentgen b75cab
        and calling conventions required for each tag supported by the library.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    TIFF Compression Schemes

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> includes support for a wide variety of
    roentgen b75cab
        data compression schemes.
    roentgen b75cab
        In normal operation a compression scheme is automatically used when
    roentgen b75cab
        the TIFF <tt>Compression</tt> tag is set, either by opening a file
    roentgen b75cab
        for reading, or by setting the tag when writing.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        Compression schemes are implemented by software modules termed codecs
    roentgen b75cab
        that implement decoder and encoder routines that hook into the
    roentgen b75cab
        core library i/o support.
    roentgen b75cab
        Codecs other than those bundled with the library can be registered
    roentgen b75cab
        for use with the <tt>TIFFRegisterCODEC</tt> routine.
    roentgen b75cab
        This interface can also be used to override the core-library
    roentgen b75cab
        implementation for a compression scheme.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Byte Order

    roentgen b75cab
      

    roentgen b75cab
        The TIFF specification says, and has always said, that
    roentgen b75cab
        a correct TIFF
    roentgen b75cab
        reader must handle images in big-endian and little-endian byte order.
    roentgen b75cab
        <tt>libtiff</tt> conforms in this respect.
    roentgen b75cab
        Consequently there is no means to force a specific
    roentgen b75cab
        byte order for the data written to a TIFF image file (data is
    roentgen b75cab
        written in the native order of the host CPU unless appending to
    roentgen b75cab
        an existing file, in which case it is written in the byte order
    roentgen b75cab
        specified in the file).
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    Data Placement

    roentgen b75cab
      

    roentgen b75cab
        The TIFF specification requires that all information except an
    roentgen b75cab
        8-byte header can be placed anywhere in a file.
    roentgen b75cab
        In particular, it is perfectly legitimate for directory information
    roentgen b75cab
        to be written after the image data itself.
    roentgen b75cab
        Consequently TIFF is inherently not suitable for passing through a
    roentgen b75cab
        stream-oriented mechanism such as UNIX pipes.
    roentgen b75cab
        Software that require that data be organized in a file in a particular
    roentgen b75cab
        order (e.g. directory information before image data) does not
    roentgen b75cab
        correctly support TIFF.
    roentgen b75cab
        <tt>libtiff</tt> provides no mechanism for controlling the placement
    roentgen b75cab
        of data in a file; image data is typically written before directory
    roentgen b75cab
        information.
    roentgen b75cab
      

    roentgen b75cab
      
    roentgen b75cab
      

    TIFFRGBAImage Support

    roentgen b75cab
      

    roentgen b75cab
        <tt>libtiff</tt> provides a high-level interface for reading image
    roentgen b75cab
        data from a TIFF file.  This interface handles the details of
    roentgen b75cab
        data organization and format for a wide variety of TIFF files;
    roentgen b75cab
        at least the large majority of those files that one would normally
    roentgen b75cab
        encounter.  Image data is, by default, returned as ABGR
    roentgen b75cab
        pixels packed into 32-bit words (8 bits per sample).  Rectangular
    roentgen b75cab
        rasters can be read or data can be intercepted at an intermediate
    roentgen b75cab
        level and packed into memory in a format more suitable to the
    roentgen b75cab
        application.
    roentgen b75cab
        The library handles all the details of the format of data stored on
    roentgen b75cab
        disk and, in most cases, if any colorspace conversions are required:
    roentgen b75cab
        bilevel to RGB, greyscale to RGB, CMYK to RGB, YCbCr to RGB, 16-bit
    roentgen b75cab
        samples to 8-bit samples, associated/unassociated alpha, etc.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        There are two ways to read image data using this interface.  If
    roentgen b75cab
        all the data is to be stored in memory and manipulated at once,
    roentgen b75cab
        then the routine <tt>TIFFReadRGBAImage</tt> can be used:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>#include "tiffio.h"
    </tt>
    roentgen b75cab
        main(int argc, char* argv[])
    roentgen b75cab
        {
    roentgen b75cab
            TIFF* tif = TIFFOpen(argv[1], "r");
    roentgen b75cab
            if (tif) {
    roentgen b75cab
                uint32 w, h;
    roentgen b75cab
                size_t npixels;
    roentgen b75cab
                uint32* raster;
    roentgen b75cab
                
    roentgen b75cab
                TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
    roentgen b75cab
                TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
    roentgen b75cab
                npixels = w * h;
    roentgen b75cab
                raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
    roentgen b75cab
                if (raster != NULL) {
    roentgen b75cab
                    if (TIFFReadRGBAImage(tif, w, h, raster, 0)) {
    roentgen b75cab
                        ...process raster data...
    roentgen b75cab
                    }
    roentgen b75cab
                    _TIFFfree(raster);
    roentgen b75cab
                }
    roentgen b75cab
                TIFFClose(tif);
    roentgen b75cab
            }
    roentgen b75cab
            exit(0);
    roentgen b75cab
        }
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        Note above that <tt>_TIFFmalloc</tt> is used to allocate memory for
    roentgen b75cab
        the raster passed to <tt>TIFFReadRGBAImage</tt>; this is important
    roentgen b75cab
        to insure the ``appropriate type of memory'' is passed on machines
    roentgen b75cab
        with segmented architectures.
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        Alternatively, <tt>TIFFReadRGBAImage</tt> can be replaced with a
    roentgen b75cab
        more low-level interface that permits an application to have more
    roentgen b75cab
        control over this reading procedure.  The equivalent to the above
    roentgen b75cab
        is:
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        <tt>#include "tiffio.h"
    </tt>
    roentgen b75cab
        main(int argc, char* argv[])
    roentgen b75cab
        {
    roentgen b75cab
            TIFF* tif = TIFFOpen(argv[1], "r");
    roentgen b75cab
            if (tif) {
    roentgen b75cab
                TIFFRGBAImage img;
    roentgen b75cab
                char emsg[1024];
    roentgen b75cab
                
    roentgen b75cab
                if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
    roentgen b75cab
                    size_t npixels;
    roentgen b75cab
                    uint32* raster;
    roentgen b75cab
                    
    roentgen b75cab
                    npixels = img.width * img.height;
    roentgen b75cab
                    raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
    roentgen b75cab
                    if (raster != NULL) {
    roentgen b75cab
                        if (TIFFRGBAImageGet(&img, raster, img.width, img.height)) {
    roentgen b75cab
                            ...process raster data...
    roentgen b75cab
                        }
    roentgen b75cab
                        _TIFFfree(raster);
    roentgen b75cab
                    }
    roentgen b75cab
                    TIFFRGBAImageEnd(&img);
    roentgen b75cab
                } else
    roentgen b75cab
                    TIFFError(argv[1], emsg);
    roentgen b75cab
                TIFFClose(tif);
    roentgen b75cab
            }
    roentgen b75cab
            exit(0);
    roentgen b75cab
        }
    roentgen b75cab
      

    roentgen b75cab
      

    roentgen b75cab
        However this usage does not take advantage of the more fine-grained
    roentgen b75cab
        control that's possible.  That is, by using this interface it is
    roentgen b75cab
        possible to:
    roentgen b75cab
      

    roentgen b75cab
      
      roentgen b75cab
          
    • repeatedly fetch (and manipulate) an image without opening
    • roentgen b75cab
            and closing the file
      roentgen b75cab
          
    • interpose a method for packing raster pixel data according to
    • roentgen b75cab
            application-specific needs (or write the data at all)
      roentgen b75cab
          
    • interpose methods that handle TIFF formats that are not already
    • roentgen b75cab
            handled by the core library
      roentgen b75cab
        
      roentgen b75cab
        

      roentgen b75cab
          The first item means that, for example, image viewers that want to
      roentgen b75cab
          handle multiple files can cache decoding information in order to
      roentgen b75cab
          speedup the work required to display a TIFF image.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          The second item is the main reason for this interface.  By interposing
      roentgen b75cab
          a "put method" (the routine that is called to pack pixel data in
      roentgen b75cab
          the raster) it is possible share the core logic that understands how
      roentgen b75cab
          to deal with TIFF while packing the resultant pixels in a format that
      roentgen b75cab
          is optimized for the application.  This alternate format might be very
      roentgen b75cab
          different than the 8-bit per sample ABGR format the library writes by
      roentgen b75cab
          default.  For example, if the application is going to display the image
      roentgen b75cab
          on an 8-bit colormap display the put routine might take the data and
      roentgen b75cab
          convert it on-the-fly to the best colormap indices for display.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          The last item permits an application to extend the library
      roentgen b75cab
          without modifying the core code.
      roentgen b75cab
          By overriding the code provided an application might add support
      roentgen b75cab
          for some esoteric flavor of TIFF that it needs, or it might
      roentgen b75cab
          substitute a packing routine that is able to do optimizations
      roentgen b75cab
          using application/environment-specific information.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          The TIFF image viewer found in tools/sgigt.c is an example
      roentgen b75cab
          of an application that makes use of the <tt>TIFFRGBAImage</tt>
      roentgen b75cab
          support.
      roentgen b75cab
        

      roentgen b75cab
        
      roentgen b75cab
        

      Scanline-based Image I/O

      roentgen b75cab
        

      roentgen b75cab
          The simplest interface provided by <tt>libtiff</tt> is a
      roentgen b75cab
          scanline-oriented interface that can be used to read TIFF
      roentgen b75cab
          images that have their image data organized in strips
      roentgen b75cab
          (trying to use this interface to read data written in tiles
      roentgen b75cab
          will produce errors.)
      roentgen b75cab
          A scanline is a one pixel high row of image data whose width
      roentgen b75cab
          is the width of the image.
      roentgen b75cab
          Data is returned packed if the image data is stored with samples
      roentgen b75cab
          packed together, or as arrays of separate samples if the data
      roentgen b75cab
          is stored with samples separated.
      roentgen b75cab
          The major limitation of the scanline-oriented interface, other
      roentgen b75cab
          than the need to first identify an existing file as having a
      roentgen b75cab
          suitable organization, is that random access to individual
      roentgen b75cab
          scanlines can only be provided when data is not stored in a
      roentgen b75cab
          compressed format, or when the number of rows in a strip
      roentgen b75cab
          of image data is set to one (<tt>RowsPerStrip</tt> is one).
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Two routines are provided for scanline-based i/o:
      roentgen b75cab
          <tt>TIFFReadScanline</tt>
      roentgen b75cab
          and
      roentgen b75cab
          <tt>TIFFWriteScanline</tt>.
      roentgen b75cab
          For example, to read the contents of a file that
      roentgen b75cab
          is assumed to be organized in strips, the following might be used:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>#include "tiffio.h"
      </tt>
      roentgen b75cab
          main()
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  uint32 imagelength;
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  uint32 row;
      roentgen b75cab
                  
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
      roentgen b75cab
                  buf = _TIFFmalloc(TIFFScanlineSize(tif));
      roentgen b75cab
                  for (row = 0; row < imagelength; row++)
      roentgen b75cab
                      tiffreadscanline(tif, buf, row);
      roentgen b75cab
                  _tifffree(buf);
      roentgen b75cab
                  tiffclose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>TIFFScanlineSize</tt> returns the number of bytes in
      roentgen b75cab
          a decoded scanline, as returned by <tt>TIFFReadScanline</tt>.
      roentgen b75cab
          Note however that if the file had been create with samples
      roentgen b75cab
          written in separate planes, then the above code would only
      roentgen b75cab
          read data that contained the first sample of each pixel;
      roentgen b75cab
          to handle either case one might use the following instead:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>#include "tiffio.h"
      </tt>
      roentgen b75cab
          main()
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  uint32 imagelength;
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  uint32 row;
      roentgen b75cab
                  
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
      roentgen b75cab
                  buf = _TIFFmalloc(TIFFScanlineSize(tif));
      roentgen b75cab
                  if (config == PLANARCONFIG_CONTIG) {
      roentgen b75cab
                      for (row = 0; row < imagelength; row++)
      roentgen b75cab
                          tiffreadscanline(tif, buf, row);
      roentgen b75cab
                  } else if (config == planarconfig_separate) {
      roentgen b75cab
                      uint16 s, nsamples;
      roentgen b75cab
                      
      roentgen b75cab
                      tiffgetfield(tif, tifftag_samplesperpixel, &nsamples);
      roentgen b75cab
                      for (s = 0; s < nsamples; s++)
      roentgen b75cab
                          for (row = 0; row < imagelength; row++)
      roentgen b75cab
                              tiffreadscanline(tif, buf, row, s);
      roentgen b75cab
                  }
      roentgen b75cab
                  _tifffree(buf);
      roentgen b75cab
                  tiffclose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Beware however that if the following code were used instead to
      roentgen b75cab
          read data in the case <tt>PLANARCONFIG_SEPARATE</tt>,...
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>            for (row = 0; row < imagelength; row++)
      </tt>
      roentgen b75cab
                          for (s = 0; s < nsamples; s++)
      roentgen b75cab
                              tiffreadscanline(tif, buf, row, s);
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          ...then problems would arise if <tt>RowsPerStrip</tt> was not one
      roentgen b75cab
          because the order in which scanlines are requested would require
      roentgen b75cab
          random access to data within strips (something that is not supported
      roentgen b75cab
          by the library when strips are compressed).
      roentgen b75cab
        

      roentgen b75cab
        
      roentgen b75cab
        

      Strip-oriented Image I/O

      roentgen b75cab
        

      roentgen b75cab
          The strip-oriented interfaces provided by the library provide
      roentgen b75cab
          access to entire strips of data.  Unlike the scanline-oriented
      roentgen b75cab
          calls, data can be read or written compressed or uncompressed.
      roentgen b75cab
          Accessing data at a strip (or tile) level is often desirable
      roentgen b75cab
          because there are no complications with regard to random access
      roentgen b75cab
          to data within strips.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          A simple example of reading an image by strips is:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>#include "tiffio.h"
      </tt>
      roentgen b75cab
          main()
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  tstrip_t strip;
      roentgen b75cab
                  
      roentgen b75cab
                  buf = _TIFFmalloc(TIFFStripSize(tif));
      roentgen b75cab
                  for (strip = 0; strip < tiffnumberofstrips(tif); strip++)
      roentgen b75cab
                      tiffreadencodedstrip(tif, strip, buf, (tsize_t) -1);
      roentgen b75cab
                  _tifffree(buf);
      roentgen b75cab
                  tiffclose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Notice how a strip size of <tt>-1</tt> is used; <tt>TIFFReadEncodedStrip</tt>
      roentgen b75cab
          will calculate the appropriate size in this case.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          The above code reads strips in the order in which the
      roentgen b75cab
          data is physically stored in the file.  If multiple samples
      roentgen b75cab
          are present and data is stored with <tt>PLANARCONFIG_SEPARATE</tt>
      roentgen b75cab
          then all the strips of data holding the first sample will be
      roentgen b75cab
          read, followed by strips for the second sample, etc.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Finally, note that the last strip of data in an image may have fewer
      roentgen b75cab
          rows in it than specified by the <tt>RowsPerStrip</tt> tag.  A
      roentgen b75cab
          reader should not assume that each decoded strip contains a full
      roentgen b75cab
          set of rows in it.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          The following is an example of how to read raw strips of data from
      roentgen b75cab
          a file:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>#include "tiffio.h"
      </tt>
      roentgen b75cab
          main()
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  tstrip_t strip;
      roentgen b75cab
                  uint32* bc;
      roentgen b75cab
                  uint32 stripsize;
      roentgen b75cab
                  
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
      roentgen b75cab
                  stripsize = bc[0];
      roentgen b75cab
                  buf = _TIFFmalloc(stripsize);
      roentgen b75cab
                  for (strip = 0; strip < tiffnumberofstrips(tif); strip++) {
      roentgen b75cab
                      if (bc[strip] > stripsize) {
      roentgen b75cab
                          buf = _TIFFrealloc(buf, bc[strip]);
      roentgen b75cab
                          stripsize = bc[strip];
      roentgen b75cab
                      }
      roentgen b75cab
                      TIFFReadRawStrip(tif, strip, buf, bc[strip]);
      roentgen b75cab
                  }
      roentgen b75cab
                  _TIFFfree(buf);
      roentgen b75cab
                  TIFFClose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          As above the strips are read in the order in which they are
      roentgen b75cab
          physically stored in the file; this may be different from the
      roentgen b75cab
          logical ordering expected by an application.
      roentgen b75cab
        

      roentgen b75cab
        
      roentgen b75cab
        

      Tile-oriented Image I/O

      roentgen b75cab
        

      roentgen b75cab
          Tiles of data may be read and written in a manner similar to strips.
      roentgen b75cab
          With this interface, an image is
      roentgen b75cab
          broken up into a set of rectangular areas that may have dimensions
      roentgen b75cab
          less than the image width and height.  All the tiles
      roentgen b75cab
          in an image have the same size, and the tile width and length must each
      roentgen b75cab
          be a multiple of 16 pixels.  Tiles are ordered left-to-right and
      roentgen b75cab
          top-to-bottom in an image.  As for scanlines, samples can be packed
      roentgen b75cab
          contiguously or separately.  When separated, all the tiles for a sample
      roentgen b75cab
          are colocated in the file.  That is, all the tiles for sample 0 appear
      roentgen b75cab
          before the tiles for sample 1, etc.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Tiles and strips may also be extended in a z dimension to form
      roentgen b75cab
          volumes.  Data volumes are organized as "slices".  That is, all the
      roentgen b75cab
          data for a slice is colocated.  Volumes whose data is organized in
      roentgen b75cab
          tiles can also have a tile depth so that data can be organized in
      roentgen b75cab
          cubes.
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          There are actually two interfaces for tiles.
      roentgen b75cab
          One interface is similar to scanlines,  to read a tiled image,
      roentgen b75cab
          code of the following sort might be used:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>main()
      </tt>
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  uint32 imageWidth, imageLength;
      roentgen b75cab
                  uint32 tileWidth, tileLength;
      roentgen b75cab
                  uint32 x, y;
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth);
      roentgen b75cab
                  TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileLength);
      roentgen b75cab
                  buf = _TIFFmalloc(TIFFTileSize(tif));
      roentgen b75cab
                  for (y = 0; y < imagelength; y += tilelength)
      roentgen b75cab
                      for (x = 0; x < imagewidth; x += tilewidth)
      roentgen b75cab
                          tiffreadtile(tif, buf, x, y, 0);
      roentgen b75cab
                  _tifffree(buf);
      roentgen b75cab
                  tiffclose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          (once again, we assume samples are packed contiguously.)
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          Alternatively a direct interface to the low-level data is provided
      roentgen b75cab
          a la strips.  Tiles can be read with
      roentgen b75cab
          <tt>TIFFReadEncodedTile</tt> or <tt>TIFFReadRawTile</tt>,
      roentgen b75cab
          and written with <tt>TIFFWriteEncodedTile</tt> or
      roentgen b75cab
          <tt>TIFFWriteRawTile</tt>. For example, to read all the tiles in an image:
      roentgen b75cab
        

      roentgen b75cab
        

      roentgen b75cab
          <tt>#include "tiffio.h"
      </tt>
      roentgen b75cab
          main()
      roentgen b75cab
          {
      roentgen b75cab
              TIFF* tif = TIFFOpen("myfile.tif", "r");
      roentgen b75cab
              if (tif) {
      roentgen b75cab
                  tdata_t buf;
      roentgen b75cab
                  ttile_t tile;
      roentgen b75cab
                  
      roentgen b75cab
                  buf = _TIFFmalloc(TIFFTileSize(tif));
      roentgen b75cab
                  for (tile = 0; tile < tiffnumberoftiles(tif); tile++)
      roentgen b75cab
                      tiffreadencodedtile(tif, tile, buf, (tsize_t) -1);
      roentgen b75cab
                  _tifffree(buf);
      roentgen b75cab
                  tiffclose(tif);
      roentgen b75cab
              }
      roentgen b75cab
          }
      roentgen b75cab
        

      roentgen b75cab
        
      roentgen b75cab
        

      Other Stuff

      roentgen b75cab
        

      roentgen b75cab
          Some other stuff will almost certainly go here...
      roentgen b75cab
        

      roentgen b75cab
        
      roentgen b75cab
        

      roentgen b75cab
          Last updated: $Date: 2005/12/28 06:53:18 $
      roentgen b75cab
        

      roentgen b75cab
      roentgen b75cab