\ .\" This man page was generated by the Netpbm tool 'makeman' from HTML source. .\" Do not hand-hack it! If you have bug fixes or improvements, please find .\" the corresponding HTML page on the Netpbm website, generate a patch .\" against that, and send it to the Netpbm maintainer. .TH "Libnetpbm Image Processing Manual" 3 "" "netpbm documentation" .SH NAME libnetpbm_ug - netpbm sample code .SH DESCRIPTION .PP The Libnetpbm programming library is part of .BR "Netpbm" (1)\c \&. .SH Contents .IP \(bu .UR #example Example .UE \& .IP \(bu .UR #classes \fBlibnetpbm\fP Classes .UE \& .IP \(bu .UR #initialization Library Initialization classes .UE \& .IP \(bu .UR #pamstruct The \fBpam\fP Structure .UE \& .IP \(bu .UR #plainvsraw Plain versus Raw Format .UE \& .IP \(bu .UR #reference Reference .UE \& .UN example .SH Example .PP Here is an example of a C program that uses \fBlibnetpbm\fP to read a Netpbm image input and produce a Netpbm image output. .nf /* 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 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); .fi .UN classes .SH \fBlibnetpbm\fP Classes .PP In this section, Guide To Using Libnetpbm, we cover only the PAM functions in \fBlibnetpbm\fP. As described in .BR "the introduction to \fBlibnetpbm\fP" (1)\c \&, 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: .BR "PBM Function Manual" (1)\c \&, .BR "PGM Function Manual" (1)\c \&, .BR "PPM Function Manual" (1)\c \&, and .BR "PNM Function Manual" (1)\c \&. .PP In case you're wondering, what makes the PAM functions easier to use is: .IP \(bu Each function handles all the formats. It does so without converting to a common format, so your program can treat the different formats differently if it wants. However, the interface makes it easy for your program to ignore the differences between the formats if that's what you want. .IP \(bu The PAM function parameter lists convey most information about the image with which you're working with a single \fBpam\fP structure, which you can build once and use over and over, whereas the older functions require you to pass up to 5 pieces of image information (height, width, etc.) as separate arguments to every function. .UN initialization .SH Library Initialization .PP Every program that uses the library must initialize the library, i.e. set up the process to use the library, as described in .UR libpm.html#initialization Initialization .UE \&. That is the purpose of the call to \fBpm_init()\fP in the example above. .UN pamstruct .SH The \fBpam\fP Structure .PP The PAM functions take most of their arguments in the form of a single \fBpam\fP 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 \fBpnm_readpaminit()\fP or \fBpnm_writepaminit()\fP to set the members in the \fBpam\fP structure before calling any other pam functions, and then just to pass the structure unchanged in all future calls to pam functions. .PP It depends upon the function to which you pass the structure what members are inputs, what members are outputs, and what members are irrelevant. .PP It is possible for a \fBpam\fP structure \fInot to specify\fP some members, by operation of its \fBlen\fP member. When you supply a \fBpam\fP 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 \fBmaxval\fP, and some require more. .PP Likewise, a function the returns a \fBpam\fP structure can return only a subset of the members defined here, according to its setting of the \fBlen\fP member. But this normally happens only because the library is old and predates the existence of the omitted members. .PP The members are: .TP \fBsize\fP The storage size in bytes of this entire structure. .TP \fBlen\fP 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 \fBsize\fP. \fBsize\fP and \fBlen\fP can be used to make programs compatible with newer and older versions of the Netpbm libraries. .TP \fBfile\fP The file. .TP \fBformat\fP The format code of the image, which tells which of the various Netpbm image formats is being processed. The following macros stand for those format codes: .TP \fBPAM_FORMAT\fP PAM .TP \fBRPBM_FORMAT\fP raw PBM format .TP \fBRPGM_FORMAT\fP raw PGM format .TP \fBRPPM_FORMAT\fP raw PPM format .TP \fBPBM_FORMAT\fP plain PBM format .TP \fBPGM_FORMAT\fP plain PGM format .TP \fBPPM_FORMAT\fP plain PPM format .sp 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, \fBlibnetpbm\fP ignores the plainness part and instead takes the plainness from the \fBplainformat\fP member. So \fBPBM_FORMAT\fP and \fBRPBM_FORMAT\fP are identical when writing. .sp This quirk exists for historical purposes; it's necessary for consistency with the older functions such as \fBpnm_writepnmrow()\fP whose \fIformat\fP and \fIforceplain\fP arguments are analogous. .sp Before Netpbm 10.32 (February 2006), \fBlibnetpbm\fP 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. .sp A common way to use this member is to copy it and the \fBplainformat\fP 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. .TP \fBplainformat\fP This is a boolean value (0 = false, 1 = true), meaningful only when writing an image file. It means to write in the plain (text) version of the format indicated by \fBformat\fP as opposed to the raw (binary) version. Note that the format code in \fBformat\fP would appear to completely specify the format, making \fBplainformat\fP redundant. But see the description of \fBformat\fP for why that isn't true. .sp Until Netpbm 10.32 (February 2006), this was defined a little differently. The \fBformat\fP member did in fact completely identify the format and \fBplainformat\fP was redundant and existed as a separate member only for computational speed. But this was inconsistent with the older \fBlibnetpbm\fP interface (e.g. \fBpnm_writepnm()\fP, and it made it difficult to write backward compatible programs. Before Netpbm 10.32, it affected reading as well as writing. .sp \fBlibnetpbm\fP image reading functions set this member to false, for your convenience in building an output image pam from an input image pam. .TP \fBheight\fP The height of the image in rows. .TP \fBwidth\fP The width of the image in number of columns (tuples per row). .TP \fBdepth\fP The depth of the image (degree of or number of samples in each tuple). .TP \fBmaxval\fP The maxval of the image. See definitions in .BR "pam" (1)\c \&. .TP \fBbytes_per_sample\fP The number of bytes used to represent each sample in the image file. See the format definition in .BR "pam" (1)\c \&. This is entirely redundant with \fBmaxval\fP. It exists as a separate member for computational speed. .TP \fBtuple_type\fP The tuple type of the image. See definitions in .BR "pam" (1)\c \&. Netpbm defines values for the most common types of visual images, but any value is legal. There are macros for these values: .TP \fBPAM_PBM_TUPLETYPE\fP black and white image, such as would alternatively be represented by a PBM image. .TP \fBPAM_PGM_TUPLETYPE\fP grayscale image, such as would alternatively be represented by a PGM image. .TP \fBPAM_PPM_TUPLETYPE\fP color image, such as would alternatively be represented by a PPM image. .TP \fBPAM_PBM_ALPHA_TUPLETYPE\fP black and white with a transparency (alpha) information. .TP \fBPAM_PGM_ALPHA_TUPLETYPE\fP grayscale with a transparency (alpha) information. .TP \fBPAM_PPM_ALPHA_TUPLETYPE\fP color with a transparency (alpha) information. .TP \fBallocation_depth\fP The number of samples for which memory is allocated for any tuple associated with this PAM structure. This must be at least as great as \&'depth'. Only the first 'depth' of the samples of a tuple are meaningful. .sp 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. .sp 0 means the allocation depth is the same as the image depth. .TP \fBcomment_p\fP Pointer to a pointer to a NUL-terminated ASCII string of comments. When reading an image, this contains the comments from the image's PAM header; when writing, the image gets these as comments, right after the magic number line. The individual comments are delimited by newlines and are in the same order as in the PAM header. The "#" at the beginning of a PAM header line that indicates the line is a comment is not part of the comment. .sp On output, NULL means no comments. .sp 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. .sp Examples: .nf \f(CW const char * comments; ... pam.comment_p = &comments; pnm_readpaminit(fileP, &pam, PAM_STRUCT_SIZE(comment_p)); printf("The comments are:\en"); printf("%s", comments) free(comments); \fP .fi .nf \f(CW const char * comments; ... comments = strdup("This is a comment 1\enThis is comment 2\en"); pam.comment_p = &comments; pnm_writepaminit(&pam); free(comments); \fP .fi .sp 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. .sp 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 \fBPAM_HAVE_COMMENT_P\fP is defined in \fBpam.h\fP where the member exists. .UN plainvsraw .SH Plain Versus Raw Format .PP 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. .PP 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. .PP The pnm functions choose between plain and raw based on the \fIforceplain\fP 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. .PP We are trying to stamp out the older plain formats, so it would be a wise choice not to write a program that sets \fIforceplain\fP true under any circumstance. A user who needs a plain format can use the \fBpnmtoplainpnm\fP program to convert the output of your program to plain format. .UN reference .SH Reference .PP The .BR "Libnetpbm Netpbm Image Processing Manual" (1)\c \& describes the the \fBlibnetpbm\fP functions for processing image data. .PP The .BR "Libnetpbm Utility Manual" (1)\c \& describes the functions that are not specifically related to the Netpbm image formats. .SH DOCUMENT SOURCE This manual page was generated by the Netpbm tool 'makeman' from HTML source. The master documentation is at .IP .B http://netpbm.sourceforge.net/doc/libnetpbm_ug.html .PP