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