Blame html/libtiff.html

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

Using The TIFF Library

Packit 7838c8
        

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

Packit 7838c8
      
Packit 7838c8
    
Packit 7838c8
  
Packit 7838c8
  
Packit 7838c8
  

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

Packit 7838c8
  

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

Packit 7838c8
  

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

Packit 7838c8
  

Packit 7838c8
    The following sections are found in this chapter:
Packit 7838c8
  

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

    How to tell which version you have

    Packit 7838c8
      

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

    Packit 7838c8
      

    Packit 7838c8
        <tt>TIFF <version> <alpha></tt>
    Packit 7838c8
      

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Library Datatypes

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Memory Management

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Error Handling

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Basic File Handling

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

    Packit 7838c8
        If the file already exists it is first truncated to zero length.
    Packit 7838c8
      

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

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

    Packit 7838c8
      
    Packit 7838c8
      

    TIFF Directories

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    TIFF Tags

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    TIFF Compression Schemes

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Byte Order

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    Data Placement

    Packit 7838c8
      

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

    Packit 7838c8
      
    Packit 7838c8
      

    TIFFRGBAImage Support

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

    Packit 7838c8
      

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

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

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        
      Packit 7838c8
        

      Scanline-based Image I/O

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        
      Packit 7838c8
        

      Strip-oriented Image I/O

      Packit 7838c8
        

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

      Packit 7838c8
        

      Packit 7838c8
          A simple example of reading an image by strips is:
      Packit 7838c8
        

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        
      Packit 7838c8
        

      Tile-oriented Image I/O

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        

      Packit 7838c8
          (once again, we assume samples are packed contiguously.)
      Packit 7838c8
        

      Packit 7838c8
        

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

      Packit 7838c8
        

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

      Packit 7838c8
        
      Packit 7838c8
        

      Other Stuff

      Packit 7838c8
        

      Packit 7838c8
          Some other stuff will almost certainly go here...
      Packit 7838c8
        

      Packit 7838c8
        
      Packit 7838c8
        

      Packit 7838c8
          Last updated: $Date: 2016-09-25 20:05:44 $
      Packit 7838c8
        

      Packit 7838c8
      </body>
      Packit 7838c8
      </html>