![]() |
Leptonica 1.85.0
Image processing and image analysis suite
|
#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) |
| PIX * | pixReadStreamPnm (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) |
| PIX * | pixReadMemPnm (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 |
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.
| l_ok freadHeaderPnm | ( | FILE * | fp, |
| l_int32 * | pw, | ||
| l_int32 * | ph, | ||
| l_int32 * | pd, | ||
| l_int32 * | ptype, | ||
| l_int32 * | pbps, | ||
| l_int32 * | pspp ) |
| [in] | fp | file 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 |
Definition at line 559 of file pnmio.c.
References pnmReadNextNumber(), pnmReadNextString(), and pnmSkipCommentLines().
Referenced by pixReadStreamPnm(), readHeaderMemPnm(), and readHeaderPnm().
| PIX * pixReadMemPnm | ( | const l_uint8 * | data, |
| size_t | size ) |
| [in] | data | const; pnm-encoded |
| [in] | size | of data |
Notes:
(1) The size byte of data must be a null character.
Definition at line 1147 of file pnmio.c.
References pixReadStreamPnm().
| PIX * pixReadStreamPnm | ( | FILE * | fp | ) |
| [in] | fp | file stream opened for read |
Definition at line 150 of file pnmio.c.
References freadHeaderPnm(), pnmReadNextAsciiValue(), SET_DATA_BYTE, SET_DATA_DIBIT, SET_DATA_QBIT, and SET_DATA_TWO_BYTES.
Referenced by pixReadMemPnm().
| l_ok pixWriteMemPam | ( | l_uint8 ** | pdata, |
| size_t * | psize, | ||
| PIX * | pix ) |
| [out] | pdata | data of PAM image |
| [out] | psize | size of returned data |
| [in] | pix |
Notes:
(1) See pixWriteStreamPnm() for usage. This version writes to
memory instead of to a file stream.
Definition at line 1274 of file pnmio.c.
References pixWriteStreamPam().
| l_ok pixWriteMemPnm | ( | l_uint8 ** | pdata, |
| size_t * | psize, | ||
| PIX * | pix ) |
| [out] | pdata | data of PNM image |
| [out] | psize | size of returned data |
| [in] | pix |
Notes:
(1) See pixWriteStreamPnm() for usage. This version writes to
memory instead of to a file stream.
Definition at line 1218 of file pnmio.c.
References pixWriteStreamPnm().
| l_ok pixWriteStreamAsciiPnm | ( | FILE * | fp, |
| PIX * | pix ) |
| [in] | fp | file stream opened for write |
| [in] | pix |
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 853 of file pnmio.c.
References COLOR_BLUE, COLOR_GREEN, COLOR_RED, GET_DATA_BYTE, and REMOVE_CMAP_BASED_ON_SRC.
| l_ok pixWriteStreamPam | ( | FILE * | fp, |
| PIX * | pix ) |
| [in] | fp | file stream opened for write |
| [in] | pix |
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 973 of file pnmio.c.
References COLOR_BLUE, COLOR_GREEN, COLOR_RED, GET_DATA_BIT, GET_DATA_BYTE, GET_DATA_DIBIT, GET_DATA_QBIT, GET_DATA_TWO_BYTES, L_ALPHA_CHANNEL, and REMOVE_CMAP_BASED_ON_SRC.
Referenced by pixWriteMemPam(), and pixWriteStreamPnm().
| l_ok pixWriteStreamPnm | ( | FILE * | fp, |
| PIX * | pix ) |
| [in] | fp | file stream opened for write |
| [in] | pix |
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 736 of file pnmio.c.
References COLOR_BLUE, COLOR_GREEN, COLOR_RED, GET_DATA_BYTE, GET_DATA_DIBIT, GET_DATA_QBIT, GET_DATA_TWO_BYTES, pixWriteStreamPam(), and REMOVE_CMAP_BASED_ON_SRC.
Referenced by pixWriteMemPnm().
|
static |
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 1327 of file pnmio.c.
Referenced by pixReadStreamPnm().
|
static |
| [in] | fp | file stream |
| [out] | pval | value as an integer |
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 1363 of file pnmio.c.
Referenced by freadHeaderPnm().
|
static |
| [in] | fp | file stream |
| [out] | buff | pointer to the string buffer |
| [in] | size | max. number of characters in buffer |
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 1419 of file pnmio.c.
References pnmSkipCommentLines().
Referenced by freadHeaderPnm().
|
static |
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 1463 of file pnmio.c.
Referenced by freadHeaderPnm(), and pnmReadNextString().
| 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 ) |
| [in] | data | const; pnm-encoded |
| [in] | size | of 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 |
Definition at line 1178 of file pnmio.c.
References freadHeaderPnm().
| l_ok readHeaderPnm | ( | const char * | filename, |
| l_int32 * | pw, | ||
| l_int32 * | ph, | ||
| l_int32 * | pd, | ||
| l_int32 * | ptype, | ||
| l_int32 * | pbps, | ||
| l_int32 * | pspp ) |
| [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 |
Definition at line 518 of file pnmio.c.
References freadHeaderPnm().