GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
color_insrt.c
Go to the documentation of this file.
1 /* This routine is public only because source is in different files.
2  * It should NEVER be called directly.
3  * It is used by G_add_color_rule() and G__read_old_colors().
4  * These routines know when it is approriate to call this routine.
5  */
6 #include <grass/gis.h>
7 #define umalloc(n) (unsigned char *) G_malloc((size_t)n)
8 #define urealloc(s,n) (unsigned char *) G_realloc(s,(size_t)n)
9 
10 #define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
11 
13  int red, int grn, int blu,
14  struct _Color_Info_ *cp)
15 {
16  long nalloc;
17  long i;
18  long newlen, curlen, gap;
19 
20  LIMIT(red);
21  LIMIT(grn);
22  LIMIT(blu);
23 
24  /* first color? */
25  if (!cp->lookup.active) {
26  cp->lookup.active = 1;
27  cp->lookup.nalloc = 256;
28  cp->lookup.red = umalloc(cp->lookup.nalloc);
29  cp->lookup.grn = umalloc(cp->lookup.nalloc);
30  cp->lookup.blu = umalloc(cp->lookup.nalloc);
31  cp->lookup.set = umalloc(cp->lookup.nalloc);
32  cp->max = cp->min = cat;
33  }
34 
35  /* extend the color table? */
36  else if (cat > cp->max) {
37  curlen = cp->max - cp->min + 1;
38  newlen = cat - cp->min + 1;
39  nalloc = newlen;
40  if (nalloc != (int)nalloc) /* check for int overflow */
41  return -1;
42 
43  if (nalloc > cp->lookup.nalloc) {
44  while (cp->lookup.nalloc < nalloc)
45  cp->lookup.nalloc += 256;
46  nalloc = cp->lookup.nalloc;
47 
48  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
49  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
50  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
51  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
52  }
53 
54  /* fill in gap with white */
55  for (i = curlen; i < newlen; i++) {
56  cp->lookup.red[i] = 255;
57  cp->lookup.grn[i] = 255;
58  cp->lookup.blu[i] = 255;
59  cp->lookup.set[i] = 0;
60  }
61  cp->max = cat;
62  }
63  else if (cat < cp->min) {
64  curlen = cp->max - cp->min + 1;
65  newlen = cp->max - cat + 1;
66  gap = newlen - curlen;
67  nalloc = newlen;
68  if (nalloc != (int)nalloc) /* check for int overflow */
69  return -1;
70 
71  if (nalloc > cp->lookup.nalloc) {
72  while (cp->lookup.nalloc < nalloc)
73  cp->lookup.nalloc += 256;
74  nalloc = cp->lookup.nalloc;
75 
76  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
77  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
78  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
79  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
80  }
81 
82  /* shift the table to make room in front */
83  for (i = 1; i <= curlen; i++) {
84  cp->lookup.red[newlen - i] = cp->lookup.red[curlen - i];
85  cp->lookup.grn[newlen - i] = cp->lookup.grn[curlen - i];
86  cp->lookup.blu[newlen - i] = cp->lookup.blu[curlen - i];
87  cp->lookup.set[newlen - i] = cp->lookup.set[curlen - i];
88  }
89 
90  /* fill in gap with white */
91  for (i = 1; i < gap; i++) {
92  cp->lookup.red[i] = 255;
93  cp->lookup.grn[i] = 255;
94  cp->lookup.blu[i] = 255;
95  cp->lookup.set[i] = 0;
96  }
97  cp->min = cat;
98  }
99 
100  /* set the color! */
101  i = cat - cp->min;
102  cp->lookup.red[i] = red;
103  cp->lookup.grn[i] = grn;
104  cp->lookup.blu[i] = blu;
105  cp->lookup.set[i] = 1;
106 
107  return 1;
108 }
#define min(x, y)
Definition: draw2.c:68
#define umalloc(n)
Definition: color_insrt.c:7
#define urealloc(s, n)
Definition: color_insrt.c:8
int G__insert_color_into_lookup(CELL cat, int red, int grn, int blu, struct _Color_Info_ *cp)
Definition: color_insrt.c:12
CELL cat
Definition: g3dcats.c:90
#define LIMIT(x)
Definition: color_insrt.c:10