about summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2024-03-27 17:34:26 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2024-03-27 17:34:26 +0000
commit7514dd26ce5ab558c7ebbca5d5bd3dce689a55d3 (patch)
tree1d2e59b1e61d5232877e672c5963e4c7172b8e1b /lib
parent6c23074f55ef954ee803262e3d452ccc6fd6af01 (diff)
downloadnetpbm-mirror-7514dd26ce5ab558c7ebbca5d5bd3dce689a55d3.tar.gz
netpbm-mirror-7514dd26ce5ab558c7ebbca5d5bd3dce689a55d3.tar.xz
netpbm-mirror-7514dd26ce5ab558c7ebbca5d5bd3dce689a55d3.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4888 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib')
-rw-r--r--lib/colorname.c80
-rw-r--r--lib/libppmcmap.c13
-rw-r--r--lib/libppmcolor.c78
3 files changed, 103 insertions, 68 deletions
diff --git a/lib/colorname.c b/lib/colorname.c
index fe580cb9..3e51055c 100644
--- a/lib/colorname.c
+++ b/lib/colorname.c
@@ -16,13 +16,14 @@
 #define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
 #define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
 
-#include "netpbm/pm_c_util.h"
+#include <stdbool.h>
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <math.h>
 
+#include "netpbm/pm_c_util.h"
 #include "netpbm/nstring.h"
 #include "netpbm/mallocvar.h"
 
