libpnm

NAME

libpnm - functions to support pnm and pam programs

EXAMPLE

/* Example program fragment to read a PAM or PNM image from stdin, add up the values of every sample in it (I don't know why), and write the image unchanged to stdout. */ #include <pam.h> struct pam inpam, outpam; unsigned int row; pnm_init(&argc, argv); pnm_readpaminit(stdin, &inpam, sizeof(inpam)); outpam = inpam; pnm_writepaminit(&outpam); tuplerow = pnm_allocpamrow(&inpam); for (row = 0; row < inpam.height; row++) { unsigned int col; pnm_readpamrow(&inpam, tuplerow); for (column = 0; column < inpam.width; column++) { unsigned int plane; for (plane = 0; plane < inpam.depth; column++) { grand_total += tuplerow[row][column][plane]; } } pnm_writepamrow(&outpam, tuplerow); } pnm_freepamrow(tuplerow);

SYNOPSIS

#include <pnm.h> void pnm_init( I int * argcP , I char * argv [] ); tuple ** pnm_allocpamarray( I struct pam * pamP ); xel ** pnm_allocarray( I int cols , I int rows ); tuple * pnm_allocpamrow( I struct pam * pamP ); xel * pnm_allocrow( I int cols ); void pnm_freepamarray( I tuple ** tuplearray , I struct pam * pamP ); void pnm_freearray( I xel ** xels , I int rows ); void pnm_freepamrow( I tuple * tuplerow ); void pnm_freerow( I xel * xelrow ); tuple * allocpamtuple( I struct pam * pamP ); void pnm_freepamtuple( I tuple tuple ); void pnm_readpaminit( I FILE * file , I struct pam * pamP , I int size ); void pnm_readpnminit( I FILE * fp , I int * colsP , I int * rowsP , I xelval * maxvalP , I int * formatP ); void pnm_readpamrow( I struct pam * pamP , I tuple * tuplerow ); void pnm_readpnmrow( I FILE * fp , I xel * xelrow , I int cols ,
I xelval maxval , I int format ); tuple ** pnm_readpam( I FILE * file , I struct pam * pamP ,
I int size ); xel ** pnm_readpnm( I FILE * fp , I int * colsP , I int * rowsP ,
I xelval * maxvalP , int * formatP ); void pnm_writepaminit( I struct pam * pamP ); void pnm_writepnminit( FILE * fp , I int cols , I int rows , I xelval maxval , I int format , I int forceplain ); void pnm_writepamrow( I struct pam * pamP , I const tuple * tuplerow ); void pnm_writepnmrow( I FILE * fp , I xel * xelrow , I int cols , I xelval maxval , I int format , I int forceplain ); void pnm_writepam( I struct pam * pamP , I const tuple * const * tuplearray ); void pnm_writepnm( I FILE * fp , I xel ** xels , I int cols , I int rows , I xelval maxval , I int format , I int forceplain ); void pnm_checkpam( I struct pam * pamP , I const enum pm_check_type check_type , I enum pm_check_code * retvalP ); void pnm_nextimage( I FILE * file , I int * const eofP ); void pnm_check( I FILE * file , I const enum pm_check_type check_type , I const int format , I const int cols , I const int rows , I const xelval maxval , I enum pm_check_code * retvalP ); void pnm_promoteformatrow( I xel * xelrow , I int cols , I xelval maxval , I int format , I xelval newmaxval , I int newformat ); void pnm_promoteformatrow( I xel ** xels , I int cols , I xelval maxval , I int format , I xelval newmaxval , I int newformat ); xel pnm_whitexel( I xelval maxval , I int format ); xel pnm_blackxel( I xelval maxval , I int format ); void pnm_invertxel( I xel * x , I xelval maxval , I int format ); xel pnm_backgroundxelrow( I xel * xelrow , I int cols , I xelval maxval , I int format ); xel pnm_backgroundxel( I xel ** xels , I int cols , I int rows , I xelval maxval , I int format ); void pnm_YCbCrtuple( I tuple tuple , I double * YP , I double * CrP , I double * CbP ); struct pam {
int size
int len
FILE * file
int format
int plainformat
int height
int width
int depth
sample maxval
int bytes_per_sample
char B tuple_type [256]; } typedef ... sample; typedef ... tuple; typedef ... xelval; typedef ... xel; extern xelval pnm_pbmmaxval; #define PNM_MAXMAXVAL ... #define PNM_OVERALLMAXVAL ... #define PNM_FORMAT ... #define I PNM_ASSIGN1( x , v ) ... #define I PNM_GET1( x ) ... #define I PNM_EQUAL( x , y ) ... #define I PAM_FORMAT_TYPE( format ) ... #define I PNM_FORMAT_TYPE( format ) ...

