| 1 |
TIFFOpen |
| 2 |
======== |
| 3 |
|
| 4 |
Synopsis |
| 5 |
-------- |
| 6 |
|
| 7 |
.. highlight:: c |
| 8 |
|
| 9 |
:: |
| 10 |
|
| 11 |
#include <tiffio.h> |
| 12 |
|
| 13 |
.. c:function:: TIFF* TIFFOpen(const char* filename, const char* mode) |
| 14 |
|
| 15 |
.. c:function:: TIFF* TIFFOpenW(const wchar_t* name, const char* mode) |
| 16 |
|
| 17 |
.. c:function:: TIFF* TIFFFdOpen(const int fd, const char* filename, const char*mode) |
| 18 |
|
| 19 |
.. c:function:: TIFF* TIFFOpenExt(const char* filename, const char* mode, TIFFOpenOptions* opts) |
| 20 |
|
| 21 |
.. c:function:: TIFF* TIFFOpenWExt(const wchar_t* name, const char* mode, TIFFOpenOptions* opts) |
| 22 |
|
| 23 |
.. c:function:: TIFF* TIFFFdOpenExt(const int fd, const char* filename, const char*mode, TIFFOpenOptions* opts) |
| 24 |
|
| 25 |
.. c:function:: const char * TIFFSetFileName(TIFF* tif) |
| 26 |
|
| 27 |
.. c:function:: int TIFFSetFileno(TIFF* tif, int fd) |
| 28 |
|
| 29 |
.. c:function:: int TIFFSetMode(TIFF* tif, int mode) |
| 30 |
|
| 31 |
.. c:type:: tsize_t (*TIFFReadWriteProc)(thandle_t, tdata_t, tsize_t) |
| 32 |
.. c:type:: toff_t (*TIFFSeekProc)(thandle_t, toff_t, int) |
| 33 |
.. c:type:: int (*TIFFCloseProc)(thandle_t) |
| 34 |
.. c:type:: toff_t (*TIFFSizeProc)(thandle_t) |
| 35 |
.. c:type:: int (*TIFFMapFileProc)(thandle_t, tdata_t*, toff_t*) |
| 36 |
.. c:type:: void (*TIFFUnmapFileProc)(thandle_t, tdata_t, toff_t) |
| 37 |
|
| 38 |
.. c:function:: TIFF* TIFFClientOpen(const char* filename, const char* mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc) |
| 39 |
|
| 40 |
.. c:function:: TIFF* TIFFClientOpenExt(const char* filename, const char* mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc, TIFFOpenOptions* opts) |
| 41 |
|
| 42 |
.. c:function:: thandle_t TIFFClientdata(TIFF* tif) |
| 43 |
|
| 44 |
.. c:function:: thandle_t TIFFSetClientdata(TIFF* tif, thandle_t newvalue) |
| 45 |
|
| 46 |
Description |
| 47 |
----------- |
| 48 |
|
| 49 |
:c:func:`TIFFOpen` opens a TIFF file whose name is *filename* |
| 50 |
and returns a handle to be used in subsequent calls to routines in |
| 51 |
:program:`libtiff`. If the open operation fails, then |
| 52 |
:c:macro:`NULL` (0) is returned. The *mode* parameter specifies if |
| 53 |
the file is to be opened for reading (``r``) or (``r+``), writing (``w``), or |
| 54 |
appending (``a``) and, optionally, whether to override certain |
| 55 |
default aspects of library operation (see below Options_). |
| 56 |
|
| 57 |
The *mode* (``r``) opens only an **existing** file for reading and (``r+``) |
| 58 |
for reading and writing. |
| 59 |
When a file is opened for appending, existing data will not |
| 60 |
be touched; instead new data will be written as additional subfiles. |
| 61 |
If an existing file is opened for writing, all previous data is |
| 62 |
overwritten. |
| 63 |
|
| 64 |
If a file is opened for reading, the first TIFF directory in the file |
| 65 |
is automatically read. |
| 66 |
If a file is opened for writing or appending, a default directory |
| 67 |
is automatically created for writing subsequent data. |
| 68 |
This directory has all the default values specified in TIFF Revision 6.0: |
| 69 |
|
| 70 |
* ``BitsPerSample`` = 1, |
| 71 |
* ``ThreshHolding`` = "bilevel art scan" |
| 72 |
* ``FillOrder`` = 1 (most significant bit of each data byte is filled first) |
| 73 |
* ``Orientation`` = 1 (the 0th row represents the visual top of the image, |
| 74 |
and the 0th column represents the visual left hand side), |
| 75 |
* ``SamplesPerPixel`` = 1, |
| 76 |
* ``RowsPerStrip`` = ∞, |
| 77 |
* ``ResolutionUnit`` = 2 (inches), and |
| 78 |
* ``Compression`` = 1 (no compression). |
| 79 |
|
| 80 |
To alter these values, or to define values for additional fields, |
| 81 |
:c:func:`TIFFSetField` must be used. |
| 82 |
|
| 83 |
:c:func:`TIFFOpenW` opens a TIFF file with a Unicode filename, for read/writing. |
| 84 |
|
| 85 |
:c:func:`TIFFFdOpen` is like :c:func:`TIFFOpen` except that it opens a |
| 86 |
TIFF file given an open file descriptor *fd*. |
| 87 |
The file's name and mode must reflect that of the open descriptor. |
| 88 |
Even for write-only mode, ``libtiff`` needs read permissions because |
| 89 |
some of its functions need to read back the partially written TIFF file. |
| 90 |
The object associated with the file descriptor **must support random access**. |
| 91 |
In order to close a TIFF file opened with :c:func:`TIFFFdOpen` |
| 92 |
first :c:func:`TIFFCleanup` should be called to free the internal |
| 93 |
TIFF structure without closing the file handle and afterwards the |
| 94 |
file should be closed using its file descriptor *fd*. |
| 95 |
|
| 96 |
:c:func:`TIFFOpenExt` (added in libtiff 4.5) is like :c:func:`TIFFOpen`, |
| 97 |
but options, such as re-entrant error and warning handlers and a limit in byte |
| 98 |
that libtiff internal memory allocation functions are allowed to request per call |
| 99 |
may be passed with the *opts* argument. The *opts* argument may be NULL. |
| 100 |
Refer to :doc:`TIFFOpenOptions` for allocating and filling the *opts* argument |
| 101 |
parameters. The allocated memory for :c:type:`TIFFOpenOptions` |
| 102 |
can be released straight after successful execution of the related |
| 103 |
"TIFFOpenExt" functions. |
| 104 |
|
| 105 |
:c:func:`TIFFOpenWExt` (added in libtiff 4.5) is like :c:func:`TIFFOpenExt`, |
| 106 |
but opens a TIFF file with a Unicode filename. |
| 107 |
|
| 108 |
:c:func:`TIFFFdOpenExt` (added in libtiff 4.5) is like :c:func:`TIFFFdOpen`, |
| 109 |
but options argument *opts* like for :c:func:`TIFFOpenExt` can be passed. |
| 110 |
|
| 111 |
:c:func:`TIFFSetFileName` sets the file name in the tif-structure |
| 112 |
and returns the old file name. |
| 113 |
|
| 114 |
:c:func:`TIFFSetFileno` overwrites a copy of the open file's I/O descriptor, |
| 115 |
that was saved when the TIFF file was first opened, |
| 116 |
and returns the previous value. See note below. |
| 117 |
|
| 118 |
:c:func:`TIFFSetMode` sets the ``libtiff`` open mode in the tif-structure |
| 119 |
and returns the old mode. |
| 120 |
|
| 121 |
:c:func:`TIFFClientOpen` is like :c:func:`TIFFOpen` except that the caller |
| 122 |
supplies a collection of functions that the library will use to do UNIX-like |
| 123 |
I/O operations. The *readproc* and *writeproc* functions are called to read |
| 124 |
and write data at the current file position. |
| 125 |
*seekproc* is called to change the current file position à la :c:func:`lseek` (2). |
| 126 |
*closeproc* is invoked to release any resources associated with an open file. |
| 127 |
*sizeproc* is invoked to obtain the size in bytes of a file. |
| 128 |
*mapproc* and *unmapproc* are called to map and unmap a file's contents in |
| 129 |
memory; c.f. :c:func:`mmap` (2) and :c:func:`munmap` (2). |
| 130 |
The *clientdata* parameter is an opaque "handle" passed to the client-specified |
| 131 |
routines passed as parameters to :c:func:`TIFFClientOpen`. |
| 132 |
|
| 133 |
:c:func:`TIFFClientOpenExt` (added in libtiff 4.5) is like :c:func:`TIFFClientOpen`, |
| 134 |
but options argument *opts* like for :c:func:`TIFFOpenExt` can be passed. |
| 135 |
|
| 136 |
:c:func:`TIFFClientdata` returns open file's clientdata handle, |
| 137 |
which is the real open file's I/O descriptor used by ``libtiff``. |
| 138 |
Note: Within tif_unix.c this handle is converted into an integer file descriptor. |
| 139 |
|
| 140 |
:c:func:`TIFFSetClientdata` sets open file's clientdata, and return previous value. |
| 141 |
The clientdata is used as open file's I/O descriptor within ``libtiff``. |
| 142 |
|
| 143 |
.. note:: |
| 144 |
*clientdata* is used as file descriptor or handle of the opened TIFF file within |
| 145 |
`libtif`, whereas the file descriptor *fd* (changeable by :c:func:`TIFFSetFileno`) |
| 146 |
is only set once to the value of *clientdata* converted to an integer |
| 147 |
(in tif_win32.c as well as in tif_unix.c). |
| 148 |
When updating the file's clientdata with :c:func:`TIFFSetClientdata`, |
| 149 |
the *fd* value is **not** updated. |
| 150 |
|
| 151 |
Options |
| 152 |
------- |
| 153 |
|
| 154 |
The open mode parameter can include the following flags in |
| 155 |
addition to the ``r``, ``r+``, ``w``, and ``a`` flags. |
| 156 |
Note however that option flags must follow the read-write-append |
| 157 |
specification. |
| 158 |
|
| 159 |
Note 2: Also for ``w`` the file will be opened with *read access* rights |
| 160 |
because ``libtiff`` needs to read back the partially written TIFF file |
| 161 |
for some of its functions. |
| 162 |
|
| 163 |
|
| 164 |
``l``: |
| 165 |
|
| 166 |
When creating a new file force information be written with |
| 167 |
Little-Endian byte order (but see below). |
| 168 |
By default the library will create new files using the native |
| 169 |
CPU byte order. |
| 170 |
|
| 171 |
``b``: |
| 172 |
|
| 173 |
When creating a new file force information be written with |
| 174 |
Big-Endian byte order (but see below). |
| 175 |
By default the library will create new files using the native |
| 176 |
CPU byte order. |
| 177 |
|
| 178 |
``L``: |
| 179 |
|
| 180 |
Force image data that is read or written to be treated with |
| 181 |
bits filled from Least Significant Bit (LSB) to |
| 182 |
Most Significant Bit (MSB). |
| 183 |
Note that this is the opposite to the way the library has |
| 184 |
worked from its inception. |
| 185 |
|
| 186 |
``B``: |
| 187 |
|
| 188 |
Force image data that is read or written to be treated with |
| 189 |
bits filled from Most Significant Bit (MSB) to |
| 190 |
Least Significant Bit (LSB); this is the default. |
| 191 |
|
| 192 |
``H``: |
| 193 |
|
| 194 |
Force image data that is read or written to be treated with |
| 195 |
bits filled in the same order as the native |
| 196 |
CPU. |
| 197 |
|
| 198 |
``M``: |
| 199 |
|
| 200 |
Enable the use of memory-mapped files for images opened read-only. |
| 201 |
If the underlying system does not support memory-mapped files |
| 202 |
or if the specific image being opened cannot be memory-mapped |
| 203 |
then the library will fallback to using the normal system interface |
| 204 |
for reading information. |
| 205 |
By default the library will attempt to use memory-mapped files. |
| 206 |
|
| 207 |
``m``: |
| 208 |
|
| 209 |
Disable the use of memory-mapped files. |
| 210 |
|
| 211 |
``C``: |
| 212 |
|
| 213 |
Enable the use of "strip chopping" when reading images |
| 214 |
that are comprised of a single strip or tile of uncompressed data. |
| 215 |
Strip chopping is a mechanism by which the library will automatically |
| 216 |
convert the single-strip image to multiple strips, |
| 217 |
each of which has about 8 Kilobytes of data. |
| 218 |
This facility can be useful in reducing the amount of memory used |
| 219 |
to read an image because the library normally reads each strip |
| 220 |
in its entirety. |
| 221 |
Strip chopping does however alter the apparent contents of the |
| 222 |
image because when an image is divided into multiple strips it |
| 223 |
looks as though the underlying file contains multiple separate |
| 224 |
strips. |
| 225 |
Finally, note that default handling of strip chopping is a compile-time |
| 226 |
configuration parameter. |
| 227 |
The default behaviour, for backwards compatibility, is to enable |
| 228 |
strip chopping. |
| 229 |
|
| 230 |
``c``: |
| 231 |
|
| 232 |
Disable the use of strip chopping when reading images. |
| 233 |
|
| 234 |
``h``: |
| 235 |
|
| 236 |
Read TIFF header only, do not load the first image directory. That could be |
| 237 |
useful in case of the broken first directory. We can open the file and proceed |
| 238 |
to the other directories. |
| 239 |
|
| 240 |
``4``: |
| 241 |
|
| 242 |
ClassicTIFF for creating a file (default) |
| 243 |
|
| 244 |
``8``: |
| 245 |
|
| 246 |
BigTIFF for creating a file. |
| 247 |
|
| 248 |
``D``: |
| 249 |
|
| 250 |
Enable use of deferred strip/tile offset/bytecount array loading. They will |
| 251 |
be loaded the first time they are accessed to. This loading will be done in |
| 252 |
its entirety unless the O flag is also specified. |
| 253 |
|
| 254 |
``O``: |
| 255 |
|
| 256 |
On-demand loading of values of the strip/tile offset/bytecount arrays, limited |
| 257 |
to the requested strip/tile, instead of whole array loading (implies ``D``) |
| 258 |
|
| 259 |
Byte order |
| 260 |
---------- |
| 261 |
|
| 262 |
The TIFF specification (**all versions**) states that compliant readers |
| 263 |
"must be capable of reading images written in either byte order" . |
| 264 |
Nonetheless some software that claims to support the reading of |
| 265 |
TIFF images is incapable of reading images in anything but the native |
| 266 |
CPU byte order on which the software was written. |
| 267 |
(Especially notorious are applications written to run on Intel-based machines.) |
| 268 |
By default the library will create new files with the native |
| 269 |
byte-order of the CPU on which the application is run. |
| 270 |
This ensures optimal performance and is portable to any application |
| 271 |
that conforms to the TIFF specification. |
| 272 |
To force the library to use a specific byte-order when creating |
| 273 |
a new file the ``b`` and ``l`` option flags may be included in |
| 274 |
the call to open a file; for example, ``wb`` or ``wl``. |
| 275 |
|
| 276 |
Return values |
| 277 |
------------- |
| 278 |
|
| 279 |
Upon successful completion |
| 280 |
:c:func:`TIFFOpen`, :c:func:`TIFFFdOpen`, and :c:func:`TIFFClientOpen` |
| 281 |
return a TIFF pointer. Otherwise, :c:macro:`NULL` is returned. |
| 282 |
|
| 283 |
Diagnostics |
| 284 |
----------- |
| 285 |
|
| 286 |
|
| 287 |
All error messages are directed to the :c:func:`TIFFErrorExtR` routine. |
| 288 |
Likewise, warning messages are directed to the :c:func:`TIFFWarningExtR` routine. |
| 289 |
|
| 290 |
``"%s": Bad mode``: |
| 291 |
|
| 292 |
The specified *mode* parameter was not one of ``r`` (read), ``w`` (write), |
| 293 |
or ``a`` (append). |
| 294 |
|
| 295 |
``"%s: Cannot open"``: |
| 296 |
|
| 297 |
:c:func:`TIFFOpen` was unable to open the specified filename for read/writing. |
| 298 |
|
| 299 |
``"Cannot read TIFF header"``: |
| 300 |
|
| 301 |
An error occurred while attempting to read the header information. |
| 302 |
|
| 303 |
``"Error writing TIFF header"``: |
| 304 |
|
| 305 |
An error occurred while writing the default header information |
| 306 |
for a new file. |
| 307 |
|
| 308 |
``"Not a TIFF file, bad magic number %d (0x%x)"``: |
| 309 |
|
| 310 |
The magic number in the header was not (hex) |
| 311 |
0x4d4d or (hex) 0x4949. |
| 312 |
|
| 313 |
``"Not a TIFF file, bad version number %d (0x%x)"``: |
| 314 |
|
| 315 |
The version field in the header was not 42 (decimal). |
| 316 |
|
| 317 |
``"Cannot append to file that has opposite byte ordering"``: |
| 318 |
|
| 319 |
A file with a byte ordering opposite to the native byte |
| 320 |
ordering of the current machine was opened for appending (``a``). |
| 321 |
This is a limitation of the library. |
| 322 |
|
| 323 |
See also |
| 324 |
-------- |
| 325 |
|
| 326 |
:doc:`libtiff` (3tiff), |
| 327 |
:doc:`TIFFClose` (3tiff), |
| 328 |
:doc:`TIFFStrileQuery` (3tiff), |
| 329 |
:doc:`TIFFOpenOptions` |