Plan 9 from Bell Labs’s /usr/web/sources/contrib/pac/sys/doc/netpbm/ms/libppm.ms

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.TL
User manual for old ppm functions
.SH 1
libppm
.LP
Updated: 22 July 2004
.br
Table Of Contents
.SH 2
NAME
.LP
libppm - functions for PPM programs
.SH 2
SYNOPSIS
.LP
\fB#include <ppm.h>\fR
.LP
\fBvoid ppm_init(\fR\fBint *\fR \fIargcP\fR\fB,\fR
\fBchar *\fR \fIargv\fR\fB[]\fR\fB);\fR
.LP
\fBpixel ** ppm_allocarray(\fR
\fBint \fR\fIcols\fR\fB,\fR\fBint\fR \fIrows\fR\fB);\fR
.LP
\fBpixel * ppm_allocrow(\fR\fBint\fR \fIcols\fR\fB);\fR
.LP
\fBvoid ppm_freearray(\fR\fBpixel **\fR \fIpixels\fR\fB,\fR
\fBint\fR \fIrows\fR\fB);\fR
.LP
\fBvoid ppm_freerow(\fR\fBpixel *\fR \fIpixelrow\fR\fB);\fR
.LP
\fBvoid ppm_readppminit(\fR\fBFILE *\fR \fIfp\fR\fB,\fR
\fBint *\fR \fIcolsP\fR\fB,\fR
\fBint *\fR \fIrowsP\fR\fB,\fR
\fBpixval *\fR \fImaxvalP\fR\fB,\fR\fBint *\fR \fIformatP\fR\fB );\fR
.LP
\fBvoid ppm_readppmrow(\fR\fBFILE *\fR\fIfp\fR\fB,\fR
\fBpixel *\fR \fIpixelrow\fR\fB,\fR
\fBint \fR\fIcols\fR\fB,\fR
\fBpixval \fR\fImaxval\fR\fB,\fR
\fBint \fR\fIformat\fR\fB);\fR
.LP
\fBpixel ** ppm_readppm(\fR\fBFILE *\fR \fIfp\fR\fB,\fR
\fBint *\fR \fIcolsP\fR\fB,\fR
\fBint *\fR \fIrowsP\fR\fB,\fR
\fBpixvalP *\fR \fImaxvalP\fR\fB);\fR
.LP
\fBvoid ppm_writeppminit(\fR\fBFILE * \fIfp\fB,\fR
\fBint\fR \fIcols\fR\fB,\fR
\fBint\fR \fIrows\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB,\fR
\fBint\fR \fIforceplain\fR\fB);\fR
.LP
\fBvoid ppm_writeppmrow(\fR\fBFILE *\fR \fIfp\fR\fB,\fR
\fBpixel *\fR \fIpixelrow\fR\fB,\fR
\fBint\fR \fIcols\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB,\fR
\fBint\fR \fIforceplain\fR\fB);\fR
.LP
\fBvoid ppm_writeppm(\fR\fBFILE *\fR \fIfp\fR\fB,\fR
\fBpixel **\fR \fIpixels\fR\fB,\fR
\fBint\fR \fIcols\fR\fB,\fR
\fBint\fR \fIrows\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB,\fR
\fBint\fR \fIforceplain\fR\fB);\fR
.LP
\fBvoid ppm_writeppm(\fR\fBFILE *\fR \fIfp\fR\fB,\fR
\fBpixel **\fR \fIpixels\fR\fB,\fR
\fBint\fR \fIcols\fR\fB,\fR
\fBint\fR \fIrows\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB,\fR
\fBint\fR \fIforceplain\fR\fB);\fR
.LP
\fBvoid ppm_nextimage(\fR\fBFILE *\fR \fIfile\fR\fB,\fR
\fBint * const\fR \fIeofP\fR\fB);\fR
.LP
\fBvoid ppm_check(\fR\fBFILE *\fR \fIfile\fR\fB,\fR
\fBconst enum pm_check_type\fR \fIcheck_type\fR\fB,\fR
\fBconst int\fR \fIformat\fR\fB,\fR
\fBconst int\fR \fIcols\fR\fB,\fR
\fBconst int\fR \fIrows\fR\fB,\fR
\fBconst int\fR \fImaxval\fR\fB,\fR
.br
\fBenum pm_check_code * const\fR \fIretval\fR\fB);\fR
.LP
\fBtypedef ... pixel;\fR
\fBtypedef ... pixval;\fR
.LP
\fB#define PPM_MAXMAXVAL ...\fR
.LP
\fB#define PPM_OVERALLMAXVAL ...\fR
.LP
\fB#define PPM_FORMAT ...\fR
.LP
\fB#define RPPM_FORMAT ...\fR
.LP
\fB#define PPM_TYPE PPM_FORMAT\fR
.LP
\fB#define\fR \fBPPM_FORMAT_TYPE(\fR\fIformat\fR\fB)\fR \fB...\fR
.LP
\fBextern pixval ppm_pbmmaxval;\fR
.LP
\fBpixval PPM_GETR(pixel\fR \fIp\fR\fB)\fR
.LP
\fBpixval PPM_GETG(pixel\fR \fIp\fR\fB)\fR
.LP
\fBpixval PPM_GETB(pixel\fR \fIp\fR\fB)\fR
.LP
\fBvoid PPM_ASSIGN(pixel\fR \fIp\fR\fB,\fR
\fBpixval\fR \fIred\fR\fB,\fR
\fBpixval\fR \fIgrn\fR\fB,\fR
\fBpixval\fR \fIblu\fR\fB)\fR
.LP
\fBint PPM_EQUAL(pixel\fR \fIp\fR\fB,\fR
\fBpixel\fR \fIq\fR\fB)\fR
.LP
\fBint PPM_ISGRAY(pixel\fR \fIp\fR\fB)\fR
.LP
\fBvoid\fR
\fBPPM_DEPTH(pixel\fR \fInewp\fR\fB,\fR
\fBpixel\fR \fIp\fR\fB,\fR
\fBpixval\fR \fIoldmaxval\fR\fB,\fR
\fBpixval\fR \fInewmaxval\fR\fB)\fR
.LP
\fBpixel ppm_parsecolor(char *\fR \fIcolorname\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB)\fR
.LP
\fBchar * ppm_colorname(pixel *\fR \fIcolorP\fR\fB,\fR
\fBpixval\fR \fImaxval\fR\fB,\fR
\fBint\fR \fIhexok\fR\fB)\fR
.LP
\fBvoid ppm_readcolornamefile(\fR
\fBconst char *\fR\fIfileName\fR, 
\fBint\fR \fImustOpen\fR,
\fBcolorhash_table *\fR \fIchtP\fR, 
\fBconst char ***\fR \fIcolornamesP\fR
\fB)\fR
.SH 2
DESCRIPTION
.LP
.LP
These library functions are part of Netpbm.
.SH 3
TYPES AND CONSTANTS
.LP
Each \fBpixel\fR contains three \fBpixval\fRs, each of which should
contain only the values between \fB0\fR and \fBPPM_MAXMAXVAL\fR.
\fBppm_pbmmaxval\fR 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.
.SH 3
MANIPULATING PIXELS
.LP
.LP
The macros \fBPPM_GETR\fR, \fBPPM_GETG\fR, and \fBPPM_GETB\fR
retrieve the red, green, or blue sample, respectively, from the given
pixel.
.LP
The \fBPPM_ASSIGN\fR macro assigns the given values to the red,
green, and blue samples of the given pixel.
.LP
The \fBPPM_EQUAL\fR macro tests two pixels for equality.
.LP
The \fBPPM_ISGRAY\fR macro tests a pixel for being gray.  It
returns true if and only if the color of pixel \fIp\fR is black,
white, or gray.
.LP
The \fBPPM_DEPTH\fR macro scales the colors of pixel \fIp\fR
according the old and new maxvals and assigns the new values to
\fInewp\fR.  It is intended to make writing ppmtowhatever easier.
.LP
The \fBPPM_LUMIN\fR, \fBPPM_CHROM_R\fR, and \fBPPM_CHROM_B\fR
macros determine the luminance, red chrominance, and blue chrominance,
respectively, of the pixel \fIp\fR.  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).
.LP
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.
.SH 3
INITIALIZATION
.LP
.LP
\fBppm_init()\fR is identical to \fBpm_init\fR.
.SH 3
MEMORY MANAGEMENT
.LP
\fBppm_allocarray()\fR allocates an array of pixels.
.LP
\fBppm_allocrow()\fR allocates a row of the given number of
pixels.
.LP
\fBppm_freearray()\fR frees the array allocated with
\fBppm_allocarray()\fR containing the given number of rows.
.LP
\fBppm_freerow()\fR frees a row of pixelss allocated with
\fBppm_allocrow()\fR.
.SH 3
READING FILES
.LP
.LP
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
\fIformat\fR value returned by \fBppm_readppminit()\fR is, however,
not translated.  It represents the actual format of the PBM or PGM
file.
.LP
\fBppm_readppminit()\fR reads the header of a PPM file, returning
all the information from the header and leaving the file positioned
just after the header.
.LP
\fBppm_readppmrow()\fR reads a row of pixels into the
\fIpixelrow\fR array.  \fIformat\fR, \fIcols\fR, and \fImaxval\fR
are the values returned by \fBppm_readppminit()\fR.
.LP
\fBppm_readppm()\fR reads an entire PPM image into memory,
returning the allocated array as its return value and returning the
information from the header as \fIrows\fR, \fIcols\fR, and
\fImaxval\fR.  This function combines \fBppm_readppminit()\fR,
\fBppm_allocarray()\fR, and \fBppm_readppmrow()\fR.
.SH 3
WRITING FILES
.LP
\fBppm_writeppminit()\fR writes the header for a PPM file and leaves
it positioned just after the header.
.LP
\fIforceplain\fR is a logical value that tells
\fBppm_writeppminit() \fR to write a header for a plain PPM format
file, as opposed to a raw PPM format file.
.LP
\fBppm_writeppmrow()\fR writes the row \fIpixelrow\fR to a PPM
file.  For meaningful results, \fIcols\fR, \fImaxval\fR, and
\fIforceplain\fR must be the same as was used with
\fBppm_writeppminit()\fR.
.LP
\fBppm_writeppm()\fR write the header and all data for a PPM
image.  This function combines \fBppm_writeppminit()\fR and
\fBppm_writeppmrow()\fR.
.SH 3
MISCELLANEOUS
.LP
.LP
\fBppm_nextimage()\fR positions a PPM input file to the next image
in it (so that a subsequent \fBppm_readppminit()\fR reads its
header).
.LP
\fBppm_nextimage()\fR is analogous to \fBpbm_nextimage()\fR, but
works on PPM, PGM, and PBM files.
.LP
\fBppm_check() \fR checks for the common file integrity error
where the file is the wrong size to contain all the image data.
.LP
\fBppm_check() \fR is analogous to \fBpbm_check()\fR, but works
on PPM, PGM, and PBM files.
.SH 3
COLOR
.LP
.SH 4
Luminance, Chrominance (YcbCr)
.LP
.DS L
    float PPM_LUMIN(pixel p);
    float PPM_CHROM_B(pixel p);
    float PPM_CHROM_R(pixel p);