DESCRIPTION

PAM VERSUS PNM FUNCTIONS

The PNM library contains two classes of functions: The pam functions and the pnm functions. The pam functions are enhancements of the pnm functions and you should use them unless you need to be compatible with older PNM libraries that don't have them (those released before August 2000). The pnm functions operate on PBM, PGM, and PPM images and files. They are similar to the functions in the PBM, PGM, and PPM libraries, except the pnm functions let you operate on all three, both reading and writing, without a lot of concern for which of the three formats you are processing. The pam functions provide all the same functions for operating on PBM, PGM, and PPM libraries, but also operate on the newer PAM images and files. The pam functions are easier to use than the pnm functions due to improved parameter lists. There is no separate PAM library specific to the PAM format, as there is for PBM, PGM, and PPM.

THE pam STRUCTURE

The pam functions take most of their arguments in the form of a single pam structure. This is not an opaque object, but just a convenient way to organize the information upon which most the functions depend. So you are free to access or set the elements of the structure however you want. But you will find in most cases it is most convenient to call pnm_readpaminit() or pnm_writepaminit() to set the fields in the pam structure before calling any other pam functions, and then just to pass the structure unchanged in all future calls to pam functions. The fields are:
size
The storage size in bytes of this entire structure.
len
The length, in bytes, of the information in this structure. The information starts in the first byte and is contiguous. This cannot be greater than R size . size and len can be used to make programs compatible with newer and older versions of the Netpbm libraries.
file
The file.
format
The format code of the raw image. This is PAM_FORMAT unless the PAM image is really a view of a PBM, PGM, or PPM image. Then it's R PBM_FORMAT , R RPBM_FORMAT , etc.
plainformat
This is a boolean value and means: The format above is a plain (text) format as opposed to a raw (binary) format. This is entirely redundant with the format member and exists as a separate member only for computational speed.
height
The height of the image in rows.
width
The width of the image in number of columns (tuples per row).
depth
The depth of the image (degree of or number of samples in each tuple).
maxval
The maxval of the image. See definitions in pam(5).
bytes_per_sample
The number of bytes used to represent each sample in the image file. See the format definition in pam(5). This is entirely redundant with R maxval . It exists as a separate member for computational speed.
tuple_type
The tuple type of the image. See definitions in pam(5). Netpbm does not define any values for this except the following, which are used for a PAM image which is really a view of a PBM, PGM, or PPM image: R PAM_PBM_TUPLETYPE , R PAM_PGM_TUPLETYPE , R PAM_PPM_TUPLETYPE .

PLAIN VERSUS RAW FORMAT

The PNM formats each come in two varieties: the older plain (text) format and the newer raw (binary) format. There are different format codes for the plain and raw formats, but which of the two formats the pnm and pam functions write is independent of the format code you pass to them. The pam functions always write raw formats. If you specify the format code for a plain format, a pam function assumes instead the raw version of that format. The pnm functions choose between plain and raw based on the forceplain parameter that every write-type pnm function has. If this boolean value is true, the function writes the plain version of the format specified by the format code. If it is false, the function writes the raw version of the format specified by the format code. We are trying to stamp out the older plain formats, so it would be a wise choice not to write a program that sets forceplain true under any circumstance. A user who needs a plain format can use the pnmtoplainpnm program to convert the output of your program to plain format.

