Blame html/libtiff.html

Packit 994f1a
Packit 994f1a
<html lang="en">
Packit 994f1a
<head>
Packit 994f1a
  <title>Using The TIFF Library</title>
Packit 994f1a
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
Packit 994f1a
  <meta http-equiv="content-language" content="en">
Packit 994f1a
  <style type="text/css">
Packit 994f1a
  
Packit 994f1a
    th {text-align: left; vertical-align: top; font-style: italic; font-weight: normal}
Packit 994f1a
  -->
Packit 994f1a
  </style>
Packit 994f1a
</head>
Packit 994f1a
<body lang="en" text="#000000" bgcolor="#ffffff" link="#0000ff" alink="#0000ff" vlink="#0000ff">
Packit 994f1a
  
Packit 994f1a
    
Packit 994f1a
      
Packit 994f1a
      
Packit 994f1a
        

Using The TIFF Library

Packit 994f1a
        

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

Packit 994f1a
      
Packit 994f1a
    
Packit 994f1a
  
Packit 994f1a
  
Packit 994f1a
  

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

Packit 994f1a
  

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

Packit 994f1a
  

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

Packit 994f1a
  

Packit 994f1a
    The following sections are found in this chapter:
Packit 994f1a
  

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

    How to tell which version you have

    Packit 994f1a
      

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

    Packit 994f1a
      

    Packit 994f1a
        <tt>TIFF <version> <alpha></tt>
    Packit 994f1a
      

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Library Datatypes

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Memory Management

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Error Handling

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Basic File Handling

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

    Packit 994f1a
        If the file already exists it is first truncated to zero length.
    Packit 994f1a
      

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

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

    Packit 994f1a
      
    Packit 994f1a
      

    TIFF Directories

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    TIFF Tags

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    TIFF Compression Schemes

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Byte Order

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    Data Placement

    Packit 994f1a
      

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

    Packit 994f1a
      
    Packit 994f1a
      

    TIFFRGBAImage Support

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

    Packit 994f1a
      

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

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

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        
      Packit 994f1a
        

      Scanline-based Image I/O

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

      Packit 994f1a
          <tt>            for (row = 0; row < imagelength; row++)
      Packit 994f1a
                          for (s = 0; s < nsamples; s++)
      Packit 994f1a
                              tiffreadscanline(tif, buf, row, s);</tt>
      Packit 994f1a
        

      Packit 994f1a
        

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

      Packit 994f1a
        
      Packit 994f1a
        

      Strip-oriented Image I/O

      Packit 994f1a
        

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

      Packit 994f1a
        

      Packit 994f1a
          A simple example of reading an image by strips is:
      Packit 994f1a
        

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

      Packit 994f1a
          The following is an example of how to read raw strips of data from
      Packit 994f1a
          a file:
      Packit 994f1a
        

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        
      Packit 994f1a
        

      Tile-oriented Image I/O

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        

      Packit 994f1a
          (once again, we assume samples are packed contiguously.)
      Packit 994f1a
        

      Packit 994f1a
        

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

      Packit 994f1a
        

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

      Packit 994f1a
        
      Packit 994f1a
        

      Other Stuff

      Packit 994f1a
        

      Packit 994f1a
          Some other stuff will almost certainly go here...
      Packit 994f1a
        

      Packit 994f1a
        
      Packit 994f1a
        

      Packit 994f1a
          Last updated: $Date: 2005/12/28 06:53:18 $
      Packit 994f1a
        

      Packit 994f1a
      </body>
      Packit 994f1a
      </html>