Leptonica  1.83.1
Image processing and image analysis suite
spixio.c File Reference
#include <string.h>
#include "allheaders.h"

Go to the source code of this file.

Macros

#define DEBUG_SERIALIZE   0
 

Functions

PIXpixReadStreamSpix (FILE *fp)
 
l_ok readHeaderSpix (const char *filename, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
 
l_ok freadHeaderSpix (FILE *fp, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
 
l_ok sreadHeaderSpix (const l_uint32 *data, size_t size, l_int32 *pwidth, l_int32 *pheight, l_int32 *pbps, l_int32 *pspp, l_int32 *piscmap)
 
l_ok pixWriteStreamSpix (FILE *fp, PIX *pix)
 
PIXpixReadMemSpix (const l_uint8 *data, size_t size)
 
l_ok pixWriteMemSpix (l_uint8 **pdata, size_t *psize, PIX *pix)
 
l_ok pixSerializeToMemory (PIX *pixs, l_uint32 **pdata, size_t *pnbytes)
 
PIXpixDeserializeFromMemory (const l_uint32 *data, size_t nbytes)
 

Variables

static const l_int32 MaxAllowedWidth = 1000000
 
static const l_int32 MaxAllowedHeight = 1000000
 
static const l_int64 MaxAllowedArea = 400000000LL
 

Detailed Description


   This does fast serialization of a pix in memory to file,
   copying the raw data for maximum speed.  The underlying
   function serializes it to memory, and it is wrapped to be
   callable from standard pixRead() and pixWrite() file functions.

     Reading spix from file
          PIX        *pixReadStreamSpix()
          l_int32     readHeaderSpix()
          l_int32     freadHeaderSpix()
          l_int32     sreadHeaderSpix()

     Writing spix to file
          l_int32     pixWriteStreamSpix()

     Low-level serialization of pix to/from memory (uncompressed)
          PIX        *pixReadMemSpix()
          l_int32     pixWriteMemSpix()
          l_int32     pixSerializeToMemory()
          PIX        *pixDeserializeFromMemory()

   Note: these functions have not been extensively tested for fuzzing
   (bad input data that can result in, e.g., memory faults).
   The spix serialization format is only defined here, in leptonica.
   The image data is uncompressed and the serialization is not intended
   to be a secure file format from untrusted sources.

Definition in file spixio.c.

Function Documentation

◆ freadHeaderSpix()

l_ok freadHeaderSpix ( FILE *  fp,
l_int32 *  pwidth,
l_int32 *  pheight,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  piscmap 
)

freadHeaderSpix()

Parameters
[in]fpfile stream
[out]pwidthwidth
[out]pheightheight
[out]pbpsbits/sample
[out]psppsamples/pixel
[out]piscmap[optional] input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) If there is a colormap, iscmap is returned as 1; else 0.

Definition at line 167 of file spixio.c.

References fnbytesInFile(), and sreadHeaderSpix().

Referenced by readHeaderSpix().

◆ pixDeserializeFromMemory()

PIX* pixDeserializeFromMemory ( const l_uint32 *  data,
size_t  nbytes 
)

pixDeserializeFromMemory()

Parameters
[in]dataserialized data in memory
[in]nbytesnumber of bytes in data string
Returns
pix, or NULL on error
Notes:
     (1) See pixSerializeToMemory() for the binary format.
     (2) Note the image size limits.

Definition at line 424 of file spixio.c.

Referenced by pixReadMemSpix().

◆ pixReadMemSpix()

PIX* pixReadMemSpix ( const l_uint8 *  data,
size_t  size 
)

pixReadMemSpix()

Parameters
[in]dataconst; uncompressed
[in]sizebytes of data
Returns
pix, or NULL on error

Definition at line 295 of file spixio.c.

References pixDeserializeFromMemory().

Referenced by pixReadStreamSpix().

◆ pixReadStreamSpix()

PIX* pixReadStreamSpix ( FILE *  fp)

pixReadStreamSpix()

Parameters
[in]fpfile stream
Returns
pix, or NULL on error.
Notes:
     (1) If called from pixReadStream(), the stream is positioned
         at the beginning of the file.

Definition at line 92 of file spixio.c.

References l_binaryReadStream(), and pixReadMemSpix().

◆ pixSerializeToMemory()

l_ok pixSerializeToMemory ( PIX pixs,
l_uint32 **  pdata,
size_t *  pnbytes 
)

pixSerializeToMemory()

Parameters
[in]pixsall depths, colormap OK
[out]pdataserialized data in memory
[out]pnbytesnumber of bytes in data string
Returns
0 if OK, 1 on error
Notes:
     (1) This does a fast serialization of the principal elements
         of the pix, as follows:
           "spix"    (4 bytes) -- ID for file type
           w         (4 bytes)
           h         (4 bytes)
           d         (4 bytes)
           wpl       (4 bytes)
           ncolors   (4 bytes) -- in colormap; 0 if there is no colormap
           cdata     (4 * ncolors)  -- size of serialized colormap array
           rdatasize (4 bytes) -- size of serialized raster data
                                  = 4 * wpl * h
           rdata     (rdatasize)

Definition at line 344 of file spixio.c.

References pixGetDimensions().

Referenced by pixWriteMemSpix().

◆ pixWriteMemSpix()

l_ok pixWriteMemSpix ( l_uint8 **  pdata,
size_t *  psize,
PIX pix 
)

pixWriteMemSpix()

Parameters
[out]pdatadata of serialized, uncompressed pix
[out]psizesize of returned data
[in]pixall depths; colormap OK
Returns
0 if OK, 1 on error

Definition at line 311 of file spixio.c.

References pixSerializeToMemory().

Referenced by pixWriteStreamSpix().

◆ pixWriteStreamSpix()

l_ok pixWriteStreamSpix ( FILE *  fp,
PIX pix 
)

pixWriteStreamSpix()

Parameters
[in]fpfile stream
[in]pix
Returns
0 if OK; 1 on error

Definition at line 265 of file spixio.c.

References pixWriteMemSpix().

◆ readHeaderSpix()

l_ok readHeaderSpix ( const char *  filename,
l_int32 *  pwidth,
l_int32 *  pheight,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  piscmap 
)

readHeaderSpix()

Parameters
[in]filename
[out]pwidthwidth
[out]pheightheight
[out]pbpsbits/sample
[out]psppsamples/pixel
[out]piscmap[optional] input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) If there is a colormap, iscmap is returned as 1; else 0.

Definition at line 128 of file spixio.c.

References fopenReadStream(), and freadHeaderSpix().

◆ sreadHeaderSpix()

l_ok sreadHeaderSpix ( const l_uint32 *  data,
size_t  size,
l_int32 *  pwidth,
l_int32 *  pheight,
l_int32 *  pbps,
l_int32 *  pspp,
l_int32 *  piscmap 
)

sreadHeaderSpix()

Parameters
[in]data
[in]sizeof data
[out]pwidthwidth
[out]pheightheight
[out]pbpsbits/sample
[out]psppsamples/pixel
[out]piscmap[optional] input NULL to ignore
Returns
0 if OK, 1 on error
Notes:
     (1) If there is a colormap, iscmap is returned as 1; else 0.

Definition at line 210 of file spixio.c.

Referenced by freadHeaderSpix().