PNM TYPES AND CONSTANTS

Each R xel contains three R xelval s, each of which should contain only the values between R 0 and R PNM_MAXMAXVAL , inclusive. R pnm_pbmmaxval is the maxval used when a PNM program reads a PBM file. Normally it is 1; however, for some programs, a larger value gives better results.

PNM XEL MANIPULATIONS

The PNM_GET1 macro extracts a single value from an xel, when you know it's from a PBM or PGM file. When it's from a PPM file, use R PPM_GETR() , R PPM_GETG() , and R PPM_GETB() . The PNM_ASSIGN1 macro assigns a single value to an xel, when you know it's from a PBM or PGM file. When it's from a PPM file, use R PPM_ASSIGN . The PNM_EQUAL macro checks two xels for equality. The PNM_FORMAT_TYPE and PAM_FORMAT_TYPE macros compute a format type code from a format code. The format types are PBM, PGM, PPM, and PAM. But note that PBM, PGM, and PPM each are two different formats: a plain one and a raw one. So there are four format types, but seven formats. PNM_FORMAT_TYPE does not work on the PAM format code.

INITIALIZATION

All PNM and PAM programs must call pnm_init() just after startup, before they process their arguments. R pnm_init() , among other things, processes Netpbm universal parameters and removes them from the parameter list.

MEMORY MANAGEMENT

pnm_allocpamarray() allocates space for an array of tuples. pnm_freepamarray() frees an array space allocated by pnm_allocpamarray() or R pnm_readpam() . pnm_allocarray() allocates space for an array of xels. pnm_freearray() frees an array space allocated by pnm_allocarray() or R pnm_readpnm() . pnm_allocpamrow() allocates space for a row of a PAM image. pnm_freepamrow() frees it. pnm_allocrow() allocates space for a row of a PNM image. pnm_freerow() frees it.

READING PNM FILES

pnm_readpaminit() reads the header of a PAM or PNM image. It returns the information from the header in the I * pamP structure. It does not require any members of I * pamP to be set at invocation, and sets every member. size is the storage size of the I * pamP structure, normally R sizeof(struct pam) . The function expects to find the image file positioned to the start of the header and leaves it positioned to the start of the raster. pnm_readpnminit() is similar to R pnm_readpaminit() , but reads only PNM images and has a different parameter list. pnm_readpamrow() reads a row of the raster from a PAM or PNM image file. It expects all of the members of the I *pamP structure to be set upon invocation and does not modify any of them. It expects to find the file positioned to the start of the row in question in the raster and leaves it positioned just after it. It returns the row as the array of tuples R tuplerow , which must already have its column pointers set up so that it forms a C 2-dimensional array. The leftmost tuple is Element 0 of this array. pnm_readpnmrow() is similar to pnm_readpamrow() but only works on PNM images and has a different parameter list and returns the row as an array of xels instead of tuples. pnm_readpam() reads an entire image from a PAM or PNM image file and allocates the space in which to return the raster. It expects to find the file positioned to the first byte of the image and leaves it positioned just after the image. The function does not require I * pamP to have any of its members set and sets them all. size is the storage size in bytes of the I * pamP structure, normally R sizeof(struct pam) . The return value is a newly allocated array of the rows of the image, with the top row being Element 0 of the array. Each row is represented as pnm_readpamrow() would return. The return value is also effectively a 3-dimensional C array of samples, with the dimensions corresponding to the height, width, and depth of the image, in that order. pnm_readpam() combines the functions of R pnm_allocpamarray() , R pnm_readpaminit() , and iterations of R pnm_readpamrow() . It may require more dynamic storage than you can afford. pnm_readpnm() is similar to pnm_readpam() except that it reads only PNM images and uses a different parameter list and returns an array of rows such that pnm_readpnmrow() would return rather than such that pnm_readpamrow() would return.

