Leptonica  1.83.1
Image processing and image analysis suite
tiffio.c File Reference
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "allheaders.h"
#include "tiff.h"
#include "tiffio.h"

Go to the source code of this file.

Data Structures

struct  tiff_transform
 
struct  L_Memstream
 

Typedefs

typedef struct L_Memstream L_MEMSTREAM
 

Functions

static PIXpixReadFromTiffStream (TIFF *tif)
 
static l_int32 getTiffStreamResolution (TIFF *tif, l_int32 *pxres, l_int32 *pyres)
 
static l_int32 tiffReadHeaderTiff (TIFF *tif, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
 
static l_int32 writeCustomTiffTags (TIFF *tif, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
 
static l_int32 pixWriteToTiffStream (TIFF *tif, PIX *pix, l_int32 comptype, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
 
static TIFF * fopenTiff (FILE *fp, const char *modestring)
 
static TIFF * openTiff (const char *filename, const char *modestring)
 
static l_int32 getTiffCompressedFormat (l_uint16 tiffcomp)
 
static TIFF * fopenTiffMemstream (const char *filename, const char *operation, l_uint8 **pdata, size_t *pdatasize)
 
static tsize_t lept_read_proc (thandle_t cookie, tdata_t buff, tsize_t size)
 
static tsize_t lept_write_proc (thandle_t cookie, tdata_t buff, tsize_t size)
 
static toff_t lept_seek_proc (thandle_t cookie, toff_t offs, int whence)
 
static int lept_close_proc (thandle_t cookie)
 
static toff_t lept_size_proc (thandle_t cookie)
 
PIXpixReadTiff (const char *filename, l_int32 n)
 
PIXpixReadStreamTiff (FILE *fp, l_int32 n)
 
l_ok pixWriteTiff (const char *filename, PIX *pix, l_int32 comptype, const char *modestr)
 
l_ok pixWriteTiffCustom (const char *filename, PIX *pix, l_int32 comptype, const char *modestr, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
 
l_ok pixWriteStreamTiff (FILE *fp, PIX *pix, l_int32 comptype)
 
l_ok pixWriteStreamTiffWA (FILE *fp, PIX *pix, l_int32 comptype, const char *modestr)
 
PIXpixReadFromMultipageTiff (const char *fname, size_t *poffset)
 
PIXApixaReadMultipageTiff (const char *filename)
 
l_ok pixaWriteMultipageTiff (const char *fname, PIXA *pixa)
 
l_ok writeMultipageTiff (const char *dirin, const char *substr, const char *fileout)
 
l_ok writeMultipageTiffSA (SARRAY *sa, const char *fileout)
 
l_ok fprintTiffInfo (FILE *fpout, const char *tiffile)
 
l_ok tiffGetCount (FILE *fp, l_int32 *pn)
 
l_ok getTiffResolution (FILE *fp, l_int32 *pxres, l_int32 *pyres)
 
l_ok readHeaderTiff (const char *filename, l_int32 n, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
 
l_ok freadHeaderTiff (FILE *fp, l_int32 n, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
 
l_ok readHeaderMemTiff (const l_uint8 *cdata, size_t size, l_int32 n, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
 
l_ok findTiffCompression (FILE *fp, l_int32 *pcomptype)
 
l_ok extractG4DataFromFile (const char *filein, l_uint8 **pdata, size_t *pnbytes, l_int32 *pw, l_int32 *ph, l_int32 *pminisblack)
 
static L_MEMSTREAMmemstreamCreateForRead (l_uint8 *indata, size_t pinsize)
 
static L_MEMSTREAMmemstreamCreateForWrite (l_uint8 **poutdata, size_t *poutsize)
 
static tsize_t tiffReadCallback (thandle_t handle, tdata_t data, tsize_t length)
 
static tsize_t tiffWriteCallback (thandle_t handle, tdata_t data, tsize_t length)
 
static toff_t tiffSeekCallback (thandle_t handle, toff_t offset, l_int32 whence)
 
static l_int32 tiffCloseCallback (thandle_t handle)
 
static toff_t tiffSizeCallback (thandle_t handle)
 
static l_int32 tiffMapCallback (thandle_t handle, tdata_t *data, toff_t *length)
 
static void tiffUnmapCallback (thandle_t handle, tdata_t data, toff_t length)
 
PIXpixReadMemTiff (const l_uint8 *cdata, size_t size, l_int32 n)
 
PIXpixReadMemFromMultipageTiff (const l_uint8 *cdata, size_t size, size_t *poffset)
 
PIXApixaReadMemMultipageTiff (const l_uint8 *data, size_t size)
 
l_ok pixaWriteMemMultipageTiff (l_uint8 **pdata, size_t *psize, PIXA *pixa)
 
l_ok pixWriteMemTiff (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
 
l_ok pixWriteMemTiffCustom (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
 

Variables

static const l_int32 DefaultResolution = 300
 
static const l_int32 ManyPagesInTiffFile = 3000
 
static const l_int32 MaxTiffWidth = 1 << 20
 
static const l_int32 MaxTiffHeight = 1 << 20
 
static const size_t MaxNumTiffBytes = (1 << 28) - 1
 
static struct tiff_transform tiff_orientation_transforms []
 
static struct tiff_transform tiff_partial_orientation_transforms []
 

Detailed Description


    TIFFClientOpen() wrappers for FILE*:
     static tsize_t    lept_read_proc()
     static tsize_t    lept_write_proc()
     static toff_t     lept_seek_proc()
     static int        lept_close_proc()
     static toff_t     lept_size_proc()

    Reading tiff:
            PIX       *pixReadTiff()             [ special top level ]
            PIX       *pixReadStreamTiff()
     static PIX       *pixReadFromTiffStream()

    Writing tiff:
            l_int32    pixWriteTiff()            [ special top level ]
            l_int32    pixWriteTiffCustom()      [ special top level ]
            l_int32    pixWriteStreamTiff()
            l_int32    pixWriteStreamTiffWA()
     static l_int32    pixWriteToTiffStream()
     static l_int32    writeCustomTiffTags()

    Reading and writing multipage tiff
            PIX       *pixReadFromMultipageTiff()
            PIXA      *pixaReadMultipageTiff()   [ special top level ]
            l_int32    pixaWriteMultipageTiff()  [ special top level ]
            l_int32    writeMultipageTiff()      [ special top level ]
            l_int32    writeMultipageTiffSA()

    Information about tiff file
            l_int32    fprintTiffInfo()
            l_int32    tiffGetCount()
            l_int32    getTiffResolution()
     static l_int32    getTiffStreamResolution()
            l_int32    readHeaderTiff()
            l_int32    freadHeaderTiff()
            l_int32    readHeaderMemTiff()
     static l_int32    tiffReadHeaderTiff()
            l_int32    findTiffCompression()
     static l_int32    getTiffCompressedFormat()

    Extraction of tiff g4 data:
            l_int32    extractG4DataFromFile()

    Open tiff stream from file stream
     static TIFF      *fopenTiff()

    Wrapper for TIFFOpen:
     static TIFF      *openTiff()

    Memory I/O: reading memory --> pix and writing pix --> memory
       Ten static low-level memstream functions
          static L_MEMSTREAM  *memstreamCreateForRead()
          static L_MEMSTREAM  *memstreamCreateForWrite()
          static tsize_t       tiffReadCallback()
          static tsize_t       tiffWriteCallback()
          static toff_t        tiffSeekCallback()
          static l_int32       tiffCloseCallback()
          static toff_t        tiffSizeCallback()
          static l_int32       tiffMapCallback()
          static void          tiffUnmapCallback()
          static TIFF         *fopenTiffMemstream()

          PIX       *pixReadMemTiff();
          PIX       *pixReadMemFromMultipageTiff();
          PIXA      *pixaReadMemMultipageTiff()    [ special top level ]
          l_int32    pixaWriteMemMultipageTiff()   [ special top level ]
          l_int32    pixWriteMemTiff();
          l_int32    pixWriteMemTiffCustom();

 Note 1: To include all necessary functions, use libtiff version 3.7.4
         (from 2005) or later.
 Note 2: What compression methods in tiff are supported?
         * We support most methods that are fully implemented in the
           tiff library, such as G3, G4, RLE and LZW.
         * The exception is the old-style jpeg tiff format (OJPEG), which
           is not supported.
         * We support only one format, ZIP, that uses an external library.
         * At present we do not support WEBP in tiff, which uses
           libwebp and was added in tifflib 4.1.0 in 2019.
 Note 3: On Windows with 2 bpp or 4 bpp images, the bytes in the
         tiff-compressed file depend on the pad bits, but not on the
         decoded raster image when read.  Because it is sometimes
         convenient to use a golden file with a byte-by-byte check
         to verify invariance, we set the pad bits to 0 before writing,
         in pixWriteToTiffStream().

Definition in file tiffio.c.

Function Documentation

◆ extractG4DataFromFile()

l_ok extractG4DataFromFile ( const char *  filein,
l_uint8 **  pdata,
size_t *  pnbytes,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pminisblack 
)

extractG4DataFromFile()

Parameters
[in]filein
[out]pdatabinary data of ccitt g4 encoded stream
[out]pnbytessize of binary data
[out]pw[optional] image width
[out]ph[optional] image height
[out]pminisblack[optional] boolean
Returns
0 if OK, 1 on error

Definition at line 2111 of file tiffio.c.

References fileFormatIsTiff(), fopenReadStream(), l_binaryRead(), and openTiff().

Referenced by l_generateG4Data().

◆ findTiffCompression()

l_ok findTiffCompression ( FILE *  fp,
l_int32 *  pcomptype 
)

findTiffCompression()

Parameters
[in]fpfile stream; must be rewound to BOF
[out]pcomptypecompression type
Returns
0 if OK, 1 on error
Notes:
     (1) The returned compression type is that defined in
         the enum in imageio.h.  It is not the tiff flag value.
     (2) The compression type is initialized to IFF_UNKNOWN.
         If it is not one of the specified types, the returned
         type is IFF_TIFF, which indicates no compression.
     (3) When this function is called, the stream must be at BOF.
         If the opened stream is to be used again to read the
         file, it must be rewound to BOF after calling this function.

Definition at line 2025 of file tiffio.c.

◆ fopenTiff()

static TIFF * fopenTiff ( FILE *  fp,
const char *  modestring 
)
static

fopenTiff()

Parameters
[in]fpfile stream
[in]modestring"r", "w", ...
Returns
tiff data structure, opened for a file descriptor
Notes:
     (1) Why is this here?  Leffler did not provide a function that
         takes a stream and gives a TIFF.  He only gave one that
         generates a TIFF starting with a file descriptor.  So we
         need to make it here, because it is useful to have functions
         that take a stream as input.
     (2) We use TIFFClientOpen() together with a set of static wrapper
         functions which map TIFF read, write, seek, close and size.
         to functions expecting a cookie of type stream (i.e. FILE *).
         This implementation was contributed by Jürgen Buchmüller.

Definition at line 2236 of file tiffio.c.

Referenced by getTiffResolution(), pixaReadMultipageTiff(), pixReadStreamTiff(), and tiffGetCount().

◆ fopenTiffMemstream()

static TIFF * fopenTiffMemstream ( const char *  filename,
const char *  operation,
l_uint8 **  pdata,
size_t *  pdatasize 
)
static

fopenTiffMemstream()

Parameters
[in]filenamefor error output; can be ""
[in]operation"w" for write, "r" for read
[out]pdatawritten data
[out]pdatasizesize of written data
Returns
tiff data structure, opened for write to memory
Notes:
     (1) This wraps up a number of callbacks for either:
           * reading from tiff in memory buffer --> pix
           * writing from pix --> tiff in memory buffer
     (2) After use, the memstream is automatically destroyed when
         TIFFClose() is called.  TIFFCleanup() doesn't free the memstream.
     (3) This does not work in append mode, and in write mode it
         does not append.

Definition at line 2538 of file tiffio.c.

Referenced by pixReadMemFromMultipageTiff(), pixReadMemTiff(), and readHeaderMemTiff().

◆ fprintTiffInfo()

l_ok fprintTiffInfo ( FILE *  fpout,
const char *  tiffile 
)

fprintTiffInfo()

Parameters
[in]fpoutstream for output of tag data
[in]tiffileinput
Returns
0 if OK; 1 on error

Definition at line 1602 of file tiffio.c.

References openTiff().

◆ freadHeaderTiff()

l_ok freadHeaderTiff ( FILE *  fp,
l_int32  n,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  pres,
l_int32 *  pcmap,
l_int32 *  pformat 
)

freadHeaderTiff()

Parameters
[in]fpfile stream
[in]npage image number: 0-based
[out]pw[optional] width
[out]ph[optional] height
[out]pbps[optional] bits per sample – 1, 2, 4 or 8
[out]pspp[optional] samples per pixel – 1 or 3
[out]pres[optional] resolution in x dir; NULL to ignore
[out]pcmap[optional] colormap exists; input NULL to ignore
[out]pformat[optional] tiff format; input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) If there is a colormap, cmap is returned as 1; else 0.
     (2) If n is equal to or greater than the number of images, returns 1.

Definition at line 1831 of file tiffio.c.

References findFileFormatStream().

Referenced by readHeaderTiff().

◆ getTiffCompressedFormat()

static l_int32 getTiffCompressedFormat ( l_uint16  tiffcomp)
static

getTiffCompressedFormat()

Parameters
[in]tiffcompdefined in tiff.h
Returns
compression format defined in imageio.h
Notes:
     (1) The input must be the actual tiff compression type
         returned by a tiff library call.  It should always be
         a valid tiff type.
     (2) The return type is defined in the enum in imageio.h.

Definition at line 2061 of file tiffio.c.

Referenced by tiffReadHeaderTiff().

◆ getTiffResolution()

l_ok getTiffResolution ( FILE *  fp,
l_int32 *  pxres,
l_int32 *  pyres 
)

getTiffResolution()

Parameters
[in]fpfile stream opened for read
[out]pxres,pyresresolution in ppi
Returns
0 if OK; 1 on error
Notes:
     (1) If neither resolution field is set, this is not an error;
         the returned resolution values are 0 (designating 'unknown').

Definition at line 1679 of file tiffio.c.

References fopenTiff(), and getTiffStreamResolution().

Referenced by l_generateG4Data().

◆ getTiffStreamResolution()

static l_int32 getTiffStreamResolution ( TIFF *  tif,
l_int32 *  pxres,
l_int32 *  pyres 
)
static

getTiffStreamResolution()

Parameters
[in]tifTIFF handle opened for read
[out]pxres,pyresresolution in ppi
Returns
0 if OK; 1 on error
Notes:
     (1) If neither resolution field is set, this is not an error;
         the returned resolution values are 0 (designating 'unknown').

Definition at line 1713 of file tiffio.c.

Referenced by getTiffResolution(), and tiffReadHeaderTiff().

◆ openTiff()

static TIFF * openTiff ( const char *  filename,
const char *  modestring 
)
static

openTiff()

Parameters
[in]filename
[in]modestring"r", "w", ...
Returns
tiff data structure
Notes:
     (1) This handles multi-platform file naming.

Definition at line 2270 of file tiffio.c.

References genPathname().

Referenced by extractG4DataFromFile(), fprintTiffInfo(), pixReadFromMultipageTiff(), and pixWriteTiffCustom().

◆ pixaReadMemMultipageTiff()

PIXA* pixaReadMemMultipageTiff ( const l_uint8 *  data,
size_t  size 
)

pixaReadMemMultipageTiff()

Parameters
[in]dataconst; multiple pages; tiff-encoded
[in]sizesize of cdata
Returns
pixa, or NULL on error
Notes:
     (1) This is an O(n) read-from-memory version of pixaReadMultipageTiff().

Definition at line 2716 of file tiffio.c.

References L_INSERT, pixaAddPix(), pixaCreate(), and pixReadMemFromMultipageTiff().

◆ pixaReadMultipageTiff()

PIXA* pixaReadMultipageTiff ( const char *  filename)

pixaReadMultipageTiff()

Parameters
[in]filenameinput tiff file
Returns
pixa of page images, or NULL on error

Definition at line 1407 of file tiffio.c.

References fileFormatIsTiff(), fopenReadStream(), fopenTiff(), L_INSERT, pixaAddPix(), pixaCreate(), pixReadFromTiffStream(), and tiffGetCount().

Referenced by convertTiffMultipageToPdf().

◆ pixaWriteMemMultipageTiff()

l_ok pixaWriteMemMultipageTiff ( l_uint8 **  pdata,
size_t *  psize,
PIXA pixa 
)

pixaWriteMemMultipageTiff()

Parameters
[out]pdataconst; tiff-encoded
[out]psizesize of data
[in]pixaany depth; colormap will be removed
Returns
0 if OK, 1 on error
Notes:
     (1) fopenTiffMemstream() does not work in append mode, so we
         must work-around with a temporary file.
     (2) Getting a file stream from
           open_memstream((char **)pdata, psize)
         does not work with the tiff directory.

Definition at line 2754 of file tiffio.c.

References fopenWriteWinTempfile(), L_CLONE, pixaGetCount(), and pixaGetPix().

◆ pixaWriteMultipageTiff()

l_ok pixaWriteMultipageTiff ( const char *  fname,
PIXA pixa 
)

pixaWriteMultipageTiff()

Parameters
[in]fnameinput tiff file
[in]pixaany depth; colormap will be removed
Returns
0 if OK, 1 on error
Notes:
     (1) The tiff directory overhead is O(n^2).  I have not been
         able to reduce it to O(n).  The overhead for n = 2000 is
         about 1 second.

Definition at line 1465 of file tiffio.c.

References L_CLONE, pixaGetCount(), and pixaGetPix().

◆ pixReadFromMultipageTiff()

PIX* pixReadFromMultipageTiff ( const char *  fname,
size_t *  poffset 
)

pixReadFromMultipageTiff()

Parameters
[in]fnamefilename
[in,out]poffsetset offset to 0 for first image
Returns
pix, or NULL on error or if previous call returned the last image
Notes:
     (1) This allows overhead for traversal of a multipage tiff file
         to be linear in the number of images.  This will also work
         with a singlepage tiff file.
     (2) No TIFF internal data structures are exposed to the caller
         (thanks to Jeff Breidenbach).
     (3) offset is the byte offset of a particular image in a multipage
         tiff file. To get the first image in the file, input the
         special offset value of 0.
     (4) The offset is updated to point to the next image, for a
         subsequent call.
     (5) On the last image, the offset returned is 0.  Exit the loop
         when the returned offset is 0.
     (6) For reading a multipage tiff from a memory buffer, see
           pixReadMemFromMultipageTiff()
     (7) Example usage for reading all the images in the tif file:
           size_t offset = 0;
           do {
               Pix *pix = pixReadFromMultipageTiff(filename, &offset);
               // do something with pix
           } while (offset != 0);

Definition at line 1360 of file tiffio.c.

References openTiff(), and pixReadFromTiffStream().

◆ pixReadFromTiffStream()

static PIX * pixReadFromTiffStream ( TIFF *  tif)
static

pixReadFromTiffStream()

Parameters
[in]tifTIFF handle
Returns
pix, or NULL on error
Notes:
     (1) We can read the following images (up to 32 bits/pixel):
         1 spp (grayscale): 1, 2, 4, 8, 16 bps
         1 spp (colormapped): 1, 2, 4, 8 bps
         2 spp (gray+alpha): 8 bps
         3 spp (rgb) and 4 spp (rgba): 8 or 16 bps
         Note that 16 bps rgb and rgba are converted to 8 bps in the pix.
     (2) In particular, we do not support
            16 bps for spp == 2
             4 bps for spp == 3 or spp == 4.
     (3) We only support uint image data.
     (4) We do not support tiled format, old-style jpeg encoding,
         or webp encoded tiff.
     (5) 2 bpp gray+alpha are rasterized as 32 bit/pixel rgba, with
         the gray value replicated in r, g and b.
     (6) For colormapped images, we support 8 bits/color in the palette.
         Tiff colormaps have 16 bits/color, and we reduce them to 8.
     (7) Quoting the libtiff documentation at
              http://libtiff.maptools.org/libtiff.html
         "libtiff provides a high-level interface for reading image data
         from a TIFF file. This interface handles the details of data
         organization and format for a wide variety of TIFF files;
         at least the large majority of those files that one would
         normally encounter. Image data is, by default, returned as
         ABGR pixels packed into 32-bit words (8 bits per sample).
         Rectangular rasters can be read or data can be intercepted
         at an intermediate level and packed into memory in a format
         more suitable to the application. The library handles all
         the details of the format of data stored on disk and,
         in most cases, if any colorspace conversions are required:
         bilevel to RGB, greyscale to RGB, CMYK to RGB, YCbCr to RGB,
         16-bit samples to 8-bit samples, associated/unassociated alpha,
         etc."

Definition at line 497 of file tiffio.c.

Referenced by pixaReadMultipageTiff(), pixReadFromMultipageTiff(), pixReadMemFromMultipageTiff(), pixReadMemTiff(), and pixReadStreamTiff().

◆ pixReadMemFromMultipageTiff()

PIX* pixReadMemFromMultipageTiff ( const l_uint8 *  cdata,
size_t  size,
size_t *  poffset 
)

pixReadMemFromMultipageTiff()

Parameters
[in]cdataconst; tiff-encoded
[in]sizesize of cdata
[in,out]poffsetset offset to 0 for first image
Returns
pix, or NULL on error or if previous call returned the last image
Notes:
     (1) This is a read-from-memory version of pixReadFromMultipageTiff().
         See that function for usage.
     (2) If reading sequentially from the tiff data, this is more
         efficient than pixReadMemTiff(), which has an overhead
         proportional to the image index n.
     (3) Example usage for reading all the images:
           size_t offset = 0;
           do {
               Pix *pix = pixReadMemFromMultipageTiff(data, size, &offset);
               // do something with pix
           } while (offset != 0);

Definition at line 2662 of file tiffio.c.

References fopenTiffMemstream(), and pixReadFromTiffStream().

Referenced by pixaReadMemMultipageTiff().

◆ pixReadMemTiff()

PIX* pixReadMemTiff ( const l_uint8 *  cdata,
size_t  size,
l_int32  n 
)

pixReadMemTiff()

Parameters
[in]cdataconst; tiff-encoded
[in]sizesize of cdata
[in]npage image number: 0-based
Returns
pix, or NULL on error
Notes:
     (1) This is a version of pixReadTiff(), where the data is read
         from a memory buffer and uncompressed.
     (2) Use TIFFClose(); TIFFCleanup() doesn't free internal memstream.
     (3) No warning messages on failure, because of how multi-page
         TIFF reading works. You are supposed to keep trying until
         it stops working.
     (4) Tiff directory overhead is linear in the input page number.
         If reading many images, use pixReadMemFromMultipageTiff().

Definition at line 2599 of file tiffio.c.

References fopenTiffMemstream(), and pixReadFromTiffStream().

◆ pixReadStreamTiff()

PIX* pixReadStreamTiff ( FILE *  fp,
l_int32  n 
)

pixReadStreamTiff()

Parameters
[in]fpfile stream
[in]npage number: 0 based
Returns
pix, or NULL on error or if there are no more images in the file
Notes:
     (1) No warning messages on failure, because of how multi-page
         TIFF reading works. You are supposed to keep trying until
         it stops working.

Definition at line 429 of file tiffio.c.

References fopenTiff(), and pixReadFromTiffStream().

Referenced by pixReadTiff().

◆ pixReadTiff()

PIX* pixReadTiff ( const char *  filename,
l_int32  n 
)

pixReadTiff()

Parameters
[in]filename
[in]npage number 0 based
Returns
pix, or NULL on error
Notes:
     (1) This is a version of pixRead(), specialized for tiff
         files, that allows specification of the page to be returned
     (2) No warning messages on failure, because of how multi-page
         TIFF reading works. You are supposed to keep trying until
         it stops working.

Definition at line 394 of file tiffio.c.

References fopenReadStream(), and pixReadStreamTiff().

◆ pixWriteMemTiff()

l_ok pixWriteMemTiff ( l_uint8 **  pdata,
size_t *  psize,
PIX pix,
l_int32  comptype 
)

pixWriteMemTiff()

Parameters
[out]pdatadata of tiff compressed image
[out]psizesize of returned data
[in]pix
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
Returns
0 if OK, 1 on error

Usage: 1) See pixWriteTiff(. This version writes to memory instead of to a file.

Definition at line 2811 of file tiffio.c.

References pixWriteMemTiffCustom().

◆ pixWriteMemTiffCustom()

l_ok pixWriteMemTiffCustom ( l_uint8 **  pdata,
size_t *  psize,
PIX pix,
l_int32  comptype,
NUMA natags,
SARRAY savals,
SARRAY satypes,
NUMA nasizes 
)

pixWriteMemTiffCustom()

Parameters
[out]pdatadata of tiff compressed image
[out]psizesize of returned data
[in]pix
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
[in]natags[optional] NUMA of custom tiff tags
[in]savals[optional] SARRAY of values
[in]satypes[optional] SARRAY of types
[in]nasizes[optional] NUMA of sizes
Returns
0 if OK, 1 on error

Usage: 1) See pixWriteTiffCustom(. This version writes to memory instead of to a file. 2) Use TIFFClose(); TIFFCleanup( doesn't free internal memstream.

Definition at line 2842 of file tiffio.c.

Referenced by pixWriteMemTiff().

◆ pixWriteStreamTiff()

l_ok pixWriteStreamTiff ( FILE *  fp,
PIX pix,
l_int32  comptype 
)

pixWriteStreamTiff()

Parameters
[in]fpfile stream
[in]pix
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
Returns
0 if OK, 1 on error
Notes:
     (1) This writes a single image to a file stream opened for writing.
     (2) If the pix has a colormap, it is preserved in the output file.
     (3) For images with bpp > 1, this resets the comptype, if
         necessary, to write uncompressed data.
     (4) G3 and G4 are only defined for 1 bpp.
     (5) We only allow PACKBITS for bpp = 1, because for bpp > 1
         it typically expands images that are not synthetically generated.
     (6) G4 compression is typically about twice as good as G3.
         G4 is excellent for binary compression of text/line-art,
         but terrible for halftones and dithered patterns.  (In
         fact, G4 on halftones can give a file that is larger
         than uncompressed!)  If a binary image has dithered
         regions, it is usually better to compress with png.

Definition at line 925 of file tiffio.c.

References pixWriteStreamTiffWA().

◆ pixWriteStreamTiffWA()

l_ok pixWriteStreamTiffWA ( FILE *  fp,
PIX pix,
l_int32  comptype,
const char *  modestr 
)

pixWriteStreamTiffWA()

Parameters
[in]fpfile stream opened for append or write
[in]pix
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
[in]modestr"w" or "a"
Returns
0 if OK, 1 on error
Notes:
     (1) See pixWriteStreamTiff()

Definition at line 950 of file tiffio.c.

Referenced by pixWriteStreamTiff().

◆ pixWriteTiff()

l_ok pixWriteTiff ( const char *  filename,
PIX pix,
l_int32  comptype,
const char *  modestr 
)

pixWriteTiff()

Parameters
[in]filenameto write to
[in]pixany depth, colormap will be removed
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
[in]modestr"a" or "w"
Returns
0 if OK, 1 on error
Notes:
     (1) For multipage tiff, write the first pix with mode "w" and
         all subsequent pix with mode "a".
     (2) For multipage tiff, there is considerable overhead in the
         machinery to append an image and add the directory entry,
         and the time required for each image increases linearly
         with the number of images in the file.

Definition at line 811 of file tiffio.c.

References pixWriteTiffCustom().

◆ pixWriteTiffCustom()

l_ok pixWriteTiffCustom ( const char *  filename,
PIX pix,
l_int32  comptype,
const char *  modestr,
NUMA natags,
SARRAY savals,
SARRAY satypes,
NUMA nasizes 
)

pixWriteTiffCustom()

Parameters
[in]filenameto write to
[in]pix
[in]comptypeIFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_TIFF_JPEG
[in]modestr"a" or "w"
[in]natags[optional] NUMA of custom tiff tags
[in]savals[optional] SARRAY of values
[in]satypes[optional] SARRAY of types
[in]nasizes[optional] NUMA of sizes
Returns
0 if OK, 1 on error

Usage: 1 This writes a page image to a tiff file, with optional extra tags defined in tiff.h 2 For multipage tiff, write the first pix with mode "w" and all subsequent pix with mode "a". 3 For the custom tiff tags: a The three arrays {natags, savals, satypes} must all be either NULL or defined and of equal size. b If they are defined, the tags are an array of integers, the vals are an array of values in string format, and the types are an array of types in string format. c All valid tags are definined in tiff.h. d The types allowed are the set of strings: "char*" "l_uint8*" "l_uint16" "l_uint32" "l_int32" "l_float64" "l_uint16-l_uint16" note the dash; use it between the two l_uint16 vals in the val string Of these, "char*" and "l_uint16" are the most commonly used. e The last array, nasizes, is also optional. It is for tags that take an array of bytes for a value, a number of elements in the array, and a type that is either "char*" or "l_uint8*" probably either will work. Use NULL if there are no such tags. f VERY IMPORTANT: if there are any tags that require the extra size value, stored in nasizes, they must be written first!

Definition at line 868 of file tiffio.c.

References openTiff(), and pixWriteToTiffStream().

Referenced by pixWriteTiff().

◆ pixWriteToTiffStream()

static l_int32 pixWriteToTiffStream ( TIFF *  tif,
PIX pix,
l_int32  comptype,
NUMA natags,
SARRAY savals,
SARRAY satypes,
NUMA nasizes 
)
static

pixWriteToTiffStream()

Parameters
[in]tifdata structure, opened to a file
[in]pix
[in]comptypeIFF_TIFF: for any image; no compression IFF_TIFF_RLE, IFF_TIFF_PACKBITS: for 1 bpp only IFF_TIFF_G4 and IFF_TIFF_G3: for 1 bpp only IFF_TIFF_LZW, IFF_TIFF_ZIP: lossless for any image IFF_TIFF_JPEG: lossy 8 bpp gray or rgb
[in]natags[optional] NUMA of custom tiff tags
[in]savals[optional] SARRAY of values
[in]satypes[optional] SARRAY of types
[in]nasizes[optional] NUMA of sizes
Returns
0 if OK, 1 on error
Notes:
     (1) This static function should only be called through higher
         level functions in this file; namely, pixWriteTiffCustom(),
         pixWriteTiff(), pixWriteStreamTiff(), pixWriteMemTiff()
         and pixWriteMemTiffCustom().
     (2) We only allow PACKBITS for bpp = 1, because for bpp > 1
         it typically expands images that are not synthetically generated.
     (3) See pixWriteTiffCustom() for details on how to use
         the last four parameters for customized tiff tags.
     (4) The only valid pixel depths in leptonica are 1, 2, 4, 8, 16
         and 32.  However, it is possible, and in some cases desirable,
         to write out a tiff file using an rgb pix that has 24 bpp.
         This can be created by appending the raster data for a 24 bpp
         image (with proper scanline padding) directly to a 24 bpp
         pix that was created without a data array.  See note in
         pixWriteStreamPng() for an example.

Definition at line 1023 of file tiffio.c.

References pixGetDimensions(), and pixSetPadBits().

Referenced by pixWriteTiffCustom().

◆ readHeaderMemTiff()

l_ok readHeaderMemTiff ( const l_uint8 *  cdata,
size_t  size,
l_int32  n,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  pres,
l_int32 *  pcmap,
l_int32 *  pformat 
)

readHeaderMemTiff()

Parameters
[in]cdataconst; tiff-encoded
[in]sizesize of data
[in]npage image number: 0-based
[out]pw[optional] width
[out]ph[optional] height
[out]pbps[optional] bits per sample – 1, 2, 4 or 8
[out]pspp[optional] samples per pixel – 1 or 3
[out]pres[optional] resolution in x dir; NULL to ignore
[out]pcmap[optional] colormap exists; input NULL to ignore
[out]pformat[optional] tiff format; input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) Use TIFFClose(); TIFFCleanup() doesn't free internal memstream.
     (2) Returns res = 0 if not set in the file.

Definition at line 1898 of file tiffio.c.

References fopenTiffMemstream(), and tiffReadHeaderTiff().

◆ readHeaderTiff()

l_ok readHeaderTiff ( const char *  filename,
l_int32  n,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  pres,
l_int32 *  pcmap,
l_int32 *  pformat 
)

readHeaderTiff()

Parameters
[in]filename
[in]npage image number: 0-based
[out]pw[optional] width
[out]ph[optional] height
[out]pbps[optional] bits per sample – 1, 2, 4 or 8
[out]pspp[optional] samples per pixel – 1 or 3
[out]pres[optional] resolution in x dir; NULL to ignore
[out]pcmap[optional] colormap exists; input NULL to ignore
[out]pformat[optional] tiff format; input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) If there is a colormap, cmap is returned as 1; else 0.
     (2) If n is equal to or greater than the number of images, returns 1.

Definition at line 1777 of file tiffio.c.

References fopenReadStream(), and freadHeaderTiff().

◆ tiffGetCount()

l_ok tiffGetCount ( FILE *  fp,
l_int32 *  pn 
)

tiffGetCount()

Parameters
[in]fpfile stream opened for read
[out]pnnumber of images
Returns
0 if OK; 1 on error

Definition at line 1633 of file tiffio.c.

References fopenTiff().

Referenced by convertTiffMultipageToPS(), l_generateG4Data(), and pixaReadMultipageTiff().

◆ tiffReadHeaderTiff()

static l_int32 tiffReadHeaderTiff ( TIFF *  tif,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  pres,
l_int32 *  pcmap,
l_int32 *  pformat 
)
static

tiffReadHeaderTiff()

Parameters
[in]tif
[out]pw[optional] width
[out]ph[optional] height
[out]pbps[optional] bits per sample – 1, 2, 4 or 8
[out]pspp[optional] samples per pixel – 1 or 3
[out]pres[optional] resolution in x dir; NULL to ignore
[out]pcmap[optional] cmap exists; input NULL to ignore
[out]pformat[optional] tiff format; input NULL to ignore
Returns
0 if OK, 1 on error

Definition at line 1957 of file tiffio.c.

References getTiffCompressedFormat(), and getTiffStreamResolution().

Referenced by readHeaderMemTiff().

◆ writeCustomTiffTags()

static l_int32 writeCustomTiffTags ( TIFF *  tif,
NUMA natags,
SARRAY savals,
SARRAY satypes,
NUMA nasizes 
)
static

writeCustomTiffTags()

Parameters
[in]tif
[in]natags[optional] NUMA of custom tiff tags
[in]savals[optional] SARRAY of values
[in]satypes[optional] SARRAY of types
[in]nasizes[optional] NUMA of sizes
Returns
0 if OK, 1 on error
Notes:
     (1) This static function should be called indirectly through
         higher level functions, such as pixWriteTiffCustom(),
         which call pixWriteToTiffStream().  See details in
         pixWriteTiffCustom() for using the 4 input arrays.
     (2) This is a no-op if the first 3 arrays are all NULL.
     (3) Otherwise, the first 3 arrays must be defined and all
         of equal size.
     (4) The fourth array is always optional.
     (5) The most commonly used types are "char*" and "u_int16".
         See tiff.h for a full listing of the tiff tags.
         Note that many of these tags, in particular the bit tags,
         are intended to be private, and cannot be set by this function.
         Examples are the STRIPOFFSETS and STRIPBYTECOUNTS tags,
         which are bit tags that are automatically set in the header,
         and can be extracted using tiffdump.

Definition at line 1234 of file tiffio.c.

References L_NOCOPY, lept_stderr(), numaGetCount(), numaGetIValue(), sarrayGetCount(), and sarrayGetString().

◆ writeMultipageTiff()

l_ok writeMultipageTiff ( const char *  dirin,
const char *  substr,
const char *  fileout 
)

writeMultipageTiff()

Parameters
[in]dirininput directory
[in]substr[optional] substring filter on filenames; can be NULL
[in]fileoutoutput multipage tiff file
Returns
0 if OK, 1 on error
Notes:
     (1) This writes a set of image files in a directory out
         as a multipage tiff file.  The images can be in any
         initial file format.
     (2) Images with a colormap have the colormap removed before
         re-encoding as tiff.
     (3) All images are encoded losslessly.  Those with 1 bpp are
         encoded 'g4'.  The rest are encoded as 'zip' (flate encoding).
         Because it is lossless, this is an expensive method for
         saving most rgb images.
     (4) The tiff directory overhead is quadratic in the number of
         images.  To avoid this for very large numbers of images to be
         written, apply the method used in pixaWriteMultipageTiff().

Definition at line 1517 of file tiffio.c.

References getSortedPathnamesInDirectory(), sarrayDestroy(), and writeMultipageTiffSA().

◆ writeMultipageTiffSA()

l_ok writeMultipageTiffSA ( SARRAY sa,
const char *  fileout 
)

writeMultipageTiffSA()

Parameters
[in]sastring array of full path names
[in]fileoutoutput ps file
Returns
0 if OK, 1 on error
Notes:
     (1) See writeMultipageTiff()

Definition at line 1551 of file tiffio.c.

References findFileFormat(), L_NOCOPY, sarrayGetCount(), and sarrayGetString().

Referenced by writeMultipageTiff().

Variable Documentation

◆ tiff_orientation_transforms

struct tiff_transform tiff_orientation_transforms[]
static
Initial value:
= {
{0, 0, 0},
{0, 1, 0},
{1, 1, 0},
{1, 0, 0},
{0, 1, -1},
{0, 0, 1},
{0, 1, 1},
{0, 0, -1}
}

Definition at line 171 of file tiffio.c.

◆ tiff_partial_orientation_transforms

struct tiff_transform tiff_partial_orientation_transforms[]
static
Initial value:
= {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 1, -1},
{0, 1, 1},
{1, 0, 1},
{0, 1, -1}
}

Definition at line 171 of file tiffio.c.