.TL Libnetbpm Utility Functions .SH 1 Libnetpbm Utility Functions .LP Updated: 27 August 2006 .br .LP Table Of Contents .SH 2 NAME .LP libpm - netpbm utility functions .SH 2 DESCRIPTION .LP .LP These library functions are part of Netpbm. .LP This page documents functions in the Netpbm subroutine library that are not directly related to image data. .LP For introductory and general information using \fBlibnetpbm\fR, see Libnetpbm User's Guide. .LP The most commonly used \fBlibnetpbm\fR functions are those that read and write and process Netpbm images. Those are documented in Libnetpbm Netpbm Image Processing Manual .LP To use these services, #include \fBpam.h\fR. .SH 2 Functions .LP .SH 3 Initialization .LP .SH 4 Overview .LP .LP \fBvoid pm_init(\fR \fBint *\fR\fIargcP\fR\fB,\fR \fBchar *\fR\fIargv\fR\fB[]\fR \fB);\fR .SH 4 Description .LP .LP All Netpbm programs must call \fBpm_init()\fR just after startup, before they process their arguments. \fBpm_init()\fR, among other things, processes Netpbm universal arguments and removes them from the argument list. .LP A program that isn't a Netpbm program, but just uses \fBlibnetpbm\fR services, need not invoke \fBpm_init\fR. .SH 3 File Or Image Stream Access .LP .SH 4 Overview .LP .LP \fBFILE *pm_openr(\fR \fBchar *\fR \fIname\fR\fB)\fR .LP \fBFILE *pm_openw(\fR \fBchar *\fR \fIname\fR \fB);\fR .LP \fBFILE *pm_openr_seekable(\fR \fBconst char *\fR \fIname\fR \fB);\fR .LP \fBFILE *pm_close(\fR \fBFILE *\fR \fIfp\fR \fB);\fR .LP \fBvoid pm_tell2(\fR \fBFILE * \fR \fIfileP\fR\fB,\fR \fBpm_filepos *\fR \fIfileposP\fR\fB,\fR \fBunsigned int\fR \fIfileposSize\fR \fB);\fR .LP \fBunsigned int pm_tell(\fR \fBFILE *\fR \fIfileP\fR \fB);\fR .LP \fBvoid pm_seek2(\fR \fBFILE * \fR \fIfileP\fR\fB,\fR \fBconst pm_filepos *\fR \fIfileposP\fR\fB,\fR \fBunsigned int\fR \fIfileposSize\fR \fB);\fR .LP \fBvoid pm_seek(\fR \fBFILE * \fR \fIfileP\fR\fB,\fR \fBunsigned long\fR \fIfilepos\fR \fB);\fR .LP \fBchar *pm_read_unknown_size(\fR \fBFILE *\fR \fIfp\fR\fB,\fR \fBlong *\fR \fInread\fR \fB);\fR .SH 4 Description .LP .LP An image stream is just a file stream (represented in the standard C library as type \fBFILE *\fR). .LP 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. .LP \fBpm_openr()\fR opens the given file for reading, with appropriate error checking. A filename of \fB-\fR is taken to mean Standard Input. \fBpm_openw()\fR opens the given file for writing, with appropriate error checking. \fBpm_close()\fR closes the file descriptor, with appropriate error checking. .LP \fBpm_openr_seekable()\fR appears to open the file just like \fBpm_openr()\fR, but the file thus opened is guaranteed to be seekable (you can use ftell() and fseek() on it). \fBpm_openr_seekable()\fR 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()\fR just does the same thing as \fBpm_openr()\fR. But if it is, say, a pipe, it isn't seekable. So \fBpm_openr_seekable()\fR 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. .LP The file \fBpm_openr_seekable()\fR creates is one that the operating system recognizes as temporary, so when you close the file, by any means, it gets deleted. .LP 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. .LP \fBpm_tell2()\fR 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()\fR to reposition the file there later. The file must be seekable (which you can ensure by opening it with \fBpm_openr_seekable()\fR) or this may fail. .LP The file position handle is of type \fBpm_filepos\fR, 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\fR have a \fIfileposSize\fR argument for the size of the data structure. In C, simply code \fBsizeof(pm_filepos)\fR for that. .LP \fBpm_seek()\fR and \fBpm_tell\fR 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. .LP \fBpm_tell2()\fR and \fBpm_seek2()\fR replaced \fBpm_tell()\fR and \fBpm_seek()\fR in Netpbm 10.15 (April 2003). .LP \fBpm_read_unknown_size()\fR 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()\fR. .LP \fBpm_read_unknown_size()\fR returns a pointer to the allocated buffer. The \fBnread\fR argument returns the number of bytes read. .SH 3 Endian I/O .LP .SH 4 Entry Points .LP .LP \fBvoid pm_readchar(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBchar *\fR \fIsP\fR \fB);\fR .LP \fBvoid pm_writechar(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBchar\fR \fIs\fR \fB);\fR .LP \fBint pm_readbigshort(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBshort *\fR \fIsP\fR \fB);\fR .LP \fBint pm_writebigshort(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBshort\fR \fIs\fR \fB);\fR .LP \fBint pm_readbiglong(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBlong *\fR \fIlP\fR \fB);\fR .LP \fBint pm_writebiglong(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBlong\fR \fIl\fR \fB);\fR .LP \fBint pm_readlittleshort(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBshort *\fR \fIsP\fR \fB);\fR .LP \fBint pm_writelittleshort(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBshort\fR \fIs\fR \fB);\fR .LP \fBint pm_readlittlelong(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBlong *\fR \fIlP\fR \fB);\fR .LP \fBint pm_writelittlelong(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBlong\fR \fIl\fR \fB);\fR .LP \fBvoid pm_readcharu(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBchar *\fR \fIsP\fR \fB);\fR .LP \fBvoid pm_writecharu(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBchar\fR \fIs\fR \fB);\fR .LP \fBint pm_readbigshortu(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBshort *\fR \fIsP\fR \fB);\fR .LP \fBint pm_writebigshortu(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBshort\fR \fIs\fR \fB);\fR .LP \fBint pm_readbiglongu(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBlong *\fR \fIlP\fR \fB);\fR .LP \fBint pm_writebiglongu(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBlong\fR \fIl\fR \fB);\fR .LP \fBint pm_readlittleshortu(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBshort *\fR \fIsP\fR \fB);\fR .LP \fBint pm_writelittleshortu(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBshort\fR \fIs\fR \fB);\fR .LP \fBint pm_readlittlelongu(\fR \fBFILE *\fR \fIin\fR\fB,\fR \fBlong *\fR \fIlP\fR \fB);\fR .LP \fBint pm_writelittlelongu(\fR \fBFILE *\fR \fIout\fR\fB,\fR \fBlong\fR \fIl\fR \fB);\fR .SH 4 Description .LP \fBpm_readchar()\fR, \fBpm_writechar()\fR, \fBpm_readbigshort()\fR, \fBpm_writebigshort()\fR, \fBpm_readbiglong()\fR, \fBpm_writebiglong()\fR, \fBpm_readlittleshort()\fR, \fBpm_writelittleshort()\fR, \fBpm_readlittlelong()\fR, and \fBpm_writelittlelong()\fR 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. .LP \fBpm_readbiglongu()\fR, etc. (names ending in \fBu\fR) are the same except they work on unsigned versions of the type. .LP 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. .LP The 1-byte routines were new in Netpbm 10.27 (March 2005). The unsigned versions were new somewhere around Netpbm 10.21 (2004). .SH 3 Maxval Arithmetic .LP .SH 4 Entry Points .LP .LP \fBint pm_maxvaltobits(\fR \fBint\fR \fImaxval\fR \fB);\fR .LP \fBint pm_bitstomaxval(\fR \fBint\fR \fIbits\fR \fB);\fR .LP \fBunsigned int pm_lcm(\fR \fBunsigned int\fR \fIx\fR\fB,\fR \fBunsigned int\fR \fIy\fR\fB,\fR \fBunsigned int\fR \fIz\fR\fB,\fR \fBunsigned int\fR \fIlimit\fR \fB);\fR .SH 4 Description .LP .LP \fBpm_maxvaltobits()\fR and \fBpm_bitstomaxval()\fR convert between a maxval and the minimum number of bits required to hold it. .LP \fBpm_lcm()\fR 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()\fR just returns that limit. .SH 3 Gamma Arithmetic .LP .SH 4 Entry Points .LP .LP \fBfloat pm_gamma(\fR \fBfloat\fR \fIintensity\fR \fB);\fR .LP \fBfloat pm_ungamma(\fR \fBfloat\fR \fIbrightness\fR \fB);\fR .SH 4 Description .LP .LP In graphics processing, there are two common ways of representing numerically the intensity of a pixel, or a component of a pixel. .LP 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. .LP .LP 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. .LP 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. .LP \fBpm_gamma709\fR 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. .LP \fBpm_ungamma709\fR is the inverse of \fBpm_gamma709\fR. .SH 3 Messages .LP .SH 4 Overview .LP .LP \fBvoid pm_message(\fR \fBchar *\fR \fIfmt\fR\fB,\fR \fB... );\fR .LP \fBvoid pm_setusermessagefn(pm_usermessagefn *\fR \fIfunction\fR\fB);\fR .SH 4 Description .LP .LP \fBpm_message()\fR is a \fBprintf()\fR style routine to write an informational message to the Standard Error file stream. \fBpm_message()\fR suppresses the message, however, if the user specified the \fB-quiet\fR option on the command line. See the initialization functions, e.g. \fBpnm_init()\fR, for information on the \fB-quiet\fR option. 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()\fR 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\fR option. .LP Netpbm distinguishes between error messages and information messages; \fBpm_message()\fR is just for informational messages. To issue an error message, see \fBpm_errormsg()\fR. .LP \fBpm_setusermessagefn\fR registers a handler for informational messages, called a user message routine. Any library function (including \fBpm_message()\fR) 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. .LP 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). .LP To capture error messages in addition to informational messages, see \fBpm_setusererrormsgfn()\fR. .LP 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\fR. Example: .DS L 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"); .DE .SH 3 System Utilities .LP .IP \(bu pm_system .IP \(bu pm_tmpfile .LP .SH 3 Keyword Matching .LP .SH 4 Entry Points .LP .LP \fBvoid pm_keymatch();\fR .SH 4 Description .LP .LP 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()\fR does a case-insensitive match of \fBstr\fR against \fBkeyword\fR. \fBstr\fR can be a leading substring of \fBkeyword\fR, but at least \fBminchars\fR must be present. .br \l'5i' .SH 2 Table Of Contents .LP .IP \(bu Functions .IP \(bu Initialization .IP \(bu File Or Image Stream Access .IP \(bu Endian I/O .IP \(bu Maxval Arithmetic .IP \(bu Messages And Errors .IP \(bu Keyword .LP .LP