GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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) \
13  if (x < 0) \
14  x = 0; \
15  else if (x > 255) \
16  x = 255;
17 
18 int Rast__insert_color_into_lookup(CELL cat, int red, int grn, int blu,
19  struct _Color_Info_ *cp)
20 {
21  long nalloc;
22  long i;
23  long newlen, curlen, gap;
24 
25  LIMIT(red);
26  LIMIT(grn);
27  LIMIT(blu);
28 
29  /* first color? */
30  if (!cp->lookup.active) {
31  cp->lookup.active = 1;
32  cp->lookup.nalloc = 256;
33  cp->lookup.red = umalloc(cp->lookup.nalloc);
34  cp->lookup.grn = umalloc(cp->lookup.nalloc);
35  cp->lookup.blu = umalloc(cp->lookup.nalloc);
36  cp->lookup.set = umalloc(cp->lookup.nalloc);
37  cp->max = cp->min = cat;
38  }
39 
40  /* extend the color table? */
41  else if (cat > cp->max) {
42  curlen = cp->max - cp->min + 1;
43  newlen = cat - cp->min + 1;
44  nalloc = newlen;
45  if (nalloc != (int)nalloc) /* check for int overflow */
46  return -1;
47 
48  if (nalloc > cp->lookup.nalloc) {
49  while (cp->lookup.nalloc < nalloc)
50  cp->lookup.nalloc += 256;
51  nalloc = cp->lookup.nalloc;
52 
53  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
54  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
55  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
56  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
57  }
58 
59  /* fill in gap with white */
60  for (i = curlen; i < newlen; i++) {
61  cp->lookup.red[i] = 255;
62  cp->lookup.grn[i] = 255;
63  cp->lookup.blu[i] = 255;
64  cp->lookup.set[i] = 0;
65  }
66  cp->max = cat;
67  }
68  else if (cat < cp->min) {
69  curlen = cp->max - cp->min + 1;
70  newlen = cp->max - cat + 1;
71  gap = newlen - curlen;
72  nalloc = newlen;
73  if (nalloc != (int)nalloc) /* check for int overflow */
74  return -1;
75 
76  if (nalloc > cp->lookup.nalloc) {
77  while (cp->lookup.nalloc < nalloc)
78  cp->lookup.nalloc += 256;
79  nalloc = cp->lookup.nalloc;
80 
81  cp->lookup.red = urealloc((char *)cp->lookup.red, nalloc);
82  cp->lookup.grn = urealloc((char *)cp->lookup.grn, nalloc);
83  cp->lookup.blu = urealloc((char *)cp->lookup.blu, nalloc);
84  cp->lookup.set = urealloc((char *)cp->lookup.set, nalloc);
85  }
86 
87  /* shift the table to make room in front */
88  for (i = 1; i <= curlen; i++) {
89  cp->lookup.red[newlen - i] = cp->lookup.red[curlen - i];
90  cp->lookup.grn[newlen - i] = cp->lookup.grn[curlen - i];
91  cp->lookup.blu[newlen - i] = cp->lookup.blu[curlen - i];
92  cp->lookup.set[newlen - i] = cp->lookup.set[curlen - i];
93  }
94 
95  /* fill in gap with white */
96  for (i = 1; i < gap; i++) {
97  cp->lookup.red[i] = 255;
98  cp->lookup.grn[i] = 255;
99  cp->lookup.blu[i] = 255;
100  cp->lookup.set[i] = 0;
101  }
102  cp->min = cat;
103  }
104 
105  /* set the color! */
106  i = cat - cp->min;
107  cp->lookup.red[i] = red;
108  cp->lookup.grn[i] = grn;
109  cp->lookup.blu[i] = blu;
110  cp->lookup.set[i] = 1;
111 
112  return 1;
113 }
int Rast__insert_color_into_lookup(CELL cat, int red, int grn, int blu, struct _Color_Info_ *cp)
Definition: color_insrt.c:18
#define LIMIT(x)
Definition: color_insrt.c:12
#define umalloc(n)
Definition: color_insrt.c:9
#define urealloc(s, n)
Definition: color_insrt.c:10
#define min(x, y)
Definition: draw2.c:29
int CELL
Definition: gis.h:628
struct _Color_Info_::@3 lookup
unsigned char * red
Definition: gis.h:667
DCELL max
Definition: gis.h:683
int active
Definition: gis.h:672
unsigned char * set
Definition: gis.h:670
unsigned char * blu
Definition: gis.h:669
unsigned char * grn
Definition: gis.h:668
DCELL min
Definition: gis.h:683
int nalloc
Definition: gis.h:671