GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 Rast_add_c_color_rule() and G__read_old_colors().
4  * These routines know when it is appropriate to call this routine.
5  */
6 #include <grass/gis.h>
7 #include <grass/raster.h>
8 
9 #define umalloc(n) (unsigned char *) G_malloc((size_t)n)
10 #define urealloc(s,n) (unsigned char *) G_realloc(s,(size_t)n)
11 
12 #define LIMIT(x) if (x < 0) x = 0; else if (x > 255) x = 255;
13 
15  int red, int grn, int blu,
16  struct _Color_Info_ *cp)
17 {
18  long nalloc;
19  long i;
20  long newlen, curlen, gap;
21 
22  LIMIT(red);
23  LIMIT(grn);
24  LIMIT(blu);
25 
26  /* first color? */
27  if (!cp->lookup.active) {
28  cp->lookup.active = 1;
29  cp->lookup.nalloc = 256;
30  cp->lookup.red = umalloc(cp->lookup.nalloc);
31  cp->lookup.grn = umalloc(cp->lookup.nalloc);
32  cp->lookup.blu = umalloc(cp->lookup.nalloc);
33  cp->lookup.set = umalloc(cp->lookup.nalloc);
34  cp->max = cp->min = cat;
35  }
36 
37  /* extend the color table? */
38  else if (cat > cp->max) {
39  curlen = cp->max - cp->min + 1;
40  newlen = cat - cp->min + 1;
41  nalloc = newlen;
42  if (nalloc != (int)nalloc) /* check for int overflow */
43  return -1;
44 
45  if (nalloc > cp->lookup.nalloc) {
46  while (cp->lookup.nalloc < nalloc)
47  cp->lookup.nalloc += 256;
48  nalloc = cp->lookup.nalloc;
49 
50  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
51  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
52  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
53  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
54  }
55 
56  /* fill in gap with white */
57  for (i = curlen; i < newlen; i++) {
58  cp->lookup.red[i] = 255;
59  cp->lookup.grn[i] = 255;
60  cp->lookup.blu[i] = 255;
61  cp->lookup.set[i] = 0;
62  }
63  cp->max = cat;
64  }
65  else if (cat < cp->min) {
66  curlen = cp->max - cp->min + 1;
67  newlen = cp->max - cat + 1;
68  gap = newlen - curlen;
69  nalloc = newlen;
70  if (nalloc != (int)nalloc) /* check for int overflow */
71  return -1;
72 
73  if (nalloc > cp->lookup.nalloc) {
74  while (cp->lookup.nalloc < nalloc)
75  cp->lookup.nalloc += 256;
76  nalloc = cp->lookup.nalloc;
77 
78  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
79  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
80  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
81  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
82  }
83 
84  /* shift the table to make room in front */
85  for (i = 1; i <= curlen; i++) {
86  cp->lookup.red[newlen - i] = cp->lookup.red[curlen - i];
87  cp->lookup.grn[newlen - i] = cp->lookup.grn[curlen - i];
88  cp->lookup.blu[newlen - i] = cp->lookup.blu[curlen - i];
89  cp->lookup.set[newlen - i] = cp->lookup.set[curlen - i];
90  }
91 
92  /* fill in gap with white */
93  for (i = 1; i < gap; i++) {
94  cp->lookup.red[i] = 255;
95  cp->lookup.grn[i] = 255;
96  cp->lookup.blu[i] = 255;
97  cp->lookup.set[i] = 0;
98  }
99  cp->min = cat;
100  }
101 
102  /* set the color! */
103  i = cat - cp->min;
104  cp->lookup.red[i] = red;
105  cp->lookup.grn[i] = grn;
106  cp->lookup.blu[i] = blu;
107  cp->lookup.set[i] = 1;
108 
109  return 1;
110 }
int Rast__insert_color_into_lookup(CELL cat, int red, int grn, int blu, struct _Color_Info_ *cp)
Definition: color_insrt.c:14
unsigned char * blu
Definition: gis.h:647
unsigned char * red
Definition: gis.h:645
#define min(x, y)
Definition: draw2.c:31
#define umalloc(n)
Definition: color_insrt.c:9
unsigned char * grn
Definition: gis.h:646
int nalloc
Definition: gis.h:649
unsigned char * set
Definition: gis.h:648
#define urealloc(s, n)
Definition: color_insrt.c:10
DCELL min
Definition: gis.h:662
int active
Definition: gis.h:650
int CELL
Definition: gis.h:602
struct _Color_Info_::@3 lookup
DCELL max
Definition: gis.h:662
#define LIMIT(x)
Definition: color_insrt.c:12