@@ -68,7 +69,7 @@ openColornameFileSearch(const char * const searchPath,
         bool eol;
 
         cursor = &buffer[0];
-        eol = FALSE;    /* initial value */
+        eol = false;    /* initial value */
         *filePP = NULL;  /* initial value */
         while (!eol && !*filePP) {
             const char * token;
@@ -76,7 +77,7 @@ openColornameFileSearch(const char * const searchPath,
             if (token) {
                 *filePP = fopen(token, "r");
             } else
-                eol = TRUE;
+                eol = true;
         }
         free(buffer);
     } else
@@ -86,7 +87,8 @@ openColornameFileSearch(const char * const searchPath,
 
 
 FILE *
-pm_openColornameFile(const char * const fileName, const int must_open) {
+pm_openColornameFile(const char * const fileName,
+                     int          const mustOpen) {
 /*----------------------------------------------------------------------------
    Open the colorname dictionary file.  Its file name is 'fileName', unless
    'fileName' is NULL.  In that case, its file name is the value of the
@@ -94,19 +96,19 @@ pm_openColornameFile(const char * const fileName, const int must_open) {
    if that environment variable is not set, it is the first file found,
    if any, in the search path RGB_DB_PATH.
 
-   'must_open' is a logical: we must get the file open or die.  If
-   'must_open' is true and we can't open the file (e.g. it doesn't
-   exist), exit the program with an error message.  If 'must_open' is
+   'mustOpen' is a logical: we must get the file open or die.  If
+   'mustOpen' is true and we can't open the file (e.g. it doesn't
+   exist), exit the program with an error message.  If 'mustOpen' is
    false and we can't open the file, just return a null pointer.
 -----------------------------------------------------------------------------*/
-    FILE *f;
+    FILE * fileP;
 
     if (fileName == NULL) {
         const char * rgbdef = getenv(RGBENV);
         if (rgbdef) {
             /* The environment variable is set */
-            f = fopen(rgbdef, "r");
-            if (f == NULL && must_open)
+            fileP = fopen(rgbdef, "r");
+            if (fileP == NULL && mustOpen)
                 pm_error("Can't open the color names dictionary file "
                          "named %s, per the %s environment variable.  "
                          "errno = %d (%s)",
@@ -115,9 +117,9 @@ pm_openColornameFile(const char * const fileName, const int must_open) {
             /* The environment variable isn't set, so try the hardcoded
                default color name dictionary locations.
             */
-            openColornameFileSearch(RGB_DB_PATH, &f);
+            openColornameFileSearch(RGB_DB_PATH, &fileP);
 
-            if (f == NULL && must_open) {
+            if (fileP == NULL && mustOpen) {
                 pm_error("can't open color names dictionary file from the "
                          "path '%s' "
                          "and Environment variable %s not set.  Set %s to "
@@ -127,20 +129,20 @@ pm_openColornameFile(const char * const fileName, const int must_open) {
             }
         }
     } else {
-        f = fopen(fileName, "r");
-        if (f == NULL && must_open)
+        fileP = fopen(fileName, "r");
+        if (fileP == NULL && mustOpen)
             pm_error("Can't open the color names dictionary file '%s'.  "
                      "errno = %d (%s)", fileName, errno, strerror(errno));
 
     }
     lineNo = 0;
-    return(f);
+    return fileP;
 }
 
 
 
 struct colorfile_entry
-pm_colorget(FILE * const f) {
+pm_colorget(FILE * const fileP) {
 /*----------------------------------------------------------------------------
    Get next color entry from the color name dictionary file 'f'.
 
@@ -155,20 +157,18 @@ pm_colorget(FILE * const f) {
     struct colorfile_entry retval;
     char * rc;
 
-    gotOne = FALSE;  /* initial value */
-    eof = FALSE;
-    while (!gotOne && !eof) {
+    for (gotOne = false, eof = false; !gotOne && !eof; ) {
         lineNo++;
-        rc = fgets(buf, sizeof(buf), f);
+        rc = fgets(buf, sizeof(buf), fileP);
         if (rc == NULL)
-            eof = TRUE;
+            eof = true;
         else {
             if (buf[0] != '#' && buf[0] != '\n' && buf[0] != '!' &&
                 buf[0] != '\0') {
                 if (sscanf(buf, "%ld %ld %ld %[^\n]",
                            &retval.r, &retval.g, &retval.b, colorname)
                     == 4 )
-                    gotOne = TRUE;
+                    gotOne = true;
                 else {
                     if (buf[strlen(buf)-1] == '\n')
                         buf[strlen(buf)-1] = '\0';
@@ -191,14 +191,27 @@ pm_colorget(FILE * const f) {
 void
 pm_parse_dictionary_namen(char   const colorname[],
                           tuplen const color) {
+/*----------------------------------------------------------------------------
+   Return as *tuplen a tuple of type RGB that represents the color named
+   'colorname'.  This is an actual name, like "pink", not just a color
+   specification, like "rgb:0/0/0".
+
+   If the color name is unknown, abort the program.  If there are two entries
+   in the dictionary for the same color name, use the first one.
+
+   We use the Netpbm color dictionary used by 'pm_openColornamefile'.
 
-    FILE * fP;
+   Caller must ensure there is enough memory at 'tuplen' for at least 3
+   samples.  We set the first 3 samples of the tuple value and ignore any
+   others.
+-----------------------------------------------------------------------------*/
+    FILE * fileP;
     bool gotit;
     bool colorfileExhausted;
     struct colorfile_entry colorfileEntry;
     char * canoncolor;
 
-    fP = pm_openColornameFile(NULL, TRUE);  /* exits if error */
+    fileP = pm_openColornameFile(NULL, true);  /* exits if error */
     canoncolor = strdup(colorname);
 
     if (!canoncolor)
@@ -207,18 +220,18 @@ pm_parse_dictionary_namen(char   const colorname[],
 
     pm_canonstr(canoncolor);
 
-    for(gotit = FALSE, colorfileExhausted = FALSE;
+    for (gotit = false, colorfileExhausted = false;
         !gotit && !colorfileExhausted; ) {
 
-        colorfileEntry = pm_colorget(fP);
+        colorfileEntry = pm_colorget(fileP);
         if (colorfileEntry.colorname) {
             pm_canonstr(colorfileEntry.colorname);
             if (streq(canoncolor, colorfileEntry.colorname))
-                gotit = TRUE;
+                gotit = true;
         } else
-            colorfileExhausted = TRUE;
+            colorfileExhausted = true;
     }
-    fclose(fP);
+    fclose(fileP);
 
     if (!gotit)
         pm_error("unknown color '%s'", colorname);
@@ -237,7 +250,15 @@ pm_parse_dictionary_name(char    const colorname[],
                          pixval  const maxval,
                          int     const closeOk,
                          pixel * const colorP) {
+/*----------------------------------------------------------------------------
+   Same as 'pm_parse_dictionary_name' except return the color as a
+   pixel value of type 'pixel', with a maxval of 'maxval'.
 
+   We round the color to the nearest one that can be represented with the
+   resolution indicated by 'maxval' (rounding each component independently).
+   Iff rounding is necessary and 'closeOK' is false, we issue an informational
+   message about the rounding.
+-----------------------------------------------------------------------------*/
     double const epsilon = 1.0/65536.0;
 
     tuplen color;
@@ -271,4 +292,3 @@ pm_parse_dictionary_name(char    const colorname[],
 }
 
 
-
diff --git a/lib/libppmcmap.c b/lib/libppmcmap.c
index 0f6439ae..24302c6a 100644
--- a/lib/libppmcmap.c
+++ b/lib/libppmcmap.c
@@ -784,17 +784,23 @@ int
 ppm_findclosestcolor(const pixel * const colormap,
                      int           const ncolors,
                      const pixel * const pP) {
+/*----------------------------------------------------------------------------
+  The index in colormap[] (which has 'ncolors' entries) of the color that is
+  closest to color *pP.
 
-    /* Search colormap for closest match.       */
+  Where two entries in colormap[] are identical, return the lesser of their
+  two indices.
 
-    int i;
+  Iff there are no colors in the amp ('ncolors' is zero), return -1.
+-----------------------------------------------------------------------------*/
+    unsigned int i;
     int ind;
     unsigned int bestDist;
 
     bestDist = UINT_MAX;
     ind = -1;
 
-    for(i = 0; i < ncolors && bestDist > 0; ++i) {
+    for (i = 0; i < ncolors && bestDist > 0; ++i) {
         unsigned int const dist = PPM_DISTANCE(*pP, colormap[i]);
 
         if (dist < bestDist ) {
@@ -806,6 +812,7 @@ ppm_findclosestcolor(const pixel * const colormap,
 }
 
 
+
 void
 ppm_colorrowtomapfile(FILE *ofp, pixel *colormap, int ncolors, pixval maxval)
 {
diff --git a/lib/libppmcolor.c b/lib/libppmcolor.c
index c0a88dc2..d6f00218 100644
--- a/lib/libppmcolor.c
+++ b/lib/libppmcolor.c
@@ -9,6 +9,7 @@
 ** implied warranty.
 */
 
+#include <stdbool.h>
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
@@ -46,7 +47,7 @@ pixel
 ppm_parsecolor(const char * const colorname,
                pixval       const maxval) {
 
-    return ppm_parsecolor2(colorname, maxval, TRUE);
+    return ppm_parsecolor2(colorname, maxval, true);
 }
 
 
@@ -57,7 +58,7 @@ ppm_colorname(const pixel * const colorP,
               int           const hexok)   {
 
     int r, g, b;
-    FILE * f;
+    FILE * fileP;
     static char colorname[200];
         /* Null string means no suitable name so far */
 
@@ -71,17 +72,17 @@ ppm_colorname(const pixel * const colorP,
         b = (int) PPM_GETB(*colorP) * 255 / (int) maxval;
     }
 
-    f = pm_openColornameFile(NULL, !hexok);
+    fileP = pm_openColornameFile(NULL, !hexok);
 
-    if (!f)
+    if (!fileP)
         STRSCPY(colorname, "");
     else {
         int bestDiff;
         bool eof;
 
-        for (bestDiff = 32767, eof = FALSE;
+        for (bestDiff = 32767, eof = false;
              !eof && bestDiff > 0; ) {
-            struct colorfile_entry const ce = pm_colorget(f);
+            struct colorfile_entry const ce = pm_colorget(fileP);
             if (ce.colorname)  {
                 int const thisDiff =
                     abs(r - (int)ce.r) +
@@ -93,9 +94,9 @@ ppm_colorname(const pixel * const colorP,
                     STRSCPY(colorname, ce.colorname);
                 }
             } else
-                eof = TRUE;
+                eof = true;
         }
-        fclose(f);
+        fclose(fileP);
 
         if (bestDiff == 32767) {
             /* Color file contain no entries, so we can't even pick a close
@@ -247,14 +248,14 @@ readOpenColorFile(FILE *          const colorFileP,
     bool done;
 
     nColorsDone = 0;
-    done = FALSE;
+    done = false;
     *errorP = NULL;
 
     while (!done && !*errorP) {
         struct colorfile_entry const ce = pm_colorget(colorFileP);
 
         if (!ce.colorname)
-            done = TRUE;
+            done = true;
         else
             processColorfileEntry(ce, cht, colornames, colors,
                                   &nColorsDone, errorP);
@@ -284,7 +285,7 @@ readColorFile(const char *    const fileName,
    to colornames[], colors[], and 'cht'.  Return as *nColorsP the number
    of colors in it.
 
-   If the file is not openable (e.g. not file by that name exists), abort the
+   If the file is not openable (e.g. no file by that name exists), abort the
    program if 'mustOpen' is true; otherwise, return values indicating a
    dictionary with no colors.
 
@@ -318,19 +319,25 @@ static void
 readcolordict(const char *      const fileName,
               bool              const mustOpen,
               unsigned int *    const nColorsP,
-              const char ***    const colornamesP,
+              const char ***    const colorNamesP,
               pixel **          const colorsP,
               colorhash_table * const chtP,
               const char **     const errorP) {
+/*----------------------------------------------------------------------------
+  Same as 'ppm_readcolordict' except that a) if we fail, we return a message
+  as *errorP instead of issuing a message; and b) we always return all three
+  return values.
+-----------------------------------------------------------------------------*/
+    const char ** colorNames;
+        /* List of all the color names in the dictionary */
 
-    const char ** colornames;
-
-    colornames = allocColorNames();
+    colorNames = allocColorNames();
 
-    if (colornames == NULL)
+    if (colorNames == NULL)
         pm_asprintf(errorP, "Unable to allocate space for colorname table.");
     else {
         pixel * colors;
+            /* colors[i] is the color that goes with name colorNames[i] */
 
         MALLOCARRAY(colors, MAXCOLORNAMES);
 
@@ -338,6 +345,7 @@ readcolordict(const char *      const fileName,
             pm_asprintf(errorP, "Unable to allocate space for color table.");
         else {
             colorhash_table cht;
+                /* Hash table mapping colorNames[] to colors[] */
 
             cht = allocColorHash();
 
@@ -345,7 +353,7 @@ readcolordict(const char *      const fileName,
                 pm_asprintf(errorP, "Unable to allocate space for color hash");
             else {
                 readColorFile(fileName, mustOpen,
-                              nColorsP, colornames, colors, cht,
+                              nColorsP, colorNames, colors, cht,
                               errorP);
 
                 if (*errorP)
@@ -359,9 +367,9 @@ readcolordict(const char *      const fileName,
                 *colorsP = colors;
         }
         if (*errorP)
-            free(colornames);
+            free(colorNames);
         else
-            *colornamesP = colornames;
+            *colorNamesP = colorNames;
     }
 }
 
@@ -371,7 +379,7 @@ void
 ppm_readcolordict(const char *      const fileName,
                   int               const mustOpen,
                   unsigned int *    const nColorsP,
-                  const char ***    const colornamesP,
+                  const char ***    const colorNamesP,
                   pixel **          const colorsP,
                   colorhash_table * const chtP) {
 /*----------------------------------------------------------------------------
@@ -382,8 +390,8 @@ ppm_readcolordict(const char *      const fileName,
 
    Return as *nColorsP the number of colors in the dictionary.
 
-   Return as *colornamesP the names of those colors.  *colornamesP is a
-   malloced array that Caller must free with ppm_freecolornames().
+   Return as *colorNamesP the names of those colors.  *colorNamesP is a
+   malloced array that Caller must free with ppm_freecolorNames().
    The first *nColorsP entries are valid; *chtP contains indices into this
    array.
 
@@ -391,18 +399,18 @@ ppm_readcolordict(const char *      const fileName,
    MAXCOLORS with the first elements filled in and the rest undefined.
 
    Return as *chtP a color hash table mapping each color in the dictionary
-   to the index into *colornamesP for the name of the color.
+   to the index into *colorNamesP for the name of the color.
 
-   Each of 'nColorsP, 'colornamesP', and 'colorsP' may be null, in which case
+   Each of 'nColorsP, 'colorNamesP', and 'colorsP' may be null, in which case
    we do not return the corresponding information (or allocate memory for it).
 -----------------------------------------------------------------------------*/
     colorhash_table cht;
-    const char ** colornames;
+    const char ** colorNames;
     pixel * colors;
     unsigned int nColors;
     const char * error;
 
-    readcolordict(fileName, mustOpen, &nColors, &colornames, &colors, &cht,
+    readcolordict(fileName, mustOpen, &nColors, &colorNames, &colors, &cht,
                   &error);
 
     if (error) {
@@ -414,10 +422,10 @@ ppm_readcolordict(const char *      const fileName,
             *chtP = cht;
         else
             ppm_freecolorhash(cht);
-        if (colornamesP)
-            *colornamesP = colornames;
+        if (colorNamesP)
+            *colorNamesP = colorNames;
         else
-            ppm_freecolornames(colornames);
+            ppm_freecolornames(colorNames);
         if (colorsP)
             *colorsP = colors;
         else
@@ -433,23 +441,23 @@ void
 ppm_readcolornamefile(const char *      const fileName,
                       int               const mustOpen,
                       colorhash_table * const chtP,
-                      const char ***    const colornamesP) {
+                      const char ***    const colorNamesP) {
 
-    ppm_readcolordict(fileName, mustOpen, NULL, colornamesP, NULL, chtP);
+    ppm_readcolordict(fileName, mustOpen, NULL, colorNamesP, NULL, chtP);
 }
 
 
 
 void
-ppm_freecolornames(const char ** const colornames) {
+ppm_freecolornames(const char ** const colorNames) {
 
     unsigned int i;
 
     for (i = 0; i < MAXCOLORNAMES; ++i)
-        if (colornames[i])
-            free((char *)colornames[i]);
+        if (colorNames[i])
+            free((char *)colorNames[i]);
 
-    free(colornames);
+    free(colorNames);
 }