WRITING FILES

pnm_writepnminit() writes the header of a PAM or PNM image and computes some of the fields of the pam structure. The following members of the I * pamP structure must be set upon invocation to tell the function how and what to write. R size , R len , R file , R format , R height , R width , R depth , R maxval , R tuple_type . pnm_writepaminit() sets the plainformat and bytes_per_sample members based on the information supplied. pnm_writepnminit() is similar to pnm_writepaminit() except that it can write only a PNM header and has a different parameter list. See the description of forceplain above. pnm_writepamrow() writes a row of the raster into a PAM or PNM image file. It expects to find the file positioned where the row should start and leaves it positioned just after the row. The function requires all the elements of I * pamP to be set upon invocation and doesn't modify them. tuplerow is an array of tuples representing the row. The leftmost tuple is Element 0 of this array. pnm_writepnmrow() is similar to pnm_writepamrow() except that it works only on PNM images and has a different parameter list and takes an array of xels instead of an array of tuples. See the description of forceplain above. pnm_writepam() writes an entire PAM or PNM image to a PAM or PNM image file. It expects to find the file positioned to where the image should start and leaves it positioned just after the image. The following members of the I * pamP structure must be set upon invocation to tell the function how and what to write: R size , R len , R file , R format , R height , R width , R depth , R maxval , R tuple_type . pnm_writepam() sets the plainformat and bytes_per_sample members based on the information supplied. tuplearray is an array of rows such that you would pass to R pnm_writepamrow() , with the top row being Element 0 of the array. pnm_writepam() combines the functions of R pnm_writepaminit() , and iterations of R pnm_writepamrow() . It's raster input may be more storage than you can afford. pnm_writepnm() is similar to pnm_writepam() except that it works only on PNM image, has a different parameter list, and takes an array of rows of xels instead of an array of rows of tuples. See the description of forceplain above.

MISCELLANEOUS

pnm_nextimage() positions a PNM input file to the next image in it (so that a subsequent pnm_readpnminit() reads its header). pnm_nextimage() is identical to R pbm_nextimage() . pam_check() checks for the common file integrity error where the file is the wrong size to contain the raster, according to the information in the header. This works on PAM and PNM images. pnm_check() is similar to pam_check() except it works only on PNM images. pnm_check() is identical to R ppm_check() .

PNM FORMAT PROMOTION

pnm_promoteformatrow() promotes a row of xels from one maxval and format to a new set. Use this when you are combining multiple anymaps of different types - just take the maximum of the maxvals and the maximum of the formats, and promote them all to that. pnm_promoteformat() promotes an entire anymap.

PNM XEL MANIPULATION

pnm_whitexel() and pnm_blackxel() return a white or black xel, respectively, for the given maxval and R format . pnm_invertxel() inverts an xel. pnm_backgroundxelrow() figures out an appropriate background xel based on the row of xels R xelrow , which is cols xels wide, has maxval R maxval , and represents an image with format R format . This estimate works best when the row is the top or bottom row of the image. pnm_backgroundxel() does the same thing as R pnm_backgroundxelrow() , except based on an entire image instead of just one row. This tends to do a slightly better job than R pnmbackgroundxelrow() . pnm_YCbCrtuple() Returns the Y/Cb/Cr luminance/chrominance representation of the color represented by the input tuple, assuming that the tuple is an RGB color representation (which is the case if it was read from a PPM image). The output components are based on the same scale (maxval) as the input tuple, but are floating point nonetheless to avoid losing information due to rounding. Divide them by the maxval to get normalized [0..1] values.

SEE ALSO

pbm(5), pgm(5), ppm(5), pam(5), libpbm(3), libpgm(3), libppm(3)

AUTHOR

Copyright (C) 1989, 1991 by Tony Hansen and Jef Poskanzer.