Leptonica  1.82.0
Image processing and image analysis suite
pnmio.c File Reference
#include <string.h>
#include <ctype.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

static l_int32 pnmReadNextAsciiValue (FILE *fp, l_int32 *pval)
 
static l_int32 pnmReadNextNumber (FILE *fp, l_int32 *pval)
 
static l_int32 pnmReadNextString (FILE *fp, char *buff, l_int32 size)
 
static l_int32 pnmSkipCommentLines (FILE *fp)
 
PIXpixReadStreamPnm (FILE *fp)
 
l_ok readHeaderPnm (const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pd, l_int32 *ptype, l_int32 *pbps, l_int32 *pspp)
 
l_ok freadHeaderPnm (FILE *fp, l_int32 *pw, l_int32 *ph, l_int32 *pd, l_int32 *ptype, l_int32 *pbps, l_int32 *pspp)
 
l_ok pixWriteStreamPnm (FILE *fp, PIX *pix)
 
l_ok pixWriteStreamAsciiPnm (FILE *fp, PIX *pix)
 
l_ok pixWriteStreamPam (FILE *fp, PIX *pix)
 
PIXpixReadMemPnm (const l_uint8 *data, size_t size)
 
l_ok readHeaderMemPnm (const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pd, l_int32 *ptype, l_int32 *pbps, l_int32 *pspp)
 
l_ok pixWriteMemPnm (l_uint8 **pdata, size_t *psize, PIX *pix)
 
l_ok pixWriteMemPam (l_uint8 **pdata, size_t *psize, PIX *pix)
 

Variables

static const l_int32 MAX_PNM_WIDTH = 100000
 
static const l_int32 MAX_PNM_HEIGHT = 100000
 

Detailed Description

     Stream interface
         PIX             *pixReadStreamPnm()
         l_int32          readHeaderPnm()
         l_int32          freadHeaderPnm()
         l_int32          pixWriteStreamPnm()
         l_int32          pixWriteStreamAsciiPnm()
         l_int32          pixWriteStreamPam()
     Read/write to memory
         PIX             *pixReadMemPnm()
         l_int32          readHeaderMemPnm()
         l_int32          pixWriteMemPnm()
         l_int32          pixWriteMemPam()
     Local helpers
         static l_int32   pnmReadNextAsciiValue();
         static l_int32   pnmReadNextNumber();
         static l_int32   pnmReadNextString();
         static l_int32   pnmSkipCommentLines();
     These are here by popular demand, with the help of Mattias
     Kregert (matti.nosp@m.as@k.nosp@m.reger.nosp@m.t.se), who provided the first implementation.
     The pnm formats are exceedingly simple, because they have
     no compression and no colormaps.  They support images that
     are 1 bpp; 2, 4, 8 and 16 bpp grayscale; and rgb.
     The original pnm formats ("ASCII") are included for completeness,
     but their use is deprecated for all but tiny iconic images.
     They are extremely wasteful of memory; for example, the P1 binary
     ASCII format is 16 times as big as the packed uncompressed
     format, because 2 characters are used to represent every bit
     (pixel) in the image.  Reading is slow because we check for extra
     white space and EOL at every sample value.
     The packed pnm formats ("raw") give file sizes similar to
     bmp files, which are uncompressed packed.  However, bmp
     are more flexible, because they can support colormaps.
     We don't differentiate between the different types ("pbm",
     "pgm", "ppm") at the interface level, because this is really a
     "distinction without a difference."  You read a file, you get
     the appropriate Pix.  You write a file from a Pix, you get the
     appropriate type of file.  If there is a colormap on the Pix,
     and the Pix is more than 1 bpp, you get either an 8 bpp pgm
     or a 24 bpp RGB pnm, depending on whether the colormap colors
     are gray or rgb, respectively.
     This follows the general policy that the I/O routines don't
     make decisions about the content of the image -- you do that
     with image processing before you write it out to file.
     The I/O routines just try to make the closest connection
     possible between the file and the Pix in memory.
     On systems like windows without fmemopen() and open_memstream(),
     we write data to a temp file and read it back for operations
     between pix and compressed-data, such as pixReadMemPnm() and
     pixWriteMemPnm().
     The P7 format is new. It introduced a header with multiple
     lines containing distinct tags for the various fields.
     See: http://netpbm.sourceforge.net/doc/pam.html
       WIDTH <int>         ; mandatory, exactly once
       HEIGHT <int>        ; mandatory, exactly once
       DEPTH <int>         ; mandatory, exactly once,
                           ; its meaning is equivalent to spp
       MAXVAL <int>        ; mandatory, one of 1, 3, 15, 255 or 65535
       TUPLTYPE <string>   ; optional; BLACKANDWHITE, GRAYSCALE, RGB
                           ; and optional suffix _ALPHA, e.g. RGB_ALPHA
       ENDHDR              ; mandatory, last header line
     Reading BLACKANDWHITE_ALPHA and GRAYSCALE_ALPHA, which have a DEPTH
     value of 2, is supported. The original image is converted to a Pix
     with 32-bpp and alpha channel (spp == 4).
     Writing P7 format is currently selected for 32-bpp with alpha
     channel, i.e. for Pix which have spp == 4, using pixWriteStreamPam().
     Jürgen Buchmüller provided the implementation for the P7 (pam) format.
     Giulio Lunati made an elegant reimplementation of the static helper
     functions using fscanf() instead of fseek(), so that it works with
     pnm data from stdin.