.DE
.LP
\fBPPM_LUMIN\fR takes a \fBpixel\fR as an argument and returns
the luminance of that pixel, with the same maxval as the pixel
(e.g. if the pixel's maxval is 255, a \fBPPM_LUMIN\fR value of 255
means fully luminant).
.LP
\fBPPM_CHROM_B\fR and \fBPPM_CHROM_R\fR are similar, for the red
and blue chrominance values.
.DS L
    pixel
    ppm_color_from_ycbcr(unsigned int y, 
                         int          cb, 
                         int          cr);
.DE
.LP
\fBppm_color_from_ycbcr()\fR converts in the other direction.
Given luminance and chrominance, it returns a pixel value.
.SH 4
Hue, Saturation, Value (HSV)
.LP
.DS L
    struct hsv {
        double h;  /* hue (degrees)  0..360 */
        double s;  /* saturation (0-1) */
        double v;  /* value (0-1) */
    };
.DE
.DS L
    pixel
    ppm_color_from_hsv(struct hsv const hsv,
                       pixval     const maxval);
.DE
.DS L
    struct hsv
    ppm_hsv_from_color(pixel  const color,
                       pixval const maxval);
.DE
.LP
These convert a color between from \fBpixel\fR (RGB) form and HSV.
.DS L
    pixval
    ppm_saturation(pixel  const p,
                   pixval const maxval);
