This page documents functions in the Netpbm subroutine library that are not directly related to image data.  

SYNOPSIS

#include

int pm_maxvaltobits( int maxval );

int pm_bitstomaxval( int bits );

unsigned int pm_lcm( unsigned int x, unsigned int y, unsigned int z, unsigned int limit );

void pm_message( char * fmt, ... );

void pm_error( char * fmt, ... );

FILE *pm_openr( char * name)

FILE *pm_openw( char * name );

FILE *pm_openr_seekable( const char * name );

FILE *pm_close( FILE * fp );

char *pm_read_unknown_size( FILE * fp, long * nread );

void pm_tell2( FILE * fileP, pm_filepos * fileposP, unsigned int fileposSize );

unsigned int pm_tell( FILE * fileP );

void pm_seek2( FILE * fileP, const pm_filepos * fileposP, unsigned int fileposSize );

void pm_seek( FILE * fileP, unsigned long filepos );

int pm_readbigshort( FILE * in, short * sP );

int pm_writebigshort( FILE * out, short s );

int pm_readbiglong( FILE * in, long * lP );

int pm_writebiglong( FILE * out, long l );

int pm_readlittleshort( FILE * in, short * sP );

int pm_writelittleshort( FILE * out, short s );

int pm_readlittlelong( FILE * in, long * lP );

int pm_writelittlelong( FILE * out, long l );

pm_system(...) (see pm_system)

DESCRIPTION

 

KEYWORD MATCHING

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. pm_keymatch() does a case-insensitive match of str against keyword. str can be a leading substring of keyword, but at least minchars must be present.  

MAXVAL ARITHMETIC

pm_maxvaltobits() and pm_bitstomaxval() convert between a maxval and the minimum number of bits required to hold it.

pm_lcm() computes the least common multiple of 3 integers. You also specify a limit and if the LCM would be higher than that limit, pm_lcm() just returns that limit.

 

MESSAGES AND ERRORS

pm_message() is a printf() style routine to write an informational message to the Standard Error file stream. pm_message() suppresses the message, however, if the user specified the -quiet option on the command line. See the initialization functions, e.g. pbm_init(), for information on the -quiet 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 pm_message() 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 -quiet option.

pm_error() is a printf() style routine that writes an error message to the Standard Error file stream and exits the program with an exit code of 1.  

FILE OR IMAGE STREAM ACCESS

An image stream is just a file stream (represented in the standard C library as type FILE *).

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.

OPENING, CLOSING

pm_openr() opens the given file for reading, with appropriate error checking. A filename of - is taken to mean Standard Input. pm_openw() opens the given file for writing, with appropriate error checking. pm_close() closes the file descriptor, with appropriate error checking.

pm_openr_seekable() appears to open the file just like pm_openr(), but the file thus opened is guaranteed to be seekable (you can use ftell() and fseek() on it). pm_openr_seekable() 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 pm_openr_seekable() just does the same thing as pm_openr(). But if it is, say, a pipe, it isn't seekable. So pm_openr_seekable() 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.

The file pm_openr_seekable() creates is one that the operating system recognizes as temporary, so when you close the file, by any means, it gets deleted.

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.

IMAGE STREAM POSITIONING

pm_tell2() 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 pm_seek2() to reposition the file there later. The file must be seekable (which you can ensure by opening it with pm_openr_seekable()) or this may fail.

The file position handle is of type pm_filepos, 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 pm_filepos have a fileposSize argument for the size of the data structure. In C, simply code sizeof(pm_filepos) for that.

pm_seek() and pm_tell 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.

pm_tell2() and pm_seek2() replaced pm_tell() and pm_seek() in Netpbm 10.15 (April 2003).

READING A WHOLE FILE

pm_read_unknown_size() 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 free().

pm_read_unknown_size() returns a pointer to the allocated buffer. The nread argument returns the number of bytes read.  

ENDIAN I/O

  pm_readbigshort()

pm_writebigshort(), pm_readbiglong(), pm_writebiglong(), pm_readlittleshort(), pm_writelittleshort(), pm_readlittlelong(), and pm_writelittlelong() are routines to read and write short and long ints in either big- or little-endian byte order. The return value is 0 upon success and -1 upon failure (either EOF or I/O error).