\ .\" 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 "Libnetbpm Utility Functions" 3 "27 August 2006" "netpbm documentation" .PP .PP .UR #toc Table Of Contents .UE \& .PP These library functions are part of .BR Netpbm (1) . .PP This page documents functions in the Netpbm subroutine library that are not directly related to image data. .PP For introductory and general information using \fBlibnetpbm\fP, see .BR Libnetpbm User's Guide (3) . .PP The most commonly used \fBlibnetpbm\fP functions are those that read and write and process Netpbm images. Those are documented in .BR Libnetpbm Netpbm Image Processing Manual (3) .PP To use these services, #include \fBpam.h\fP. .UN functions .SH Functions .UN initialization .SS Initialization .B Overview .PP \fBvoid pm_init(\fP \fBconst char * \fP\fIprogname\fP\fB,\fP \fBunsigned int \fP\fIflags\fP \fB);\fP .PP \fBvoid pm_proginit(\fP \fBint * \fP\fIargcP\fP\fB,\fP \fBchar * \fP\fIargv\fP\fB[]\fP \fB);\fP .B Description .PP All Netpbm programs must call \fBpm_proginit()\fP just after startup, before they process their arguments. \fBpm_proginit()\fP, among other things, processes Netpbm universal arguments and removes them from the argument list. .PP A program that isn't a Netpbm program, but just uses \fBlibnetpbm\fP services, need not invoke \fBpm_proginit\fP. But such a program must invoke \fBpm_init()\fP. .PP By 'Netpbm program,' we mean a program that is part of the Netpbm package or is intended to act like one. \fBpm_proginit()\fP does things that all Netpbm programs do by convention. For example, it scans the argument list for .UR index.html#commonoptions common options .UE \&, handles them, and removes them from the argument list. Ergo, if you want your program to have the same common options as those in the Netpbm package, you might use \fBpm_proginit()\fP, and if you don't, you must not. .PP \fBpm_proginit()\fP is primarily intended for Netpbm developers, so you should not expect it to have stable function across releases, and you must go to the comments in the source code to see exactly what it does. .PP Any program that uses \fBlibnetpbm\fP but does not call \fBpm_proginit\fP (i.e. is not a Netpbm program) must call \fBpm_init()\fP. The conventional place to do this is at the very beginning of the program. This sets up some program-global variables for use by the \fBlibnetpbm\fP functions. .PP The \fIprogname\fP argument is the program name for \fBlibnetpbm\fP functions to use in messages they issue. Normally, you would use argv[0] for this. .PP \fIflags\fP is meaningless, but for forward compatibility, you must set it to zero. .PP \fBpm_init()\fP and \fBpm_proginit()\fP have been around at least since Netpbm 9.25 (March 2002). Another function named \fBpm_init()\fP exists in older Netpbm, but was for internal use. Netpbm programs of that era use \fBpbm_init()\fP, etc to do what \fBpm_proginit()\fP does today. Today, \fBpbm_init()\fP, etc. exist for backward compatibility and are identical the \fBpm_proginit()\fP. .UN file .SS File Or Image Stream Access .B Overview .PP \fBFILE *pm_openr(\fP \fBchar *\fP \fIname\fP \fB);\fP .PP \fBFILE *pm_openw(\fP \fBchar *\fP \fIname\fP \fB);\fP .PP \fBFILE *pm_openr_seekable(\fP \fBconst char *\fP \fIname\fP \fB);\fP .PP \fBFILE *pm_close(\fP \fBFILE *\fP \fIfp\fP \fB);\fP .PP \fBvoid pm_tell2(\fP \fBFILE * \fP \fIfileP\fP\fB,\fP \fBpm_filepos *\fP \fIfileposP\fP\fB,\fP \fBunsigned int\fP \fIfileposSize\fP \fB);\fP .PP \fBunsigned int pm_tell(\fP \fBFILE *\fP \fIfileP\fP \fB);\fP .PP \fBvoid pm_seek2(\fP \fBFILE * \fP \fIfileP\fP\fB,\fP \fBconst pm_filepos *\fP \fIfileposP\fP\fB,\fP \fBunsigned int\fP \fIfileposSize\fP \fB);\fP .PP \fBvoid pm_seek(\fP \fBFILE * \fP \fIfileP\fP\fB,\fP \fBunsigned long\fP \fIfilepos\fP \fB);\fP .PP \fBchar *pm_read_unknown_size(\fP \fBFILE *\fP \fIfp\fP\fB,\fP \fBlong *\fP \fInread\fP \fB);\fP .B Description .PP An image stream is just a file stream (represented in the standard C library as type \fBFILE *\fP). .PP These routines work on files > 2 GiB if the underlying system does, using the standard large file interface. Before Netpbm 10.15 (April 2003), though, they would fail to open any file that large or process any offset in a file that could not be represented in 32 bits. .PP \fBpm_openr()\fP opens the given file for reading, with appropriate error checking. A filename of \fB-\fP is taken to mean Standard Input. \fBpm_openw()\fP opens the given file for writing, with appropriate error checking. \fBpm_close()\fP closes the file descriptor, with appropriate error checking. .PP \fBpm_openr_seekable()\fP appears to open the file just like \fBpm_openr()\fP, but the file thus opened is guaranteed to be seekable (you can use ftell() and fseek() on it). \fBpm_openr_seekable()\fP pulls this off by copying the entire file to a temporary file and giving you the handle of the temporary file, if it has to. If the file you name is a regular file, it's already seekable so \fBpm_openr_seekable()\fP just does the same thing as \fBpm_openr()\fP. But if it is, say, a pipe, it isn't seekable. So \fBpm_openr_seekable()\fP reads the pipe until EOF into a temporary file, then opens that temporary file and returns the handle of the temporary file. The temporary file is seekable. .PP The file \fBpm_openr_seekable()\fP creates is one that the operating system recognizes as temporary, so when you close the file, by any means, it gets deleted. .PP You need a seekable file if you intend to make multiple passes through the file. The only alternative is to read the entire image into memory and work from that copy. That may use too much memory. Note that the image takes less space in the file cache than in a buffer in memory. As much as 96 times less space! Each sample is an integer in the buffer, which is usually 96 bits. In the file, a sample may be as small as 1 bit and rarely more than 8 bits. .PP \fBpm_tell2()\fP returns a handle for the current position of the image stream (file), whether it be the header or a row of the raster. Use the handle as an argument to \fBpm_seek2()\fP to reposition the file there later. The file must be seekable (which you can ensure by opening it with \fBpm_openr_seekable()\fP) or this may fail. .PP The file position handle is of type \fBpm_filepos\fP, which is intended to be opaque, i.e. used only with these two functions. In practice, it is a file offset and is 32 bits or 64 bits depending upon the capability of the underlying system. For maximum backward and forward compatibility, the functions that take or return a \fBpm_filepos\fP have a \fIfileposSize\fP argument for the size of the data structure. In C, simply code \fBsizeof(pm_filepos)\fP for that. .PP \fBpm_seek()\fP and \fBpm_tell\fP are for backward compatibility only. Do not use them in new code. These functions are not capable of handle positions in files whose byte offset cannot be represented in 32 bits. .PP \fBpm_tell2()\fP and \fBpm_seek2()\fP replaced \fBpm_tell()\fP and \fBpm_seek()\fP in Netpbm 10.15 (April 2003). .PP \fBpm_read_unknown_size()\fP reads an entire file or input stream of unknown size to a buffer. It allocates more memory as needed. The calling routine has to free the allocated buffer with \fBfree()\fP. .PP \fBpm_read_unknown_size()\fP returns a pointer to the allocated buffer. The \fBnread\fP argument returns the number of bytes read. .UN endian .SS Endian I/O .B Entry Points .PP \fBvoid pm_readchar(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBchar *\fP \fIsP\fP \fB);\fP .PP \fBvoid pm_writechar(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBchar\fP \fIs\fP \fB);\fP .PP \fBint pm_readbigshort(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBshort *\fP \fIsP\fP \fB);\fP .PP \fBint pm_writebigshort(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBshort\fP \fIs\fP \fB);\fP .PP \fBint pm_readbiglong(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBlong *\fP \fIlP\fP \fB);\fP .PP \fBint pm_writebiglong(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBlong\fP \fIl\fP \fB);\fP .PP \fBint pm_readlittleshort(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBshort *\fP \fIsP\fP \fB);\fP .PP \fBint pm_writelittleshort(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBshort\fP \fIs\fP \fB);\fP .PP \fBint pm_readlittlelong(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBlong *\fP \fIlP\fP \fB);\fP .PP \fBint pm_writelittlelong(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBlong\fP \fIl\fP \fB);\fP .PP \fBvoid pm_readcharu(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBchar *\fP \fIsP\fP \fB);\fP .PP \fBvoid pm_writecharu(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBchar\fP \fIs\fP \fB);\fP .PP \fBint pm_readbigshortu(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBshort *\fP \fIsP\fP \fB);\fP .PP \fBint pm_writebigshortu(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBshort\fP \fIs\fP \fB);\fP .PP \fBint pm_readbiglongu(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBlong *\fP \fIlP\fP \fB);\fP .PP \fBint pm_writebiglongu(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBlong\fP \fIl\fP \fB);\fP .PP \fBint pm_readlittleshortu(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBshort *\fP \fIsP\fP \fB);\fP .PP \fBint pm_writelittleshortu(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBshort\fP \fIs\fP \fB);\fP .PP \fBint pm_readlittlelongu(\fP \fBFILE *\fP \fIin\fP\fB,\fP \fBlong *\fP \fIlP\fP \fB);\fP .PP \fBint pm_writelittlelongu(\fP \fBFILE *\fP \fIout\fP\fB,\fP \fBlong\fP \fIl\fP \fB);\fP .B Description \fBpm_readchar()\fP, \fBpm_writechar()\fP, \fBpm_readbigshort()\fP, \fBpm_writebigshort()\fP, \fBpm_readbiglong()\fP, \fBpm_writebiglong()\fP, \fBpm_readlittleshort()\fP, \fBpm_writelittleshort()\fP, \fBpm_readlittlelong()\fP, and \fBpm_writelittlelong()\fP are routines to read and write 1-byte, 2-byte, and 4-byte pure binary integers in either big- or little-endian byte order. Note that a 'long int' C type might be wider than 4 bytes, but the 'long' routines still read and write 4 bytes. .PP \fBpm_readbiglongu()\fP, etc. (names ending in \fBu\fP) are the same except they work on unsigned versions of the type. .PP The routines with declared return values always return 0. Before Netpbm 10.27 (March 2005), they returned -1 on failure, including EOF. Now, they issue an error message to Standard Error and abort the program if the I/O fails or encounters EOF. .PP The 1-byte routines were new in Netpbm 10.27 (March 2005). The unsigned versions were new somewhere around Netpbm 10.21 (2004). .UN maxval .SS Maxval Arithmetic .B Entry Points .PP \fBint pm_maxvaltobits(\fP \fBint\fP \fImaxval\fP \fB);\fP .PP \fBint pm_bitstomaxval(\fP \fBint\fP \fIbits\fP \fB);\fP .PP \fBunsigned int pm_lcm(\fP \fBunsigned int\fP \fIx\fP\fB,\fP \fBunsigned int\fP \fIy\fP\fB,\fP \fBunsigned int\fP \fIz\fP\fB,\fP \fBunsigned int\fP \fIlimit\fP \fB);\fP .B Description .PP \fBpm_maxvaltobits()\fP and \fBpm_bitstomaxval()\fP convert between a maxval and the minimum number of bits required to hold it. .PP \fBpm_lcm()\fP computes the least common multiple of 3 integers. You also specify a limit and if the LCM would be higher than that limit, \fBpm_lcm()\fP just returns that limit. .UN gamma .SS Gamma Arithmetic .B Entry Points .PP \fBfloat pm_gamma709(\fP \fBfloat\fP \fIintensity\fP \fB);\fP .PP \fBfloat pm_ungamma709(\fP \fBfloat\fP \fIbrightness\fP \fB);\fP .B Description .PP In graphics processing, there are two common ways of representing numerically the intensity of a pixel, or a component of a pixel. .PP The obvious way is with a number that is directly proportional to the light intensity (e.g. 10 means twice as many milliwatts per square centimeter as 5). There are two problems with this: .IP \(bu To the human eye, a 1 milliwatt per square centimeter difference in a bright image is much less apparent than a 1 milliwatt per square centimeter difference in a dark image. So if you have a fixed number of bits in which to store the intensity value, you're wasting resolution at the bright end and skimping on it at the dark end. .IP \(bu Monitor inputs and camera outputs aren't directly proportional to the light intensity they project or detect. .PP For these reasons, light intensities are often represented in graphics processing by an exponential scale. The transfer function is called a gamma function and the resulting numbers are called gamma-corrected or gamma-adjusted. There are various gamma functions. The Netpbm formats specify that intensities are represented by gamma-adjusted numbers of a particular gamma transfer function. .PP These functions let you convert back and forth between these two scales, using the same gamma transfer function that is specified in the Netpbm format specifications. .PP \fBpm_gamma709\fP converts from an intensity-proportional intensity value to a gamma-adjusted intensity value (roughly proportional to brightness, which is the human subjective perception of intensity), using the ITU-R Recommendation BT.709 gamma transfer function. .PP \fBpm_ungamma709\fP is the inverse of \fBpm_gamma709\fP. .UN message .SS Messages .B Overview .PP \fBvoid pm_message(\fP \fBchar *\fP \fIfmt\fP\fB,\fP \fB... );\fP .PP \fBvoid pm_setusermessagefn(pm_usermessagefn *\fP \fIfunction\fP\fB);\fP .B Description .PP \fBpm_message()\fP is a \fBprintf()\fP style routine to write an informational message to the Standard Error file stream. \fBpm_message()\fP suppresses the message, however, if the user specified the \fB-quiet\fP .UR index.html#commonoptions common option .UE \& on the command line. Note that Netpbm programs are often used interactively, but also often used by programs. In the interactive case, it is nice to issue messages about what the program is doing, but in the program case, such messages are usually undesirable. By using \fBpm_message()\fP for all your messages, you make your program usable in both cases. Without any effort on your part, program users of your program can avoid the messages by specifying the \fB-quiet\fP option. .PP Netpbm distinguishes between error messages and information messages; \fBpm_message()\fP is just for informational messages. To issue an error message, see .UR liberror.html#pm_errormsg \fBpm_errormsg()\fP .UE \&. .PP \fBpm_setusermessagefn\fP registers a handler for informational messages, called a user message routine. Any library function (including \fBpm_message()\fP) that wants to issue an informational message in the future will call that function with the message as an argument instead of writing the message to Standard Error. .PP The argument the user message routine gets is English text designed for human reading. It is just the text of the message; there is no attempt at formatting in it (so you won't see any newline or tab characters). .PP To capture error messages in addition to informational messages, see .UR liberror.html#pm_setusererrormsgfn \fBpm_setusererrormsgfn()\fP .UE \&. .PP You can remove the user message routine, so that the library issues future informational messages in its default way (write to Standard Error) by specifying a null pointer for \fIfunction\fP. Example: .nf \f(CW static pm_usermessagefn logfilewrite; static void logfilewrite(const char * const msg) { fprintf(mymsglog, 'Netpbm message: %s', msg); } pm_setusermessagefn(&logfilewrite); pm_message('Message for the message log'); \fP .fi .UN system .SS System Utilities .IP \(bu .BR pm_system (3) .IP \(bu .BR pm_tmpfile (3) .UN keyword .SS Keyword Matching .B Entry Points .PP \fBvoid pm_keymatch();\fP .B Description .PP This subroutine is obsolete. It used to be used for command line option processing. Today, you can do better option processing more easily with the shhopt facility. See any recent program in the Netpbm package for an example. \fBpm_keymatch()\fP does a case-insensitive match of \fBstr\fP against \fBkeyword\fP. \fBstr\fP can be a leading substring of \fBkeyword\fP, but at least \fBminchars\fP must be present.