Definition in file pnmio.c.

Function Documentation

◆ freadHeaderPnm()

l_ok freadHeaderPnm ( FILE *  fp,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pd,
l_int32 *  ptype,
l_int32 *  pbps,
l_int32 *  pspp 
)

freadHeaderPnm()

Parameters
[in]fpfile stream opened for read
[out]pw[optional]
[out]ph[optional]
[out]pd[optional]
[out]ptype[optional] pnm type
[out]pbps[optional] bits/sample
[out]pspp[optional] samples/pixel
Returns
0 if OK, 1 on error

Definition at line 563 of file pnmio.c.

References pnmReadNextNumber(), pnmReadNextString(), and pnmSkipCommentLines().

Referenced by pixReadStreamPnm(), readHeaderMemPnm(), and readHeaderPnm().

◆ pixReadMemPnm()

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

pixReadMemPnm()

Parameters
[in]dataconst; pnm-encoded
[in]sizeof data
Returns
pix, or NULL on error
Notes:
     (1) The size byte of data must be a null character.

Definition at line 1159 of file pnmio.c.

References fopenReadFromMemory(), and pixReadStreamPnm().

◆ pixReadStreamPnm()

PIX* pixReadStreamPnm ( FILE *  fp)

pixReadStreamPnm()

Parameters
[in]fpfile stream opened for read
Returns
pix, or NULL on error

Definition at line 150 of file pnmio.c.

References freadHeaderPnm(), and pixCreate().

Referenced by pixReadMemPnm().

◆ pixWriteMemPam()

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

pixWriteMemPam()

Parameters
[out]pdatadata of PAM image
[out]psizesize of returned data
[in]pix
Returns
0 if OK, 1 on error
Notes:
     (1) See pixWriteStreamPnm() for usage.  This version writes to
         memory instead of to a file stream.

Definition at line 1292 of file pnmio.c.

References fopenWriteWinTempfile(), l_binaryReadStream(), and pixWriteStreamPam().

◆ pixWriteMemPnm()

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

pixWriteMemPnm()

Parameters
[out]pdatadata of PNM image
[out]psizesize of returned data
[in]pix
Returns
0 if OK, 1 on error
Notes:
     (1) See pixWriteStreamPnm() for usage.  This version writes to
         memory instead of to a file stream.

Definition at line 1234 of file pnmio.c.

References fopenWriteWinTempfile(), l_binaryReadStream(), and pixWriteStreamPnm().

◆ pixWriteStreamAsciiPnm()

l_ok pixWriteStreamAsciiPnm ( FILE *  fp,
PIX pix 
)

pixWriteStreamAsciiPnm()

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

Writes "ASCII" format only: 1 bpp --> pbm P1 2, 4, 8, 16 bpp, no colormap or grayscale colormap --> pgm P2 2, 4, 8 bpp with color-valued colormap, or rgb --> rgb ppm P3

Definition at line 861 of file pnmio.c.

References pixGetDimensions().

◆ pixWriteStreamPam()

l_ok pixWriteStreamPam ( FILE *  fp,
PIX pix 
)

pixWriteStreamPam()

Parameters
[in]fpfile stream opened for write
[in]pix
Returns
0 if OK; 1 on error
Notes:
     (1) This writes arbitrary PAM (P7) packed format.
     (2) 24 bpp rgb are not supported in leptonica, but this will
         write them out as a packed array of bytes (3 to a pixel).

