The Libnetpbm programming library is part of Netpbm.
Here is an example of a C program that uses libnetpbm to read a Netpbm image input and produce a Netpbm image output.
/* 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 <netpbm/pam.h> struct pam inpam, outpam; tuple * tuplerow; unsigned int row; pm_init(argv[0], 0); pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type)); outpam = inpam; outpam.file = stdout; pnm_writepaminit(&outpam); tuplerow = pnm_allocpamrow(&inpam); for (row = 0; row < inpam.height; row++) { unsigned int column; pnm_readpamrow(&inpam, tuplerow); for (column = 0; column < inpam.width; ++column) { unsigned int plane; for (plane = 0; plane < inpam.depth; ++plane) { grand_total += tuplerow[column][plane]; } } pnm_writepamrow(&outpam, tuplerow); } pnm_freepamrow(tuplerow);
In this section, Guide To Using Libnetpbm, we cover only the PAM functions in libnetpbm. As described in the introduction to libnetpbm, there are four other classes of image processing functions (PBM, PGM, PPM, PNM). They are less important, since you can do everything more easily with the PAM functions, but if you're working on old programs or need the extra efficiency those older functions can sometimes provide, you can find them documented as here: PBM Function Manual, PGM Function Manual, PPM Function Manual, and PNM Function Manual.
In case you're wondering, what makes the PAM functions easier to use is:
Every program that uses the library must initialize the library, i.e. set up the process to use the library, as described in Initialization. That is the purpose of the call to pm_init() in the example above.
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 members 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.
It depends upon the function to which you pass the structure what members are inputs, what members are outputs, and what members are irrelevant.
It is possible for a pam structure not to specify some members, by operation of its len member. When you supply a pam structure as an argument to a function, the function has default behavior defined for unspecified members. All the functions require that you specify at least up through maxval, and some require more.
Likewise, a function the returns a pam structure can return only a subset of the members defined here, according to its setting of the len member. But this normally happens only because the library is old and predates the existence of the omitted members.
The members are:
There is an important quirk in the meaning of this member when you use the pam structure to write an image: Only the type portion of it is meaningful. A Netpbm format code conveys two pieces of information: The format type (PBM, PGM, PPM, or PAM) and the plainness (plain PBM vs raw PBM, etc.). But when writing, libnetpbm ignores the plainness part and instead takes the plainness from the plainformat member. So PBM_FORMAT and RPBM_FORMAT are identical when writing.
This quirk exists for historical purposes; it's necessary for consistency with the older functions such as pnm_writepnmrow() whose format and forceplain arguments are analogous.
Before Netpbm 10.32 (February 2006), libnetpbm did not ignore the plainness. This caused many programs to behave poorly, producing plain format output when they should, for backward compatibility at the very least, produce raw format output.
A common way to use this member is to copy it and the plainformat member from a pam for an input image to a pam for an output image. When you do that, your output image will be raw format regardless of whether your input image was plain or raw, and this is the conventional behavior of Netpbm programs.
Until Netpbm 10.32 (February 2006), this was defined a little differently. The format member did in fact completely identify the format and plainformat was redundant and existed as a separate member only for computational speed. But this was inconsistent with the older libnetpbm interface (e.g. pnm_writepnm(), and it made it difficult to write backward compatible programs. Before Netpbm 10.32, it affected reading as well as writing.
libnetpbm image reading functions set this member to false, for your convenience in building an output image pam from an input image pam.
The purpose of this is to make it possible for a program to change the type of a tuple to one with more or fewer planes.
0 means the allocation depth is the same as the image depth.
On output, NULL means no comments.
On input, libnetpbm mallocs storage for the comments and placed the pointer at *comment_p. Caller must free it. NULL means libnetpbm does not return comments and does not allocate any storage.
Examples:
const char * comments;
...
pam.comment_p = &comments;
pnm_readpaminit(fileP, &pam, PAM_STRUCT_SIZE(comment_p));
printf("The comments are:\n");
printf("%s", comments)
free(comments);
const char * comments;
...
comments = strdup("This is a comment 1\nThis is comment 2\n");
pam.comment_p = &comments;
pnm_writepaminit(&pam);
free(comments);
This works only for PAM images. If you read a PNM image, you always get back a null string. If you write a PNM image, you always get an image that contains no comments.
This member does not exist before Netpbm 10.35 (August 2006). Before that, there is no way with libnetpbm to get or set comments. The macro PAM_HAVE_COMMENT_P is defined in pam.h where the member exists.
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.
The Libnetpbm Netpbm Image Processing Manual describes the the libnetpbm functions for processing image data.
The Libnetpbm Utility Manual describes the functions that are not specifically related to the Netpbm image formats.