.DE
.LP
This gives you the saturation of a color, as a pixval.  (e.g. if
the saturation of \fIp\fR is 50% and \fImaxval\fR is 100,
\fBppm_saturation()\fR returns 50).
.SH 4
Berlin-Kay Color
.LP
.LP
Brent Berlin and Paul Kay in 1969 did a study which identified
a set of 11 basic colors people universally recognize.  They are:
.IP \(bu
black
.IP \(bu
gray
.IP \(bu
white
.IP \(bu
red
.IP \(bu
orange
.IP \(bu
yellow
.IP \(bu
green
.IP \(bu
blue
.IP \(bu
violet
.IP \(bu
purple
.IP \(bu
brown
.LP
.LP
The \fBbk_color\fR type represents a color from this set:
.DS L
    typedef enum {
        BKCOLOR_BLACK = 0,
        BKCOLOR_GRAY,
        BKCOLOR_WHITE,
        BKCOLOR_RED,
        BKCOLOR_ORANGE,
        BKCOLOR_YELLOW,
        BKCOLOR_GREEN,
        BKCOLOR_BLUE,
        BKCOLOR_VIOLET,
        BKCOLOR_PURPLE,
        BKCOLOR_BROWN
    } bk_color;
.DE
.LP
You can use this as an index of an array, in which case you might also
want macro \fBBKCOLOR_COUNT\fR, which is the number of colors in the
set (11).
.LP
To translate between the \fBbk_color\fR type and the English names
of the colors, use \fBppm_bk_color_from_name()\fR and
\fBppm_name_from_bk_color()\fR:
.DS L
    bk_color
    ppm_bk_color_from_name(const char * name);
    
    const char *
    ppm_name_from_bk_color(bk_color bkColor);
.DE
.LP
\fBppm_bk_color_from_color()\fR tells you to which Berlin-Kay color
a certain color is closest, by way of a fuzzy color matching algorithm:
.DS L
    bk_color
    ppm_bk_color_from_color(pixel  color,
                            pixval maxval);
.DE
.LP
\fImaxval\fR is the maxval on which \fIcolor\fR is based.
.LP
\fBppm_color_from_bk_color()\fR converts the opposite way: given
a Berlin-Kay color, it gives the color, in \fBpixel\fR form, that best
represents it.
.DS L
    pixel
    ppm_color_from_bk_color(bk_color bkColor,
                            pixval   maxval);
.DE
.LP
\fImaxval\fR is the maxval on which the returned color is based.
.LP
All of the facilities in this section were new in Netpbm 10.34
(June 2006).
.SH 3
COLOR NAMES
.LP
.SH 4
System Color Dictionary
.LP
.LP
Netpbm uses the system's X11 color dictionary (usually in
\fB/usr/lib/X11/rgb.txt\fR).  This is the same file the X Window
System typically uses to associate colors with their names.
.LP
The color dictionary that Netpbm uses is in the file whose name is
the value of the \fBRGBDEF\fR environment variable.  If \fBRGBDEF\fR
is not set, Netpbm defaults to the first existing file from this list:
.IP 1
\fB/usr/lib/X11/rgb.txt\fR
.IP 2
\fB/usr/openwinlib/rgb.txt\fR
.IP 3
\fB/usr/X11R6/lib/X11/rgb.txt\fR
.LP
.LP
You can see the color names from a typical X11 color dictionary,
which is probably very close to what is on your system, along with the
colors, here.  This
website shows a bunch of other versions you could use.
.LP
Netpbm is packaged with a color dictionary.  A standard Netpbm
installation installs this file as "misc/rgb.txt" in the Netpbm
directory.  This color dictionary has colors from everywhere the
Netpbm maintainer could find them, and is a superset of XFree 86's
color dictionary.
.SH 4
ppm_parsecolor
.LP
.LP
\fBppm_parsecolor()\fR interprets a color specification and returns a
pixel of the color that it indicates.  The color specification is
ASCII text, in one of these formats:
.IP \(bu
a name, as defined in the system color dictionary
.
.IP \(bu
 An X11-style hexadecimal specifier:
\f(CWrgb:\fIr\f(CW/\fIg\f(CW/\fIb\f(CW\fR, where \fIr\fR, \fIg\fR, and
\fIb\fR are each 1- to 4-digit hexadecimal numbers.  For each, the maxval
is the maximum number that can be represented in the number of hexadecimal
digits given.  Example: \f(CWrgb:01/ff/8000\fR specifies 1/255 red
intensity, maximum green intensity, and about half blue intensity.
.IP \(bu
 An X11-style decimal specifier:
\f(CWrgbi:\fIr\f(CW/\fIg\f(CW/\fIb\f(CW\fR, where \fIr\fR, \fIg\fR,
and \fIb\fR are floating point numbers from 0 to 1.
.IP \(bu
an old-X11-style hexadecimal triple: \f(CW#rgb\fR, \f(CW#rrggbb\fR, 
\f(CW#rrrgggbbb\fR, or \f(CW#rrrrggggbbbb\fR.
.IP \(bu
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. \f(CW1.0,0.5,.25\fR.  This is for backwards compatibility;
it was in use before MIT came up with the similar and preferred rgbi style).
.LP
.LP
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
system color dictionary, \fBppm_parsecolor()\fR throws an error.
.SH 4
ppm_colorname
.LP
.LP
\fBppm_colorname()\fR returns a string that describes the color
of the given pixel.  If a system color dictionary
is available and the color appears in it, \fBppm_colorname()\fR
returns the name of the color from the file.  If the color does not
appear in a system color dictionary and \fIhexok\fR is true,
\fBppm_colorname()\fR returns a hexadecimal color specification
triple (#rrggbb).  If a system color dictionary is available but the
color does not appear in it and \fIhexok\fR is false,
\fBppm_colorname()\fR returns the name of the closest matching color
in the color file.  Finally, if there is no system color dictionary
available and \fIhexok\fR is false, \fBppm_colorname()\fR fails and
exits the program with an error message.
.LP
The string returned is in static libppm library storage which is
overwritten by every call to \fBppm_colorname()\fR.
.SH 4
ppm_readcolornamefile
.LP
.LP
\fBppm_readcolornamefile()\fR reads the entire contents of the color
dictionary in the file named \fIfileName\fR into data structures you
can use to access it easily.
.LP
The function returns all the color names as an array of
null-terminated strings.  It mallocs the space for this array and
returns its address at \fIcolornamesP\fR.
\fB(*colornamesP)[\fR\fIi\fR\fB]\fR is the address of the first
character in the null-terminated string that is the name of the
\fIi\fRth color in the dictionary.
.LP
The function also returns a \fBcolorhash_table\fR (see COLOR INDEXING) that matches all these color names
up to the colors they represent.  It mallocs the space for the
\fBcolorhash_table\fR and returns its address at \fIchtP\fR.  The
number that the \fBcolorhash_table\fR associates with each color is
the index into the color name array described above of the name of
that color.
.LP
You may specify a null pointer for \fIfileName\fR to indicate the
default color dictionary.
.LP
\fImustOpen\fR is a boolean.  If it is nonzero, the function fails
and aborts the program if it is unable to open the specified color dictionary
file.  If it is zero, though, it simply treats an unopenable color dictionary
as an empty one.  The colorhash and color name array it returns contain no
colors or names.
.LP
\fBppm_readcolornamefile()\fR was new in Netpbm 10.15 (April 2003).
.SH 3
COLOR INDEXING
.LP
.LP
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.
.LP
So, continuing the above example, say you have a \fBpixel\fR 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
\fBpixel\fR is 12.  Netpbm's color indexing data types and functions
give you that.
.LP
\fBcolorhash_table\fR 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.
.LP
The problem with a \fBcolorhash_table\fR 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 \fBcolorhist_vector\fR.  A
\fBcolorhist_vector\fR is just an array.  Each entry represents a
color and contains the color's value (as a \fBpixel\fR) 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.
.LP
(The reason the name is poor is because a color histogram is only
one of many things that could be represented by it).
.LP
\fBcolorhash_table ppm_alloccolorhash()\fR
.LP
This creates a \fBcolorhash_table\fR using dynamically allocated
storage.  There are no colors in it.  If there is not enough storage,
it exits the program with an error message.
.LP
\fBvoid ppm_freecolorhash()\fR
.LP
This destroys a \fBppm_freecolorhash \fR and frees all the storage
associated with it.
.LP
\fBint ppm_addtocolorhash( colorhash_table cht, const pixel * const
colorP, const int value)\fR
.LP
This adds the specified color to the specified \fBcolorhash_table
\fR and associates the specified value with it.
.LP
You must ensure that the color you are adding isn't already present
in the \fBcolorhash_table\fR.
.LP
There is no way to update an entry or delete an entry from a 
\fBcolorhash_table\fR.
.LP
\fBint ppm_lookupcolor( const colorhash_table cht, const pixel *
const colorP )\fR
.LP
This looks up the specified color in the specified
\fBcolorhash_table\fR.  It returns the integer value associated with
that color.
.LP
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).
.LP
\fBcolorhist_vector ppm_colorhashtocolorhist( const colorhash_table cht,\fR
\fBconst int ncolors )\fR
.LP
This converts a \fBcolorhash_table\fR to a
\fBcolorhist_vector\fR.  The return value is a new
\fBcolorhist_vector\fR which you must eventually free with
\fBppm_freecolorhist()\fR.
.LP
\fBncolors\fR is the number of colors in \fBcht\fR.  If it has
more colors than that, \fBppm_colorhashtocolorhist\fR does not create
a \fBcolorhist_vector\fR and returns NULL.
.LP
\fBcolorhash_table ppm_colorhisttocolorhash( const colorhist_vector chv, 
const int ncolors ) \fR
.LP
This poorly named function does not convert from a
\fBcolorhist_vector\fR to a \fBcolorhash_table\fR.
.LP
It does create a \fBcolorhash_table\fR based on a
\fBcolorhist_vector\fR 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.  \fBppm_colorhisttocolorhash()\fR ignores the integer
values in the input.  In the output, the integer value for a color is
the index in the input \fBcolorhist_vector\fR for that color.
.LP
You can easily create a color map for an image by running
\fBppm_computecolorhist() \fR over the image, then
\fBppm_colorhisttocolorhash()\fR over the result.  Now you can use
\fBppm_lookupcolor()\fR to find a unique color index for any pixel in
the input.
.LP
If the same color appears twice in the input,
\fBppm_colorhisttocolorhash() \fR exit the program with an error
message.
.LP
\fBncolors\fR is the number of colors in \fBchv\fR.
.LP
The return value is a new \fBcolorhash_table\fR which you must
eventually free with \fBppm_freecolorhash()\fR.
.SH 3
COLOR HISTOGRAMS
.LP
.LP
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 \fBcolorhash_table\fR data type to represent a color
histogram.
.LP
\fBcolorhash_table ppm_computecolorhash( pixel ** const pixels,
const int cols, const int rows, const int maxcolors, int* const colorsP )\fR
.LP
This poorly but historically named function generates a
\fBcolorhash_table\fR 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.
.LP
(It's poorly named because not all \fBcolorhash_table\fRs are
color histograms, but that's all it generates).
.LP
\fBpixels\fR, \fBcols\fR, and \fBrows\fR describe the input
image.
.LP
\fBmaxcolors\fR is the maximum number of colors you want
processed.  If there are more colors that that in the input image,
\fBppm_computecolorhash()\fR 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 \fBmaxcolors\fR 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
\fBmaxcolors\fR=\fB0\fR.
.LP
\fBppm_computecolorhash()\fR returns the actual number of colors
in the image as \fB*colorsP\fR, but only if it is less than or equal
to \fBmaxcolors\fR.
.LP
\fBcolorhash_table ppm_computecolorhash2( FILE * const ifp,
const int cols, const int rows, const pixval maxval, const int format,\fR
\fBconst int maxcolors, int* const colorsP )\fR
.LP
This is the same as \fBppm_computecolorhash()\fR 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.
.LP
\fBmaxval\fR and \fBformat\fR are the values for the image
(i.e. information from the file's header).
.LP
\fBcolorhist_vector ppm_computecolorhist( pixel ** pixels,
int cols, int rows, int maxcolors, int * colorsP )\fR
.LP
This is like \fBppm_computecolorhash()\fR except that it creates a
\fBcolorhist_vector\fR instead of a \fBcolorhash_table\fR.
.LP
If you supply a nonzero \fBmaxcolors\fR 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,
\fBppm_computecolorhist()\fR returns a null pointer as its return
value and nothing meaningful as \fB*colorsP\fR.
.LP
If not, the function returns the new \fBcolorhist_vector \fR as
its return value and the actual number of colors in the image as
\fB*colorsP\fR.  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 \fBcolorhist_vector\fR.
.LP
If you specify \fBmaxcolors\fR=\fB0\fR, 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
\fBcolorhist_vector\fR.
.LP
\fBcolorhist_vector ppm_computecolorhist2( FILE * ifp,
int cols, int rows, int maxcolors, pixval maxval, int format,
int * colorsP )\fR
.LP
This is the same as \fBppm_computecolorhist()\fR 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 2
SEE ALSO
.LP
\fBpbm\fR,
\fBpgm\fR,
\fBlibpbm\fR
.SH 2
AUTHOR
.LP
Copyright (C) 1989, 1991 by Tony Hansen and Jef Poskanzer.
.br
\l'5i'
.SH 2
Table Of Contents
.LP
.IP \(bu
NAME
.IP \(bu
SYNOPSIS
.IP \(bu
DESCRIPTION
.IP \(bu
TYPES AND CONSTANTS
.IP \(bu
MANIPULATING PIXELS
.IP \(bu
INITIALIZATION
.IP \(bu
MEMORY MANAGEMENT
.IP \(bu
READING FILES
.IP \(bu
WRITING FILES
.IP \(bu
MISCELLANEOUS
.IP \(bu
COLOR
.IP \(bu
COLOR NAMES
.IP \(bu
COLOR INDEXING
.IP \(bu
COLOR HISTOGRAMS
.LP
.IP \(bu
SEE ALSO
.IP \(bu
AUTHOR
.LP

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.