Definition at line 983 of file pnmio.c.

References pixGetDimensions().

Referenced by pixWriteMemPam().

◆ pixWriteStreamPnm()

l_ok pixWriteStreamPnm ( FILE *  fp,
PIX pix 
)

pixWriteStreamPnm()

Parameters
[in]fpfile stream opened for write
[in]pix
Returns
0 if OK; 1 on error
Notes:
     (1) This writes "raw" packed format only:
         1 bpp --> pbm (P4)
         2, 4, 8, 16 bpp, no colormap or grayscale colormap --> pgm (P5)
         2, 4, 8 bpp with color-valued colormap, or rgb --> rgb ppm (P6)
     (2) 24 bpp rgb are not supported in leptonica, but this will
         write them out as a packed array of bytes (3 to a pixel).

Definition at line 742 of file pnmio.c.

References pixGetDimensions().

Referenced by pixWriteMemPnm().

◆ pnmReadNextAsciiValue()

static l_int32 pnmReadNextAsciiValue ( FILE *  fp,
l_int32 *  pval 
)
static

pnmReadNextAsciiValue()

 Return: 0 if OK, 1 on error or EOF.

Notes: (1) This reads the next sample value in ASCII from the file.

Definition at line 1347 of file pnmio.c.

◆ pnmReadNextNumber()

static l_int32 pnmReadNextNumber ( FILE *  fp,
l_int32 *  pval 
)
static

pnmReadNextNumber()

Parameters
[in]fpfile stream
[out]pvalvalue as an integer
Returns
0 if OK, 1 on error or EOF.
Notes:
     (1) This reads the next set of numeric chars, returning
         the value and swallowing initial whitespaces and ONE
         trailing whitespace character.  This is needed to read
         the maxval in the header, which precedes the binary data.

Definition at line 1385 of file pnmio.c.

Referenced by freadHeaderPnm().

◆ pnmReadNextString()

static l_int32 pnmReadNextString ( FILE *  fp,
char *  buff,
l_int32  size 
)
static

pnmReadNextString()

Parameters
[in]fpfile stream
[out]buffpointer to the string buffer
[in]sizemax. number of characters in buffer
Returns
0 if OK, 1 on error or EOF.
Notes:
     (1) This reads the next set of alphanumeric chars, returning the string.
         This is needed to read header lines, which precede the P7
         format binary data.

Definition at line 1443 of file pnmio.c.

References pnmSkipCommentLines().

Referenced by freadHeaderPnm().

◆ pnmSkipCommentLines()

static l_int32 pnmSkipCommentLines ( FILE *  fp)
static

pnmSkipCommentLines()

 Return: 0 if OK, 1 on error or EOF

Notes: (1) Comment lines begin with '#' (2) Usage: caller should check return value for EOF (3) The previous implementation used fseek(fp, -1L, SEEK_CUR) to back up one character, which doesn't work with stdin.

Definition at line 1490 of file pnmio.c.

Referenced by freadHeaderPnm(), and pnmReadNextString().

◆ readHeaderMemPnm()

l_ok readHeaderMemPnm ( const l_uint8 *  data,
size_t  size,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pd,
l_int32 *  ptype,
l_int32 *  pbps,
l_int32 *  pspp 
)

readHeaderMemPnm()

Parameters
[in]dataconst; pnm-encoded
[in]sizeof data
[out]pw[optional]
[out]ph[optional]
[out]pd[optional]
[out]ptype[optional] pnm type
[out]pbps[optional] bits/sample
[out]pspp[optional] samples/pixel
Returns
0 if OK, 1 on error

Definition at line 1192 of file pnmio.c.

References fopenReadFromMemory(), and freadHeaderPnm().

◆ readHeaderPnm()

l_ok readHeaderPnm ( const char *  filename,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pd,
l_int32 *  ptype,
l_int32 *  pbps,
l_int32 *  pspp 
)

readHeaderPnm()

Parameters
[in]filename
[out]pw[optional]
[out]ph[optional]
[out]pd[optional]
[out]ptype[optional] pnm type
[out]pbps[optional] bits/sample
[out]pspp[optional] samples/pixel
Returns
0 if OK, 1 on error

Definition at line 520 of file pnmio.c.

References fopenReadStream(), and freadHeaderPnm().