Файл src/tiffio.c

#include <stdio.h>
#include <string.h>
#include <stdlib.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 tiffGetResolution (TIFF *tif, l_uint32 *pxres, l_uint32 *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)
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 *modestr)
static TIFF * fopenTiffMemstream (const char *filename, const char *operation, l_uint8 **pdata, size_t *pdatasize)
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)
l_int32 fprintTiffInfo (FILE *fpout, const char *tiffile)
l_int32 tiffGetCount (FILE *fp, l_int32 *pn)
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 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 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 findTiffCompression (FILE *fp, l_int32 *pcomp)
l_int32 extractTiffG4DataFromFile (const char *filein, l_uint8 **pdata, l_int32 *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 []

Типы

typedef struct L_Memstream L_MEMSTREAM


Функции

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

extractTiffG4DataFromFile()

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 pcomp 
)

findTiffCompression()

Input: stream &comp (<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.

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) Requires lseek to rewind to BOF; fseek won't hack it. (3) When linking with windows, suggest you use tif_unix.c instead of tif_win32.c, because it has been reported that the file descriptor returned from fileno() does not work with TIFFFdOpen() in tif_win32.c. (win32 requires a "handle", which is an integer returned by _get_osfhandle(fd).)

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 
)

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) 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_MEMSTREAM * memstreamCreateForRead ( l_uint8 indata,
size_t  pinsize 
) [static]

static L_MEMSTREAM * memstreamCreateForWrite ( l_uint8 **  poutdata,
size_t *  poutsize 
) [static]

static PIX * pixReadFromTiffStream ( TIFF *  tif  )  [static]

pixReadFromTiffStream()

Input: stream Return: pix, or null on error

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.

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)

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) We must call findFileFormat() to get the actual tiff compression format.

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 
)

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) 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 
)

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) 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 tiffGetResolution ( TIFF *  tif,
l_uint32 pxres,
l_uint32 pyres 
) [static]

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 
) [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>="">; colormap exists; 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.


Переменные

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}
}


Документация по Leptonica. Последние изменения: Fri Aug 7 20:31:38 2009. Создано системой  doxygen 1.5.9