|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<title>Using The TIFF Library</title>
|
|
kusano |
7d535a |
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
|
|
kusano |
7d535a |
<meta content="en" http-equiv="content-language">
|
|
kusano |
7d535a |
<style type="text/css"></style>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
th {text-align: left; vertical-align: top; font-style: italic; font-weight: normal}
|
|
kusano |
7d535a |
-->
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Using The TIFF Library
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> is a set of C functions (a library) that support
|
|
kusano |
7d535a |
the manipulation of TIFF image files.
|
|
kusano |
7d535a |
The library requires an ANSI C compilation environment for building
|
|
kusano |
7d535a |
and presumes an ANSI C environment for use.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt>
|
|
kusano |
7d535a |
provides interfaces to image data at several layers of abstraction (and cost).
|
|
kusano |
7d535a |
At the highest level image data can be read into an 8-bit/sample,
|
|
kusano |
7d535a |
ABGR pixel raster format without regard for the underlying data organization,
|
|
kusano |
7d535a |
colorspace, or compression scheme. Below this high-level interface
|
|
kusano |
7d535a |
the library provides scanline-, strip-, and tile-oriented interfaces that
|
|
kusano |
7d535a |
return data decompressed but otherwise untransformed. These interfaces
|
|
kusano |
7d535a |
require that the application first identify the organization of stored
|
|
kusano |
7d535a |
data and select either a strip-based or tile-based API for manipulating
|
|
kusano |
7d535a |
data. At the lowest level the library
|
|
kusano |
7d535a |
provides access to the raw uncompressed strips or tiles,
|
|
kusano |
7d535a |
returning the data exactly as it appears in the file.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The material presented in this chapter is a basic introduction
|
|
kusano |
7d535a |
to the capabilities of the library; it is not an attempt to describe
|
|
kusano |
7d535a |
everything a developer needs to know about the library or about TIFF.
|
|
kusano |
7d535a |
Detailed information on the interfaces to the library are given in
|
|
kusano |
7d535a |
the UNIX
|
|
kusano |
7d535a |
manual pages that accompany this software.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Michael Still has also written a useful introduction to libtiff for the
|
|
kusano |
7d535a |
IBM DeveloperWorks site available at
|
|
kusano |
7d535a |
http://www.ibm.com/developerworks/linux/library/l-libtiff.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The following sections are found in this chapter:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
How to tell which version you have
|
|
kusano |
7d535a |
Library Datatypes
|
|
kusano |
7d535a |
Memory Management
|
|
kusano |
7d535a |
Error Handling
|
|
kusano |
7d535a |
Basic File Handling
|
|
kusano |
7d535a |
TIFF Directories
|
|
kusano |
7d535a |
TIFF Tags
|
|
kusano |
7d535a |
TIFF Compression Schemes
|
|
kusano |
7d535a |
Byte Order
|
|
kusano |
7d535a |
Data Placement
|
|
kusano |
7d535a |
TIFFRGBAImage Support
|
|
kusano |
7d535a |
Scanline-based Image I/O
|
|
kusano |
7d535a |
Strip-oriented Image I/O
|
|
kusano |
7d535a |
Tile-oriented Image I/O
|
|
kusano |
7d535a |
Other Stuff
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
How to tell which version you have
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The software version can be found by looking at the file named
|
|
kusano |
7d535a |
<tt>VERSION</tt>
|
|
kusano |
7d535a |
that is located at the top of the source tree; the precise alpha number
|
|
kusano |
7d535a |
is given in the file <tt>dist/tiff.alpha</tt>.
|
|
kusano |
7d535a |
If you have need to refer to this
|
|
kusano |
7d535a |
specific software, you should identify it as:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>TIFF <version> <alpha></tt>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
where <tt><version></tt> is whatever you get from
|
|
kusano |
7d535a |
<tt>"cat VERSION"</tt> and <tt><alpha></tt> is
|
|
kusano |
7d535a |
what you get from <tt>"cat dist/tiff.alpha"</tt>.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Within an application that uses <tt>libtiff</tt> the <tt>TIFFGetVersion</tt>
|
|
kusano |
7d535a |
routine will return a pointer to a string that contains software version
|
|
kusano |
7d535a |
information.
|
|
kusano |
7d535a |
The library include file <tt><tiffio.h></tt> contains a C pre-processor
|
|
kusano |
7d535a |
define <tt>TIFFLIB_VERSION</tt> that can be used to check library
|
|
kusano |
7d535a |
version compatiblity at compile time.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Library Datatypes
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> defines a portable programming interface through the
|
|
kusano |
7d535a |
use of a set of C type definitions.
|
|
kusano |
7d535a |
These definitions, defined in in the files tiff.h and
|
|
kusano |
7d535a |
tiffio.h,
|
|
kusano |
7d535a |
isolate the <tt>libtiff</tt> API from the characteristics
|
|
kusano |
7d535a |
of the underlying machine.
|
|
kusano |
7d535a |
To insure portable code and correct operation, applications that use
|
|
kusano |
7d535a |
<tt>libtiff</tt> should use the typedefs and follow the function
|
|
kusano |
7d535a |
prototypes for the library API.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Memory Management
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> uses a machine-specific set of routines for managing
|
|
kusano |
7d535a |
dynamically allocated memory.
|
|
kusano |
7d535a |
<tt>_TIFFmalloc</tt>, <tt>_TIFFrealloc</tt>, and <tt>_TIFFfree</tt>
|
|
kusano |
7d535a |
mimic the normal ANSI C routines.
|
|
kusano |
7d535a |
Any dynamically allocated memory that is to be passed into the library
|
|
kusano |
7d535a |
should be allocated using these interfaces in order to insure pointer
|
|
kusano |
7d535a |
compatibility on machines with a segmented architecture.
|
|
kusano |
7d535a |
(On 32-bit UNIX systems these routines just call the normal <tt>malloc</tt>,
|
|
kusano |
7d535a |
<tt>realloc</tt>, and <tt>free</tt> routines in the C library.)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
To deal with segmented pointer issues <tt>libtiff</tt> also provides
|
|
kusano |
7d535a |
<tt>_TIFFmemcpy</tt>, <tt>_TIFFmemset</tt>, and <tt>_TIFFmemmove</tt>
|
|
kusano |
7d535a |
routines that mimic the equivalent ANSI C routines, but that are
|
|
kusano |
7d535a |
intended for use with memory allocated through <tt>_TIFFmalloc</tt>
|
|
kusano |
7d535a |
and <tt>_TIFFrealloc</tt>.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Error Handling
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> handles most errors by returning an invalid/erroneous
|
|
kusano |
7d535a |
value when returning from a function call.
|
|
kusano |
7d535a |
Various diagnostic messages may also be generated by the library.
|
|
kusano |
7d535a |
All error messages are directed to a single global error handler
|
|
kusano |
7d535a |
routine that can be specified with a call to <tt>TIFFSetErrorHandler</tt>.
|
|
kusano |
7d535a |
Likewise warning messages are directed to a single handler routine
|
|
kusano |
7d535a |
that can be specified with a call to <tt>TIFFSetWarningHandler</tt>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Basic File Handling
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The library is modeled after the normal UNIX stdio library.
|
|
kusano |
7d535a |
For example, to read from an existing TIFF image the
|
|
kusano |
7d535a |
file must first be opened:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("foo.tif", "r");
|
|
kusano |
7d535a |
... do stuff ...
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The handle returned by <tt>TIFFOpen</tt> is opaque, that is
|
|
kusano |
7d535a |
the application is not permitted to know about its contents.
|
|
kusano |
7d535a |
All subsequent library calls for this file must pass the handle
|
|
kusano |
7d535a |
as an argument.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
To create or overwrite a TIFF image the file is also opened, but with
|
|
kusano |
7d535a |
a <tt>"w"</tt> argument:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("foo.tif", "w");
|
|
kusano |
7d535a |
... do stuff ...
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
If the file already exists it is first truncated to zero length.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Note that unlike the stdio library TIFF image files may not be
|
|
kusano |
7d535a |
opened for both reading and writing;
|
|
kusano |
7d535a |
there is no support for altering the contents of a TIFF file.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> buffers much information associated with writing a
|
|
kusano |
7d535a |
valid TIFF image. Consequently, when writing a TIFF image it is necessary
|
|
kusano |
7d535a |
to always call <tt>TIFFClose</tt> or <tt>TIFFFlush</tt> to flush any
|
|
kusano |
7d535a |
buffered information to a file. Note that if you call <tt>TIFFClose</tt>
|
|
kusano |
7d535a |
you do not need to call <tt>TIFFFlush</tt>.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFF Directories
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFF supports the storage of multiple images in a single file.
|
|
kusano |
7d535a |
Each image has an associated data structure termed a directory
|
|
kusano |
7d535a |
that houses all the information about the format and content of the
|
|
kusano |
7d535a |
image data.
|
|
kusano |
7d535a |
Images in a file are usually related but they do not need to be; it
|
|
kusano |
7d535a |
is perfectly alright to store a color image together with a black and
|
|
kusano |
7d535a |
white image.
|
|
kusano |
7d535a |
Note however that while images may be related their directories are
|
|
kusano |
7d535a |
not.
|
|
kusano |
7d535a |
That is, each directory stands on its own; their is no need to read
|
|
kusano |
7d535a |
an unrelated directory in order to properly interpret the contents
|
|
kusano |
7d535a |
of an image.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> provides several routines for reading and writing
|
|
kusano |
7d535a |
directories. In normal use there is no need to explicitly
|
|
kusano |
7d535a |
read or write a directory: the library automatically reads the first
|
|
kusano |
7d535a |
directory in a file when opened for reading, and directory information
|
|
kusano |
7d535a |
to be written is automatically accumulated and written when writing
|
|
kusano |
7d535a |
(assuming <tt>TIFFClose</tt> or <tt>TIFFFlush</tt> are called).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
For a file open for reading the <tt>TIFFSetDirectory</tt> routine can
|
|
kusano |
7d535a |
be used to select an arbitrary directory; directories are referenced by
|
|
kusano |
7d535a |
number with the numbering starting at 0. Otherwise the
|
|
kusano |
7d535a |
<tt>TIFFReadDirectory</tt> and <tt>TIFFWriteDirectory</tt> routines can
|
|
kusano |
7d535a |
be used for sequential access to directories.
|
|
kusano |
7d535a |
For example, to count the number of directories in a file the following
|
|
kusano |
7d535a |
code might be used:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main(int argc, char* argv[])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen(argv[1], "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
int dircount = 0;
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
dircount++;
|
|
kusano |
7d535a |
} while (TIFFReadDirectory(tif));
|
|
kusano |
7d535a |
printf("%d directories in %s\n", dircount, argv[1]);
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
exit(0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Finally, note that there are several routines for querying the
|
|
kusano |
7d535a |
directory status of an open file:
|
|
kusano |
7d535a |
<tt>TIFFCurrentDirectory</tt> returns the index of the current
|
|
kusano |
7d535a |
directory and
|
|
kusano |
7d535a |
<tt>TIFFLastDirectory</tt> returns an indication of whether the
|
|
kusano |
7d535a |
current directory is the last directory in a file.
|
|
kusano |
7d535a |
There is also a routine, <tt>TIFFPrintDirectory</tt>, that can
|
|
kusano |
7d535a |
be called to print a formatted description of the contents of
|
|
kusano |
7d535a |
the current directory; consult the manual page for complete details.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFF Tags
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Image-related information such as the image width and height, number
|
|
kusano |
7d535a |
of samples, orientation, colorimetric information, etc.
|
|
kusano |
7d535a |
are stored in each image
|
|
kusano |
7d535a |
directory in fields or tags.
|
|
kusano |
7d535a |
Tags are identified by a number that is usually a value registered
|
|
kusano |
7d535a |
with the Aldus (now Adobe) Corporation.
|
|
kusano |
7d535a |
Beware however that some vendors write
|
|
kusano |
7d535a |
TIFF images with tags that are unregistered; in this case interpreting
|
|
kusano |
7d535a |
their contents is usually a waste of time.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> reads the contents of a directory all at once
|
|
kusano |
7d535a |
and converts the on-disk information to an appropriate in-memory
|
|
kusano |
7d535a |
form. While the TIFF specification permits an arbitrary set of
|
|
kusano |
7d535a |
tags to be defined and used in a file, the library only understands
|
|
kusano |
7d535a |
a limited set of tags.
|
|
kusano |
7d535a |
Any unknown tags that are encountered in a file are ignored.
|
|
kusano |
7d535a |
There is a mechanism to extend the set of tags the library handles
|
|
kusano |
7d535a |
without modifying the library itself;
|
|
kusano |
7d535a |
this is described elsewhere.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> provides two interfaces for getting and setting tag
|
|
kusano |
7d535a |
values: <tt>TIFFGetField</tt> and <tt>TIFFSetField</tt>.
|
|
kusano |
7d535a |
These routines use a variable argument list-style interface to pass
|
|
kusano |
7d535a |
parameters of different type through a single function interface.
|
|
kusano |
7d535a |
The get interface takes one or more pointers to memory locations
|
|
kusano |
7d535a |
where the tag values are to be returned and also returns one or
|
|
kusano |
7d535a |
zero according to whether the requested tag is defined in the directory.
|
|
kusano |
7d535a |
The set interface takes the tag values either by-reference or
|
|
kusano |
7d535a |
by-value.
|
|
kusano |
7d535a |
The TIFF specification defines
|
|
kusano |
7d535a |
default values for some tags.
|
|
kusano |
7d535a |
To get the value of a tag, or its default value if it is undefined,
|
|
kusano |
7d535a |
the <tt>TIFFGetFieldDefaulted</tt> interface may be used.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The manual pages for the tag get and set routines specifiy the exact data types
|
|
kusano |
7d535a |
and calling conventions required for each tag supported by the library.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFF Compression Schemes
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> includes support for a wide variety of
|
|
kusano |
7d535a |
data compression schemes.
|
|
kusano |
7d535a |
In normal operation a compression scheme is automatically used when
|
|
kusano |
7d535a |
the TIFF <tt>Compression</tt> tag is set, either by opening a file
|
|
kusano |
7d535a |
for reading, or by setting the tag when writing.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Compression schemes are implemented by software modules termed codecs
|
|
kusano |
7d535a |
that implement decoder and encoder routines that hook into the
|
|
kusano |
7d535a |
core library i/o support.
|
|
kusano |
7d535a |
Codecs other than those bundled with the library can be registered
|
|
kusano |
7d535a |
for use with the <tt>TIFFRegisterCODEC</tt> routine.
|
|
kusano |
7d535a |
This interface can also be used to override the core-library
|
|
kusano |
7d535a |
implementation for a compression scheme.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Byte Order
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The TIFF specification says, and has always said, that
|
|
kusano |
7d535a |
a correct TIFF
|
|
kusano |
7d535a |
reader must handle images in big-endian and little-endian byte order.
|
|
kusano |
7d535a |
<tt>libtiff</tt> conforms in this respect.
|
|
kusano |
7d535a |
Consequently there is no means to force a specific
|
|
kusano |
7d535a |
byte order for the data written to a TIFF image file (data is
|
|
kusano |
7d535a |
written in the native order of the host CPU unless appending to
|
|
kusano |
7d535a |
an existing file, in which case it is written in the byte order
|
|
kusano |
7d535a |
specified in the file).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Data Placement
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The TIFF specification requires that all information except an
|
|
kusano |
7d535a |
8-byte header can be placed anywhere in a file.
|
|
kusano |
7d535a |
In particular, it is perfectly legitimate for directory information
|
|
kusano |
7d535a |
to be written after the image data itself.
|
|
kusano |
7d535a |
Consequently TIFF is inherently not suitable for passing through a
|
|
kusano |
7d535a |
stream-oriented mechanism such as UNIX pipes.
|
|
kusano |
7d535a |
Software that require that data be organized in a file in a particular
|
|
kusano |
7d535a |
order (e.g. directory information before image data) does not
|
|
kusano |
7d535a |
correctly support TIFF.
|
|
kusano |
7d535a |
<tt>libtiff</tt> provides no mechanism for controlling the placement
|
|
kusano |
7d535a |
of data in a file; image data is typically written before directory
|
|
kusano |
7d535a |
information.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFRGBAImage Support
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>libtiff</tt> provides a high-level interface for reading image
|
|
kusano |
7d535a |
data from a TIFF file. This interface handles the details of
|
|
kusano |
7d535a |
data organization and format for a wide variety of TIFF files;
|
|
kusano |
7d535a |
at least the large majority of those files that one would normally
|
|
kusano |
7d535a |
encounter. Image data is, by default, returned as ABGR
|
|
kusano |
7d535a |
pixels packed into 32-bit words (8 bits per sample). Rectangular
|
|
kusano |
7d535a |
rasters can be read or data can be intercepted at an intermediate
|
|
kusano |
7d535a |
level and packed into memory in a format more suitable to the
|
|
kusano |
7d535a |
application.
|
|
kusano |
7d535a |
The library handles all the details of the format of data stored on
|
|
kusano |
7d535a |
disk and, in most cases, if any colorspace conversions are required:
|
|
kusano |
7d535a |
bilevel to RGB, greyscale to RGB, CMYK to RGB, YCbCr to RGB, 16-bit
|
|
kusano |
7d535a |
samples to 8-bit samples, associated/unassociated alpha, etc.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
There are two ways to read image data using this interface. If
|
|
kusano |
7d535a |
all the data is to be stored in memory and manipulated at once,
|
|
kusano |
7d535a |
then the routine <tt>TIFFReadRGBAImage</tt> can be used:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main(int argc, char* argv[])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen(argv[1], "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
uint32 w, h;
|
|
kusano |
7d535a |
size_t npixels;
|
|
kusano |
7d535a |
uint32* raster;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
|
|
kusano |
7d535a |
npixels = w * h;
|
|
kusano |
7d535a |
raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
|
|
kusano |
7d535a |
if (raster != NULL) {
|
|
kusano |
7d535a |
if (TIFFReadRGBAImage(tif, w, h, raster, 0)) {
|
|
kusano |
7d535a |
...process raster data...
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(raster);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
exit(0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Note above that <tt>_TIFFmalloc</tt> is used to allocate memory for
|
|
kusano |
7d535a |
the raster passed to <tt>TIFFReadRGBAImage</tt>; this is important
|
|
kusano |
7d535a |
to insure the ``appropriate type of memory'' is passed on machines
|
|
kusano |
7d535a |
with segmented architectures.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Alternatively, <tt>TIFFReadRGBAImage</tt> can be replaced with a
|
|
kusano |
7d535a |
more low-level interface that permits an application to have more
|
|
kusano |
7d535a |
control over this reading procedure. The equivalent to the above
|
|
kusano |
7d535a |
is:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main(int argc, char* argv[])
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen(argv[1], "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
TIFFRGBAImage img;
|
|
kusano |
7d535a |
char emsg[1024];
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
|
|
kusano |
7d535a |
size_t npixels;
|
|
kusano |
7d535a |
uint32* raster;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
npixels = img.width * img.height;
|
|
kusano |
7d535a |
raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
|
|
kusano |
7d535a |
if (raster != NULL) {
|
|
kusano |
7d535a |
if (TIFFRGBAImageGet(&img, raster, img.width, img.height)) {
|
|
kusano |
7d535a |
...process raster data...
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(raster);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
TIFFRGBAImageEnd(&img);
|
|
kusano |
7d535a |
} else
|
|
kusano |
7d535a |
TIFFError(argv[1], emsg);
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
exit(0);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
However this usage does not take advantage of the more fine-grained
|
|
kusano |
7d535a |
control that's possible. That is, by using this interface it is
|
|
kusano |
7d535a |
possible to:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
repeatedly fetch (and manipulate) an image without opening
|
|
kusano |
7d535a |
and closing the file
|
|
kusano |
7d535a |
interpose a method for packing raster pixel data according to
|
|
kusano |
7d535a |
application-specific needs (or write the data at all)
|
|
kusano |
7d535a |
interpose methods that handle TIFF formats that are not already
|
|
kusano |
7d535a |
handled by the core library
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The first item means that, for example, image viewers that want to
|
|
kusano |
7d535a |
handle multiple files can cache decoding information in order to
|
|
kusano |
7d535a |
speedup the work required to display a TIFF image.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The second item is the main reason for this interface. By interposing
|
|
kusano |
7d535a |
a "put method" (the routine that is called to pack pixel data in
|
|
kusano |
7d535a |
the raster) it is possible share the core logic that understands how
|
|
kusano |
7d535a |
to deal with TIFF while packing the resultant pixels in a format that
|
|
kusano |
7d535a |
is optimized for the application. This alternate format might be very
|
|
kusano |
7d535a |
different than the 8-bit per sample ABGR format the library writes by
|
|
kusano |
7d535a |
default. For example, if the application is going to display the image
|
|
kusano |
7d535a |
on an 8-bit colormap display the put routine might take the data and
|
|
kusano |
7d535a |
convert it on-the-fly to the best colormap indices for display.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The last item permits an application to extend the library
|
|
kusano |
7d535a |
without modifying the core code.
|
|
kusano |
7d535a |
By overriding the code provided an application might add support
|
|
kusano |
7d535a |
for some esoteric flavor of TIFF that it needs, or it might
|
|
kusano |
7d535a |
substitute a packing routine that is able to do optimizations
|
|
kusano |
7d535a |
using application/environment-specific information.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The TIFF image viewer found in tools/sgigt.c is an example
|
|
kusano |
7d535a |
of an application that makes use of the <tt>TIFFRGBAImage</tt>
|
|
kusano |
7d535a |
support.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Scanline-based Image I/O
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The simplest interface provided by <tt>libtiff</tt> is a
|
|
kusano |
7d535a |
scanline-oriented interface that can be used to read TIFF
|
|
kusano |
7d535a |
images that have their image data organized in strips
|
|
kusano |
7d535a |
(trying to use this interface to read data written in tiles
|
|
kusano |
7d535a |
will produce errors.)
|
|
kusano |
7d535a |
A scanline is a one pixel high row of image data whose width
|
|
kusano |
7d535a |
is the width of the image.
|
|
kusano |
7d535a |
Data is returned packed if the image data is stored with samples
|
|
kusano |
7d535a |
packed together, or as arrays of separate samples if the data
|
|
kusano |
7d535a |
is stored with samples separated.
|
|
kusano |
7d535a |
The major limitation of the scanline-oriented interface, other
|
|
kusano |
7d535a |
than the need to first identify an existing file as having a
|
|
kusano |
7d535a |
suitable organization, is that random access to individual
|
|
kusano |
7d535a |
scanlines can only be provided when data is not stored in a
|
|
kusano |
7d535a |
compressed format, or when the number of rows in a strip
|
|
kusano |
7d535a |
of image data is set to one (<tt>RowsPerStrip</tt> is one).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Two routines are provided for scanline-based i/o:
|
|
kusano |
7d535a |
<tt>TIFFReadScanline</tt>
|
|
kusano |
7d535a |
and
|
|
kusano |
7d535a |
<tt>TIFFWriteScanline</tt>.
|
|
kusano |
7d535a |
For example, to read the contents of a file that
|
|
kusano |
7d535a |
is assumed to be organized in strips, the following might be used:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
uint32 imagelength;
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
uint32 row;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
|
|
kusano |
7d535a |
buf = _TIFFmalloc(TIFFScanlineSize(tif));
|
|
kusano |
7d535a |
for (row = 0; row < imagelength; row++)
|
|
kusano |
7d535a |
tiffreadscanline(tif, buf, row);
|
|
kusano |
7d535a |
_tifffree(buf);
|
|
kusano |
7d535a |
tiffclose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>TIFFScanlineSize</tt> returns the number of bytes in
|
|
kusano |
7d535a |
a decoded scanline, as returned by <tt>TIFFReadScanline</tt>.
|
|
kusano |
7d535a |
Note however that if the file had been create with samples
|
|
kusano |
7d535a |
written in separate planes, then the above code would only
|
|
kusano |
7d535a |
read data that contained the first sample of each pixel;
|
|
kusano |
7d535a |
to handle either case one might use the following instead:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
uint32 imagelength;
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
uint32 row;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
|
|
kusano |
7d535a |
buf = _TIFFmalloc(TIFFScanlineSize(tif));
|
|
kusano |
7d535a |
if (config == PLANARCONFIG_CONTIG) {
|
|
kusano |
7d535a |
for (row = 0; row < imagelength; row++)
|
|
kusano |
7d535a |
tiffreadscanline(tif, buf, row);
|
|
kusano |
7d535a |
} else if (config == planarconfig_separate) {
|
|
kusano |
7d535a |
uint16 s, nsamples;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
tiffgetfield(tif, tifftag_samplesperpixel, &nsamples);
|
|
kusano |
7d535a |
for (s = 0; s < nsamples; s++)
|
|
kusano |
7d535a |
for (row = 0; row < imagelength; row++)
|
|
kusano |
7d535a |
tiffreadscanline(tif, buf, row, s);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_tifffree(buf);
|
|
kusano |
7d535a |
tiffclose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Beware however that if the following code were used instead to
|
|
kusano |
7d535a |
read data in the case <tt>PLANARCONFIG_SEPARATE</tt>,...
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt> for (row = 0; row < imagelength; row++) </tt>
|
|
kusano |
7d535a |
for (s = 0; s < nsamples; s++)
|
|
kusano |
7d535a |
tiffreadscanline(tif, buf, row, s);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
...then problems would arise if <tt>RowsPerStrip</tt> was not one
|
|
kusano |
7d535a |
because the order in which scanlines are requested would require
|
|
kusano |
7d535a |
random access to data within strips (something that is not supported
|
|
kusano |
7d535a |
by the library when strips are compressed).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Strip-oriented Image I/O
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The strip-oriented interfaces provided by the library provide
|
|
kusano |
7d535a |
access to entire strips of data. Unlike the scanline-oriented
|
|
kusano |
7d535a |
calls, data can be read or written compressed or uncompressed.
|
|
kusano |
7d535a |
Accessing data at a strip (or tile) level is often desirable
|
|
kusano |
7d535a |
because there are no complications with regard to random access
|
|
kusano |
7d535a |
to data within strips.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
A simple example of reading an image by strips is:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
tstrip_t strip;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
buf = _TIFFmalloc(TIFFStripSize(tif));
|
|
kusano |
7d535a |
for (strip = 0; strip < tiffnumberofstrips(tif); strip++)
|
|
kusano |
7d535a |
tiffreadencodedstrip(tif, strip, buf, (tsize_t) -1);
|
|
kusano |
7d535a |
_tifffree(buf);
|
|
kusano |
7d535a |
tiffclose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Notice how a strip size of <tt>-1</tt> is used; <tt>TIFFReadEncodedStrip</tt>
|
|
kusano |
7d535a |
will calculate the appropriate size in this case.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The above code reads strips in the order in which the
|
|
kusano |
7d535a |
data is physically stored in the file. If multiple samples
|
|
kusano |
7d535a |
are present and data is stored with <tt>PLANARCONFIG_SEPARATE</tt>
|
|
kusano |
7d535a |
then all the strips of data holding the first sample will be
|
|
kusano |
7d535a |
read, followed by strips for the second sample, etc.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Finally, note that the last strip of data in an image may have fewer
|
|
kusano |
7d535a |
rows in it than specified by the <tt>RowsPerStrip</tt> tag. A
|
|
kusano |
7d535a |
reader should not assume that each decoded strip contains a full
|
|
kusano |
7d535a |
set of rows in it.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The following is an example of how to read raw strips of data from
|
|
kusano |
7d535a |
a file:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
tstrip_t strip;
|
|
kusano |
7d535a |
uint32* bc;
|
|
kusano |
7d535a |
uint32 stripsize;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
|
|
kusano |
7d535a |
stripsize = bc[0];
|
|
kusano |
7d535a |
buf = _TIFFmalloc(stripsize);
|
|
kusano |
7d535a |
for (strip = 0; strip < tiffnumberofstrips(tif); strip++) {
|
|
kusano |
7d535a |
if (bc[strip] > stripsize) {
|
|
kusano |
7d535a |
buf = _TIFFrealloc(buf, bc[strip]);
|
|
kusano |
7d535a |
stripsize = bc[strip];
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
TIFFReadRawStrip(tif, strip, buf, bc[strip]);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
_TIFFfree(buf);
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
As above the strips are read in the order in which they are
|
|
kusano |
7d535a |
physically stored in the file; this may be different from the
|
|
kusano |
7d535a |
logical ordering expected by an application.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Tile-oriented Image I/O
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Tiles of data may be read and written in a manner similar to strips.
|
|
kusano |
7d535a |
With this interface, an image is
|
|
kusano |
7d535a |
broken up into a set of rectangular areas that may have dimensions
|
|
kusano |
7d535a |
less than the image width and height. All the tiles
|
|
kusano |
7d535a |
in an image have the same size, and the tile width and length must each
|
|
kusano |
7d535a |
be a multiple of 16 pixels. Tiles are ordered left-to-right and
|
|
kusano |
7d535a |
top-to-bottom in an image. As for scanlines, samples can be packed
|
|
kusano |
7d535a |
contiguously or separately. When separated, all the tiles for a sample
|
|
kusano |
7d535a |
are colocated in the file. That is, all the tiles for sample 0 appear
|
|
kusano |
7d535a |
before the tiles for sample 1, etc.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Tiles and strips may also be extended in a z dimension to form
|
|
kusano |
7d535a |
volumes. Data volumes are organized as "slices". That is, all the
|
|
kusano |
7d535a |
data for a slice is colocated. Volumes whose data is organized in
|
|
kusano |
7d535a |
tiles can also have a tile depth so that data can be organized in
|
|
kusano |
7d535a |
cubes.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
There are actually two interfaces for tiles.
|
|
kusano |
7d535a |
One interface is similar to scanlines, to read a tiled image,
|
|
kusano |
7d535a |
code of the following sort might be used:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>main() </tt>
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
uint32 imageWidth, imageLength;
|
|
kusano |
7d535a |
uint32 tileWidth, tileLength;
|
|
kusano |
7d535a |
uint32 x, y;
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth);
|
|
kusano |
7d535a |
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileLength);
|
|
kusano |
7d535a |
buf = _TIFFmalloc(TIFFTileSize(tif));
|
|
kusano |
7d535a |
for (y = 0; y < imagelength; y += tilelength)
|
|
kusano |
7d535a |
for (x = 0; x < imagewidth; x += tilewidth)
|
|
kusano |
7d535a |
tiffreadtile(tif, buf, x, y, 0);
|
|
kusano |
7d535a |
_tifffree(buf);
|
|
kusano |
7d535a |
tiffclose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
(once again, we assume samples are packed contiguously.)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Alternatively a direct interface to the low-level data is provided
|
|
kusano |
7d535a |
a la strips. Tiles can be read with
|
|
kusano |
7d535a |
<tt>TIFFReadEncodedTile</tt> or <tt>TIFFReadRawTile</tt>,
|
|
kusano |
7d535a |
and written with <tt>TIFFWriteEncodedTile</tt> or
|
|
kusano |
7d535a |
<tt>TIFFWriteRawTile</tt>. For example, to read all the tiles in an image:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
<tt>#include "tiffio.h" </tt>
|
|
kusano |
7d535a |
main()
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF* tif = TIFFOpen("myfile.tif", "r");
|
|
kusano |
7d535a |
if (tif) {
|
|
kusano |
7d535a |
tdata_t buf;
|
|
kusano |
7d535a |
ttile_t tile;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
buf = _TIFFmalloc(TIFFTileSize(tif));
|
|
kusano |
7d535a |
for (tile = 0; tile < tiffnumberoftiles(tif); tile++)
|
|
kusano |
7d535a |
tiffreadencodedtile(tif, tile, buf, (tsize_t) -1);
|
|
kusano |
7d535a |
_tifffree(buf);
|
|
kusano |
7d535a |
tiffclose(tif);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Other Stuff
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Some other stuff will almost certainly go here...
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Last updated: $Date: 2005/12/28 06:53:18 $
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|