Debian Codegraph @

src/rgb.c

raw
1 /*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26 /*****************************************************************************\
27 * rgb.c: *
28 * *
29 * XPM library *
30 * Rgb file utilities *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 /*
36 * Part of this code has been taken from the ppmtoxpm.c file written by Mark
37 * W. Snitily but has been modified for my special need
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43 #include "XpmI.h"
44 #include <ctype.h>
45
46 /*
47 * Read a rgb text file. It stores the rgb values (0->65535)
48 * and the rgb mnemonics (malloc'ed) into the "rgbn" array. Returns the
49 * number of entries stored.
50 */
51 int
52 xpmReadRgbNames(
53 const char *rgb_fname,
54 xpmRgbName rgbn[])
55 {
56 FILE *rgbf;
57 int n, items, red, green, blue;
58 char line[512], name[512], *rgbname, *s1, *s2;
59 xpmRgbName *rgb;
60
61 /* Open the rgb text file. Abort if error. */
62 if ((rgbf = fopen(rgb_fname, "r" FOPEN_CLOEXEC)) == NULL)
63 return 0;
64
65 /* Loop reading each line in the file. */
66 n = 0;
67 rgb = rgbn;
68 /* Quit if rgb text file has too many entries. */
69 while (fgets(line, sizeof(line), rgbf) && n < MAX_RGBNAMES) {
70
71 /* Skip silently if line is bad. */
72 items = sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
73 if (items != 4)
74 continue;
75
76 /*
77 * Make sure rgb values are within 0->255 range. Skip silently if
78 * bad.
79 */
80 if (red < 0 || red > 0xFF ||
81 green < 0 || green > 0xFF ||
82 blue < 0 || blue > 0xFF)
83 continue;
84
85 /* Allocate memory for ascii name. If error give up here. */
86 if (!(rgbname = (char *) XpmMalloc(strlen(name) + 1)))
87 break;
88
89 /* Copy string to ascii name and lowercase it. */
90 for (s1 = name, s2 = rgbname; *s1; s1++)
91 *s2++ = tolower(*s1);
92 *s2 = '\0';
93
94 /* Save the rgb values and ascii name in the array. */
95 rgb->r = red * 257; /* 65535/255 = 257 */
96 rgb->g = green * 257;
97 rgb->b = blue * 257;
98 rgb->name = rgbname;
99 rgb++;
100 n++;
101 }
102
103 fclose(rgbf);
104
105 /* Return the number of read rgb names. */
106 return n < 0 ? 0 : n;
107 }
108
109 /*
110 * Return the color name corresponding to the given rgb values
111 */
112 char *
113 xpmGetRgbName(
114 xpmRgbName rgbn[], /* rgb mnemonics from rgb text file */
115 int rgbn_max, /* number of rgb mnemonics in table */
116 int red, /* rgb values */
117 int green,
118 int blue)
119 {
120 int i;
121 xpmRgbName *rgb;
122
123 /*
124 * Just perform a dumb linear search over the rgb values of the color
125 * mnemonics. One could speed things up by sorting the rgb values and
126 * using a binary search, or building a hash table, etc...
127 */
128 for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
129 if (red == rgb->r && green == rgb->g && blue == rgb->b)
130 return rgb->name;
131
132 /* if not found return NULL */
133 return NULL;
134 }
135
136 /*
137 * Free the strings which have been malloc'ed in xpmReadRgbNames
138 */
139 void
140 xpmFreeRgbNames(
141 xpmRgbName rgbn[],
142 int rgbn_max)
143 {
144 int i;
145 xpmRgbName *rgb;
146
147 for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
148 XpmFree(rgb->name);
149 }