Leptonica  1.54
Файл src/tiffio.c
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "allheaders.h"
#include "tiff.h"
#include "tiffio.h"

Классы

struct  tiff_transform
struct  L_Memstream

Определения типов

typedef struct L_Memstream L_MEMSTREAM

Функции

static PIXpixReadFromTiffStream (TIFF *tif)
static l_int32 getTiffStreamResolution (TIFF *tif, l_int32 *pxres, l_int32 *pyres)
static l_int32 tiffReadHeaderTiff (TIFF *tif, l_int32 *pwidth, l_int32 *pheight, 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_int32 pixWriteTiff (const char *filename, PIX *pix, l_int32 comptype, const char *modestring)
l_int32 pixWriteTiffCustom (const char *filename, PIX *pix, l_int32 comptype, const char *modestring, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)
l_int32 pixWriteStreamTiff (FILE *fp, PIX *pix, l_int32 comptype)
PIXApixaReadMultipageTiff (const char *filename)
l_int32 writeMultipageTiff (const char *dirin, const char *substr, const char *fileout)
l_int32 writeMultipageTiffSA (SARRAY *sa, const char *fileout)
l_int32 fprintTiffInfo (FILE *fpout, const char *tiffile)
l_int32 tiffGetCount (FILE *fp, l_int32 *pn)
l_int32 getTiffResolution (FILE *fp, l_int32 *pxres, l_int32 *pyres)
l_int32 readHeaderTiff (const char *filename, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
l_int32 freadHeaderTiff (FILE *fp, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
l_int32 readHeaderMemTiff (const l_uint8 *cdata, size_t size, l_int32 n, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *pres, l_int32 *pcmap, l_int32 *pformat)
l_int32 findTiffCompression (FILE *fp, l_int32 *pcomptype)
l_int32 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)
l_int32 pixWriteMemTiff (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype)
l_int32 pixWriteMemTiffCustom (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 comptype, NUMA *natags, SARRAY *savals, SARRAY *satypes, NUMA *nasizes)

Переменные

static const l_int32 DEFAULT_RESOLUTION = 300
static const l_int32 MAX_PAGES_IN_TIFF_FILE = 3000
static struct tiff_transform tiff_orientation_transforms []
static struct tiff_transform tiff_partial_orientation_transforms []

Типы

typedef struct L_Memstream L_MEMSTREAM

Функции

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

extractG4DataFromFile()

Input: filein &data (<return> binary data of ccitt g4 encoded stream) &nbytes (<return> size of binary data) &w (<return optional>=""> image width) &h (<return optional>=""> image height) &minisblack (<return optional>=""> boolean) Return: 0 if OK, 1 on error

l_int32 findTiffCompression ( FILE *  fp,
l_int32 pcomptype 
)

findTiffCompression()

Input: stream (must be rewound to BOF) &comptype (<return> compression type) Return: 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.

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

fopenTiff()

Input: stream modestring ("r", "w", ...) Return: 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.

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

fopenTiffMemstream()

Input: filename (for error output; can be "") operation ("w" for write, "r" for read) &data (<return> written data) &datasize (<return> size of written data) Return: 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.

l_int32 fprintTiffInfo ( FILE *  fpout,
const char *  tiffile 
)
l_int32 freadHeaderTiff ( FILE *  fp,
l_int32  n,
l_int32 pwidth,
l_int32 pheight,
l_int32 pbps,
l_int32 pspp,
l_int32 pres,
l_int32 pcmap,
l_int32 pformat 
)

freadHeaderTiff()

Input: stream n (page image number: 0-based) &width (<return>) &height (<return>) &bps (<return> bits per sample -- 1, 2, 4 or 8) &spp (<return>; samples per pixel -- 1 or 3) &res (<optional return>="">; resolution in x dir; NULL to ignore) &cmap (<optional return>="">; colormap exists; input NULL to ignore) &format (<optional return>="">; tiff format; input NULL to ignore) Return: 0 if OK, 1 on error

Notes: (1) If there is a colormap, cmap is returned as 1; else 0. (2) If
is equal to or greater than the number of images, returns 1.

static l_int32 getTiffCompressedFormat ( l_uint16  tiffcomp) [static]

getTiffCompressedFormat()

Input: tiffcomp (defined in tiff.h) Return: 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.

l_int32 getTiffResolution ( FILE *  fp,
l_int32 pxres,
l_int32 pyres 
)
static l_int32 getTiffStreamResolution ( TIFF *  tif,
l_int32 pxres,
l_int32 pyres 
) [static]
static int lept_close_proc ( thandle_t  cookie) [static]
static tsize_t lept_read_proc ( thandle_t  cookie,
tdata_t  buff,
tsize_t  size 
) [static]
static toff_t lept_seek_proc ( thandle_t  cookie,
toff_t  offs,
int  whence 
) [static]
static toff_t lept_size_proc ( thandle_t  cookie) [static]
static tsize_t lept_write_proc ( thandle_t  cookie,
tdata_t  buff,
tsize_t  size 
) [static]
static L_MEMSTREAM * memstreamCreateForRead ( l_uint8 indata,
size_t  pinsize 
) [static]
static L_MEMSTREAM * memstreamCreateForWrite ( l_uint8 **  poutdata,
size_t *  poutsize 
) [static]
static TIFF * openTiff ( const char *  filename,
const char *  modestring 
) [static]

openTiff()

Input: filename modestring ("r", "w", ...) Return: tiff (data structure)

Notes: (1) This handles multi-platform file naming.

PIXA* pixaReadMultipageTiff ( const char *  filename)
static PIX * pixReadFromTiffStream ( TIFF *  tif) [static]

pixReadFromTiffStream()

Input: stream Return: pix, or null on error

Notes: (1) We handle pixels up to 32 bits. This includes: 1 spp (grayscale): 1, 2, 4, 8, 16 bpp 1 spp (colormapped): 1, 2, 4, 8 bpp 3 spp (color): 8 bpp We do not handle 3 spp, 16 bpp (48 bits/pixel) (2) For colormapped images, we support 8 bits/color in the palette. Tiff colormaps have 16 bits/color, and we reduce them to 8. (3) Quoting the libtiff documenation 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."

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

pixReadMemTiff()

Input: data (const; tiff-encoded) datasize (size of data) n (page image number: 0-based) Return: 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.

PIX* pixReadStreamTiff ( FILE *  fp,
l_int32  n 
)

pixReadStreamTiff()

Input: stream n (page number: 0 based) Return: pix, or null on error (e.g., if the page number is invalid)

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.

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

pixReadTiff()

Input: filename page number (0 based) Return: 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.

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

pixWriteMemTiff()

Input: &data (<return> data of tiff compressed image) &size (<return> size of returned data) pix comptype (IFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP) Return: 0 if OK, 1 on error

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

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

pixWriteMemTiffCustom()

Input: &data (<return> data of tiff compressed image) &size (<return> size of returned data) pix comptype (IFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP) natags (<optional> NUMA of custom tiff tags) savals (<optional> SARRAY of values) satypes (<optional> SARRAY of types) nasizes (<optional> NUMA of sizes) Return: 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.

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

pixWriteStreamTiff()

Input: stream (opened for append or write) pix comptype (IFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP) Return: 0 if OK, 1 on error

Notes: (1) For images with bpp > 1, this resets the comptype, if necessary, to write uncompressed data. (2) G3 and G4 are only defined for 1 bpp. (3) We only allow PACKBITS for bpp = 1, because for bpp > 1 it typically expands images that are not synthetically generated. (4) 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.

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

pixWriteTiff()

Input: filename (to write to) pix comptype (IFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP) modestring ("a" or "w") Return: 0 if OK, 1 on error

Notes: (1) For multi-page tiff, write the first pix with mode "w" and all subsequent pix with mode "a".

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

pixWriteTiffCustom()

Input: filename (to write to) pix comptype (IFF_TIFF, IFF_TIFF_RLE, IFF_TIFF_PACKBITS, IFF_TIFF_G3, IFF_TIFF_G4) IFF_TIFF_LZW, IFF_TIFF_ZIP) modestring ("a" or "w") natags (<optional> NUMA of custom tiff tags) savals (<optional> SARRAY of values) satypes (<optional> SARRAY of types) nasizes (<optional> NUMA of sizes) Return: 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 multi-page 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!

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

pixWriteToTiffStream()

Input: tif (data structure, opened to a file) pix comptype (IFF_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: for any image natags (<optional> NUMA of custom tiff tags) savals (<optional> SARRAY of values) satypes (<optional> SARRAY of types) nasizes (<optional> NUMA of sizes) Return: 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.

l_int32 readHeaderMemTiff ( const l_uint8 cdata,
size_t  size,
l_int32  n,
l_int32 pwidth,
l_int32 pheight,
l_int32 pbps,
l_int32 pspp,
l_int32 pres,
l_int32 pcmap,
l_int32 pformat 
)

readHeaderMemTiff()

Input: cdata (const; tiff-encoded) size (size of data) n (page image number: 0-based) &width (<return>) &height (<return>) &bps (<return> bits per sample -- 1, 2, 4 or 8) &spp (<return>; samples per pixel -- 1 or 3) &res (<optional return>="">; resolution in x dir; NULL to ignore) &cmap (<optional return>="">; colormap exists; input NULL to ignore) &format (<optional return>="">; tiff format; input NULL to ignore) Return: 0 if OK, 1 on error

Notes: (1) Use TIFFClose(); TIFFCleanup() doesn't free internal memstream.

l_int32 readHeaderTiff ( const char *  filename,
l_int32  n,
l_int32 pwidth,
l_int32 pheight,
l_int32 pbps,
l_int32 pspp,
l_int32 pres,
l_int32 pcmap,
l_int32 pformat 
)

readHeaderTiff()

Input: filename n (page image number: 0-based) &width (<return>) &height (<return>) &bps (<return> bits per sample -- 1, 2, 4 or 8) &spp (<return>; samples per pixel -- 1 or 3) &res (<optional return>="">; resolution in x dir; NULL to ignore) &cmap (<optional return>="">; colormap exists; input NULL to ignore) &format (<optional return>="">; tiff format; input NULL to ignore) Return: 0 if OK, 1 on error

Notes: (1) If there is a colormap, cmap is returned as 1; else 0. (2) If
is equal to or greater than the number of images, returns 1.

static l_int32 tiffCloseCallback ( thandle_t  handle) [static]
l_int32 tiffGetCount ( FILE *  fp,
l_int32 pn 
)
static l_int32 tiffMapCallback ( thandle_t  handle,
tdata_t *  data,
toff_t *  length 
) [static]
static tsize_t tiffReadCallback ( thandle_t  handle,
tdata_t  data,
tsize_t  length 
) [static]
static l_int32 tiffReadHeaderTiff ( TIFF *  tif,
l_int32 pwidth,
l_int32 pheight,
l_int32 pbps,
l_int32 pspp,
l_int32 pres,
l_int32 pcmap,
l_int32 pformat 
) [static]

tiffReadHeaderTiff()

Input: tif &width (<return>) &height (<return>) &bps (<return> bits per sample -- 1, 2, 4 or 8) &spp (<return>; samples per pixel -- 1 or 3) &res (<optional return>="">; resolution in x dir; NULL to ignore) &cmap (<optional return>="">; cmap exists; input NULL to ignore) &format (<optional return>="">; tiff format; input NULL to ignore) Return: 0 if OK, 1 on error

static toff_t tiffSeekCallback ( thandle_t  handle,
toff_t  offset,
l_int32  whence 
) [static]
static toff_t tiffSizeCallback ( thandle_t  handle) [static]
static void tiffUnmapCallback ( thandle_t  handle,
tdata_t  data,
toff_t  length 
) [static]
static tsize_t tiffWriteCallback ( thandle_t  handle,
tdata_t  data,
tsize_t  length 
) [static]
static l_int32 writeCustomTiffTags ( TIFF *  tif,
NUMA natags,
SARRAY savals,
SARRAY satypes,
NUMA nasizes 
) [static]

writeCustomTiffTags()

Input: tif natags (<optional> NUMA of custom tiff tags) savals (<optional> SARRAY of values) satypes (<optional> SARRAY of types) nasizes (<optional> NUMA of sizes) Return: 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.

l_int32 writeMultipageTiff ( const char *  dirin,
const char *  substr,
const char *  fileout 
)
l_int32 writeMultipageTiffSA ( SARRAY sa,
const char *  fileout 
)

Переменные

const l_int32 DEFAULT_RESOLUTION = 300 [static]
const l_int32 MAX_PAGES_IN_TIFF_FILE = 3000 [static]
Инициализатор
 {
    {0, 0, 0},
    {0, 1, 0},
    {1, 1, 0},
    {1, 0, 0},
    {0, 1, -1},
    {0, 0, 1},
    {0, 1, 1},
    {0, 0, -1}
}
Инициализатор
 {
    {0, 0, 0},
    {0, 0, 0},
    {0, 0, 0},
    {0, 0, 0},
    {0, 1, -1},
    {0, 1, 1},
    {1, 0, 1},
    {0, 1, -1}
}