.TH libppm 3 .SH NAME libppm - functions to support portable pixmap (PPM) programs .SH SYNOPSIS .B #include .B void ppm_init( .BI "int *" argcP , .BI "char *" argv [] .B ); .B pixel ** ppm_allocarray( .BI "int " cols , .BI "int " rows " );" .B "pixel * ppm_allocrow(" .BI "int " cols " );" .B void ppm_freearray( .BI "pixel **" pixels , .BI "int " rows " );" .B void ppm_freerow( .BI "pixel *" pixelrow ");" .B void ppm_readppminit( .BI "FILE *" fp , .BI "int *" colsP , .BI "int *" rowsP , .BI "pixval *" maxvalP , .BI "int *" formatP " );" .B void ppm_readppmrow( .BI "FILE *" fp , .BI "pixel *" pixelrow , .BI "int " cols , .BI "pixval " maxval , .BI "int " format " );" .B pixel ** ppm_readppm( .BI "FILE *" fp , .BI "int *" colsP , .BI "int *" rowsP , .BI "pixvalP *" maxvalP " );" .B void ppm_writeppminit( .B "FILE * " fp , .BI "int " cols , .BI "int " rows , .BI "pixval " maxval , .BI "int " forceplain " );" .B void ppm_writeppmrow( .BI "FILE *" fp , .BI "pixel *" pixelrow , .BI "int "cols , .BI "pixval " maxval , .BI "int " forceplain " );" .B void ppm_writeppm( .BI "FILE *" fp , .BI "pixel ** " pixels , .BI "int " cols , .BI "int " rows , .BI "pixval " maxval , .BI "int " forceplain " );" .B void ppm_writeppm( .BI "FILE *" fp , .BI "pixel **" pixels , .BI "int " cols , .BI "int " rows , .BI "pixval " maxval , .BI "int " forceplain " );" .B void ppm_nextimage( .BI "FILE *" file , .BI "int * const " eofP ); .B void ppm_check( .BI "FILE * " file , .BI "const enum pm_check_type " check_type , .BI "const int " format , .BI "const int " cols , .BI "const int " rows , .BI "const int " maxval , .br .BI "enum pm_check_code * const " retval ); .B typedef ... pixel; .B typedef ... pixval; .B #define PPM_MAXMAXVAL ... .B #define PPM_OVERALLMAXVAL ... .B #define PPM_FORMAT ... .B #define RPPM_FORMAT ... .B #define PPM_TYPE PPM_FORMAT .B #define .BI PPM_FORMAT_TYPE( format ) .B ... .B extern pixval ppm_pbmmaxval; .BI "pixval PPM_GETR( pixel " p ) .BI "pixval PPM_GETG( pixel " p ) .BI "pixval PPM_GETB( pixel " p ) .BI "void PPM_ASSIGN( pixel " p ", " .BI "pixval " red ", pixval " grn ", pixval " blu ) .BI "int PPM_EQUAL( pixel " p ", pixel " q ) .BI "void PPM_DEPTH( pixel " newp ", pixel " p ", " .BI "pixval " oldmaxval ", pixval " newmaxval ) .BI "float PPM_LUMIN( pixel " p ) .BI "float PPM_CHROM_R( pixel " p ) .BI "float PPM_CHROM_B( pixel " p ) .BI "pixel ppm_parsecolor( char *" colorname ", pixval " maxval ) .BI "char * ppm_colorname( pixel *" colorP ", pixval " maxval ", int " hexok ) .SH DESCRIPTION .SS TYPES AND CONSTANTS Each .BR pixel contains three .BR pixval s, each of which should contain only the values between .BR 0 and .BR PPM_MAXMAXVAL . .BR ppm_pbmmaxval is the maxval used when a PPM program reads a PBM file. Normally it is 1; however, for some programs, a larger value gives better results. .SS MANIPULATING PIXELS The macros .BR PPM_GETR , .BR PPM_GETG , and .B PPM_GETB retrieve the red, green, or blue sample, respectively, from the given pixel. The .B PPM_ASSIGN macro assigns the given values to the red, green, and blue samples of the given pixel. The .B PPM_EQUAL macro tests two pixels for equality. The .B PPM_DEPTH macro scales the colors of pixel .I p according the old and new maxvals and assigns the new values to .IR newp . It is intended to make writing ppmtowhatever easier. The .BR PPM_LUMIN , .BR PPM_CHROM_R , and .BR PPM_CHROM_B , macros determine the luminance, red chrominance, and blue chrominance, respectively, of the pixel .IR p . The scale of all these values is the same as the scale of the input samples (i.e. 0 to maxval for luminance, -maxval/2 to maxval/2 for chrominance). Note that the macros do it by floating point multiplication. If you are computing these values over an entire image, it may be significantly faster to do it with multiplication tables instead. Compute all the possible products once up front, then for each pixel, just look up the products in the tables. .SS INITIALIZATION All PPM programs must call .B ppm_init() just after invocation, before they process their arguments. .SS MEMORY MANAGEMENT .B ppm_allocarray() allocates an array of pixels. .B ppm_allocrow() allocates a row of the given number of pixels. .B ppm_freearray() frees the array allocated with .B ppm_allocarray() containing the given number of rows. .B ppm_freerow() frees a row of pixelss allocated with .BR ppm_allocrow() . .SS READING FILES If a function in this section is called on a PBM or PGM format file, it translates the PBM or PGM file into a PPM file on the fly and functions as if it were called on the equivalent PPM file. The .I format value returned by .B ppm_readppminit() is, however, not translated. It represents the actual format of the PBM or PGM file. .B ppm_readppminit() reads the header of a PPM file, returning all the information from the header and leaving the file positioned just after the header. .B ppm_readppmrow() reads a row of pixels into the .I pixelrow array. .IR format , .IR cols , and .I maxval are the values returned by .BR ppm_readppminit() . .B ppm_readppm() reads an entire PPM image into memory, returning the allocated array as its return value and returning the information from the header as .IR rows , .IR cols , and .IR maxval . This function combines .BR ppm_readppminit() , .BR ppm_allocarray() , and .BR ppm_readppmrow() . .SS WRITING FILES .B ppm_writeppminit() writes the header for a PPM file and leaves it positioned just after the header. .I forceplain is a logical value that tells .B ppm_writeppminit() to write a header for a plain PPM format file, as opposed to a raw PPM format file. .B ppm_writeppmrow() writes the row .I pixelrow to a PPM file. For meaningful results, .IR cols , .IR maxval , and .I forceplain must be the same as was used with .BR ppm_writeppminit() . .B ppm_writeppm() write the header and all data for a PPM image. This function combines .B ppm_writeppminit() and .BR ppm_writeppmrow() . .SS MISCELLANEOUS .B ppm_nextimage() positions a PPM input file to the next image in it (so that a subsequent .B ppm_readppminit() reads its header). .B ppm_nextimage() is analogous to .BR pbm_nextimage() , but works on PPM, PGM, and PBM files. .B ppm_check() checks for the common file integrity error where the file is the wrong size to contain all the image data. .B ppm_check() is analogous to .BR pbm_check() , but works on PPM, PGM, and PBM files. .SS COLOR NAMES .B ppm_parsecolor() Interprets a color specification and returns a pixel of the color that it indicates. The color specification is ASCII text, in one of three formats: 1) a name, as defined in the system's X11-style color names file (e.g. .BR rgb.txt ). 2) an X11-style hexadecimal triple: #rgb, #rrggbb, #rrrgggbbb, or #rrrrggggbbbb. 3) A triplet of decimal floating point numbers from 0.0 to 1.0, representing red, green, and blue intensities respectively, separated by commas. E.g. "1.0, 0.5, .25". If the color specification does not conform to any of these formats, including the case that it is a name, but is not in the rgb.txt database, .B ppm_parsecolor() exits the program via .BR pm_error() . .B ppm_colorname() Returns a string that describes the color of the given pixel. If an X11-style color names file (e.g. .BR rgb.txt ) is available and the color appears in it, .B ppm_colorname() returns the name of the color from the file. If the color does not appear in a X11-style color file and .I hexok is true, .B ppm_colorname() returns a hexadecimal color specification triple (#rrggbb). If a X11-style color file is available but the color does not appear in it and .I hexok is false, .B ppm_colorname() returns the name of the closest matching color in the color file. Finally, if their is no X11-style color file available and .I hexok is false, .B ppm_colorname() fails and exits the program with an error message. The string returned is in static libppm library storage which is overwritten by every call to .BR ppm_colorname() . .SS COLOR INDEXING Sometimes in processing images, you want to associate a value with a particular color. Most often, that's because you're generating a color mapped graphics format. In a color mapped graphics format, the raster contains small numbers, and the file contains a color map that tells what color each of those small numbers refers to. If your image has only 256 colors, but each color takes 24 bits to describe, this can make your output file much smaller than a straightforward RGB raster would. So, continuing the above example, say you have a .B pixel value for chartreuse and in your output file and you are going to represent chartreuse by the number 12. You need a data structure that allows your program quickly to find out that the number for a chartreuse .BR pixel is 12. Netpbm's color indexing data types and functions give you that. .B colorhash_table is a C data type that associates an integer with each of an arbitrary number of colors. It is a hash table, so it uses far less space than an array indexed by the color's RGB values would. The problem with a .B colorhash_table is that you can only look things up in it. You can't find out what colors are in it. So Netpbm has another data type for representing the same information, the poorly but historically named .BR colorhist_vector . A .B colorhist_vector is just an array. Each entry represents a color and contains the color's value (as a .BR pixel ) and the integer value associated with it. The entries are filled in starting with subscript 0 and going consecutively up for the number of colors in the histogram. (The reason the name is poor is because a color histogram is only one of many things that could be represented by it). .B colorhash_table ppm_alloccolorhash() This creates a .B colorhash_table using dynamically allocated storage. There are no colors in it. If there is not enough storage, it exits the program with an error message. .B void ppm_freecolorhash() This destroys a .B ppm_freecolorhash and frees all the storage associated with it. .B int ppm_addtocolorhash( colorhash_table cht, const pixel * const colorP, .B const int value) This adds the specified color to the specified .B colorhash_table and associates the specified value with it. You must ensure that the color you are adding isn't already present in the .BR colorhash_table . There is no way to update an entry or delete an entry from a .BR colorhash_table . .B int ppm_lookupcolor( const colorhash_table cht, const pixel * const colorP ) This looks up the specified color in the specified .BR colorhash_table . It returns the integer value associated with that color. If the specified color is not in the hash table, the function returns -1. (So if you assign the value -1 to a color, the return value is ambiguous). .B colorhist_vector ppm_colorhashtocolorhist( const colorhash_table cht, .B const int ncolors ) This converts a .B colorhash_table to a .BR colorhist_vector . The return value is a new .B colorhist_vector which you must eventually free with .BR ppm_freecolorhist() . .B ncolors is the number of colors in .BR cht . If it has more colors than that, .B ppm_colorhashtocolorhist does not create a .B colorhist_vector and returns NULL. .B colorhash_table ppm_colorhisttocolorhash( const colorhist_vector chv, .B const int ncolors ) This poorly named function does .I not convert from a .B colorhist_vector to a .BR colorhash_table . It does create a .B colorhash_table based on a .B colorhist_vector input, but the integer value for a given color in the output is not the same as the integer value for that same color in the input. .B ppm_colorhisttocolorhash() ignores the integer values in the input. In the output, the integer value for a color is the index in the input .B colorhist_vector for that color. You can easily create a color map for an image by running .B ppm_computecolorhist() over the image, then .B ppm_colorhisttocolorhash() over the result. Now you can use .B ppm_lookupcolor() to find a unique color index for any pixel in the input. If the same color appears twice in the input, .B ppm_colorhisttocolorhash() exit the program with an error message. .B ncolors is the number of colors in .BR chv . The return value is a new .B colorhash_table which you must eventually free with .BR ppm_freecolorhash() . .SS COLOR HISTOGRAMS The Netpbm libraries give you functions to examine a Netpbm image and determine what colors are in it and how many pixels of each color are in it. This information is known as a color histogram. Netpbm uses its .B colorhash_table data type to represent a color histogram. .B colorhash_table ppm_computecolorhash( pixel ** const pixels, .B const int cols, const int rows, const int maxcolors, int* const colorsP ) This poorly but historically named function generates a .B colorhash_table whose value for each color is the number of pixels in a specified image that have that color. (I.e. a color histogram). As a bonus, it returns the number of colors in the image. (It's poorly named because not all .BR colorhash_table s are color histograms, but that's all it generates). .BR pixels ", " cols ", and " rows describe the input image. .B maxcolors is the maximum number of colors you want processed. If there are more colors that that in the input image, .B ppm_computecolorhash() returns NULL as its return value and stops processing as soon as it discovers this. This makes it run faster and use less memory. One use for .B maxcolors is when you just want to find out whether or not the image has more than N colors and don't want to wait to generate a huge color table if so. If you don't want any limit on the number of colors, specify .BR maxcolors = 0 . .B ppm_computecolorhash() returns the actual number of colors in the image as .BR *colorsP , but only if it is less than or equal to .BR maxcolors . .B colorhash_table ppm_computecolorhash2( FILE * const ifp, .B const int cols, const int rows, const pixval maxval, const int format, .B const int maxcolors, int* const colorsP ) This is the same as .B ppm_computecolorhash() except that instead of feeding it an array of pixels in storage, you give it an open file stream and it reads the image from the file. The file must be positioned after the header, at the raster. Upon return, the file is still open, but its position is undefined. .B maxval and .B format are the values for the image (i.e. information from the file's header). .B colorhist_vector ppm_computecolorhist( pixel ** pixels, .B int cols, int rows, int maxcolors, int * colorsP ) This is like .B ppm_computecolorhash() except that it creates a .B colorhist_vector instead of a .BR colorhash_table . If you supply a nonzero .B maxcolors argument, that is the maximum number of colors you expect to find in the input image. If there are more colors than you say in the image, .BR ppm_computecolorhist() returns a null pointer as its return value and nothing meaningful as .BR *colorsP . If not, the function returns the new .B colorhist_vector as its return value and the actual number of colors in the image as .BR *colorsP . The returned array has space allocated for the specified number of colors regardless of how many actually exist. The extra space is at the high end of the array and is available for your use in expanding the .BR colorhist_vector . If you specify .BR maxcolors = 0 , there is no limit on the number of colors returned and the return array has space for 5 extra colors at the high end for your use in expanding the .BR colorhist_vector . .B colorhist_vector ppm_computecolorhist2( FILE * ifp, .br .B int cols, int rows, int maxcolors, pixval maxval, int format, .br .B int * colorsP ) This is the same as .B ppm_computecolorhist() except that instead of feeding it an array of pixels in storage, you give it an open file stream and it reads the image from the file. The file must be positioned after the header, at the raster. Upon return, the file is still open, but its position is undefined. .SH "SEE ALSO" .BR pbm (5), .BR pgm (5), .BR libpbm (3) .SH AUTHOR Copyright (C) 1989, 1991 by Tony Hansen and Jef Poskanzer. .\" Permission to use, copy, modify, and distribute this software and its .\" documentation for any purpose and without fee is hereby granted, provided .\" that the above copyright notice appear in all copies and that both that .\" copyright notice and this permission notice appear in supporting .\" documentation. This software is provided "as is" without express or .\" implied warranty.